jdk/src/share/classes/sun/security/smartcardio/ChannelImpl.java
author mchung
Wed, 26 Mar 2014 09:00:49 -0700
changeset 23582 d5fa3327ab3a
parent 5506 202f599c92aa
permissions -rw-r--r--
8038177: Eliminate unnecessary dependency to sun.security.action Reviewed-by: chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
23582
d5fa3327ab3a 8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents: 5506
diff changeset
    30
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.smartcardio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import static sun.security.smartcardio.PCSC.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * CardChannel implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
final class ChannelImpl extends CardChannel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // the card this channel is associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final CardImpl card;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // the channel number, 0 for the basic logical channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final int channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // whether this channel has been closed. only logical channels can be closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private volatile boolean isClosed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    ChannelImpl(CardImpl card, int channel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.card = card;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    void checkClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        card.checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (isClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            throw new IllegalStateException("Logical channel has been closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public Card getCard() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return card;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public int getChannelNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static void checkManageChannel(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (b.length < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                ("Command APDU must be at least 4 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if ((b[0] >= 0) && (b[1] == 0x70)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                ("Manage channel command not allowed, use openLogicalChannel()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public ResponseAPDU transmit(CommandAPDU command) throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        card.checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        byte[] commandBytes = command.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        byte[] responseBytes = doTransmit(commandBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return new ResponseAPDU(responseBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public int transmit(ByteBuffer command, ByteBuffer response) throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        card.checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if ((command == null) || (response == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (response.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (command == response) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    ("command and response must not be the same object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (response.remaining() < 258) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    ("Insufficient space in response buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        byte[] commandBytes = new byte[command.remaining()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        command.get(commandBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        byte[] responseBytes = doTransmit(commandBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        response.put(responseBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return responseBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private final static boolean t0GetResponse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        getBooleanProperty("sun.security.smartcardio.t0GetResponse", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private final static boolean t1GetResponse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        getBooleanProperty("sun.security.smartcardio.t1GetResponse", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private final static boolean t1StripLe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        getBooleanProperty("sun.security.smartcardio.t1StripLe", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static boolean getBooleanProperty(String name, boolean def) {
23582
d5fa3327ab3a 8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents: 5506
diff changeset
   127
        String val = AccessController.doPrivileged(
d5fa3327ab3a 8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents: 5506
diff changeset
   128
            (PrivilegedAction<String>) () -> System.getProperty(name));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (val.equalsIgnoreCase("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else if (val.equalsIgnoreCase("false")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                (name + " must be either 'true' or 'false'");
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
    private byte[] concat(byte[] b1, byte[] b2, int n2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int n1 = b1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if ((n1 == 0) && (n2 == b2.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        byte[] res = new byte[n1 + n2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        System.arraycopy(b1, 0, res, 0, n1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        System.arraycopy(b2, 0, res, n1, n2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private final static byte[] B0 = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private byte[] doTransmit(byte[] command) throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // note that we modify the 'command' array in some cases, so it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // be a copy of the application provided data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            checkManageChannel(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            setChannel(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            int n = command.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            boolean t0 = card.protocol == SCARD_PROTOCOL_T0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            boolean t1 = card.protocol == SCARD_PROTOCOL_T1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (t0 && (n >= 7) && (command[4] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                throw new CardException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        ("Extended length forms not supported for T=0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if ((t0 || (t1 && t1StripLe)) && (n >= 7)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                int lc = command[4] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (lc != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    if (n == lc + 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    lc = ((command[5] & 0xff) << 8) | (command[6] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    if (n == lc + 9) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        n -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            boolean getresponse = (t0 && t0GetResponse) || (t1 && t1GetResponse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            byte[] result = B0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (++k >= 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    throw new CardException("Could not obtain response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                byte[] response = SCardTransmit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    (card.cardId, card.protocol, command, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                int rn = response.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (getresponse && (rn >= 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    // see ISO 7816/2005, 5.1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    if ((rn == 2) && (response[0] == 0x6c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        // Resend command using SW2 as short Le field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        command[n - 1] = response[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    if (response[rn - 2] == 0x61) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        // Issue a GET RESPONSE command with the same CLA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        // using SW2 as short Le field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        if (rn > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            result = concat(result, response, rn - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        command[1] = (byte)0xC0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        command[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        command[3] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        command[4] = response[rn - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        n = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                result = concat(result, response, rn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            card.handleError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new CardException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private static int getSW(byte[] res) throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (res.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new CardException("Invalid response length: " + res.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int sw1 = res[res.length - 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int sw2 = res[res.length - 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return (sw1 << 8) | sw2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private static boolean isOK(byte[] res) throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return (res.length == 2) && (getSW(res) == 0x9000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private void setChannel(byte[] com) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int cla = com[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (cla < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            // proprietary class format, cannot set or check logical channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // for now, just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // classes 001x xxxx is reserved for future use in ISO, ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if ((cla & 0xe0) == 0x20) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // see ISO 7816/2005, table 2 and 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (channel <= 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // mask of bits 7, 1, 0 (channel number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // 0xbc == 1011 1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            com[0] &= 0xbc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            com[0] |= channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else if (channel <= 19) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // mask of bits 7, 3, 2, 1, 0 (channel number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // 0xbc == 1011 0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            com[0] &= 0xb0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            com[0] |= 0x40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            com[0] |= (channel - 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new RuntimeException("Unsupported channel number: " + channel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public void close() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (getChannelNumber() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throw new IllegalStateException("Cannot close basic logical channel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (isClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        card.checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            byte[] com = new byte[] {0x00, 0x70, (byte)0x80, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            com[3] = (byte)getChannelNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            setChannel(com);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            byte[] res = SCardTransmit(card.cardId, card.protocol, com, 0, com.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (isOK(res) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                throw new CardException("close() failed: " + PCSC.toString(res));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            card.handleError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new CardException("Could not close channel", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return "PC/SC channel " + channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}