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

private BigInteger getGenerator(int pin, int groupID, int genID) {
    if (cachedGenerator.containsKey(groupID + ":" + genID)) {
        BigInteger cached = cachedGenerator.get(groupID + ":" + genID);
        System.out.println("Cached readGenerator: " + groupID + " : " + genID + " : " + cached);
        return cached;
    }/*from w  w w .  jav a 2 s.  c  om*/

    ByteBuffer buf = ByteBuffer.allocate(15);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readGenerator, 0, 0, 0, 0, 6 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { (byte) groupID, (byte) genID, 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readGenerator: " + groupID + " : " + genID + " : "
                    + Arrays.toString(buf.array()));

        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(readGenerator)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(readGenerator)", false);

        System.out.println("Response from readGenerator: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            BigInteger generator = new BigInteger(1, response.getData());
            System.out.println("Generator - is : " + groupID + " : " + genID + " : " + generator);
            cachedGenerator.put(groupID + ":" + genID, generator);
            return generator;
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

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

private BigInteger getScopeExclusiveCommitment(int pin, URI scope) {
    byte[] uri = this.uriToByteArr(scope);
    ByteBuffer buf = ByteBuffer.allocate(13 + uri.length);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getScopeExclusiveCommitment, 0, 0, 0 });
    buf.put(this.intLengthToShortByteArr(4 + uri.length));
    buf.put(this.pinToByteArr(pin));
    buf.put(uri);/* w  ww.  j a v a2s  . c o m*/
    buf.put(new byte[] { 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for getScopeExclusiveCommitment: " + Arrays.toString(buf.array()));

        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getScopeExclusiveCommitment)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getScopeExclusiveCommitment)", false);

        System.out.println("Response from getScopeExclusiveCommitment: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return new BigInteger(1, response.getData());
        } else {
            throw new RuntimeException("Failed scope exclusive Commitment. Card answered: " + response);
        }
    } catch (CardException e) {
        throw new RuntimeException("getScopeExclusiveCommitment crashed.", e);
    }
}

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

/**
 * // w w w  . j  ava 2  s. c o m
 * @param pin
 * @param groupID
 * @param compType 0: modulus, 1: group order 2: cofactor
 * @return
 */
