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:org.opensc.pkcs15.token.impl.CardOSToken.java

@Override
public DF createDF(int path, long size, DFAcl acl) throws IOException {

    if (size < 0 || size > 65535L)
        throw new PKCS15Exception(
                "Illegal size [" + size + "] for DF ["
                        + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "].",
                PKCS15Exception.ERROR_INVALID_PARAMETER);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(256);
    DataOutputStream dos = new DataOutputStream(bos);

    dos.write(0x62);// w  ww .  java  2  s . c  om
    // length of subsequent FCP data field, to be filled at end.
    dos.write(0x00);

    // fill in FCP data
    //  DF body size
    dos.write(0x81);
    dos.write(0x02);
    dos.writeShort((int) size);

    // File descriptor: 38h DF
    dos.write(0x82);
    dos.write(0x01);
    dos.write(0x38);

    // File ID
    dos.write(0x83);
    dos.write(0x02);
    dos.writeShort(path);

    // Default file status.
    dos.write(0x85);
    dos.write(0x01);
    dos.write(0x00);

    // ACL definitions
    dos.write(0x86);
    dos.write(0x08);
    dos.write(acl.getAcLifeCycle());
    dos.write(acl.getAcUpdate());
    dos.write(acl.getAcAppend());
    dos.write(acl.getAcDeactivate());
    dos.write(acl.getAcActivate());
    dos.write(acl.getAcDelete());
    dos.write(acl.getAcAdmin());
    dos.write(acl.getAcCreate());

    // get command data.
    dos.flush();
    byte[] data = bos.toByteArray();

    // fill in length of subsequent FCP data field, to be filled at end.
    data[1] = (byte) (data.length - 2);

    // CREATE FILE, P1=0x00, P2=0x00, ID -> read current EF from position 0.
    CommandAPDU cmd = new CommandAPDU(0x00, 0xE0, 0x00, 0x00, data, DEFAULT_LE);

    try {
        ResponseAPDU resp = this.channel.transmit(cmd);

        if (resp.getSW() != PKCS15Exception.ERROR_OK)
            throw new PKCS15Exception("CREATE FILE for DF ["
                    + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "] returned error",
                    resp.getSW());

    } catch (CardException e) {
        throw new PKCS15Exception("Error sending CREATE FILE for DF ["
                + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "]", e);
    }

    return new DF(new TokenPath(this.currentFile.getPath(), path), size, acl);
}

From source file:org.opensc.pkcs15.token.impl.CardOSToken.java

@Override
public EF createEF(int path, long size, EFAcl acl) throws IOException {

    if (size < 0 || size > 65535L)
        throw new PKCS15Exception(
                "Illegal size [" + size + "] for EF ["
                        + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "].",
                PKCS15Exception.ERROR_INVALID_PARAMETER);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(256);
    DataOutputStream dos = new DataOutputStream(bos);

    dos.write(0x62);// w w w  . j  av a2 s.com
    // length of subsequent FCP data field, to be filled at end.
    dos.write(0x00);

    // *** fill in FCP data
    //   Only EF:      Net size in bytes
    dos.write(0x80);
    dos.write(0x02);
    dos.writeShort((int) size);

    // File descriptor: 01h BINARY
    dos.write(0x82);
    dos.write(0x01);
    dos.write(0x01);

    // File ID
    dos.write(0x83);
    dos.write(0x02);
    dos.writeShort(path);

    // Default file status.
    dos.write(0x85);
    dos.write(0x01);
    dos.write(0x00);

    // ACL definitions
    dos.write(0x86);
    dos.write(0x09);
    dos.write(acl.getAcRead());
    dos.write(acl.getAcUpdate());
    dos.write(acl.getAcAppend());
    dos.write(acl.getAcDeactivate());
    dos.write(acl.getAcActivate());
    dos.write(acl.getAcDelete());
    dos.write(acl.getAcAdmin());
    dos.write(acl.getAcIncrease());
    dos.write(acl.getAcDecrease());

    // *** get command data.
    dos.flush();
    byte[] data = bos.toByteArray();

    // fill in length of subsequent FCP data field, to be filled at end.
    data[1] = (byte) (data.length - 2);

    // CREATE FILE, P1=0x00, P2=0x00, ID -> read current EF from position 0.
    CommandAPDU cmd = new CommandAPDU(0x00, 0xE0, 0x00, 0x00, data, DEFAULT_LE);

    try {
        ResponseAPDU resp = this.channel.transmit(cmd);

        if (resp.getSW() != PKCS15Exception.ERROR_OK)
            throw new PKCS15Exception("CREATE FILE for EF ["
                    + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "] returned error",
                    resp.getSW());

    } catch (CardException e) {
        throw new PKCS15Exception("Error sending CREATE FILE for EF ["
                + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "]", e);
    }

    return new EF(new TokenPath(this.currentFile.getPath(), path), size, acl);
}

