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, int ne) 

Source Link

Document

Constructs a CommandAPDU from the four header bytes, command data, and expected response data length.

Usage

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@Override
public short getDeviceID(int pin) {
    try {/*from ww  w  . j a  va2s  .  c  o  m*/
        ResponseAPDU response = this.transmitCommand(
                new CommandAPDU(this.ABC4TRUSTCMD, this.getDeviceID, 0, 0, this.pinToByteArr(pin), 2));
        System.out.println("Response from getdeviceID: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return ByteBuffer.wrap(response.getData()).getShort();
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return -1;
}

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 .j  a  v a2s. 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);//ww  w.  j a va 2  s  .c o  m
    // 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   ww  w. jav a 2s  . 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 {//  ww  w. ja v a 2 s .  co  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

@Override
public TokenFile select(int path) throws IOException {

    if (this.currentFile == null)
        throw new IOException("No current DF selected.");

    // SELECT FILE, P1=0x00, P2=0x00, ID -> select EF or DF
    CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x00, 0x00, PathHelper.idToPath(path), DEFAULT_LE);

    try {//  ww  w.  ja  v  a2s.co m
        ResponseAPDU resp = this.channel.transmit(cmd);

        DataInputStream dis = getSelectFileData(resp);

        long bodySize = -1;
        long fileSize = -1;
        int acRead = TokenFileAcl.AC_ALWAYS;
        int acUpdate = TokenFileAcl.AC_ALWAYS;
        int acAppend = TokenFileAcl.AC_ALWAYS;
        int acDeactivate = TokenFileAcl.AC_ALWAYS;
        int acActivate = TokenFileAcl.AC_ALWAYS;
        int acDelete = TokenFileAcl.AC_ALWAYS;
        int acAdmin = TokenFileAcl.AC_ALWAYS;
        int acIncrease = TokenFileAcl.AC_ALWAYS;
        int acDecrease = TokenFileAcl.AC_ALWAYS;

        int tag;

        while ((tag = dis.read()) >= 0) {
            int n = dis.read();
            if (n < 0)
                break;

            switch (tag) {
            case 0x80:
                if (n != 2)
                    throw new IOException("Invalid length [" + n + "] of FCI tag 0x80.");
                fileSize = dis.readUnsignedShort();
                break;

            case 0x83:
                if (n != 2)
                    throw new IOException("Invalid length [" + n + "] of FCI tag 0x83.");
                int tpath = dis.readUnsignedShort();
                if (tpath != path)
                    throw new IOException("File ID [" + PathHelper.formatID(tpath)
                            + "] reported by SELECT FILE differs from requested ID ["
                            + PathHelper.formatID(path) + "].");
                break;

            case 0x81:
                if (n != 2)
                    throw new IOException("Invalid length [" + n + "] of FCI tag 0x81.");
                bodySize = dis.readUnsignedShort();
                break;

            case 0x86:
                if (n >= 1)
                    acRead = dis.read();
                if (n >= 2)
                    acUpdate = dis.read();
                if (n >= 3)
                    acAppend = dis.read();
                if (n >= 4)
                    acDeactivate = dis.read();
                if (n >= 5)
                    acActivate = dis.read();
                if (n >= 6)
                    acDelete = dis.read();
                if (n >= 7)
                    acAdmin = dis.read();
                if (n >= 8)
                    acIncrease = dis.read();
                if (n >= 9)
                    acDecrease = dis.read();

                if (n != 9 && n != 8)
                    log.warn("Invalid length [" + n + "] of FCI tag 0x86 for EF.");

                if (n > 9)
                    dis.skipBytes(n - 9);
                break;

            default:
                byte[] tmp = new byte[n];
                dis.readFully(tmp);
                log.warn("skipping FCI tag [0x" + Integer.toHexString(tag) + "], data [" + Util.asHex(tmp)
                        + "].");
            }
        }

        if (fileSize >= 0)
            this.currentFile = new EF(new TokenPath(this.currentFile.getPath(), path), fileSize, acRead,
                    acUpdate, acAppend, acDeactivate, acActivate, acDelete, acAdmin, acIncrease, acDecrease);
        else if (bodySize >= 0)
            this.currentFile = new DF(new TokenPath(this.currentFile.getPath(), path), bodySize, acRead,
                    acUpdate, acAppend, acDeactivate, acActivate, acDelete, acAdmin, acIncrease);
        else
            throw new IOException("No 0x80 or 0x81 tag specified in order to distinguish between DF an EF.");

        return this.currentFile;

    } catch (CardException e) {
        throw new PKCS15Exception("Error sending SELECT FILE", e);
    }
}

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

