Example usage for javax.smartcardio ResponseAPDU getData

List of usage examples for javax.smartcardio ResponseAPDU getData

Introduction

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

Prototype

public byte[] getData() 

Source Link

Document

Returns a copy of the data bytes in the response body.

Usage

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

/**
 * //from ww  w .ja  v a2s .  com
 * @param pin
 * @param counterID
 * @return the 7-byte stream keyID || index || threshold || cursor(4 bytes).
 */
private byte[] readCounter(int pin, int counterID) {
    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) counterID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readCounter, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 7);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readCounter: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readCounter: " + response);
        System.out.println("With data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        } else {
            return null;
        }
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

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

/**
  * //from w ww  . j av  a  2  s .c o  m
  * @param pin
  * @param credentialID
  * @return byte array containing: issuerID || size(v) [2 bytes] || size(kv) [2 bytes] || status || prescount
  */
private byte[] readCredential(int pin, int credentialID) {
    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) credentialID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readCredential, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 7);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readCredential: " + Arrays.toString(buf.array()));
        System.out.println("Reading the on-board credential with ID=" + credentialID);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readCredential: " + response);
        System.out.println("With the data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

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

/**
 * @param pin//from w w w  . j  a va  2s. c om
 * @param issuerID
 * @return byte array containing: groupID || genID1 || genID2 || numpres || counterID
 */
private byte[] readIssuer(int pin, int issuerID) {
    if (cachedIssuerByteArray.containsKey(issuerID)) {
        byte[] cached = cachedIssuerByteArray.get(issuerID);
        System.out.println("ReadIssuer - use cached : " + (cached == null ? null : Arrays.toString(cached)));
        return cached;
    }

    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) issuerID;
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readIssuer, 0, 0, 5 });
    buf.put(data);
    buf.put((byte) 5);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readIssuer: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readIssuer: " + response);
        System.out.println("With the data: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            cachedIssuerByteArray.put(issuerID, response.getData());
            return response.getData();
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    cachedIssuerByteArray.put(issuerID, null);
    return null;
}

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

@Override
public int pinTrialsLeft() {
    try {/*www. j  av a  2  s . co  m*/
        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 {/*w  w  w. j  av a2 s.  co 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

private BigInteger computeDevicePublicKeyResponse(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(13);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getDeviceResponse, 0, 0, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { 0, 0 });
    buf.position(0);/*from  w w w.  j a  v  a 2 s .  com*/
    try {
        if (printInput)
            System.out.println("Input for getDeviceResponse: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from getDeviceResponse: " + response);
        System.out.println("And this is the output: " + Arrays.toString(response.getData()));
        System.out.println("which gives this BigInteger: " + new BigInteger(1, response.getData()));
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return new BigInteger(1, response.getData());
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

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

public int getMode() {
    try {//from   w  w  w.  jav  a 2 s. c  o m
        ByteBuffer buf = ByteBuffer.allocate(5);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getMode, 0, 0, 1 });
        buf.position(0);
        System.out.println("Input to GetMode: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Reponse from getMode: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData()[0];
        }
    } catch (CardException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return -1;
}

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

public byte[] getChallenge(int size) {
    //TODO: Make this work for challenge sizes of 256 (or 0)
    if ((size > 256) || (size < 1)) {
        System.err.println("Argument 'size' for getChallenge should be in the range [1,256]");
        return null;
    }//from  w  w  w. j  a va2s .  co m
    try {
        int realSize = size;
        if (realSize == 256) {
            realSize = 0;
        }
        ByteBuffer buf = ByteBuffer.allocate(7);
        System.out.println("Le: " + (byte) size);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getChallenge, 0, 0, 1, (byte) realSize, 0 });
        buf.position(0);
        System.out.println("Input for getChallenge: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from getChallenge: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        } else {
            return null;
        }
    } catch (CardException e) {
        return null;
    }
}

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

@Override
public ZkProofResponse finalizeZkProof(int pin, byte[] challengeHashPreimage, Set<URI> credentialIDs,
        Set<URI> scopeExclusivePseudonyms, byte[] nonceCommitment) {
    byte[] data = new byte[4 + 1 + 1 + 16 + challengeHashPreimage.length]; //pin, prooverID, d which is the number of proofs, proofsession and h
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = 1; //TODO: ProoverID - Hardcoded for now
    data[5] = 1; //number of proofs - hardcoded to 1 for pilot.
    System.out.println("nonce length: " + nonceCommitment.length);
    System.out.println("data length: " + data.length);
    System.arraycopy(nonceCommitment, 0, data, 6, 16);
    System.arraycopy(challengeHashPreimage, 0, data, 4 + 1 + 1 + 16, challengeHashPreimage.length);

    ByteBuffer buf = ByteBuffer.allocate(7 + data.length);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.startResponses, 0, 0, 0 });
    buf.put(this.intLengthToShortByteArr(data.length));
    buf.put(data);//from   w ww.  j a va 2  s.  co m
    buf.position(0);
    if (printInput)
        System.out.println("Input for startResponses: " + Arrays.toString(buf.array()));
    try {
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from startResponses: " + response);
        System.out.println("And this is the output: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
            return null;
        }
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }

    ZkProofResponse zkpr = new ZkProofResponse();

    zkpr.responseForDeviceSecret = this.computeDevicePublicKeyResponse(pin);

    //For Get issuance response
    for (URI uri : credentialIDs) {
        byte credID = this.getCredentialIDFromUri(pin, uri);
        byte[] credInfo = readCredential(pin, credID);
        byte status = credInfo[5];
        String command = "getIssuanceResponse";
        byte issueOrPresent = this.getIssuanceResponse;
        if (status >= 2) {
            System.out.println("Presentation. Status: " + status);
            //credential has already been issued, so we want to present response.
            command = "getPresentationResponse";
            issueOrPresent = this.getPresentationResponse;
        }
        buf = ByteBuffer.allocate(14);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, issueOrPresent, 0, 0, 0, 0, 5 });
        buf.put(this.pinToByteArr(pin));
        buf.put(credID);
        buf.put(new byte[] { 0, 0 });
        buf.position(0);
        try {
            if (printInput)
                System.out.println("Input for " + command + ": " + Arrays.toString(buf.array()));
            ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from " + command + ": " + response);
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return null;
            }
            System.out.println("data returned: size: " + response.getData().length + " value: "
                    + Arrays.toString(response.getData()));
            byte[] zx = new byte[response.getNr() / 2];
            byte[] zv = new byte[response.getNr() / 2];
            System.arraycopy(response.getData(), 0, zx, 0, zx.length);
            System.arraycopy(response.getData(), zx.length, zv, 0, zv.length);
            System.out.println("zx: " + Arrays.toString(zx));
            System.out.println("zv: " + Arrays.toString(zv));
            zkpr.responseForCourses.put(uri, new BigInteger(1, zv));
            zkpr.responseForDeviceSecret = new BigInteger(1, zx);
        } catch (CardException e) {
            e.printStackTrace();
            return null;
        }
    }

    return zkpr;
}

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

