Java Code Examples for javax.smartcardio.CommandAPDU#getNc()

The following examples show how to use javax.smartcardio.CommandAPDU#getNc() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CardUtils.java    From OpenPGP-Card with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a human friendly representation of a @CommandAPDU.
 */
public static String formatCommandAPDU(CommandAPDU command) {
  if (command.getNc() > 0) {
    if (command.getNe() > 0) {
      return format("[Sent] %02X %02X %02X %02X Lc = %02X Le = %02X ", command.getCLA(),
                    command.getINS(), command.getP1(), command.getP2(), command.getNc(),
                    command.getNe())
             + HexString.bytesToHex(command.getData());
    } else {
      return format("[Sent] %02X %02X %02X %02X %02X  ", command.getCLA(),
                    command.getINS(), command.getP1(), command.getP2(), command.getNc())
             + HexString.bytesToHex(command.getData());
    }
  } else {
    return format("[Sent] %02X %02X %02X %02X %02X  ", command.getCLA(),
                  command.getINS(), command.getP1(), command.getP2(), command.getNe());
  }
}
 
Example 2
Source File: CommandAPDUTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
    //expected values of apdu, data, headers, nc, ne
    CommandAPDU capdu = new CommandAPDU(C1);
    apdu = capdu.getBytes();
    data = capdu.getData();

    cla = capdu.getCLA();
    if (cla != (C1[0] & 0xff)) {
        throw new RuntimeException("Failure: cla is not right");
    }

    ins = capdu.getINS();
    if (ins != (C1[1] & 0xff)) {
        throw new RuntimeException("Failure: ins is not right");
    }

    p1 = capdu.getP1();
    if (p1 != (C1[2] & 0xff)) {
        throw new RuntimeException("Failure: p1 is not right");
    }

    p2 = capdu.getP2();
    if (p2 != (C1[3] & 0xff)) {
        throw new RuntimeException("Failure: p2 is not right");
    }

    nc = capdu.getNc();
    ne = capdu.getNe();

    //Test on following constructors
    cm1 = new CommandAPDU(apdu);
    cm2 = new CommandAPDU(cla, ins, p1, p2);
    cm3 = new CommandAPDU(cla, ins, p1, p2, data);
    cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
    cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
    cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
    cm7 = new CommandAPDU(apdu, 0, apdu.length);
    cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
    cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
}
 
Example 3
Source File: CommandAPDUTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
    //expected values of apdu, data, headers, nc, ne
    CommandAPDU capdu = new CommandAPDU(C1);
    apdu = capdu.getBytes();
    data = capdu.getData();

    cla = capdu.getCLA();
    if (cla != (C1[0] & 0xff)) {
        throw new RuntimeException("Failure: cla is not right");
    }

    ins = capdu.getINS();
    if (ins != (C1[1] & 0xff)) {
        throw new RuntimeException("Failure: ins is not right");
    }

    p1 = capdu.getP1();
    if (p1 != (C1[2] & 0xff)) {
        throw new RuntimeException("Failure: p1 is not right");
    }

    p2 = capdu.getP2();
    if (p2 != (C1[3] & 0xff)) {
        throw new RuntimeException("Failure: p2 is not right");
    }

    nc = capdu.getNc();
    ne = capdu.getNe();

    //Test on following constructors
    cm1 = new CommandAPDU(apdu);
    cm2 = new CommandAPDU(cla, ins, p1, p2);
    cm3 = new CommandAPDU(cla, ins, p1, p2, data);
    cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
    cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
    cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
    cm7 = new CommandAPDU(apdu, 0, apdu.length);
    cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
    cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
}
 
