Example usage for javax.smartcardio ResponseAPDU getSW

List of usage examples for javax.smartcardio ResponseAPDU getSW

Introduction

In this page you can find the example usage for javax.smartcardio ResponseAPDU getSW.

Prototype

public int getSW() 

Source Link

Document

Returns the value of the status bytes SW1 and SW2 as a single status word SW.

Usage

From source file:test.be.fedict.eid.applet.PcscTest.java

@Test
public void testCardSignature() throws Exception {
    PcscEid pcscEid = new PcscEid(new TestView(), this.messages);
    if (false == pcscEid.isEidPresent()) {
        LOG.debug("insert eID card");
        pcscEid.waitForEidPresent();//from   w  w  w .  j  a  v a 2s  .  co  m
    }
    try {
        CardChannel cardChannel = pcscEid.getCardChannel();
        CommandAPDU setApdu = new CommandAPDU(0x00, 0x22, 0x41, 0xB6, new byte[] { 0x04, // length of following data
                (byte) 0x80, // algo ref
                0x01, // rsa pkcs#1
                (byte) 0x84, // tag for private key ref
                (byte) 0x81 });
        ResponseAPDU responseApdu = cardChannel.transmit(setApdu);
        if (0x9000 != responseApdu.getSW()) {
            throw new RuntimeException("SELECT error");
        }

        byte[] message = "hello world".getBytes();
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        byte[] digestValue = messageDigest.digest(message);

        ByteArrayOutputStream digestInfo = new ByteArrayOutputStream();
        digestInfo.write(Constants.SHA1_DIGEST_INFO_PREFIX);
        digestInfo.write(digestValue);
        CommandAPDU computeDigitalSignatureApdu = new CommandAPDU(0x00, 0x2A, 0x9E, 0x9A,
                digestInfo.toByteArray());
        responseApdu = cardChannel.transmit(computeDigitalSignatureApdu);
        if (0x9000 != responseApdu.getSW()) {
            throw new RuntimeException("error CDS: " + Integer.toHexString(responseApdu.getSW()));
        }

    } finally {
        pcscEid.close();
    }
}

From source file:test.be.fedict.eid.applet.PcscTest.java

@Test
public void signWhatever() throws Exception {
    PcscEid pcscEid = new PcscEid(new TestView(), this.messages);
    if (false == pcscEid.isEidPresent()) {
        LOG.debug("insert eID card");
        pcscEid.waitForEidPresent();/*from   ww  w.j ava  2  s. com*/
    }
    CardChannel cardChannel = pcscEid.getCardChannel();

    CommandAPDU setApdu = new CommandAPDU(0x00, 0x22, 0x41, 0xB6, new byte[] { 0x04, // length of following data
            (byte) 0x80, // algo ref
            0x01, // rsa pkcs#1
            (byte) 0x84, // tag for private key ref
            (byte) 0x82 }); // auth key
    ResponseAPDU responseApdu = cardChannel.transmit(setApdu);
    assertEquals(0x9000, responseApdu.getSW());

    pcscEid.verifyPin();

    // CommandAPDU computeDigitalSignatureApdu = new CommandAPDU(0x00, 0x2A,
    // 0x9E, 0x9A, new byte[] {
    // 0x30, // DER
    // 0x1f, // length
    // 0x30, // DER
    // 0x07, // length
    // // OID = SHA1
    // 0x06, // OID tag
    // 0x05, 0x2b, 0x0e, 0x03,
    // 0x02,
    // 0x1a,
    // 0x04, // tag OCTET STRING
    // 0x14, // length
    // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
    // 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
    // 0x13, 0x14 });

    // CommandAPDU computeDigitalSignatureApdu = new CommandAPDU(0x00, 0x2A,
    // 0x9E, 0x9A, new byte[] {
    // 0x30, // DER DigestInfo
    // 0x18, // length
    // 0x30, // DER AlgorithmIdentifier
    // 0x00, // length: no OID
    // 0x04, // tag OCTET STRING
    // 0x14, // length
    // 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
    // 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
    // 0x13, 0x14 });

    CommandAPDU computeDigitalSignatureApdu = new CommandAPDU(0x00, 0x2A, 0x9E, 0x9A,
            "Hello world encrypted".getBytes());

    responseApdu = cardChannel.transmit(computeDigitalSignatureApdu);
    assertEquals(0x9000, responseApdu.getSW());
    byte[] signatureValue = responseApdu.getData();
    LOG.debug("signature value size: " + signatureValue.length);

    List<X509Certificate> authnCertChain = pcscEid.getAuthnCertificateChain();

    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, authnCertChain.get(0).getPublicKey());
    byte[] decryptedSignatureValue = cipher.doFinal(signatureValue);
    LOG.debug("decrypted signature value: " + new String(decryptedSignatureValue));

    pcscEid.close();
}

