Example usage for javax.smartcardio CardException printStackTrace

List of usage examples for javax.smartcardio CardException printStackTrace

Introduction

In this page you can find the example usage for javax.smartcardio CardException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

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

@Override
public SmartcardBlob getBlob(int pin, URI uri) {
    //this.resetCard();

    uri = URI.create(uri.toString().replaceAll(":", "_"));
    byte[] uriBytes = this.uriToByteArr(uri);
    if (uriBytes.length > 199) {
        throw new RuntimeException("URI is too long. Cannot have been stored on smartcard.");
    }//from  w w w.  jav a 2s. c o  m

    // BLOB CACHE!
    if (blobCache.containsKey(uri)) {
        SmartcardBlob cached = blobCache.get(uri);
        System.out.println("Cached readBlob: " + uri + " : " + cached.blob.length); // Arrays.toString(cached.blob));
        return cached;
    }
    ByteBuffer buf = ByteBuffer.allocate(9 + 4 + uriBytes.length);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readBlob, 0, 0, 0 });
    buf.put(this.intLengthToShortByteArr(uriBytes.length + 4));
    buf.put(this.pinToByteArr(pin));
    buf.put(uriBytes);
    buf.put(new byte[] { 0, 0 });
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for readBlob: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from readBlob: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            SmartcardBlob blob = new SmartcardBlob();
            blob.blob = response.getData();

            // BLOB CACHE!
            blobCache.put(uri, blob);
            return blob;
        } else {
            return null;
        }
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

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

@Override
public SmartcardStatusCode allocateCredential(int pin, URI credentialId, URI issuerParameters) {
    byte[] credIdBytes = null;
    credIdBytes = this.uriToByteArr(credentialId);
    if (credIdBytes.length > 199) {
        return SmartcardStatusCode.REQUEST_URI_TOO_LONG;
    }/* w  w  w .  ja v a2s  .  co  m*/

    byte issuerID = this.getIssuerIDFromUri(pin, issuerParameters);
    byte newCredentialID = this.getNewCredentialID(pin);
    if (newCredentialID == (byte) -1) {
        return SmartcardStatusCode.INSUFFICIENT_STORAGE;
    }
    ByteBuffer buf = ByteBuffer.allocate(11);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setCredential, 0, 0, 6 });
    buf.put(this.pinToByteArr(pin));
    buf.put(newCredentialID);
    buf.put(issuerID);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for setCredential: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from setCredential: " + response);
        if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
            return this.evaluateStatus(response);
        }
    } catch (CardException e) {
        e.printStackTrace();
        return SmartcardStatusCode.NOT_FOUND;
    }

    //Then store the mapping from credentialURI to credentialID:
    TimingsLogger.logTiming("HardwareSmartcard.storeCredentialUriAndID", true);
    SmartcardStatusCode code = this.storeCredentialUriAndID(pin, credentialId, newCredentialID);
    TimingsLogger.logTiming("HardwareSmartcard.storeCredentialUriAndID", false);
    if (code != SmartcardStatusCode.OK) {
        System.err.println(
                "Credential stored correctly on card, but storing the Uri/ID failed with code: " + code);
        return code;
    }

    return SmartcardStatusCode.OK;
}

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

@Override
public SmartcardStatusCode storeBlob(int pin, URI uri, SmartcardBlob blob) {
    //this.resetCard();

    String[] forbiddenChars = new String[] { "\u0167", ":", "*", "?", "<", ">", " ", "|" };
    if (uri.toString().contains(":") && !uri.toString().contains("_")) {
        uri = URI.create(uri.toString().replaceAll(":", "_")); //change all ':' to '_'
    } else {//from   w w w  .j  a  v  a 2s. co m
        for (int i = 0; i < forbiddenChars.length; i++) {
            if (uri.toString().contains(forbiddenChars[i])) {
                throw new RuntimeException(
                        "Cannot store a blob under a URI containing the following char: " + forbiddenChars[i]);
            }
        }
    }
    byte[] uriBytes = null;
    uriBytes = this.uriToByteArr(uri);
    if (uriBytes.length > 199) {
        return SmartcardStatusCode.REQUEST_URI_TOO_LONG;
    }

    // BLOB CACHE!
    blobCache.put(uri, blob);
    blobUrisCache.add(uri);

    //first put data from blob followed by the STORE BLOB command
    this.putData(blob.blob);

    byte[] data = new byte[4 + uriBytes.length];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    System.arraycopy(uriBytes, 0, data, 4, uriBytes.length);
    ByteBuffer buf = ByteBuffer.allocate(9 + uriBytes.length);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.storeBlob, 0, 0, (byte) data.length });
    buf.put(data);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for storeBlob: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from storeBlob: " + response);
        if ((response.getSW1() != STATUS_OK) && (response.getSW1() != STATUS_BAD_PIN)) {
            throw new InsufficientStorageException("Could not store blob. Response from card: " + response);
        }
        return this.evaluateStatus(response);
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

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