private BigInteger getGroupComponent(int pin, int groupID, int compType) {
    if (cachedGroupComponent.containsKey(groupID + ":" + compType)) {
        BigInteger cached = cachedGroupComponent.get(groupID + ":" + compType);
        System.out.println("Cached readGroupComponent: " + groupID + " : " + compType + " : " + cached);
        return cached;
    }
    ByteBuffer buf = ByteBuffer.allocate(15);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readGroupComponent, 0, 0, 0, 0, 6 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { (byte) groupID, (byte) compType, 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readGroupComponent: " + groupID + " : " + compType + " : "
                    + Arrays.toString(buf.array()));

        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(readGroupComponent)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(readGroupComponent)", false);

        System.out.println("Response from readGroupComponent: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            BigInteger groupComponent = new BigInteger(1, response.getData());
            System.out.println("GroupComponent - is : " + groupID + " : " + compType + " : " + groupComponent);

            cachedGroupComponent.put(groupID + ":" + compType, groupComponent);
            return groupComponent;
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

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

@Override
public BigInteger computeScopeExclusivePseudonym(int pin, URI scope) {
    if (cachedScopeExclusivePseudonym.containsKey(scope)) {
        BigInteger pv = cachedScopeExclusivePseudonym.get(scope);
        System.out.println("Cached from getScopeExclusivePseudonym: " + scope + " : " + pv);
        return pv;
    }//from   ww w.  j  av a 2 s  .  com
    try {
        byte[] scopeBytes = this.uriToByteArr(scope);
        if (scopeBytes.length > 2044) {
            throw new RuntimeException("The inputted scope is too large.");
        }
        byte[] begin = new byte[] { (byte) this.ABC4TRUSTCMD, this.getScopeExclusivePseudonym, 0, 0, 0 };
        ByteBuffer buf = ByteBuffer.allocate(9 + 4 + scopeBytes.length);
        buf.put(begin);
        buf.put(this.intLengthToShortByteArr(4 + scopeBytes.length));
        buf.put(this.pinToByteArr(pin));
        buf.put(scopeBytes);
        buf.put(new byte[] { 0, 0 });
        buf.position(0);

        if (printInput)
            System.out.println("Input for getScopeExclusivePseudonym: " + Arrays.toString(buf.array()));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getScopeExclusivePseudonym)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getScopeExclusivePseudonym)", false);
        System.out.println("Response from getScopeExclusivePseudonym: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            BigInteger pv = new BigInteger(1, response.getData());
            cachedScopeExclusivePseudonym.put(scope, pv);
            return pv;
        }
        return null;
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

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

private BigInteger computeDevicePublicKeyCommitment(int pin) {
    ByteBuffer buf = ByteBuffer.allocate(13);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getDeviceCommitment, 0, 0, 0, 0, 4 });
    buf.put(this.pinToByteArr(pin));
    buf.put(new byte[] { 0, 0 });
    buf.position(0);/* w  w w  .  j av  a  2s  . c  om*/
    try {
        if (printInput)
            System.out.println("Input for getDeviceCommitment: " + Arrays.toString(buf.array()));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getDeviceCommitment)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getDeviceCommitment)", false);
        System.out.println("Response from getDeviceCommitment: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            System.out.println("And this is the output: " + Arrays.toString(response.getData()));
            System.out.println("Or this bigInt: " + new BigInteger(1, response.getData()));
            return new BigInteger(1, response.getData());
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

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

@Override
public ZkProofCommitment prepareZkProof(int pin, Set<URI> credentialIds, Set<URI> scopeExclusivePseudonyms,
        boolean includeDevicePublicKeyProof) {
    TimingsLogger.logTiming("HardwareSmartcard.prepareZkProof", true);

    ZkProofCommitment comm = new ZkProofCommitment();

    SystemParameters params = this.getSystemParameters(pin);
    comm.spec = new ZkProofSpecification(params);
    comm.spec.parametersForPseudonyms = params;
    comm.spec.credentialBases = new HashMap<URI, GroupParameters>();
    comm.spec.credFragment = new HashMap<URI, BigInteger>();
    for (URI courseId : credentialIds) {
        byte credID = this.getCredentialIDFromUri(pin, courseId);
        byte[] cred = this.readCredential(pin, credID);
        byte issuerID = cred[0];
        GroupParameters groupParams = this.getGroupParameters(pin, issuerID);
        comm.spec.credentialBases.put(courseId, groupParams);
        comm.spec.credFragment.put(courseId, this.computeCredentialFragment(pin, courseId));
    }//from w w  w  . ja  v  a  2s.co  m
    comm.spec.scopeExclusivePseudonymValues = new HashMap<URI, BigInteger>();

    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
    byte[] proofSession = null;
    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);
    try {
        if (printInput)
            System.out.println("Input for startCommitments: " + Arrays.toString(buf.array()));

        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(startCommitments)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(startCommitments)", false);

        System.out.println("Response from startCommitments: " + response);
        System.out.println("And this is the output: " + Arrays.toString(response.getData()));
        if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
            return null;
        }
        proofSession = response.getData();
    } catch (CardException e) {
        throw new RuntimeException("PrepareZkProof crashed.", e);
    }
    //ProofStatus set to 1        
    comm.nonceCommitment = proofSession;

    if (includeDevicePublicKeyProof) {
        comm.spec.devicePublicKey = this.computeDevicePublicKey(pin);
        comm.commitmentForDevicePublicKey = this.computeDevicePublicKeyCommitment(pin);
    }

    boolean notEnoughAttendance = false;
    for (URI uri : credentialIds) {
        byte credID = this.getCredentialIDFromUri(pin, uri);
        byte[] credInfo = readCredential(pin, credID);
        //byte issuerID = credInfo[0];
        //byte counterID = this.readIssuer(pin, issuerID)[4];
        byte status = credInfo[5];
        byte presentOrIssuance = this.getIssuanceCommitment;
        String command = "getIssuanceCommitment";
        //System.out.println("\nStatus of credential before commitments are made: " + status);
        if (status == 2) {
            //credential has already been issued. So we assume we want to present it.
            command = "getPresentationCommitment";
            presentOrIssuance = this.getPresentationCommitment;
        }
        /*
        if(counterID != 0){
           //Counter active. We must know if the attendance is high enough.
           byte[] counterInfo = readCounter(pin, counterID);
           int index = counterInfo[1];
           int threshold = counterInfo[2];
           if(index < threshold && presentOrIssuance == this.getPresentationCommitment){
          //Not enough attendance. aborting at the end; Done because of timing attacks.
          notEnoughAttendance = true;
           }
        } 
        */

        buf = ByteBuffer.allocate(14);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, presentOrIssuance, 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()));

            TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(" + command + ")", true);
            ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
            TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(" + command + ")", false);

            System.out.println("Response from " + command + ": " + response);
            if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
                comm.commitmentForCreds.put(uri, new BigInteger(1, response.getData()));
            } else {
                return null;
            }
        } catch (CardException e) {
            throw new RuntimeException("PrepareZkProof crashed.", e);
        }
    }

    for (URI scope : scopeExclusivePseudonyms) {
        BigInteger pseudonymCommitment = this.getScopeExclusiveCommitment(pin, scope);
        comm.commitmentForScopeExclusivePseudonyms.put(scope, pseudonymCommitment);
        comm.spec.scopeExclusivePseudonymValues.put(scope, this.computeScopeExclusivePseudonym(pin, scope));
    }
    if (notEnoughAttendance) {
        System.out.println("Because of not enough attendance?");
        TimingsLogger.logTiming("HardwareSmartcard.prepareZkProof", false);
        return null;
    } else {
        TimingsLogger.logTiming("HardwareSmartcard.prepareZkProof", false);
        return comm;
    }
}

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

@Override
public BigInteger computeCredentialFragment(int pin, URI credentialId) {
    //fragment is equal to the public key of a credential
    if (cachedCredentialFragment.containsKey(credentialId)) {
        BigInteger cached = cachedCredentialFragment.get(credentialId);
        System.out.println("Cached getCredentialPublicKey: " + credentialId + " - " + cached);
        return cached;
    }//ww  w  .j av a 2  s . c o m
    int credID = this.getCredentialIDFromUri(pin, credentialId);
    ByteBuffer buf = ByteBuffer.allocate(14);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getCredentialPublicKey, 0, 0, 0, 0, 5 });
    buf.put(this.pinToByteArr(pin));
    buf.put((byte) credID);
    buf.put(new byte[] { 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println(
                    "Input for getCredentialPublicKey: " + credentialId + " : " + Arrays.toString(buf.array()));

        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getCredentialPublicKey)", true);
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getCredentialPublicKey)", false);

        System.out.println("Response from getCredentialPublicKey (fragment): " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            System.out.println("And this is the output: " + Arrays.toString(response.getData()));
            BigInteger credentialFragment = new BigInteger(1, response.getData());
            System.out.println("which gives this BigInteger:  " + credentialFragment);
            cachedCredentialFragment.put(credentialId, credentialFragment);
            return credentialFragment;
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
    return null;
}

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 {//from  w  ww. ja v a 2  s  .c o  m
        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

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);
    }/*  ww w .  j  a  v  a 2s  .  c  om*/

    return responseAPDU.getData();
}