1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/* * @(#)CardChannel.java 1.4 06/08/21 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.smartcardio; import java.nio.*; /** * A logical channel connection to a Smart Card. It is used to exchange APDUs * with a Smart Card. * A CardChannel object can be obtained by calling the method * {@linkplain Card#getBasicChannel} or {@linkplain Card#openLogicalChannel}. * * @see Card * @see CommandAPDU * @see ResponseAPDU * * @version 1.4, 08/21/06 * @since 1.6 * @author Andreas Sterbenz * @author JSR 268 Expert Group */ public abstract class CardChannel { /** * Constructs a new CardChannel object. * * <p>This constructor is called by subclasses only. Application should * call the {@linkplain Card#getBasicChannel} and * {@linkplain Card#openLogicalChannel} methods to obtain a CardChannel * object. */ protected CardChannel() { // empty } /** * Returns the Card this channel is associated with. * * @return the Card this channel is associated with */ public abstract Card getCard(); /** * Returns the channel number of this CardChannel. A channel number of * 0 indicates the basic logical channel. * * @return the channel number of this CardChannel. * * @throws IllegalStateException if this channel has been * {@linkplain #close closed} or if the corresponding Card has been * {@linkplain Card#disconnect disconnected}. */ public abstract int getChannelNumber(); /** * Transmits the specified command APDU to the Smart Card and returns the * response APDU. * * <p>The CLA byte of the command APDU is automatically adjusted to * match the channel number of this CardChannel. * * <p>Note that this method cannot be used to transmit * <code>MANAGE CHANNEL</code> APDUs. Logical channels should be managed * using the {@linkplain Card#openLogicalChannel} and {@linkplain * CardChannel#close CardChannel.close()} methods. * * <p>Implementations should transparently handle artifacts * of the transmission protocol. * For example, when using the T=0 protocol, the following processing * should occur as described in ISO/IEC 7816-4: * * <ul> * <li><p>if the response APDU has an SW1 of <code>61</code>, the * implementation should issue a <code>GET RESPONSE</code> command * using <code>SW2</code> as the <code>Le</code>field. * This process is repeated as long as an SW1 of <code>61</code> is * received. The response body of these exchanges is concatenated * to form the final response body. * * <li><p>if the response APDU is <code>6C XX</code>, the implementation * should reissue the command using <code>XX</code> as the * <code>Le</code> field. * </ul> * * <p>The ResponseAPDU returned by this method is the result * after this processing has been performed. * * @param command the command APDU * @return the response APDU received from the card * * @throws IllegalStateException if this channel has been * {@linkplain #close closed} or if the corresponding Card has been * {@linkplain Card#disconnect disconnected}. * @throws IllegalArgumentException if the APDU encodes a * <code>MANAGE CHANNEL</code> command * @throws NullPointerException if command is null * @throws CardException if the card operation failed */ public abstract ResponseAPDU transmit(CommandAPDU command) throws CardException; /** * Transmits the command APDU stored in the command ByteBuffer and receives * the reponse APDU in the response ByteBuffer. * * <p>The command buffer must contain valid command APDU data starting * at <code>command.position()</code> and the APDU must be * <code>command.remaining()</code> bytes long. * Upon return, the command buffer's position will be equal * to its limit; its limit will not have changed. The output buffer * will have received the response APDU bytes. Its position will have * advanced by the number of bytes received, which is also the return * value of this method. * * <p>The CLA byte of the command APDU is automatically adjusted to * match the channel number of this CardChannel. * * <p>Note that this method cannot be used to transmit * <code>MANAGE CHANNEL</code> APDUs. Logical channels should be managed * using the {@linkplain Card#openLogicalChannel} and {@linkplain * CardChannel#close CardChannel.close()} methods. * * <p>See {@linkplain #transmit transmit()} for a discussion of the handling * of response APDUs with the SW1 values <code>61</code> or <code>6C</code>. * * @param command the buffer containing the command APDU * @param response the buffer that shall receive the response APDU from * the card * @return the length of the received response APDU * * @throws IllegalStateException if this channel has been * {@linkplain #close closed} or if the corresponding Card has been * {@linkplain Card#disconnect disconnected}. * @throws NullPointerException if command or response is null * @throws ReadOnlyBufferException if the response buffer is read-only * @throws IllegalArgumentException if command and response are the * same object, if <code>response</code> may not have * sufficient space to receive the response APDU * or if the APDU encodes a <code>MANAGE CHANNEL</code> command * @throws CardException if the card operation failed */ public abstract int transmit(ByteBuffer command, ByteBuffer response) throws CardException; /** * Closes this CardChannel. The logical channel is closed by issuing * a <code>MANAGE CHANNEL</code> command that should use the format * <code>[xx 70 80 0n]</code> where <code>n</code> is the channel number * of this channel and <code>xx</code> is the <code>CLA</code> * byte that encodes this logical channel and has all other bits set to 0. * After this method returns, calling other * methods in this class will raise an IllegalStateException. * * <p>Note that the basic logical channel cannot be closed using this * method. It can be closed by calling {@link Card#disconnect}. * * @throws CardException if the card operation failed * @throws IllegalStateException if this CardChannel represents a * connection the basic logical channel */ public abstract void close() throws CardException; }