0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.smartcardio;
0N/A
0N/Aimport java.nio.*;
0N/A
0N/A/**
0N/A * A logical channel connection to a Smart Card. It is used to exchange APDUs
0N/A * with a Smart Card.
0N/A * A CardChannel object can be obtained by calling the method
0N/A * {@linkplain Card#getBasicChannel} or {@linkplain Card#openLogicalChannel}.
0N/A *
0N/A * @see Card
0N/A * @see CommandAPDU
0N/A * @see ResponseAPDU
0N/A *
0N/A * @since 1.6
0N/A * @author Andreas Sterbenz
0N/A * @author JSR 268 Expert Group
0N/A */
0N/Apublic abstract class CardChannel {
0N/A
0N/A /**
0N/A * Constructs a new CardChannel object.
0N/A *
0N/A * <p>This constructor is called by subclasses only. Application should
0N/A * call the {@linkplain Card#getBasicChannel} and
0N/A * {@linkplain Card#openLogicalChannel} methods to obtain a CardChannel
0N/A * object.
0N/A */
0N/A protected CardChannel() {
0N/A // empty
0N/A }
0N/A
0N/A /**
0N/A * Returns the Card this channel is associated with.
0N/A *
0N/A * @return the Card this channel is associated with
0N/A */
0N/A public abstract Card getCard();
0N/A
0N/A /**
0N/A * Returns the channel number of this CardChannel. A channel number of
0N/A * 0 indicates the basic logical channel.
0N/A *
0N/A * @return the channel number of this CardChannel.
0N/A *
0N/A * @throws IllegalStateException if this channel has been
0N/A * {@linkplain #close closed} or if the corresponding Card has been
0N/A * {@linkplain Card#disconnect disconnected}.
0N/A */
0N/A public abstract int getChannelNumber();
0N/A
0N/A /**
0N/A * Transmits the specified command APDU to the Smart Card and returns the
0N/A * response APDU.
0N/A *
0N/A * <p>The CLA byte of the command APDU is automatically adjusted to
0N/A * match the channel number of this CardChannel.
0N/A *
0N/A * <p>Note that this method cannot be used to transmit
0N/A * <code>MANAGE CHANNEL</code> APDUs. Logical channels should be managed
0N/A * using the {@linkplain Card#openLogicalChannel} and {@linkplain
0N/A * CardChannel#close CardChannel.close()} methods.
0N/A *
0N/A * <p>Implementations should transparently handle artifacts
0N/A * of the transmission protocol.
0N/A * For example, when using the T=0 protocol, the following processing
0N/A * should occur as described in ISO/IEC 7816-4:
0N/A *
0N/A * <ul>
0N/A * <li><p>if the response APDU has an SW1 of <code>61</code>, the
0N/A * implementation should issue a <code>GET RESPONSE</code> command
0N/A * using <code>SW2</code> as the <code>Le</code>field.
0N/A * This process is repeated as long as an SW1 of <code>61</code> is
0N/A * received. The response body of these exchanges is concatenated
0N/A * to form the final response body.
0N/A *
0N/A * <li><p>if the response APDU is <code>6C XX</code>, the implementation
0N/A * should reissue the command using <code>XX</code> as the
0N/A * <code>Le</code> field.
0N/A * </ul>
0N/A *
0N/A * <p>The ResponseAPDU returned by this method is the result
0N/A * after this processing has been performed.
0N/A *
0N/A * @param command the command APDU
0N/A * @return the response APDU received from the card
0N/A *
0N/A * @throws IllegalStateException if this channel has been
0N/A * {@linkplain #close closed} or if the corresponding Card has been
0N/A * {@linkplain Card#disconnect disconnected}.
0N/A * @throws IllegalArgumentException if the APDU encodes a
0N/A * <code>MANAGE CHANNEL</code> command
0N/A * @throws NullPointerException if command is null
0N/A * @throws CardException if the card operation failed
0N/A */
0N/A public abstract ResponseAPDU transmit(CommandAPDU command) throws CardException;
0N/A
0N/A /**
0N/A * Transmits the command APDU stored in the command ByteBuffer and receives
0N/A * the reponse APDU in the response ByteBuffer.
0N/A *
0N/A * <p>The command buffer must contain valid command APDU data starting
0N/A * at <code>command.position()</code> and the APDU must be
0N/A * <code>command.remaining()</code> bytes long.
0N/A * Upon return, the command buffer's position will be equal
0N/A * to its limit; its limit will not have changed. The output buffer
0N/A * will have received the response APDU bytes. Its position will have
0N/A * advanced by the number of bytes received, which is also the return
0N/A * value of this method.
0N/A *
0N/A * <p>The CLA byte of the command APDU is automatically adjusted to
0N/A * match the channel number of this CardChannel.
0N/A *
0N/A * <p>Note that this method cannot be used to transmit
0N/A * <code>MANAGE CHANNEL</code> APDUs. Logical channels should be managed
0N/A * using the {@linkplain Card#openLogicalChannel} and {@linkplain
0N/A * CardChannel#close CardChannel.close()} methods.
0N/A *
0N/A * <p>See {@linkplain #transmit transmit()} for a discussion of the handling
0N/A * of response APDUs with the SW1 values <code>61</code> or <code>6C</code>.
0N/A *
0N/A * @param command the buffer containing the command APDU
0N/A * @param response the buffer that shall receive the response APDU from
0N/A * the card
0N/A * @return the length of the received response APDU
0N/A *
0N/A * @throws IllegalStateException if this channel has been
0N/A * {@linkplain #close closed} or if the corresponding Card has been
0N/A * {@linkplain Card#disconnect disconnected}.
0N/A * @throws NullPointerException if command or response is null
0N/A * @throws ReadOnlyBufferException if the response buffer is read-only
0N/A * @throws IllegalArgumentException if command and response are the
0N/A * same object, if <code>response</code> may not have
0N/A * sufficient space to receive the response APDU
0N/A * or if the APDU encodes a <code>MANAGE CHANNEL</code> command
0N/A * @throws CardException if the card operation failed
0N/A */
0N/A public abstract int transmit(ByteBuffer command, ByteBuffer response)
0N/A throws CardException;
0N/A
0N/A /**
0N/A * Closes this CardChannel. The logical channel is closed by issuing
0N/A * a <code>MANAGE CHANNEL</code> command that should use the format
0N/A * <code>[xx 70 80 0n]</code> where <code>n</code> is the channel number
0N/A * of this channel and <code>xx</code> is the <code>CLA</code>
0N/A * byte that encodes this logical channel and has all other bits set to 0.
0N/A * After this method returns, calling other
0N/A * methods in this class will raise an IllegalStateException.
0N/A *
0N/A * <p>Note that the basic logical channel cannot be closed using this
0N/A * method. It can be closed by calling {@link Card#disconnect}.
0N/A *
0N/A * @throws CardException if the card operation failed
0N/A * @throws IllegalStateException if this CardChannel represents a
0N/A * connection the basic logical channel
0N/A */
0N/A public abstract void close() throws CardException;
0N/A
0N/A}