public SmartcardStatusCode issueCredentialOnSmartcard(int pin, byte credID) {
    byte[] credInfo = this.readCredential(pin, credID);
    byte status = credInfo[5];
    if (status == 0) {
        try {/*ww w .j  a  v  a  2 s  . c o  m*/
            //Start commitments
            byte[] data = new byte[5];
            System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
            data[4] = 1; //ProverID - TODO: hardcoded to 1 as of now. Assuming there can be only 1 for the pilot
            ByteBuffer buf = ByteBuffer.allocate(11);
            buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.startCommitments, 0, 0, 5 });
            buf.put(data);
            buf.put((byte) 16);
            buf.position(0);
            if (printInput)
                System.out.println("Input for startCommitments: " + Arrays.toString(buf.array()));
            ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from startCommitments: " + response);
            System.out.println("And this is the output: " + Arrays.toString(response.getData()));
            byte[] proofSession = response.getData();
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return this.evaluateStatus(response);
            }

            //Issue the credential
            buf = ByteBuffer.allocate(14);
            buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, getIssuanceCommitment, 0, 0, 0, 0, 5 });
            buf.put(this.pinToByteArr(pin));
            buf.put(credID);
            buf.put(new byte[] { 0, 0 });
            buf.position(0);

            if (printInput)
                System.out.println("Input for getIssuanceCommitment: " + Arrays.toString(buf.array()));
            response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from getIssuanceCommitment: " + response);
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return this.evaluateStatus(response);
            }

            //Start responses
            data = new byte[4 + 1 + 1 + 16 + 1]; //pin, prooverID, d which is the number of proofs, proofsession and h
            System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
            data[4] = 1; //TODO: ProoverID - Hardcoded for now
            data[5] = 1; //number of proofs - hardcoded to 1 for pilot.
            System.arraycopy(proofSession, 0, data, 6, 16);

            buf = ByteBuffer.allocate(7 + data.length);
            buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.startResponses, 0, 0, 0 });
            buf.put(this.intLengthToShortByteArr(data.length));
            buf.put(data);
            buf.position(0);
            if (printInput)
                System.out.println("Input for startResponses: " + Arrays.toString(buf.array()));
            response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from startResponses: " + response);
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return this.evaluateStatus(response);
            }

            //Set status of cred to 2 - issued finalized
            buf = ByteBuffer.allocate(14);
            buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, getIssuanceResponse, 0, 0, 0, 0, 5 });
            buf.put(this.pinToByteArr(pin));
            buf.put(credID);
            buf.put(new byte[] { 0, 0 });
            buf.position(0);
            if (printInput)
                System.out.println("Input for getIssuanceResponse: " + Arrays.toString(buf.array()));
            response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from getIssuanceResponse: " + response);
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return this.evaluateStatus(response);
            }
            credInfo = this.readCredential(pin, credID);
            status = credInfo[5];
            System.out.println(
                    "After issuing the credential with ID " + credID + ", it now has status: " + status);
        } catch (CardException e) {
            throw new RuntimeException("issueCred on smartcard failed.", e);
        }
    } else {
        System.out.println("Warn: Credential on sc attempted issued, but was already issued");
    }
    return SmartcardStatusCode.OK;
}