@Override
public DF selectDF(int path) throws IOException {

    if (this.currentFile == null)
        throw new IOException("No current DF selected.");

    // SELECT FILE, P1=0x01, P2=0x00, no data -> select DF
    CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x01, 0x00, PathHelper.idToPath(path), DEFAULT_LE);

    return this.selectDFInternal(cmd, new TokenPath(this.currentFile.getPath(), path));
}

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

@Override
public EF selectEF(int path) throws IOException {

    if (this.currentFile == null)
        throw new IOException("No current DF selected.");

    // SELECT FILE, P1=0x02, P2=0x00, no data -> select EF
    CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x02, 0x00, PathHelper.idToPath(path), DEFAULT_LE);

    try {//from  w w  w.ja  v a2  s .  c  om
        ResponseAPDU resp = this.channel.transmit(cmd);

        DataInputStream dis = getSelectFileData(resp);

        long fileSize = 0;
        int acRead = TokenFileAcl.AC_ALWAYS;
        int acUpdate = TokenFileAcl.AC_ALWAYS;
        int acAppend = TokenFileAcl.AC_ALWAYS;
        int acDeactivate = TokenFileAcl.AC_ALWAYS;
        int acActivate = TokenFileAcl.AC_ALWAYS;
        int acDelete = TokenFileAcl.AC_ALWAYS;
        int acAdmin = TokenFileAcl.AC_ALWAYS;
        int acIncrease = TokenFileAcl.AC_ALWAYS;
        int acDecrease = TokenFileAcl.AC_ALWAYS;

        int tag;

        while ((tag = dis.read()) >= 0) {
            int n = dis.read();
            if (n < 0)
                break;

            switch (tag) {
            case 0x80:
                if (n != 2)
                    throw new IOException("Invalid length [" + n + "] of FCI tag 0x80.");
                fileSize = dis.readUnsignedShort();
                break;

            case 0x83:
                if (n != 2)
                    throw new IOException("Invalid length [" + n + "] of FCI tag 0x83.");
                int tpath = dis.readUnsignedShort();
                if (tpath != path)
                    throw new IOException("File ID [" + PathHelper.formatID(tpath)
                            + "] reported by SELECT FILE differs from requested ID ["
                            + PathHelper.formatID(path) + "].");
                break;

            case 0x86:
                if (n >= 1)
                    acRead = dis.read();
                if (n >= 2)
                    acUpdate = dis.read();
                if (n >= 3)
                    acAppend = dis.read();
                if (n >= 4)
                    acDeactivate = dis.read();
                if (n >= 5)
                    acActivate = dis.read();
                if (n >= 6)
                    acDelete = dis.read();
                if (n >= 7)
                    acAdmin = dis.read();
                if (n >= 8)
                    acIncrease = dis.read();
                if (n >= 9)
                    acDecrease = dis.read();

                if (n != 9)
                    log.warn("Invalid length [" + n + "] of FCI tag 0x86 for EF.");

                if (n > 9)
                    dis.skipBytes(n - 9);
                break;

            default:
                byte[] tmp = new byte[n];
                dis.readFully(tmp);
                log.warn("skipping FCI tag [0x" + Integer.toHexString(tag) + "], data [" + Util.asHex(tmp)
                        + "].");
            }
        }

        EF ef = new EF(new TokenPath(this.currentFile.getPath(), path), fileSize, acRead, acUpdate, acAppend,
                acDeactivate, acActivate, acDelete, acAdmin, acIncrease, acDecrease);

        this.currentFile = ef;
        return ef;

    } catch (CardException e) {
        throw new PKCS15Exception("Error sending select MF", e);
    }
}