@Override
public SmartcardStatusCode incrementCourseCounter(int pin, RSAKeyPair key, URI issuerId, int lectureId) {
    //First check if the counter is enabled. //TODO!
    //       TrustedIssuerParameters tip = this.getIssuerParameters(pin, issuerId);
    //       if(!tip.course.isActivated()){
    //          if(!tip.course.updateLectureId(lectureId)){
    //             //Course not yet issued!
    //             return SmartcardStatusCode.NOT_MODIFIED;
    //          }
    //       }//w ww . ja va2s  .  c  o m

    //auth data should be counterID||cursor  , with cursor having the updated value.
    byte counterID = this.getIssuerIDFromUri(pin, issuerId); //IssuerID is the same as CounterID if the counter exists.
    byte keyID = this.readCounter(pin, counterID)[0];
    byte[] data = new byte[5];
    data[0] = counterID;
    System.arraycopy(this.getNewCursor(lectureId), 0, data, 1, 4);
    byte[] challenge = this.getNewNonceForSignature();
    byte[] sig = SmartcardCrypto.generateSignature(data, challenge, key, this.rand).sig;
    //sig = this.removeSignBit(sig);
    ByteBuffer buf = ByteBuffer.allocate(7 + 1 + sig.length);
    byte[] bufferLength = ByteBuffer.allocate(2).putShort((short) (sig.length + 1)).array();
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.incrementCounter, 0, 0, 0 });
    buf.put(bufferLength);
    buf.put(keyID);
    buf.put(sig);
    buf.position(0);

    try {
        byte[] counterInfo = this.readCounter(pin, counterID);
        byte index = counterInfo[1];
        if (printInput)
            System.out.println("Input for incrementCounter: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from incrementCounter: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            //ensure that counter was increased or return not modified
            byte[] newCounterInfo = this.readCounter(pin, counterID);
            int newIndex = newCounterInfo[1];
            if (index == newIndex) {
                return SmartcardStatusCode.NOT_MODIFIED;
            } else {
                return SmartcardStatusCode.OK;
            }
        }
        return this.evaluateStatus(response);
    } catch (CardException e) {
        e.printStackTrace();
        return SmartcardStatusCode.NOT_FOUND;
    }
}

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

public int getMode() {
    try {/*  ww w  . j ava 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 String getVersion() {
    try {//from ww  w.j  a v a2s.  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

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

@Override
public int init(int newPin, SystemParameters pseuParams, RSAKeyPair rootKey, short deviceId) {
    if (this.wasInit()) {
        return -1;
    }//from w  w  w. java2s.  c om
    try {

        byte[] deviceID = ByteBuffer.allocate(2).putShort(deviceId).array();
        this.setAuthenticationKey(rootKey.getN(), 0, null);
        byte[] deviceKeySize = this.intLengthToShortByteArr(pseuParams.deviceSecretSizeBytes);
        byte[] idAndDeviceKeySize = new byte[] { deviceID[0], deviceID[1], deviceKeySize[0], deviceKeySize[1] };
        ByteBuffer buf = ByteBuffer.allocate(13);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.initializeDevice, 0, 0, 0, 0, 4 });
        buf.put(idAndDeviceKeySize);
        buf.put(new byte[] { 0, 0 });
        buf.position(0);
        if (printInput)
            System.out.println("Input to initialize device: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
            return -1;
        }
        byte[] pinAndPuk = SmartcardCrypto.decrypt(response.getData(), rootKey);
        byte[] pin = new byte[4];
        byte[] puk = new byte[8];
        System.arraycopy(pinAndPuk, 0, pin, 0, 4);
        System.arraycopy(pinAndPuk, 4, puk, 0, 8);
        String ipin = "", ipuk = "";
        for (int i = 0; i < 4; i++) {
            ipin += (char) (pin[i] & 0xFF);
        }
        for (int i = 0; i < 8; i++) {
            ipuk += (char) (puk[i] & 0xFF);
        }
        if (this.changePin(Integer.parseInt(ipin), newPin) != SmartcardStatusCode.OK) {
            System.out.println("Could not change pin.");
            return -1;
        }

        System.out.println("Now initializing group stuff");
        int mode = this.getMode();

        if (this.setGroupComponent(mode, pseuParams.p.toByteArray(), 0, 0, null) != SmartcardStatusCode.OK) {
            return -1;
        }
        if (this.setGroupComponent(mode, pseuParams.subgroupOrder.toByteArray(), 0, 1,
                null) != SmartcardStatusCode.OK) {
            return -1;
        }
        BigInteger f = pseuParams.p.subtract(BigInteger.ONE).divide(pseuParams.subgroupOrder); //cofactor
        this.setGroupComponent(mode, f.toByteArray(), 0, 2, null);

        //then add a generator of the subgroup q
        if (this.setGenerator(mode, pseuParams.g.toByteArray(), 0, 1, null) != SmartcardStatusCode.OK) {
            return -1;
        }

        //set prover
        byte[] data = new byte[5 + MAX_CREDENTIALS + 1];
        data[0] = 1; //id 1
        int ksize = pseuParams.zkChallengeSizeBytes * 2 + pseuParams.zkStatisticalHidingSizeBytes;
        byte[] ksize_bytes = this.intLengthToShortByteArr(ksize);
        data[1] = ksize_bytes[0];
        data[2] = ksize_bytes[1]; // as large as the subgroup order is -1 to prevent overflow.
        int csize = pseuParams.zkChallengeSizeBytes;
        byte[] csize_bytes = this.intLengthToShortByteArr(csize);
        data[3] = csize_bytes[0];
        data[4] = csize_bytes[1]; // challenge size: 256 bit = 32 bytes (as per default in SystemParameters)
        for (int i = 0; i <= MAX_CREDENTIALS; i++) {
            //0 means it accepts both credentials and scope-exclusive stuff.
            //1,2,3,... means it accepts credentials with id 1,2,3,...
            data[i + 5] = (byte) i;
        }
        buf = ByteBuffer.allocate(5 + data.length);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setProver, 0, 0, (byte) data.length });
        buf.put(data);
        buf.position(0);
        System.out.println("Input to prover: " + Arrays.toString(buf.array()));
        response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from setProver: " + response);
        if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
            return -1;
        }

        //After init, one should call setIssuer which creates a group and counter.
        return Integer.parseInt(ipuk);
    } catch (CardException e) {
        e.printStackTrace();
        return -1;
    }
}

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

@Override
public SmartcardStatusCode addUProveIssuerParameters(RSAKeyPair rootKey, URI parametersUri,
        UProveParams uProveParams) {/*from  w w  w.  j  a  va 2 s  . c o m*/
    byte issuerID = this.getNewIssuerID(parametersUri);
    byte groupID = issuerID;
    byte genID1 = 1;
    byte genID2 = 0;
    byte numPres = 0; //unlimited presentations - limit not used in the pilot
    byte counterID = 0; //no counter present
    ByteBuffer buf = ByteBuffer.allocate(11);
    //SET ISSUER(BYTE issuerID, groupID, genID1, genID2, numpres, counterID)
    byte[] data = new byte[] { issuerID, groupID, genID1, genID2, numPres, counterID };
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setIssuer, 0, 0, 6 });
    buf.put(data);
    buf.position(0);

    try {
        //Before setting the issuer, we must create a group, generators as well as a counter
        int mode = this.getMode();
        this.setGroupComponent(mode, uProveParams.p.toByteArray(), groupID, 0, rootKey);
        this.setGroupComponent(mode, uProveParams.q.toByteArray(), groupID, 1, rootKey);
        this.setGroupComponent(mode, uProveParams.f.toByteArray(), groupID, 2, rootKey);
        this.setGenerator(mode, uProveParams.g.toByteArray(), groupID, genID1, rootKey);

        //prior to the actual command, if we are in working mode,
        //we have to authenticate the input data first.
        if (mode == 2) {
            System.out.println("Can only use addIssuerParameters in root mode");
            return SmartcardStatusCode.UNAUTHORIZED;
        }

        System.out.println("Input for setIssuer: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from setIssuer: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        //TODO: Error handling. Remove stuff again if something fails.
        e.printStackTrace();
        return SmartcardStatusCode.NOT_FOUND;
    }
}

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