Example 4
Source File: SCP03Wrapper.java    From openjavacard-tools with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public CommandAPDU wrap(CommandAPDU command) throws CardException {
    // fields from the original command
    int cla = command.getCLA();
    int ins = command.getINS();
    int p1 = command.getP1();
    int p2 = command.getP2();
    int dataLen = command.getNc();
    int respLen = command.getNe();
    byte[] data = command.getData();
    // and those fields that get replaced
    int wrappedCla = cla | 0x04;
    int wrappedLen = dataLen;
    byte[] wrappedData = data;

    // check for length limit
    if (data.length > getMaxSize()) {
        throw new CardException("Can not wrap: C-APDU too long");
    }

    // we can return the original command if MAC and ENC are disabled
    if (!mMAC && !mENC) {
        return command;
    }

    // perform ENC operation
    if (mENC && wrappedLen > 0) {
        GPKey encKey = mKeys.getKeyByUsage(GPKeyUsage.ENC);

        // generate counter-derived IV
        byte[] ctr = getEncryptionCounter();
        byte[] icv = GPCrypto.enc_aes_ecb(encKey, ctr);

        // perform padding
        byte[] plainBuf = GPCrypto.pad80(wrappedData, 16);

        // perform the encryption
        byte[] encrypted = GPCrypto.enc_aes_cbc(encKey, plainBuf, icv);

        // replace data, adjusting size accordingly
        wrappedLen += encrypted.length - dataLen;
        wrappedData = encrypted;
    }

    // increment encryption counter even if no data
    if(mENC) {
        incrementEncryptionCounter();
    }

    // perform MAC operation
    if (mMAC) {
        GPKey macKey = mKeys.getKeyByUsage(GPKeyUsage.MAC);

        // MAC is computed on length including MAC
        wrappedLen += 8;

        // collect data for the MAC
        ByteArrayOutputStream macBuffer = new ByteArrayOutputStream();
        macBuffer.write(mICV, 0, mICV.length);
        macBuffer.write(wrappedCla);
        macBuffer.write(ins);
        macBuffer.write(p1);
        macBuffer.write(p2);
        macBuffer.write(wrappedLen);
        macBuffer.write(wrappedData, 0, wrappedData.length);
        byte[] macData = macBuffer.toByteArray();

        // perform the MAC computation
        mICV = GPBouncy.scp03_mac(macKey, macData, 128);
    }

    // build wrapped command
    ByteArrayOutputStream wrapped = new ByteArrayOutputStream();
    wrapped.write(wrappedCla);
    wrapped.write(ins);
    wrapped.write(p1);
    wrapped.write(p2);
    wrapped.write(wrappedLen);
    wrapped.write(wrappedData, 0, wrappedData.length);
    if (mMAC) {
        wrapped.write(mICV, 0, 8);
    }
    if (respLen > 0) {
        wrapped.write(respLen);
    }

    // construct a wrapper for the command
    return new CommandAPDU(wrapped.toByteArray());
}
 
Example 5
Source File: CommandAPDUTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
    //expected values of apdu, data, headers, nc, ne
    CommandAPDU capdu = new CommandAPDU(C1);
    apdu = capdu.getBytes();
    data = capdu.getData();

    cla = capdu.getCLA();
    if (cla != (C1[0] & 0xff)) {
        throw new RuntimeException("Failure: cla is not right");
    }

    ins = capdu.getINS();
    if (ins != (C1[1] & 0xff)) {
        throw new RuntimeException("Failure: ins is not right");
    }

    p1 = capdu.getP1();
    if (p1 != (C1[2] & 0xff)) {
        throw new RuntimeException("Failure: p1 is not right");
    }

    p2 = capdu.getP2();
    if (p2 != (C1[3] & 0xff)) {
        throw new RuntimeException("Failure: p2 is not right");
    }

    nc = capdu.getNc();
    ne = capdu.getNe();

    //Test on following constructors
    cm1 = new CommandAPDU(apdu);
    cm2 = new CommandAPDU(cla, ins, p1, p2);
    cm3 = new CommandAPDU(cla, ins, p1, p2, data);
    cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
    cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
    cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
    cm7 = new CommandAPDU(apdu, 0, apdu.length);
    cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
    cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
}
 
Example 6
Source File: CommandAPDUTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
    //expected values of apdu, data, headers, nc, ne
    CommandAPDU capdu = new CommandAPDU(C1);
    apdu = capdu.getBytes();
    data = capdu.getData();

    cla = capdu.getCLA();
    if (cla != (C1[0] & 0xff)) {
        throw new RuntimeException("Failure: cla is not right");
    }

    ins = capdu.getINS();
    if (ins != (C1[1] & 0xff)) {
        throw new RuntimeException("Failure: ins is not right");
    }

    p1 = capdu.getP1();
    if (p1 != (C1[2] & 0xff)) {
        throw new RuntimeException("Failure: p1 is not right");
    }

    p2 = capdu.getP2();
    if (p2 != (C1[3] & 0xff)) {
        throw new RuntimeException("Failure: p2 is not right");
    }

    nc = capdu.getNc();
    ne = capdu.getNe();

    //Test on following constructors
    cm1 = new CommandAPDU(apdu);
    cm2 = new CommandAPDU(cla, ins, p1, p2);
    cm3 = new CommandAPDU(cla, ins, p1, p2, data);
    cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
    cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
    cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
    cm7 = new CommandAPDU(apdu, 0, apdu.length);
    cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
    cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
}
 
