jdk/src/share/classes/javax/smartcardio/CardChannel.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 javax.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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A logical channel connection to a Smart Card. It is used to exchange APDUs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * with a Smart Card.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A CardChannel object can be obtained by calling the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * {@linkplain Card#getBasicChannel} or {@linkplain Card#openLogicalChannel}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see Card
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see CommandAPDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see ResponseAPDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  JSR 268 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public abstract class CardChannel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Constructs a new CardChannel object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * <p>This constructor is called by subclasses only. Application should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * call the {@linkplain Card#getBasicChannel} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * {@linkplain Card#openLogicalChannel} methods to obtain a CardChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    protected CardChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Returns the Card this channel is associated with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @return the Card this channel is associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public abstract Card getCard();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Returns the channel number of this CardChannel. A channel number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * 0 indicates the basic logical channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @return the channel number of this CardChannel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @throws IllegalStateException if this channel has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *   {@linkplain #close closed} or if the corresponding Card has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *   {@linkplain Card#disconnect disconnected}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public abstract int getChannelNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Transmits the specified command APDU to the Smart Card and returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * response APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p>The CLA byte of the command APDU is automatically adjusted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * match the channel number of this CardChannel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <p>Note that this method cannot be used to transmit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <code>MANAGE CHANNEL</code> APDUs. Logical channels should be managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * using the {@linkplain Card#openLogicalChannel} and {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * CardChannel#close CardChannel.close()} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <p>Implementations should transparently handle artifacts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * of the transmission protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * For example, when using the T=0 protocol, the following processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * should occur as described in ISO/IEC 7816-4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <li><p>if the response APDU has an SW1 of <code>61</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * implementation should issue a <code>GET RESPONSE</code> command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * using <code>SW2</code> as the <code>Le</code>field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * This process is repeated as long as an SW1 of <code>61</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * received. The response body of these exchanges is concatenated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * to form the final response body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <li><p>if the response APDU is <code>6C XX</code>, the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * should reissue the command using <code>XX</code> as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>Le</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>The ResponseAPDU returned by this method is the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * after this processing has been performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param command the command APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return the response APDU received from the card
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @throws IllegalStateException if this channel has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *   {@linkplain #close closed} or if the corresponding Card has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *   {@linkplain Card#disconnect disconnected}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws IllegalArgumentException if the APDU encodes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *   <code>MANAGE CHANNEL</code> command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws NullPointerException if command is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public abstract ResponseAPDU transmit(CommandAPDU command) throws CardException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Transmits the command APDU stored in the command ByteBuffer and receives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the reponse APDU in the response ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p>The command buffer must contain valid command APDU data starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * at <code>command.position()</code> and the APDU must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>command.remaining()</code> bytes long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Upon return, the command buffer's position will be equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * to its limit; its limit will not have changed. The output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * will have received the response APDU bytes. Its position will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * advanced by the number of bytes received, which is also the return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * value of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>The CLA byte of the command APDU is automatically adjusted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * match the channel number of this CardChannel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p>Note that this method cannot be used to transmit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <code>MANAGE CHANNEL</code> APDUs. Logical channels should be managed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * using the {@linkplain Card#openLogicalChannel} and {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * CardChannel#close CardChannel.close()} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <p>See {@linkplain #transmit transmit()} for a discussion of the handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * of response APDUs with the SW1 values <code>61</code> or <code>6C</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param command the buffer containing the command APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param response the buffer that shall receive the response APDU from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *   the card
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the length of the received response APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws IllegalStateException if this channel has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *   {@linkplain #close closed} or if the corresponding Card has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *   {@linkplain Card#disconnect disconnected}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws NullPointerException if command or response is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws ReadOnlyBufferException if the response buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws IllegalArgumentException if command and response are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *   same object, if <code>response</code> may not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *   sufficient space to receive the response APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *   or if the APDU encodes a <code>MANAGE CHANNEL</code> command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public abstract int transmit(ByteBuffer command, ByteBuffer response)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        throws CardException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Closes this CardChannel. The logical channel is closed by issuing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * a <code>MANAGE CHANNEL</code> command that should use the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <code>[xx 70 80 0n]</code> where <code>n</code> is the channel number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * of this channel and <code>xx</code> is the <code>CLA</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * byte that encodes this logical channel and has all other bits set to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * After this method returns, calling other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * methods in this class will raise an IllegalStateException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p>Note that the basic logical channel cannot be closed using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * method. It can be closed by calling {@link Card#disconnect}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @throws CardException if the card operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @throws IllegalStateException if this CardChannel represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *   connection the basic logical channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public abstract void close() throws CardException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}