Path: blob/master/src/java.smartcardio/share/classes/javax/smartcardio/CardChannel.java
41153 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.smartcardio;2627import java.nio.*;2829/**30* A logical channel connection to a Smart Card. It is used to exchange APDUs31* with a Smart Card.32* A CardChannel object can be obtained by calling the method33* {@linkplain Card#getBasicChannel} or {@linkplain Card#openLogicalChannel}.34*35* @see Card36* @see CommandAPDU37* @see ResponseAPDU38*39* @since 1.640* @author Andreas Sterbenz41* @author JSR 268 Expert Group42*/43public abstract class CardChannel {4445/**46* Constructs a new CardChannel object.47*48* <p>This constructor is called by subclasses only. Application should49* call the {@linkplain Card#getBasicChannel} and50* {@linkplain Card#openLogicalChannel} methods to obtain a CardChannel51* object.52*/53protected CardChannel() {54// empty55}5657/**58* Returns the Card this channel is associated with.59*60* @return the Card this channel is associated with61*/62public abstract Card getCard();6364/**65* Returns the channel number of this CardChannel. A channel number of66* 0 indicates the basic logical channel.67*68* @return the channel number of this CardChannel.69*70* @throws IllegalStateException if this channel has been71* {@linkplain #close closed} or if the corresponding Card has been72* {@linkplain Card#disconnect disconnected}.73*/74public abstract int getChannelNumber();7576/**77* Transmits the specified command APDU to the Smart Card and returns the78* response APDU.79*80* <p>The CLA byte of the command APDU is automatically adjusted to81* match the channel number of this CardChannel.82*83* <p>Note that this method cannot be used to transmit84* <code>MANAGE CHANNEL</code> APDUs. Logical channels should be managed85* using the {@linkplain Card#openLogicalChannel} and {@linkplain86* CardChannel#close CardChannel.close()} methods.87*88* <p>Implementations should transparently handle artifacts89* of the transmission protocol.90* For example, when using the T=0 protocol, the following processing91* should occur as described in ISO/IEC 7816-4:92*93* <ul>94* <li><p>if the response APDU has an SW1 of <code>61</code>, the95* implementation should issue a <code>GET RESPONSE</code> command96* using <code>SW2</code> as the <code>Le</code>field.97* This process is repeated as long as an SW1 of <code>61</code> is98* received. The response body of these exchanges is concatenated99* to form the final response body.100*101* <li><p>if the response APDU is <code>6C XX</code>, the implementation102* should reissue the command using <code>XX</code> as the103* <code>Le</code> field.104* </ul>105*106* <p>The ResponseAPDU returned by this method is the result107* after this processing has been performed.108*109* @param command the command APDU110* @return the response APDU received from the card111*112* @throws IllegalStateException if this channel has been113* {@linkplain #close closed} or if the corresponding Card has been114* {@linkplain Card#disconnect disconnected}.115* @throws IllegalArgumentException if the APDU encodes a116* <code>MANAGE CHANNEL</code> command117* @throws NullPointerException if command is null118* @throws CardException if the card operation failed119*/120public abstract ResponseAPDU transmit(CommandAPDU command) throws CardException;121122/**123* Transmits the command APDU stored in the command ByteBuffer and receives124* the response APDU in the response ByteBuffer.125*126* <p>The command buffer must contain valid command APDU data starting127* at <code>command.position()</code> and the APDU must be128* <code>command.remaining()</code> bytes long.129* Upon return, the command buffer's position will be equal130* to its limit; its limit will not have changed. The output buffer131* will have received the response APDU bytes. Its position will have132* advanced by the number of bytes received, which is also the return133* value of this method.134*135* <p>The CLA byte of the command APDU is automatically adjusted to136* match the channel number of this CardChannel.137*138* <p>Note that this method cannot be used to transmit139* <code>MANAGE CHANNEL</code> APDUs. Logical channels should be managed140* using the {@linkplain Card#openLogicalChannel} and {@linkplain141* CardChannel#close CardChannel.close()} methods.142*143* <p>See {@linkplain #transmit transmit()} for a discussion of the handling144* of response APDUs with the SW1 values <code>61</code> or <code>6C</code>.145*146* @param command the buffer containing the command APDU147* @param response the buffer that shall receive the response APDU from148* the card149* @return the length of the received response APDU150*151* @throws IllegalStateException if this channel has been152* {@linkplain #close closed} or if the corresponding Card has been153* {@linkplain Card#disconnect disconnected}.154* @throws NullPointerException if command or response is null155* @throws ReadOnlyBufferException if the response buffer is read-only156* @throws IllegalArgumentException if command and response are the157* same object, if <code>response</code> may not have158* sufficient space to receive the response APDU159* or if the APDU encodes a <code>MANAGE CHANNEL</code> command160* @throws CardException if the card operation failed161*/162public abstract int transmit(ByteBuffer command, ByteBuffer response)163throws CardException;164165/**166* Closes this CardChannel. The logical channel is closed by issuing167* a <code>MANAGE CHANNEL</code> command that should use the format168* <code>[xx 70 80 0n]</code> where <code>n</code> is the channel number169* of this channel and <code>xx</code> is the <code>CLA</code>170* byte that encodes this logical channel and has all other bits set to 0.171* After this method returns, calling other172* methods in this class will raise an IllegalStateException.173*174* <p>Note that the basic logical channel cannot be closed using this175* method. It can be closed by calling {@link Card#disconnect}.176*177* @throws CardException if the card operation failed178* @throws IllegalStateException if this CardChannel represents a179* connection the basic logical channel180*/181public abstract void close() throws CardException;182183}184185186