Example 7
Source File: CommandAPDUTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
    //expected values of apdu, data, headers, nc, ne
    CommandAPDU capdu = new CommandAPDU(C1);
    apdu = capdu.getBytes();
    data = capdu.getData();

    cla = capdu.getCLA();
    if (cla != (C1[0] & 0xff)) {
        throw new RuntimeException("Failure: cla is not right");
    }

    ins = capdu.getINS();
    if (ins != (C1[1] & 0xff)) {
        throw new RuntimeException("Failure: ins is not right");
    }

    p1 = capdu.getP1();
    if (p1 != (C1[2] & 0xff)) {
        throw new RuntimeException("Failure: p1 is not right");
    }

    p2 = capdu.getP2();
    if (p2 != (C1[3] & 0xff)) {
        throw new RuntimeException("Failure: p2 is not right");
    }

    nc = capdu.getNc();
    ne = capdu.getNe();

    //Test on following constructors
    cm1 = new CommandAPDU(apdu);
    cm2 = new CommandAPDU(cla, ins, p1, p2);
    cm3 = new CommandAPDU(cla, ins, p1, p2, data);
    cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
    cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
    cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
    cm7 = new CommandAPDU(apdu, 0, apdu.length);
    cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
    cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
}
 
Example 8
Source File: CommandAPDUTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
    //expected values of apdu, data, headers, nc, ne
    CommandAPDU capdu = new CommandAPDU(C1);
    apdu = capdu.getBytes();
    data = capdu.getData();

    cla = capdu.getCLA();
    if (cla != (C1[0] & 0xff)) {
        throw new RuntimeException("Failure: cla is not right");
    }

    ins = capdu.getINS();
    if (ins != (C1[1] & 0xff)) {
        throw new RuntimeException("Failure: ins is not right");
    }

    p1 = capdu.getP1();
    if (p1 != (C1[2] & 0xff)) {
        throw new RuntimeException("Failure: p1 is not right");
    }

    p2 = capdu.getP2();
    if (p2 != (C1[3] & 0xff)) {
        throw new RuntimeException("Failure: p2 is not right");
    }

    nc = capdu.getNc();
    ne = capdu.getNe();

    //Test on following constructors
    cm1 = new CommandAPDU(apdu);
    cm2 = new CommandAPDU(cla, ins, p1, p2);
    cm3 = new CommandAPDU(cla, ins, p1, p2, data);
    cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
    cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
    cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
    cm7 = new CommandAPDU(apdu, 0, apdu.length);
    cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
    cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
}
 
Example 9
Source File: CommandAPDUTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
    //expected values of apdu, data, headers, nc, ne
    CommandAPDU capdu = new CommandAPDU(C1);
    apdu = capdu.getBytes();
    data = capdu.getData();

    cla = capdu.getCLA();
    if (cla != (C1[0] & 0xff)) {
        throw new RuntimeException("Failure: cla is not right");
    }

    ins = capdu.getINS();
    if (ins != (C1[1] & 0xff)) {
        throw new RuntimeException("Failure: ins is not right");
    }

    p1 = capdu.getP1();
    if (p1 != (C1[2] & 0xff)) {
        throw new RuntimeException("Failure: p1 is not right");
    }

    p2 = capdu.getP2();
    if (p2 != (C1[3] & 0xff)) {
        throw new RuntimeException("Failure: p2 is not right");
    }

    nc = capdu.getNc();
    ne = capdu.getNe();

    //Test on following constructors
    cm1 = new CommandAPDU(apdu);
    cm2 = new CommandAPDU(cla, ins, p1, p2);
    cm3 = new CommandAPDU(cla, ins, p1, p2, data);
    cm4 = new CommandAPDU(cla, ins, p1, p2, data, ne);
    cm5 = new CommandAPDU(cla, ins, p1, p2, ne);
    cm6 = new CommandAPDU(ByteBuffer.wrap(apdu));
    cm7 = new CommandAPDU(apdu, 0, apdu.length);
    cm8 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc);
    cm9 = new CommandAPDU(cla, ins, p1, p2, data, 0, nc, ne);
}