From source file:test.be.fedict.eid.applet.PcscTest.java

private void selectCardManager(CardChannel cardChannel) {
    CommandAPDU selectApplicationApdu = new CommandAPDU(0x00, 0xA4, 0x04, 0x00);
    ResponseAPDU responseApdu;
    try {/*from  w ww . ja  v a2 s. c  o  m*/
        responseApdu = cardChannel.transmit(selectApplicationApdu);
    } catch (CardException e) {
        LOG.debug("error selecting application");
        return;
    } catch (ArrayIndexOutOfBoundsException e) {
        LOG.debug("array error");
        return;
    }
    if (0x9000 != responseApdu.getSW()) {
        LOG.debug("could not select application");
    } else {
        LOG.debug("application selected");
    }
}

From source file:test.be.fedict.eid.applet.PcscTest.java

/**
 * @throws Exception/*from  w  w  w . j a va  2  s  .c o  m*/
 */
@Test
public void testRetrievePIN() throws Exception {
    final PcscEid pcscEid = new PcscEid(new TestView(), this.messages);
    if (false == pcscEid.isEidPresent()) {
        LOG.debug("insert eID card");
        pcscEid.waitForEidPresent();
    }

    byte[] puk12 = new byte[] { 0x22, 0x22, 0x22, 0x11, 0x11, 0x11 };

    try {
        CardChannel cardChannel = pcscEid.getCardChannel();
        for (int pin = 9999; pin >= 0; pin--) {
            LOG.debug("trying PIN: " + pin);
            byte[] bcdPin = new byte[2];
            int dec = pin;
            bcdPin[1] = (byte) (dec % 10);
            dec /= 10;
            bcdPin[1] |= (byte) (dec % 10) << 4;
            dec /= 10;
            bcdPin[0] = (byte) (dec % 10);
            dec /= 10;
            bcdPin[0] |= (byte) (dec % 10) << 4;
            ResponseAPDU responseApdu = verifyPin(bcdPin, cardChannel);
            int sw = responseApdu.getSW();
            if (0x9000 == sw) {
                LOG.debug("PIN is: " + pin);
                break;
            }
            if (0x6983 == sw) {
                unblockPin(puk12, cardChannel);
            }
        }
    } finally {
        pcscEid.close();
    }
}

From source file:test.be.fedict.eid.applet.PcscTest.java

private void unblockPin(byte[] puk12, CardChannel cardChannel) throws CardException {
    byte[] unblockPinData = new byte[] { 0x2C, puk12[0], puk12[1], puk12[2], puk12[3], puk12[4], puk12[5],
            (byte) 0xFF };

    CommandAPDU changePinApdu = new CommandAPDU(0x00, 0x2C, 0x00, 0x01, unblockPinData);
    ResponseAPDU responseApdu = cardChannel.transmit(changePinApdu);
    if (0x9000 != responseApdu.getSW()) {
        throw new RuntimeException("could not unblock PIN code");
    }/* w w w .ja v  a  2s  .c o  m*/
}

From source file:test.be.fedict.eid.applet.PcscTest.java

@Test
public void testCardDataFile() throws Exception {
    PcscEid pcscEid = new PcscEid(new TestView(), this.messages);
    if (false == pcscEid.isEidPresent()) {
        LOG.debug("insert eID card");
        pcscEid.waitForEidPresent();//from   www. java2s.  c  om
    }

    try {
        CardChannel cardChannel = pcscEid.getCardChannel();

        while (true) {
            CommandAPDU getCardApdu = new CommandAPDU(0x80, 0xe4, 0x00, 0x00, 0x1c); // Le = 0x1c
            ResponseAPDU responseApdu = cardChannel.transmit(getCardApdu);
            if (0x9000 != responseApdu.getSW()) {
                fail("SW error: " + Integer.toHexString(responseApdu.getSW()));
            }
            LOG.debug(Hex.encodeHexString(responseApdu.getData()));
        }
    } finally {
        pcscEid.close();
    }
}

From source file:test.be.fedict.eid.applet.PcscTest.java