From source file:org.opensc.pkcs15.token.impl.CardOSToken.java

@Override
public void deleteDF(int path) throws IOException {

    // DELETE FILE, P1=0x00, P2=0x00, ID -> read current EF from position 0.
    CommandAPDU cmd = new CommandAPDU(0x00, 0xE4, 0x00, 0x00, PathHelper.idToPath(path), DEFAULT_LE);

    try {/*from w w w  . j av  a  2  s. c  om*/
        ResponseAPDU resp = this.channel.transmit(cmd);

        if (resp.getSW() != PKCS15Exception.ERROR_OK)
            throw new PKCS15Exception("DELETE FILE for DF ["
                    + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "] returned error",
                    resp.getSW());

    } catch (CardException e) {
        throw new PKCS15Exception("Error sending DELETE FILE for DF ["
                + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "]", e);
    }
}

From source file:org.opensc.pkcs15.token.impl.CardOSToken.java

@Override
public void deleteEF(int path) throws IOException {

    // DELETE FILE, P1=0x00, P2=0x00, ID -> read current EF from position 0.
    CommandAPDU cmd = new CommandAPDU(0x00, 0xE4, 0x00, 0x00, PathHelper.idToPath(path), DEFAULT_LE);

    try {/*from   w w w  . j  a v  a  2  s. c o  m*/
        ResponseAPDU resp = this.channel.transmit(cmd);

        if (resp.getSW() != PKCS15Exception.ERROR_OK)
            throw new PKCS15Exception("DELETE FILE for EF ["
                    + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "] returned error",
                    resp.getSW());

    } catch (CardException e) {
        throw new PKCS15Exception("Error sending DELETE FILE for EF ["
                + PathHelper.formatPathAppend(this.currentFile.getPath(), path) + "]", e);
    }
}

From source file:org.opensc.pkcs15.token.impl.CardOSToken.java

private DataInputStream getSelectFileData(ResponseAPDU resp) throws IOException {
    if (resp.getSW() != PKCS15Exception.ERROR_OK)
        throw new PKCS15Exception("Card error in response to SELECT FILE", resp.getSW());

    if (resp.getNr() < 2)
        throw new IOException("response to SELECT FILE contains less than 2 bytes.");

    int b = resp.getData()[0];

    if (b != 0x6f)
        throw new IOException("response to SELECT FILE contains no FCI data.");

    int n = ((int) resp.getData()[1]) & 0xff;

    if (n != resp.getNr() - 2)
        throw new IOException("FCI dat in response to SELECT FILE contains invalid length.");

    return new DataInputStream(new ByteArrayInputStream(resp.getData(), 2, n));

}

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 www  .ja v  a2s  . co m*/

    return responseAPDU.getData();
}

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  .  ja  va2  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;//www. j  a  v  a  2  s .c  o  m
    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();//  w  w  w . ja  v a2 s.  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   ww w  .  ja v a 2s  . co  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();
    }
}