Path: blob/master/src/java.smartcardio/share/classes/javax/smartcardio/CommandAPDU.java
41153 views
/*1* Copyright (c) 2005, 2006, 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.util.Arrays;2829import java.nio.ByteBuffer;3031/**32* A command APDU following the structure defined in ISO/IEC 7816-4.33* It consists of a four byte header and a conditional body of variable length.34* This class does not attempt to verify that the APDU encodes a semantically35* valid command.36*37* <p>Note that when the expected length of the response APDU is specified38* in the {@linkplain #CommandAPDU(int,int,int,int,int) constructors},39* the actual length (Ne) must be specified, not its40* encoded form (Le). Similarly, {@linkplain #getNe} returns the actual41* value Ne. In other words, a value of 0 means "no data in the response APDU"42* rather than "maximum length."43*44* <p>This class supports both the short and extended forms of length45* encoding for Ne and Nc. However, note that not all terminals and Smart Cards46* are capable of accepting APDUs that use the extended form.47*48* <p>For the header bytes CLA, INS, P1, and P2 the Java type <code>int</code>49* is used to represent the 8 bit unsigned values. In the constructors, only50* the 8 lowest bits of the <code>int</code> value specified by the application51* are significant. The accessor methods always return the byte as an unsigned52* value between 0 and 255.53*54* <p>Instances of this class are immutable. Where data is passed in or out55* via byte arrays, defensive cloning is performed.56*57* @see ResponseAPDU58* @see CardChannel#transmit CardChannel.transmit59*60* @since 1.661* @author Andreas Sterbenz62* @author JSR 268 Expert Group63*/64public final class CommandAPDU implements java.io.Serializable {6566private static final long serialVersionUID = 398698301286670877L;6768private static final int MAX_APDU_SIZE = 65544;6970/** @serial */71private byte[] apdu;7273// value of nc74private transient int nc;7576// value of ne77private transient int ne;7879// index of start of data within the apdu array80private transient int dataOffset;8182/**83* Constructs a CommandAPDU from a byte array containing the complete84* APDU contents (header and body).85*86* <p>Note that the apdu bytes are copied to protect against87* subsequent modification.88*89* @param apdu the complete command APDU90*91* @throws NullPointerException if apdu is null92* @throws IllegalArgumentException if apdu does not contain a valid93* command APDU94*/95public CommandAPDU(byte[] apdu) {96this.apdu = apdu.clone();97parse();98}99100/**101* Constructs a CommandAPDU from a byte array containing the complete102* APDU contents (header and body). The APDU starts at the index103* <code>apduOffset</code> in the byte array and is <code>apduLength</code>104* bytes long.105*106* <p>Note that the apdu bytes are copied to protect against107* subsequent modification.108*109* @param apdu the complete command APDU110* @param apduOffset the offset in the byte array at which the apdu111* data begins112* @param apduLength the length of the APDU113*114* @throws NullPointerException if apdu is null115* @throws IllegalArgumentException if apduOffset or apduLength are116* negative or if apduOffset + apduLength are greater than apdu.length,117* or if the specified bytes are not a valid APDU118*/119public CommandAPDU(byte[] apdu, int apduOffset, int apduLength) {120checkArrayBounds(apdu, apduOffset, apduLength);121this.apdu = new byte[apduLength];122System.arraycopy(apdu, apduOffset, this.apdu, 0, apduLength);123parse();124}125126private void checkArrayBounds(byte[] b, int ofs, int len) {127if ((ofs < 0) || (len < 0)) {128throw new IllegalArgumentException129("Offset and length must not be negative");130}131if (b == null) {132if ((ofs != 0) && (len != 0)) {133throw new IllegalArgumentException134("offset and length must be 0 if array is null");135}136} else {137if (ofs > b.length - len) {138throw new IllegalArgumentException139("Offset plus length exceed array size");140}141}142}143144/**145* Creates a CommandAPDU from the ByteBuffer containing the complete APDU146* contents (header and body).147* The buffer's <code>position</code> must be set to the start of the APDU,148* its <code>limit</code> to the end of the APDU. Upon return, the buffer's149* <code>position</code> is equal to its limit; its limit remains unchanged.150*151* <p>Note that the data in the ByteBuffer is copied to protect against152* subsequent modification.153*154* @param apdu the ByteBuffer containing the complete APDU155*156* @throws NullPointerException if apdu is null157* @throws IllegalArgumentException if apdu does not contain a valid158* command APDU159*/160public CommandAPDU(ByteBuffer apdu) {161this.apdu = new byte[apdu.remaining()];162apdu.get(this.apdu);163parse();164}165166/**167* Constructs a CommandAPDU from the four header bytes. This is case 1168* in ISO 7816, no command body.169*170* @param cla the class byte CLA171* @param ins the instruction byte INS172* @param p1 the parameter byte P1173* @param p2 the parameter byte P2174*/175public CommandAPDU(int cla, int ins, int p1, int p2) {176this(cla, ins, p1, p2, null, 0, 0, 0);177}178179/**180* Constructs a CommandAPDU from the four header bytes and the expected181* response data length. This is case 2 in ISO 7816, empty command data182* field with Ne specified. If Ne is 0, the APDU is encoded as ISO 7816183* case 1.184*185* @param cla the class byte CLA186* @param ins the instruction byte INS187* @param p1 the parameter byte P1188* @param p2 the parameter byte P2189* @param ne the maximum number of expected data bytes in a response APDU190*191* @throws IllegalArgumentException if ne is negative or greater than192* 65536193*/194public CommandAPDU(int cla, int ins, int p1, int p2, int ne) {195this(cla, ins, p1, p2, null, 0, 0, ne);196}197198/**199* Constructs a CommandAPDU from the four header bytes and command data.200* This is case 3 in ISO 7816, command data present and Ne absent. The201* value Nc is taken as data.length. If <code>data</code> is null or202* its length is 0, the APDU is encoded as ISO 7816 case 1.203*204* <p>Note that the data bytes are copied to protect against205* subsequent modification.206*207* @param cla the class byte CLA208* @param ins the instruction byte INS209* @param p1 the parameter byte P1210* @param p2 the parameter byte P2211* @param data the byte array containing the data bytes of the command body212*213* @throws IllegalArgumentException if data.length is greater than 65535214*/215public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data) {216this(cla, ins, p1, p2, data, 0, arrayLength(data), 0);217}218219/**220* Constructs a CommandAPDU from the four header bytes and command data.221* This is case 3 in ISO 7816, command data present and Ne absent. The222* value Nc is taken as dataLength. If <code>dataLength</code>223* is 0, the APDU is encoded as ISO 7816 case 1.224*225* <p>Note that the data bytes are copied to protect against226* subsequent modification.227*228* @param cla the class byte CLA229* @param ins the instruction byte INS230* @param p1 the parameter byte P1231* @param p2 the parameter byte P2232* @param data the byte array containing the data bytes of the command body233* @param dataOffset the offset in the byte array at which the data234* bytes of the command body begin235* @param dataLength the number of the data bytes in the command body236*237* @throws NullPointerException if data is null and dataLength is not 0238* @throws IllegalArgumentException if dataOffset or dataLength are239* negative or if dataOffset + dataLength are greater than data.length240* or if dataLength is greater than 65535241*/242public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data,243int dataOffset, int dataLength) {244this(cla, ins, p1, p2, data, dataOffset, dataLength, 0);245}246247/**248* Constructs a CommandAPDU from the four header bytes, command data,249* and expected response data length. This is case 4 in ISO 7816,250* command data and Ne present. The value Nc is taken as data.length251* if <code>data</code> is non-null and as 0 otherwise. If Ne or Nc252* are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816.253*254* <p>Note that the data bytes are copied to protect against255* subsequent modification.256*257* @param cla the class byte CLA258* @param ins the instruction byte INS259* @param p1 the parameter byte P1260* @param p2 the parameter byte P2261* @param data the byte array containing the data bytes of the command body262* @param ne the maximum number of expected data bytes in a response APDU263*264* @throws IllegalArgumentException if data.length is greater than 65535265* or if ne is negative or greater than 65536266*/267public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int ne) {268this(cla, ins, p1, p2, data, 0, arrayLength(data), ne);269}270271private static int arrayLength(byte[] b) {272return (b != null) ? b.length : 0;273}274275/**276* Command APDU encoding options:277*278* case 1: |CLA|INS|P1 |P2 | len = 4279* case 2s: |CLA|INS|P1 |P2 |LE | len = 5280* case 3s: |CLA|INS|P1 |P2 |LC |...BODY...| len = 6..260281* case 4s: |CLA|INS|P1 |P2 |LC |...BODY...|LE | len = 7..261282* case 2e: |CLA|INS|P1 |P2 |00 |LE1|LE2| len = 7283* case 3e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...| len = 8..65542284* case 4e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...|LE1|LE2| len =10..65544285*286* LE, LE1, LE2 may be 0x00.287* LC must not be 0x00 and LC1|LC2 must not be 0x00|0x00288*/289private void parse() {290if (apdu.length < 4) {291throw new IllegalArgumentException("apdu must be at least 4 bytes long");292}293if (apdu.length == 4) {294// case 1295return;296}297int l1 = apdu[4] & 0xff;298if (apdu.length == 5) {299// case 2s300this.ne = (l1 == 0) ? 256 : l1;301return;302}303if (l1 != 0) {304if (apdu.length == 4 + 1 + l1) {305// case 3s306this.nc = l1;307this.dataOffset = 5;308return;309} else if (apdu.length == 4 + 2 + l1) {310// case 4s311this.nc = l1;312this.dataOffset = 5;313int l2 = apdu[apdu.length - 1] & 0xff;314this.ne = (l2 == 0) ? 256 : l2;315return;316} else {317throw new IllegalArgumentException318("Invalid APDU: length=" + apdu.length + ", b1=" + l1);319}320}321if (apdu.length < 7) {322throw new IllegalArgumentException323("Invalid APDU: length=" + apdu.length + ", b1=" + l1);324}325int l2 = ((apdu[5] & 0xff) << 8) | (apdu[6] & 0xff);326if (apdu.length == 7) {327// case 2e328this.ne = (l2 == 0) ? 65536 : l2;329return;330}331if (l2 == 0) {332throw new IllegalArgumentException("Invalid APDU: length="333+ apdu.length + ", b1=" + l1 + ", b2||b3=" + l2);334}335if (apdu.length == 4 + 3 + l2) {336// case 3e337this.nc = l2;338this.dataOffset = 7;339return;340} else if (apdu.length == 4 + 5 + l2) {341// case 4e342this.nc = l2;343this.dataOffset = 7;344int leOfs = apdu.length - 2;345int l3 = ((apdu[leOfs] & 0xff) << 8) | (apdu[leOfs + 1] & 0xff);346this.ne = (l3 == 0) ? 65536 : l3;347} else {348throw new IllegalArgumentException("Invalid APDU: length="349+ apdu.length + ", b1=" + l1 + ", b2||b3=" + l2);350}351}352353/**354* Constructs a CommandAPDU from the four header bytes, command data,355* and expected response data length. This is case 4 in ISO 7816,356* command data and Le present. The value Nc is taken as357* <code>dataLength</code>.358* If Ne or Nc359* are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816.360*361* <p>Note that the data bytes are copied to protect against362* subsequent modification.363*364* @param cla the class byte CLA365* @param ins the instruction byte INS366* @param p1 the parameter byte P1367* @param p2 the parameter byte P2368* @param data the byte array containing the data bytes of the command body369* @param dataOffset the offset in the byte array at which the data370* bytes of the command body begin371* @param dataLength the number of the data bytes in the command body372* @param ne the maximum number of expected data bytes in a response APDU373*374* @throws NullPointerException if data is null and dataLength is not 0375* @throws IllegalArgumentException if dataOffset or dataLength are376* negative or if dataOffset + dataLength are greater than data.length,377* or if ne is negative or greater than 65536,378* or if dataLength is greater than 65535379*/380public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data,381int dataOffset, int dataLength, int ne) {382checkArrayBounds(data, dataOffset, dataLength);383if (dataLength > 65535) {384throw new IllegalArgumentException("dataLength is too large");385}386if (ne < 0) {387throw new IllegalArgumentException("ne must not be negative");388}389if (ne > 65536) {390throw new IllegalArgumentException("ne is too large");391}392this.ne = ne;393this.nc = dataLength;394if (dataLength == 0) {395if (ne == 0) {396// case 1397this.apdu = new byte[4];398setHeader(cla, ins, p1, p2);399} else {400// case 2s or 2e401if (ne <= 256) {402// case 2s403// 256 is encoded as 0x00404byte len = (ne != 256) ? (byte)ne : 0;405this.apdu = new byte[5];406setHeader(cla, ins, p1, p2);407this.apdu[4] = len;408} else {409// case 2e410byte l1, l2;411// 65536 is encoded as 0x00 0x00412if (ne == 65536) {413l1 = 0;414l2 = 0;415} else {416l1 = (byte)(ne >> 8);417l2 = (byte)ne;418}419this.apdu = new byte[7];420setHeader(cla, ins, p1, p2);421this.apdu[5] = l1;422this.apdu[6] = l2;423}424}425} else {426if (ne == 0) {427// case 3s or 3e428if (dataLength <= 255) {429// case 3s430apdu = new byte[4 + 1 + dataLength];431setHeader(cla, ins, p1, p2);432apdu[4] = (byte)dataLength;433this.dataOffset = 5;434System.arraycopy(data, dataOffset, apdu, 5, dataLength);435} else {436// case 3e437apdu = new byte[4 + 3 + dataLength];438setHeader(cla, ins, p1, p2);439apdu[4] = 0;440apdu[5] = (byte)(dataLength >> 8);441apdu[6] = (byte)dataLength;442this.dataOffset = 7;443System.arraycopy(data, dataOffset, apdu, 7, dataLength);444}445} else {446// case 4s or 4e447if ((dataLength <= 255) && (ne <= 256)) {448// case 4s449apdu = new byte[4 + 2 + dataLength];450setHeader(cla, ins, p1, p2);451apdu[4] = (byte)dataLength;452this.dataOffset = 5;453System.arraycopy(data, dataOffset, apdu, 5, dataLength);454apdu[apdu.length - 1] = (ne != 256) ? (byte)ne : 0;455} else {456// case 4e457apdu = new byte[4 + 5 + dataLength];458setHeader(cla, ins, p1, p2);459apdu[4] = 0;460apdu[5] = (byte)(dataLength >> 8);461apdu[6] = (byte)dataLength;462this.dataOffset = 7;463System.arraycopy(data, dataOffset, apdu, 7, dataLength);464if (ne != 65536) {465int leOfs = apdu.length - 2;466apdu[leOfs] = (byte)(ne >> 8);467apdu[leOfs + 1] = (byte)ne;468} // else le == 65536: no need to fill in, encoded as 0469}470}471}472}473474private void setHeader(int cla, int ins, int p1, int p2) {475apdu[0] = (byte)cla;476apdu[1] = (byte)ins;477apdu[2] = (byte)p1;478apdu[3] = (byte)p2;479}480481/**482* Returns the value of the class byte CLA.483*484* @return the value of the class byte CLA.485*/486public int getCLA() {487return apdu[0] & 0xff;488}489490/**491* Returns the value of the instruction byte INS.492*493* @return the value of the instruction byte INS.494*/495public int getINS() {496return apdu[1] & 0xff;497}498499/**500* Returns the value of the parameter byte P1.501*502* @return the value of the parameter byte P1.503*/504public int getP1() {505return apdu[2] & 0xff;506}507508/**509* Returns the value of the parameter byte P2.510*511* @return the value of the parameter byte P2.512*/513public int getP2() {514return apdu[3] & 0xff;515}516517/**518* Returns the number of data bytes in the command body (Nc) or 0 if this519* APDU has no body. This call is equivalent to520* <code>getData().length</code>.521*522* @return the number of data bytes in the command body or 0 if this APDU523* has no body.524*/525public int getNc() {526return nc;527}528529/**530* Returns a copy of the data bytes in the command body. If this APDU as531* no body, this method returns a byte array with length zero.532*533* @return a copy of the data bytes in the command body or the empty534* byte array if this APDU has no body.535*/536public byte[] getData() {537byte[] data = new byte[nc];538System.arraycopy(apdu, dataOffset, data, 0, nc);539return data;540}541542/**543* Returns the maximum number of expected data bytes in a response544* APDU (Ne).545*546* @return the maximum number of expected data bytes in a response APDU.547*/548public int getNe() {549return ne;550}551552/**553* Returns a copy of the bytes in this APDU.554*555* @return a copy of the bytes in this APDU.556*/557public byte[] getBytes() {558return apdu.clone();559}560561/**562* Returns a string representation of this command APDU.563*564* @return a String representation of this command APDU.565*/566public String toString() {567return "CommmandAPDU: " + apdu.length + " bytes, nc=" + nc + ", ne=" + ne;568}569570/**571* Compares the specified object with this command APDU for equality.572* Returns true if the given object is also a CommandAPDU and its bytes are573* identical to the bytes in this CommandAPDU.574*575* @param obj the object to be compared for equality with this command APDU576* @return true if the specified object is equal to this command APDU577*/578public boolean equals(Object obj) {579if (this == obj) {580return true;581}582if (obj instanceof CommandAPDU == false) {583return false;584}585CommandAPDU other = (CommandAPDU)obj;586return Arrays.equals(this.apdu, other.apdu);587}588589/**590* Returns the hash code value for this command APDU.591*592* @return the hash code value for this command APDU.593*/594public int hashCode() {595return Arrays.hashCode(apdu);596}597598private void readObject(java.io.ObjectInputStream in)599throws java.io.IOException, ClassNotFoundException {600apdu = (byte[])in.readUnshared();601// initialize transient fields602parse();603}604605}606607608