@Test
public void testGetChallenge() throws Exception {
    PcscEid pcscEid = new PcscEid(new TestView(), this.messages);
    if (false == pcscEid.isEidPresent()) {
        LOG.debug("insert eID card");
        pcscEid.waitForEidPresent();//from w w w  .j  a  va  2s .  co m
    }

    CardChannel cardChannel = pcscEid.getCardChannel();

    int size = 256;
    CommandAPDU getChallengeApdu = new CommandAPDU(0x00, 0x84, 0x00, 0x00, new byte[] {}, 0, 0, size);
    ResponseAPDU responseApdu;
    responseApdu = cardChannel.transmit(getChallengeApdu);
    if (0x9000 != responseApdu.getSW()) {
        fail("get challenge failure: " + Integer.toHexString(responseApdu.getSW()));
    }
    LOG.debug("challenge: " + Hex.encodeHexString(responseApdu.getData()));
    assertEquals(size, responseApdu.getData().length);

    pcscEid.close();
}

From source file:test.be.fedict.eid.applet.PcscTest.java

@Test
public void testCcid() throws Exception {
    PcscEid pcscEid = new PcscEid(new TestView(), this.messages);
    if (false == pcscEid.isEidPresent()) {
        LOG.debug("insert eID card");
        pcscEid.waitForEidPresent();/*  w  w  w  .  ja  va2s . c  om*/
    }

    Card card = pcscEid.getCard();
    // GET FEATURE LIST
    byte[] features = card.transmitControlCommand(0x42000D48, new byte[0]);
    if (0 == features.length) {
        LOG.debug("no CCID reader");
        return;
    }
    LOG.debug("feature list: " + new String(Hex.encodeHex(features)));
    LOG.debug("feature verify pin direct: " + hasFeature(FEATURE_VERIFY_PIN_DIRECT_TAG, features));
    Integer verifyPinControl = findFeature(FEATURE_VERIFY_PIN_DIRECT_TAG, features);
    LOG.debug("VERIFY PIN control: 0x" + Integer.toHexString(verifyPinControl));

    CardChannel cardChannel = pcscEid.getCardChannel();
    CommandAPDU setApdu = new CommandAPDU(0x00, 0x22, 0x41, 0xB6, new byte[] { 0x04, // length of following data
            (byte) 0x80, // algo ref
            0x01, // rsa pkcs#1
            (byte) 0x84, // tag for private key ref
            (byte) 0x82 });
    ResponseAPDU responseApdu = cardChannel.transmit(setApdu);
    if (0x9000 != responseApdu.getSW()) {
        throw new RuntimeException("SELECT error");
    }

    byte[] verifyCommandData = createPINVerificationDataStructure();

    byte[] result = card.transmitControlCommand(verifyPinControl, verifyCommandData);
    responseApdu = new ResponseAPDU(result);
    LOG.debug("status work: " + Integer.toHexString(responseApdu.getSW()));
    if (0x9000 == responseApdu.getSW()) {
        LOG.debug("status OK");
    } else if (0x6401 == responseApdu.getSW()) {
        LOG.debug("canceled by user");
    } else if (0x6400 == responseApdu.getSW()) {
        LOG.debug("timeout");
    }
    /*
     * The other SW values are those from the VERIFY APDU itself.
     */
}

From source file:test.be.fedict.eid.applet.Pkcs15Test.java

@Test
public void testSelectPkcs15Application() throws Exception {
    CardChannel cardChannel = this.pcscEid.getCardChannel();
    byte[] aId = new byte[] { (byte) 0xa0, 0x00, 0x00, 0x01, 0x77, 0x50, 0x4b, 0x43, 0x53, 0x2d, 0x31, 0x35 };
    CommandAPDU selectApplicationApdu = new CommandAPDU(0x00, 0xA4, 0x04, 0x0C, aId);
    ResponseAPDU responseApdu = cardChannel.transmit(selectApplicationApdu);
    assertEquals(0x9000, responseApdu.getSW());
}

From source file:test.be.fedict.eid.applet.Pkcs15Test.java

@Test
public void testSelectBelpicApplication() throws Exception {
    CardChannel cardChannel = this.pcscEid.getCardChannel();
    byte[] belpicAID = new byte[] { (byte) 0xA0, 0x00, 0x00, 0x00, 0x30, 0x29, 0x05, 0x70, 0x00, (byte) 0xAD,
            0x13, 0x10, 0x01, 0x01, (byte) 0xFF };
    CommandAPDU selectApplicationApdu = new CommandAPDU(0x00, 0xA4, 0x04, 0x0C, belpicAID);
    ResponseAPDU responseApdu = cardChannel.transmit(selectApplicationApdu);
    assertEquals(0x9000, responseApdu.getSW());
}