Example usage for javax.smartcardio CardChannel transmit

List of usage examples for javax.smartcardio CardChannel transmit

Introduction

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

Prototype

public abstract ResponseAPDU transmit(CommandAPDU command) throws CardException;

Source Link

Document

Transmits the specified command APDU to the Smart Card and returns the response APDU.

Usage

From source file:org.opensc.pkcs15.script.SimpleCommand.java

@Override
public Command execute(CardChannel channel) throws CardException {

    log.debug("Tranmitting APDU [" + Util.asHex(this.getRequest().getBytes()) + "].");

    ResponseAPDU resp = channel.transmit(this.getRequest());

    log.debug("Got response [" + Util.asHex(resp.getBytes()) + "].");

    return this.checkResponse(resp);
}

From source file:src.eidreader.EstEIDUtil.java

public static byte[] sendCommand(CardChannel channel, CommandAPDU command) throws CardException {
    ResponseAPDU responseAPDU = channel.transmit(command);
    int responseStatus = responseAPDU.getSW();

    if (!isResponseOk(responseStatus)) {
        throw new RuntimeException("Error code: " + responseStatus);
    }//from   w  w w  .ja va 2 s .co m

    return responseAPDU.getData();
}

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();//  w ww  .ja v  a2 s . c  om
    }
    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();//  w ww  .ja va  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();//www.j a  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 ww w. j a va2  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(
            new CommandAPDU(0x00, 0xA4, 0x08, 0x0C, new byte[] { 0x3F, 0x00, (byte) 0xDF, 0x01, 0x40, 0x35 }));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int offset = 0;
    ResponseAPDU responseApdu;/*from   www . j  a  v a2s .  c  o  m*/
    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 selectCardManager(CardChannel cardChannel) {
    CommandAPDU selectApplicationApdu = new CommandAPDU(0x00, 0xA4, 0x04, 0x00);
    ResponseAPDU responseApdu;//from  w  w w.  j  av a  2 s. co  m
    try {
        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

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   w  w w .  j  a  va  2 s. c  om
}

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