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:eu.abc4trust.smartcard.HardwareSmartcard.java

public String getVersion() {
    try {//  w w w .  ja v  a  2  s .  c  o  m
        ResponseAPDU response = this
                .transmitCommand(new CommandAPDU(this.ABC4TRUSTCMD, this.getVersion, 0, 0, 64));
        System.out.println("Response from getVersion: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            String res = "";
            byte[] data = response.getData();
            for (int i = 0; i < 64; i++) {
                res += (char) (data[i] & 0xFF);
            }
            return res;
        }
    } catch (CardException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

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

public SmartcardStatusCode setVirginMode(byte[] mac) {
    try {/*from w w  w .j ava2 s .com*/
        ResponseAPDU response = this
                .transmitCommand(new CommandAPDU(this.ABC4TRUSTCMD, this.setVirginMode, 0, 0, mac));
        System.out.println("response from setVirginMode: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        System.err.println("Failed to setVirginMode : " + e);
        return SmartcardStatusCode.NOT_FOUND;
    }
}

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

public SmartcardStatusCode setAuthenticationKey(BigInteger pk, int keyID, RSAKeyPair rootKey) {
    byte[] pk_bytes = pk.toByteArray();
    pk_bytes = removeSignBit(pk_bytes);/*  ww w .j  av a2 s  .  c  om*/
    ResponseAPDU response;
    try {
        int mode = this.getMode();
        if (mode == 1) {
            this.putData(pk_bytes);
        } else if (mode == 2) {
            System.out.println("Can only use setAuthenticationKey in root mode");
            return SmartcardStatusCode.UNAUTHORIZED;
        }
        System.out.println("Input for setAuthKey: " + Arrays.toString(
                new byte[] { (byte) this.ABC4TRUSTCMD, this.setAuthenticationKey, 0, 0, 1, (byte) keyID }));
        response = this.transmitCommand(new CommandAPDU(this.ABC4TRUSTCMD, this.setAuthenticationKey, 0, 0,
                new byte[] { (byte) keyID }));
        System.out.println("response from setAuthKey: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

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

@Override
public int pinTrialsLeft() {
    try {/*from   w w w .  j  a va 2 s .  c om*/
        ResponseAPDU response = this
                .transmitCommand(new CommandAPDU(this.ABC4TRUSTCMD, this.pinTrialsLeft, 0, 0, 1));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData()[0];
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return -1;
}

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

@Override
public int pukTrialsLeft() {
    try {//  www  .j  a va2s.c o m
        ResponseAPDU response = this
                .transmitCommand(new CommandAPDU(this.ABC4TRUSTCMD, this.pukTrialsLeft, 0, 0, 1));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData()[0];
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return -1;
}

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

@Override
public SmartcardStatusCode resetPinWithPuk(int puk, int newPin) {
    byte[] data = new byte[8 + 4];
    System.arraycopy(this.pukToByteArr(puk), 0, data, 0, 8);
    System.arraycopy(this.pinToByteArr(newPin), 0, data, 8, 4);
    try {//  w w  w .  j  a  v a2  s. co m
        ResponseAPDU response = this
                .transmitCommand(new CommandAPDU(this.ABC4TRUSTCMD, this.resetPin, 0, 0, data));
        System.out.println("response from resetPinWithPuk: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

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

@Override
public SmartcardStatusCode deleteIssuer(int pin, URI issuerParameters, RSAKeyPair rootKey) {
    if (this.getMode() != 1) {
        System.out.println("Can only use deleteIssuer in root mode");
        return SmartcardStatusCode.UNAUTHORIZED;
    }//  w  ww  .  j  ava 2s. c om
    byte issuerID = this.getIssuerIDFromUri(pin, issuerParameters);
    try {
        ResponseAPDU response = this.transmitCommand(
                new CommandAPDU(this.ABC4TRUSTCMD, this.removeIssuer, 0, 0, new byte[] { issuerID }));
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

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

@Override
public InputStream readEFData() throws IOException {

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

    // READ BINARY, P1=0x00, P2=0x00, ID -> read current EF from position 0.
    CommandAPDU cmd = new CommandAPDU(0x00, 0xB0, 0x00, 0x00, DEFAULT_EXTENDED_LE);

    try {/*  w w  w  .  ja va  2 s. c om*/
        ResponseAPDU resp = this.channel.transmit(cmd);

        return new ByteArrayInputStream(resp.getData());

    } catch (CardException e) {
        throw new PKCS15Exception("Error sending READ BINARY", e);
    }
}

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

@Override
public MF selectMF() throws IOException {

    // SELECT FILE, P1=0x00, P2=0x00, no data -> select MF
    CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x00, 0x00, DEFAULT_LE);

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

        DataInputStream dis = getSelectFileData(resp);

        long bodySize = 0;
        int acLifeCycle = 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 acCreate = TokenFileAcl.AC_ALWAYS;
        int acExecute = TokenFileAcl.AC_ALWAYS;
        int acAllocate = TokenFileAcl.AC_ALWAYS;

        int tag;

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

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

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

            case 0x86:
                if (n >= 1)
                    acLifeCycle = 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)
                    acCreate = dis.read();
                if (n >= 9)
                    acExecute = dis.read();
                if (n >= 10)
                    acAllocate = dis.read();

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

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

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

        MF mf = new MF(PathHelper.MF_PATH, bodySize, acLifeCycle, acUpdate, acAppend, acDeactivate, acActivate,
                acDelete, acAdmin, acCreate, acExecute, acAllocate);

        this.currentFile = mf;
        return mf;

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

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

@Override
public DF selectParentDF() throws IOException {

    // SELECT FILE, P1=0x03, P2=0x00, no data -> select parent DF
    CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x03, 0x00, DEFAULT_LE);

    return this.selectDFInternal(cmd, this.currentFile.getPath().getParent());
}