@Override
public SmartcardStatusCode addIssuerParameters(RSAKeyPair rootKey, URI parametersUri,
        CredentialBases credBases) {/* w  ww  .  ja  v a2 s.co m*/
    byte issuerID = this.getNewIssuerID(parametersUri);
    byte groupID = issuerID;
    byte genID1 = 1;//R0
    byte genID2 = 2;//S
    byte numPres = 0; //unlimited presentations - limit not used in the pilot
    byte counterID = 0; //no counter present
    ByteBuffer buf = ByteBuffer.allocate(11);
    //SET ISSUER(BYTE issuerID, groupID, genID1, genID2, numpres, counterID)
    byte[] data = new byte[] { issuerID, groupID, genID1, genID2, numPres, counterID };
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setIssuer, 0, 0, 6 });
    buf.put(data);
    buf.position(0);

    try {
        //Before setting the issuer, we must create a group with generators. Idemix uses unknown order.
        int mode = this.getMode();
        SmartcardStatusCode status;
        status = this.setGroupComponent(mode, credBases.n.toByteArray(), groupID, 0, rootKey);
        if (status != SmartcardStatusCode.OK) {
            return status;
        }
        status = this.setGenerator(mode, credBases.R0.toByteArray(), groupID, genID1, rootKey);
        if (status != SmartcardStatusCode.OK) {
            return status;
        }
        status = this.setGenerator(mode, credBases.S.toByteArray(), groupID, genID2, rootKey);
        if (status != SmartcardStatusCode.OK) {
            return status;
        }

        //prior to the actual command, if we are in working mode,
        //we have to authenticate the input data first.
        if (mode == 2) {
            System.out.println("Can only use addIssuerParameters in root mode");
            return SmartcardStatusCode.UNAUTHORIZED;
        }

        System.out.println("Input for set Issuer: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from setIssuer: " + response);
        return evaluateStatus(response);
    } catch (CardException e) {
        //TODO: Error handling. Remove stuff again if something fails.
        e.printStackTrace();
        return SmartcardStatusCode.NOT_FOUND;
    }
}