jdk/src/share/classes/javax/smartcardio/CommandAPDU.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A command APDU following the structure defined in ISO/IEC 7816-4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * It consists of a four byte header and a conditional body of variable length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This class does not attempt to verify that the APDU encodes a semantically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * valid command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>Note that when the expected length of the response APDU is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * in the {@linkplain #CommandAPDU(int,int,int,int,int) constructors},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the actual length (Ne) must be specified, not its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * encoded form (Le). Similarly, {@linkplain #getNe} returns the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * value Ne. In other words, a value of 0 means "no data in the response APDU"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * rather than "maximum length."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>This class supports both the short and extended forms of length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * encoding for Ne and Nc. However, note that not all terminals and Smart Cards
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * are capable of accepting APDUs that use the extended form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>For the header bytes CLA, INS, P1, and P2 the Java type <code>int</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * is used to represent the 8 bit unsigned values. In the constructors, only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the 8 lowest bits of the <code>int</code> value specified by the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * are significant. The accessor methods always return the byte as an unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * value between 0 and 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>Instances of this class are immutable. Where data is passed in or out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * via byte arrays, defensive cloning is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see ResponseAPDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see CardChannel#transmit CardChannel.transmit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author  JSR 268 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public final class CommandAPDU implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static final long serialVersionUID = 398698301286670877L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final int MAX_APDU_SIZE = 65544;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** @serial */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private byte[] apdu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // value of nc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private transient int nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // value of ne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private transient int ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // index of start of data within the apdu array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private transient int dataOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Constructs a CommandAPDU from a byte array containing the complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * APDU contents (header and body).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>Note that the apdu bytes are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param apdu the complete command APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @throws NullPointerException if apdu is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throws IllegalArgumentException if apdu does not contain a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *   command APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public CommandAPDU(byte[] apdu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.apdu = apdu.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Constructs a CommandAPDU from a byte array containing the complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * APDU contents (header and body). The APDU starts at the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>apduOffset</code> in the byte array and is <code>apduLength</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * bytes long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>Note that the apdu bytes are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param apdu the complete command APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param apduOffset the offset in the byte array at which the apdu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *   data begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param apduLength the length of the APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @throws NullPointerException if apdu is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws IllegalArgumentException if apduOffset or apduLength are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *   negative or if apduOffset + apduLength are greater than apdu.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *   or if the specified bytes are not a valid APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public CommandAPDU(byte[] apdu, int apduOffset, int apduLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        checkArrayBounds(apdu, apduOffset, apduLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.apdu = new byte[apduLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.arraycopy(apdu, apduOffset, this.apdu, 0, apduLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private void checkArrayBounds(byte[] b, int ofs, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if ((ofs < 0) || (len < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                ("Offset and length must not be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if ((ofs != 0) && (len != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    ("offset and length must be 0 if array is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (ofs > b.length - len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    ("Offset plus length exceed array size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Creates a CommandAPDU from the ByteBuffer containing the complete APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * contents (header and body).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The buffer's <code>position</code> must be set to the start of the APDU,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * its <code>limit</code> to the end of the APDU. Upon return, the buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <code>position</code> is equal to its limit; its limit remains unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p>Note that the data in the ByteBuffer is copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param apdu the ByteBuffer containing the complete APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws NullPointerException if apdu is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @throws IllegalArgumentException if apdu does not contain a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *   command APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public CommandAPDU(ByteBuffer apdu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.apdu = new byte[apdu.remaining()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        apdu.get(this.apdu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Constructs a CommandAPDU from the four header bytes. This is case 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * in ISO 7816, no command body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param cla the class byte CLA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param ins the instruction byte INS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param p1 the parameter byte P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param p2 the parameter byte P2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public CommandAPDU(int cla, int ins, int p1, int p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this(cla, ins, p1, p2, null, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Constructs a CommandAPDU from the four header bytes and the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * response data length. This is case 2 in ISO 7816, empty command data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * field with Ne specified. If Ne is 0, the APDU is encoded as ISO 7816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * case 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param cla the class byte CLA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param ins the instruction byte INS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param p1 the parameter byte P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param p2 the parameter byte P2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param ne the maximum number of expected data bytes in a response APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws IllegalArgumentException if ne is negative or greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *   65536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public CommandAPDU(int cla, int ins, int p1, int p2, int ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this(cla, ins, p1, p2, null, 0, 0, ne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Constructs a CommandAPDU from the four header bytes and command data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * This is case 3 in ISO 7816, command data present and Ne absent. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * value Nc is taken as data.length. If <code>data</code> is null or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * its length is 0, the APDU is encoded as ISO 7816 case 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <p>Note that the data bytes are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param cla the class byte CLA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param ins the instruction byte INS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param p1 the parameter byte P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param p2 the parameter byte P2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param data the byte array containing the data bytes of the command body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @throws IllegalArgumentException if data.length is greater than 65535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this(cla, ins, p1, p2, data, 0, arrayLength(data), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Constructs a CommandAPDU from the four header bytes and command data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * This is case 3 in ISO 7816, command data present and Ne absent. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * value Nc is taken as dataLength. If <code>dataLength</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * is 0, the APDU is encoded as ISO 7816 case 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <p>Note that the data bytes are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param cla the class byte CLA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param ins the instruction byte INS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param p1 the parameter byte P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param p2 the parameter byte P2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param data the byte array containing the data bytes of the command body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param dataOffset the offset in the byte array at which the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *   bytes of the command body begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param dataLength the number of the data bytes in the command body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @throws NullPointerException if data is null and dataLength is not 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @throws IllegalArgumentException if dataOffset or dataLength are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *   negative or if dataOffset + dataLength are greater than data.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *   or if dataLength is greater than 65535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            int dataOffset, int dataLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        this(cla, ins, p1, p2, data, dataOffset, dataLength, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Constructs a CommandAPDU from the four header bytes, command data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * and expected response data length. This is case 4 in ISO 7816,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * command data and Ne present. The value Nc is taken as data.length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * if <code>data</code> is non-null and as 0 otherwise. If Ne or Nc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>Note that the data bytes are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @param cla the class byte CLA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param ins the instruction byte INS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param p1 the parameter byte P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param p2 the parameter byte P2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param data the byte array containing the data bytes of the command body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param ne the maximum number of expected data bytes in a response APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @throws IllegalArgumentException if data.length is greater than 65535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *   or if ne is negative or greater than 65536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        this(cla, ins, p1, p2, data, 0, arrayLength(data), ne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static int arrayLength(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return (b != null) ? b.length : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Command APDU encoding options:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * case 1:  |CLA|INS|P1 |P2 |                                 len = 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * case 2s: |CLA|INS|P1 |P2 |LE |                             len = 5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * case 3s: |CLA|INS|P1 |P2 |LC |...BODY...|                  len = 6..260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * case 4s: |CLA|INS|P1 |P2 |LC |...BODY...|LE |              len = 7..261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * case 2e: |CLA|INS|P1 |P2 |00 |LE1|LE2|                     len = 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * case 3e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...|          len = 8..65542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * case 4e: |CLA|INS|P1 |P2 |00 |LC1|LC2|...BODY...|LE1|LE2|  len =10..65544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * LE, LE1, LE2 may be 0x00.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * LC must not be 0x00 and LC1|LC2 must not be 0x00|0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private void parse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (apdu.length < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw new IllegalArgumentException("apdu must be at least 4 bytes long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (apdu.length == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // case 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int l1 = apdu[4] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (apdu.length == 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // case 2s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            this.ne = (l1 == 0) ? 256 : l1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (l1 != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (apdu.length == 4 + 1 + l1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                // case 3s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                this.nc = l1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                this.dataOffset = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            } else if (apdu.length == 4 + 2 + l1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                // case 4s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                this.nc = l1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                this.dataOffset = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                int l2 = apdu[apdu.length - 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                this.ne = (l2 == 0) ? 256 : l2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    ("Invalid APDU: length=" + apdu.length + ", b1=" + l1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (apdu.length < 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                ("Invalid APDU: length=" + apdu.length + ", b1=" + l1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int l2 = ((apdu[5] & 0xff) << 8) | (apdu[6] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (apdu.length == 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            // case 2e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            this.ne = (l2 == 0) ? 65536 : l2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (l2 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw new IllegalArgumentException("Invalid APDU: length="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    + apdu.length + ", b1=" + l1 + ", b2||b3=" + l2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (apdu.length == 4 + 3 + l2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // case 3e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            this.nc = l2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            this.dataOffset = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } else if (apdu.length == 4 + 5 + l2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // case 4e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            this.nc = l2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            this.dataOffset = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            int leOfs = apdu.length - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            int l3 = ((apdu[leOfs] & 0xff) << 8) | (apdu[leOfs + 1] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            this.ne = (l3 == 0) ? 65536 : l3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throw new IllegalArgumentException("Invalid APDU: length="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    + apdu.length + ", b1=" + l1 + ", b2||b3=" + l2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Constructs a CommandAPDU from the four header bytes, command data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * and expected response data length. This is case 4 in ISO 7816,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * command data and Le present. The value Nc is taken as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <code>dataLength</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * If Ne or Nc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p>Note that the data bytes are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param cla the class byte CLA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param ins the instruction byte INS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param p1 the parameter byte P1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param p2 the parameter byte P2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param data the byte array containing the data bytes of the command body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param dataOffset the offset in the byte array at which the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *   bytes of the command body begin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param dataLength the number of the data bytes in the command body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param ne the maximum number of expected data bytes in a response APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @throws NullPointerException if data is null and dataLength is not 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @throws IllegalArgumentException if dataOffset or dataLength are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *   negative or if dataOffset + dataLength are greater than data.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *   or if ne is negative or greater than 65536,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *   or if dataLength is greater than 65535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            int dataOffset, int dataLength, int ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        checkArrayBounds(data, dataOffset, dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (dataLength > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throw new IllegalArgumentException("dataLength is too large");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (ne < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw new IllegalArgumentException("ne must not be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (ne > 65536) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            throw new IllegalArgumentException("ne is too large");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        this.ne = ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        this.nc = dataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (dataLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (ne == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                // case 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                this.apdu = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                setHeader(cla, ins, p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                // case 2s or 2e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (ne <= 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    // case 2s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    // 256 is encoded as 0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    byte len = (ne != 256) ? (byte)ne : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    this.apdu = new byte[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    setHeader(cla, ins, p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    this.apdu[4] = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    // case 2e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    byte l1, l2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    // 65536 is encoded as 0x00 0x00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    if (ne == 65536) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        l1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        l2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        l1 = (byte)(ne >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        l2 = (byte)ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    this.apdu = new byte[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    setHeader(cla, ins, p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    this.apdu[5] = l1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    this.apdu[6] = l2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (ne == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                // case 3s or 3e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                if (dataLength <= 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    // case 3s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    apdu = new byte[4 + 1 + dataLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    setHeader(cla, ins, p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    apdu[4] = (byte)dataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    this.dataOffset = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    System.arraycopy(data, dataOffset, apdu, 5, dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    // case 3e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    apdu = new byte[4 + 3 + dataLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    setHeader(cla, ins, p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    apdu[4] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    apdu[5] = (byte)(dataLength >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    apdu[6] = (byte)dataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    this.dataOffset = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    System.arraycopy(data, dataOffset, apdu, 7, dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                // case 4s or 4e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                if ((dataLength <= 255) && (ne <= 256)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    // case 4s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    apdu = new byte[4 + 2 + dataLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    setHeader(cla, ins, p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    apdu[4] = (byte)dataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    this.dataOffset = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    System.arraycopy(data, dataOffset, apdu, 5, dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    apdu[apdu.length - 1] = (ne != 256) ? (byte)ne : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    // case 4e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    apdu = new byte[4 + 5 + dataLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    setHeader(cla, ins, p1, p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    apdu[4] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    apdu[5] = (byte)(dataLength >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    apdu[6] = (byte)dataLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    this.dataOffset = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    System.arraycopy(data, dataOffset, apdu, 7, dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    if (ne != 65536) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        int leOfs = apdu.length - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        apdu[leOfs] = (byte)(ne >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        apdu[leOfs + 1] = (byte)ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    } // else le == 65536: no need to fill in, encoded as 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    private void setHeader(int cla, int ins, int p1, int p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        apdu[0] = (byte)cla;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        apdu[1] = (byte)ins;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        apdu[2] = (byte)p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        apdu[3] = (byte)p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Returns the value of the class byte CLA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @return the value of the class byte CLA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public int getCLA() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return apdu[0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Returns the value of the instruction byte INS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @return the value of the instruction byte INS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public int getINS() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        return apdu[1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Returns the value of the parameter byte P1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @return the value of the parameter byte P1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public int getP1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return apdu[2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Returns the value of the parameter byte P2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @return the value of the parameter byte P2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public int getP2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return apdu[3] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Returns the number of data bytes in the command body (Nc) or 0 if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * APDU has no body. This call is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <code>getData().length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @return the number of data bytes in the command body or 0 if this APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * has no body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public int getNc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Returns a copy of the data bytes in the command body. If this APDU as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * no body, this method returns a byte array with length zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @return a copy of the data bytes in the command body or the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *    byte array if this APDU has no body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public byte[] getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        byte[] data = new byte[nc];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        System.arraycopy(apdu, dataOffset, data, 0, nc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Returns the maximum number of expected data bytes in a response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * APDU (Ne).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @return the maximum number of expected data bytes in a response APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public int getNe() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Returns a copy of the bytes in this APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return a copy of the bytes in this APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return apdu.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Returns a string representation of this command APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @return a String representation of this command APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return "CommmandAPDU: " + apdu.length + " bytes, nc=" + nc + ", ne=" + ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Compares the specified object with this command APDU for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Returns true if the given object is also a CommandAPDU and its bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * identical to the bytes in this CommandAPDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @param obj the object to be compared for equality with this command APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @return true if the specified object is equal to this command APDU
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if (obj instanceof CommandAPDU == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        CommandAPDU other = (CommandAPDU)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return Arrays.equals(this.apdu, other.apdu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Returns the hash code value for this command APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @return the hash code value for this command APDU.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return Arrays.hashCode(apdu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    private void readObject(java.io.ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        apdu = (byte[])in.readUnshared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        // initialize transient fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        parse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
}