Example usage for javax.smartcardio ResponseAPDU getNr

List of usage examples for javax.smartcardio ResponseAPDU getNr

Introduction

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

Prototype

public int getNr() 

Source Link

Document

Returns the number of data bytes in the response body (Nr) or 0 if this APDU has no body.

Usage

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 w  w. j  a  v  a 2 s .c om
    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: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));

}