jdk/src/java.smartcardio/share/classes/sun/security/smartcardio/CardImpl.java
author chegar
Mon, 18 Aug 2014 10:59:36 +0100
changeset 25991 e48157b42439
parent 25968 jdk/src/share/classes/sun/security/smartcardio/CardImpl.java@60fb0b1a97aa
parent 25859 jdk/src/share/classes/sun/security/smartcardio/CardImpl.java@3317bb8137f4
child 44534 a076dffbc2c1
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25968
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5301
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5301
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5301
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5301
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5301
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.smartcardio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
25968
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    29
import java.security.AccessController;
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    30
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.smartcardio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import static sun.security.smartcardio.PCSC.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Card implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
final class CardImpl extends Card {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static enum State { OK, REMOVED, DISCONNECTED };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // the terminal that created this card
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final TerminalImpl terminal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // the native SCARDHANDLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final long cardId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // atr of this card
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final ATR atr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // protocol in use, one of SCARD_PROTOCOL_T0 and SCARD_PROTOCOL_T1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    final int protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // the basic logical channel (channel 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final ChannelImpl basicChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // state of this card connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private volatile State state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // thread holding exclusive access to the card, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private volatile Thread exclusiveThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
25968
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    65
    // used for platform specific logic
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    66
    private static final boolean isWindows;
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    67
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    68
    static {
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    69
        final String osName = AccessController.doPrivileged(
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    70
            (PrivilegedAction<String>) () -> System.getProperty("os.name"));
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    71
        isWindows = osName.startsWith("Windows");
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    72
    }
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    73
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    CardImpl(TerminalImpl terminal, String protocol) throws PCSCException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.terminal = terminal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int sharingMode = SCARD_SHARE_SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int connectProtocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (protocol.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            connectProtocol = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } else if (protocol.equalsIgnoreCase("T=0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            connectProtocol = SCARD_PROTOCOL_T0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } else if (protocol.equalsIgnoreCase("T=1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            connectProtocol = SCARD_PROTOCOL_T1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else if (protocol.equalsIgnoreCase("direct")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // testing
25968
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    86
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    87
            // MSDN states that the preferred protocol can be zero, but doesn't
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    88
            //     specify whether other values are allowed.
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    89
            // pcsc-lite implementation expects the preferred protocol to be non zero.
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    90
            connectProtocol = isWindows ? 0 : SCARD_PROTOCOL_RAW;
60fb0b1a97aa 8046343: (smartcardio) CardTerminal.connect('direct') does not work on MacOSX
igerasim
parents: 24624
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            sharingMode = SCARD_SHARE_DIRECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new IllegalArgumentException("Unsupported protocol " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        cardId = SCardConnect(terminal.contextId, terminal.name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    sharingMode, connectProtocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        byte[] status = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        byte[] atrBytes = SCardStatus(cardId, status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        atr = new ATR(atrBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this.protocol = status[1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        basicChannel = new ChannelImpl(this, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        state = State.OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    void checkState()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        State s = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (s == State.DISCONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new IllegalStateException("Card has been disconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } else if (s == State.REMOVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new IllegalStateException("Card has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (state != State.OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // ping card via SCardStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            SCardStatus(cardId, new byte[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            state = State.REMOVED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private void checkSecurity(String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            sm.checkPermission(new CardPermission(terminal.name, action));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    void handleError(PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (e.code == SCARD_W_REMOVED_CARD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            state = State.REMOVED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public ATR getATR() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return atr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public String getProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        switch (protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        case SCARD_PROTOCOL_T0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return "T=0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        case SCARD_PROTOCOL_T1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return "T=1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return "Unknown protocol " + protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public CardChannel getBasicChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        checkSecurity("getBasicChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return basicChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static int getSW(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (b.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int sw1 = b[b.length - 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int sw2 = b[b.length - 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return (sw1 << 8) | sw2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private static byte[] commandOpenChannel = new byte[] {0, 0x70, 0, 0, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public CardChannel openLogicalChannel() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        checkSecurity("openLogicalChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            byte[] response = SCardTransmit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                (cardId, protocol, commandOpenChannel, 0, commandOpenChannel.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if ((response.length != 3) || (getSW(response) != 0x9000)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                throw new CardException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        ("openLogicalChannel() failed, card response: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        + PCSC.toString(response));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return new ChannelImpl(this, response[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            handleError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new CardException("openLogicalChannel() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    void checkExclusive() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        Thread t = exclusiveThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (t == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (t != Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new CardException("Exclusive access established by another Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public synchronized void beginExclusive() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        checkSecurity("exclusive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (exclusiveThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            throw new CardException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    ("Exclusive access has already been assigned to Thread "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    + exclusiveThread.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            SCardBeginTransaction(cardId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            handleError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throw new CardException("beginExclusive() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        exclusiveThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public synchronized void endExclusive() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (exclusiveThread != Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    ("Exclusive access not assigned to current Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            SCardEndTransaction(cardId, SCARD_LEAVE_CARD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            handleError(e);
5301
1bc2a3a47839 6945604: wrong error message in CardImpl.java
wetmore
parents: 2
diff changeset
   231
            throw new CardException("endExclusive() failed", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            exclusiveThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public byte[] transmitControlCommand(int controlCode, byte[] command)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        checkSecurity("transmitControl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (command == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            byte[] r = SCardControl(cardId, controlCode, command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            handleError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throw new CardException("transmitControlCommand() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public void disconnect(boolean reset) throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (reset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            checkSecurity("reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (state != State.OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        try {
24624
c46acbd3c30b 7047033: (smartcardio) Card.disconnect(boolean reset) does not reset when reset is true
igerasim
parents: 5506
diff changeset
   263
            SCardDisconnect(cardId, (reset ? SCARD_RESET_CARD : SCARD_LEAVE_CARD));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new CardException("disconnect() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            state = State.DISCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            exclusiveThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return "PC/SC card in " + terminal.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            + ", protocol " + getProtocol() + ", state " + state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (state == State.OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                SCardDisconnect(cardId, SCARD_LEAVE_CARD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
}