CommandAPDU

public final class CommandAPDU

A command APDU following the structure defined in ISO/IEC 7816-4. It consists of a four byte header and a conditional body of variable length. This class does not attempt to verify that the APDU encodes a semantically valid command.

Note that when the expected length of the response APDU is specified in the constructors, the actual length (Ne) must be specified, not its encoded form (Le). Similarly, ne returns the actual value Ne. In other words, a value of 0 means “no data in the response APDU” rather than “maximum length”.

This class supports both the short and extended forms of length encoding for Ne and Nc. However, note that not all terminals and Smart Cards are capable of accepting APDUs that use the extended form.

Instances of this class are immutable. Where data is passed in or out via byte arrays, defensive cloning is performed.

See also

ResponseAPDU

Author

Andreas Sterbenz

Author

JSR 268 Expert Group

Author

Godfrey Chung

Version

1.0

Date

6 Nov 2017

  • cla

    Returns the value of the class byte CLA.

    Declaration

    Swift

    public var cla: UInt8 { get }
  • ins

    Returns the value of the instruction byte INS.

    Declaration

    Swift

    public var ins: UInt8 { get }
  • p1

    Returns the value of the parameter byte P1.

    Declaration

    Swift

    public var p1: UInt8 { get }
  • p2

    Returns the value of the parameter byte P2.

    Declaration

    Swift

    public var p2: UInt8 { get }
  • nc

    Returns the number of data bytes in the command body (Nc) or 0 if this APDU has no body. This call is equivalent to data.count.

    Declaration

    Swift

    public private(set) var nc: Int { get }
  • Returns a copy of the data bytes in the command body. If this APDU as no body, this method returns a byte array with length zero.

    Declaration

    Swift

    public var data: [UInt8] { get }
  • ne

    Returns the maximum number of expected data bytes in a response APDU (Ne).

    Declaration

    Swift

    public private(set) var ne: Int { get }
  • Returns a copy of the bytes in this APDU.

    Declaration

    Swift

    public var bytes: [UInt8] { get }
  • Constructs a CommandAPDU from a byte array containing the complete APDU contents (header and body).

    Note that the apdu bytes are copied to protect against subsequent modification.

    Throws

    CardError.illegalArgument if apdu does not contain a valid command APDU

    Declaration

    Swift

    public init(apdu: [UInt8]) throws

    Parameters

    apdu

    the complete command APDU

  • Constructs a CommandAPDU from a byte array containing the complete APDU contents (header and body). The APDU starts at the index apduOffset in the byte array and is apduLength bytes long.

    Note that the apdu bytes are copied to protect against subsequent modification.

    Throws

    CardError.illegalArgument if apduOffset or apduLength are negative or if apduOffset + apduLength are greater than apdu.length, or if the specified bytes are not a valid APDU

    Declaration

    Swift

    public init(apdu: [UInt8], apduOffset: Int, apduLength: Int) throws

    Parameters

    apdu

    the complete command APDU

    apduOffset

    the offset in the byte array at which the apdu data begins

    apduLength

    the length of the APDU

  • Creates a CommandAPDU from the Data containing the complete APDU contents (header and body).

    Note that the data is copied to protect against subsequent modification.

    Throws

    CardError.illegalArgument if apdu does not contain a valid command APDU

    Declaration

    Swift

    public init(apdu: Data) throws

    Parameters

    apdu

    the Data containing the complete APDU

  • Constructs a CommandAPDU from the four header bytes. This is case 1 in ISO 7816, no command body.

    Declaration

    Swift

    public convenience init(cla: UInt8,
                            ins: UInt8,
                            p1: UInt8,
                            p2: UInt8) throws

    Parameters

    cla

    the class byte CLA

    ins

    the instruction byte INS

    p1

    the parameter byte P1

    p2

    the parameter byte P2

  • Constructs a CommandAPDU from the four header bytes and the expected response data length. This is case 2 in ISO 7816, empty command data field with Ne specified. If Ne is 0, the APDU is encoded as ISO 7816 case 1.

    Throws

    CardError.illegalArgument if ne is negative or greater than 65536

    Declaration

    Swift

    public convenience init(cla: UInt8,
                            ins: UInt8,
                            p1: UInt8,
                            p2: UInt8,
                            ne: Int) throws

    Parameters

    cla

    the class byte CLA

    ins

    the instruction byte INS

    p1

    the parameter byte P1

    p2

    the parameter byte P2

    ne

    the maximum number of expected data bytes in a response APDU

  • Constructs a CommandAPDU from the four header bytes and command data. This is case 3 in ISO 7816, command data present and Ne absent. The value Nc is taken as data.length. If data is null or its length is 0, the APDU is encoded as ISO 7816 case 1.

    Note that the data bytes are copied to protect against subsequent modification.

    Throws

    CardError.illegalArgument if data.count is greater than 65535

    Declaration

    Swift

    public convenience init(cla: UInt8,
                            ins: UInt8,
                            p1: UInt8,
                            p2: UInt8,
                            data: [UInt8]) throws

    Parameters

    cla

    the class byte CLA

    ins

    the instruction byte INS

    p1

    the parameter byte P1

    p2

    the parameter byte P2

    data

    the byte array containing the data bytes of the command body

  • Constructs a CommandAPDU from the four header bytes and command data. This is case 3 in ISO 7816, command data present and Ne absent. The value Nc is taken as dataLength. If dataLength is 0, the APDU is encoded as ISO 7816 case 1.

    Note that the data bytes are copied to protect against subsequent modification.

    Throws

    CardError.illegalArgument if dataOffset or dataLength are negative or if dataOffset + dataLength are greater than data.count or if dataLength is greater than 65535

    Declaration

    Swift

    public convenience init(cla: UInt8,
                            ins: UInt8,
                            p1: UInt8,
                            p2: UInt8,
                            data: [UInt8],
                            dataOffset: Int,
                            dataLength: Int) throws

    Parameters

    cla

    the class byte CLA

    ins

    the instruction byte INS

    p1

    the parameter byte P1

    p2

    the parameter byte P2

    data

    the byte array containing the data bytes of the command body

    dataOffset

    the offset in the byte array at which the data bytes of the command body begin

    dataLength

    the number of the data bytes in the command body

  • Constructs a CommandAPDU from the four header bytes, command data, and expected response data length. This is case 4 in ISO 7816, command data and Ne present. The value Nc is taken as data.count if data is non-null and as 0 otherwise. If Ne or Nc are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816.

    Note that the data bytes are copied to protect against subsequent modification.

    Throws

    CardError.illegalArgument if data.count is greater than 65535 or if ne is negative or greater than 65536

    Declaration

    Swift

    public convenience init(cla: UInt8,
                            ins: UInt8,
                            p1: UInt8,
                            p2: UInt8,
                            data: [UInt8],
                            ne: Int) throws

    Parameters

    cla

    the class byte CLA

    ins

    the instruction byte INS

    p1

    the parameter byte P1

    p2

    the parameter byte P2

    data

    the byte array containing the data bytes of the command body

    ne

    the maximum number of expected data bytes in a response APDU

  • Constructs a CommandAPDU from the four header bytes, command data, and expected response data length. This is case 4 in ISO 7816, command data and Le present. The value Nc is taken as dataLength. If Ne or Nc are zero, the APDU is encoded as case 1, 2, or 3 per ISO 7816.

    Note that the data bytes are copied to protect against subsequent modification.

    Throws

    CardError.illegalArgument if dataOffset or dataLength are negative or if dataOffset + dataLength are greater than data.count, or if ne is negative or greater than 65536, or if dataLength is greater than 65535

    Declaration

    Swift

    public init(cla: UInt8,
                ins: UInt8,
                p1: UInt8,
                p2: UInt8,
                data: [UInt8],
                dataOffset: Int,
                dataLength: Int,
                ne: Int) throws

    Parameters

    cla

    the class byte CLA

    ins

    the instruction byte INS

    p1

    the parameter byte P1

    p2

    the parameter byte P2

    data

    the byte array containing the data bytes of the command body

    dataOffset

    the offset in the byte array at which the data bytes of the command body begin

    dataLength

    the number of the data bytes in the command body

    ne

    the maximum number of expected data bytes in a response APDU