List of usage examples for javax.smartcardio CommandAPDU CommandAPDU
public CommandAPDU(ByteBuffer apdu)
From source file:eu.abc4trust.smartcard.HardwareSmartcard.java
/** * //from w w w . ja v a 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
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 ww w .j a v a 2 s. co m 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
@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; }// w w w. j a v a2 s . c om 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
@Override public BigInteger computeDevicePublicKey(int pin) { ByteBuffer buf = ByteBuffer.allocate(13); buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getDevicePublicKey, 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 va 2 s .c o m try { TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getDevicePublicKey)", true); ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf)); TimingsLogger.logTiming("HardwareSmartcard.transmitCommand(getDevicePublicKey)", false); System.out.println("Response from getDevicePublicKey: " + response); if (this.evaluateStatus(response) == SmartcardStatusCode.OK) { return new BigInteger(1, response.getData()); } } catch (CardException e) { e.printStackTrace(); return null; } 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 ww. j av a 2 s . com*/ 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
/** * //from w w w . ja va 2s . c om * @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 ww w . j a v a 2s. c o m * @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
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);/*from w w w. java 2 s . 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
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);/*from w ww . ja va 2 s . c om*/ 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
@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 ava2s.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; }