Example usage for javax.smartcardio CommandAPDU CommandAPDU

List of usage examples for javax.smartcardio CommandAPDU CommandAPDU

Introduction

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

Prototype

public CommandAPDU(int cla, int ins, int p1, int p2, byte[] data) 

Source Link

Document

Constructs a CommandAPDU from the four header bytes and command data.

Usage

From source file:src.eidreader.EstEIDUtil.java

private void selectFile(byte[] fileId) throws CardException, FileNotFoundException {
    CommandAPDU selectFileApdu = new CommandAPDU(0x00, 0xA4, 0x08, 0x0C, fileId);
    ResponseAPDU responseApdu = transmit(selectFileApdu);
    if (0x9000 != responseApdu.getSW()) {
        throw new FileNotFoundException(
                "wrong status word after selecting file: " + Integer.toHexString(responseApdu.getSW()));
    }/*from   w  w w  .j  a va 2  s . c o  m*/
    try {
        // SCARD_E_SHARING_VIOLATION fix
        Thread.sleep(20);
    } catch (InterruptedException e) {
        throw new RuntimeException("sleep error: " + e.getMessage());
    }
}

From source file:src.eidreader.EstEIDUtil.java

private byte[] readBinary() throws CardException, IOException {
    int offset = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] data;/*from   w  w w  .j a  va2  s  . com*/
    do {
        CommandAPDU readBinaryApdu = new CommandAPDU(0x00, 0xB0, offset >> 8, offset & 0xFF, BLOCK_SIZE);
        ResponseAPDU responseApdu = transmit(readBinaryApdu);
        int sw = responseApdu.getSW();
        if (0x6B00 == sw) {
            /*
             * Wrong parameters (offset outside the EF) End of file reached.
             * Can happen in case the file size is a multiple of 0xff bytes.
             */
            break;
        }
        if (0x9000 != sw) {
            throw new IOException("APDU response error: " + responseApdu.getSW());
        }

        /*
         * Introduce some delay for old Belpic V1 eID cards.
         */
        // try {
        // Thread.sleep(50);
        // } catch (InterruptedException e) {
        // throw new RuntimeException("sleep error: " + e.getMessage(), e);
        // }
        data = responseApdu.getData();
        baos.write(data);
        offset += data.length;
    } while (BLOCK_SIZE == data.length);
    return baos.toByteArray();
}

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

@Test
public void pcscMSE_SET() throws Exception {
    this.messages = new Messages(Locale.GERMAN);
    PcscEid pcscEid = new PcscEid(new TestView(), this.messages);
    if (false == pcscEid.isEidPresent()) {
        LOG.debug("insert eID card");
        pcscEid.waitForEidPresent();/*from   w  ww .jav  a2s  .  co  m*/
    }
    CardChannel cardChannel = pcscEid.getCardChannel();
    try {
        CommandAPDU setApdu = new CommandAPDU(0x00, 0x22, 0x41, 0xB6, new byte[] { 0x04, // length of following data
                (byte) 0x80, // algo ref
                // 0x01, // rsa pkcs#1
                // 0x02, // PKCS1-SHA1
                // 0x04, // PKCS1-MD5
                // 0x08, // PKCS1-SHA256
                // 0x10, // PKCS1-PSS-SHA1
                0x20, // PKCS1-PSS-SHA256
                // (byte) 0xfb, // foobar
                (byte) 0x84, // tag for private key ref
                PcscEid.AUTHN_KEY_ID });
        ResponseAPDU responseAPDU = cardChannel.transmit(setApdu);
        assertEquals(0x9000, responseAPDU.getSW());
    } finally {
        pcscEid.close();
    }
}

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

@Test
public void createPSSSignature() throws Exception {
    this.messages = new Messages(Locale.GERMAN);
    PcscEid pcscEid = new PcscEid(new TestView(), this.messages);
    if (false == pcscEid.isEidPresent()) {
        LOG.debug("insert eID card");
        pcscEid.waitForEidPresent();/*from w  ww .  j  a v  a  2  s .  c  o m*/
    }
    CardChannel cardChannel = pcscEid.getCardChannel();

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

    try {
        CommandAPDU setApdu = new CommandAPDU(0x00, 0x22, 0x41, 0xB6, new byte[] { 0x04, // length of following data
                (byte) 0x80, // algo ref
                0x10, // PKCS1-PSS-SHA1
                (byte) 0x84, // tag for private key ref
                PcscEid.AUTHN_KEY_ID });
        ResponseAPDU responseAPDU = cardChannel.transmit(setApdu);
        assertEquals(0x9000, responseAPDU.getSW());

        pcscEid.verifyPin();

        CommandAPDU computeDigitalSignatureApdu = new CommandAPDU(0x00, 0x2A, 0x9E, 0x9A, digest);
        responseAPDU = cardChannel.transmit(computeDigitalSignatureApdu);
        assertEquals(0x9000, responseAPDU.getSW());

        byte[] signatureValue = responseAPDU.getData();

        LOG.debug("signature value length: " + signatureValue.length);

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

        Signature signature = Signature.getInstance("SHA1withRSA/PSS", "BC");
        signature.initVerify(authnCertificateChain.get(0).getPublicKey());
        signature.update(message);
        boolean result = signature.verify(signatureValue);
        assertTrue(result);
    } finally {
        pcscEid.close();
    }
}

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();//  w  w  w.  ja  v a  2  s  .c o 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   www .ja v  a 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

@Test
public void testReadPhoto() throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    CardTerminals cardTerminals = terminalFactory.terminals();
    CardTerminal cardTerminal = cardTerminals.list().get(0);
    Card card = cardTerminal.connect("T=0");
    CardChannel cardChannel = card.getBasicChannel();
    // select file
    cardChannel.transmit(/*from  w w w.jav a 2s . c o  m*/
            new CommandAPDU(0x00, 0xA4, 0x08, 0x0C, new byte[] { 0x3F, 0x00, (byte) 0xDF, 0x01, 0x40, 0x35 }));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int offset = 0;
    ResponseAPDU responseApdu;
    do {
        // read binary
        responseApdu = cardChannel.transmit(new CommandAPDU(0x00, 0xB0, offset >> 8, offset & 0xFF, 0xff));
        baos.write(responseApdu.getData());
        offset += responseApdu.getData().length;
    } while (responseApdu.getData().length == 0xff);
    BufferedImage photo = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
    JOptionPane.showMessageDialog(null, new ImageIcon(photo));
}

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");
    }/*from ww  w.ja v  a 2 s. co m*/
}

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

private ResponseAPDU verifyPin(byte[] pin, CardChannel cardChannel) throws CardException {
    byte[] verifyData = new byte[] { 0x24, pin[0], pin[1], (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF };

    CommandAPDU verifyApdu = new CommandAPDU(0x00, 0x20, 0x00, 0x01, verifyData);
    ResponseAPDU responseApdu = cardChannel.transmit(verifyApdu);
    return responseApdu;
}

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();/* w ww .  j av a 2s  .  c o  m*/
    }

    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();
    }
}