List of usage examples for javax.smartcardio CommandAPDU CommandAPDU
public CommandAPDU(ByteBuffer apdu)
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 www.j a va 2 s. c om*/ 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
@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; }//from w ww . j a va 2 s .c om 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: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; }//from ww w. jav a 2s . c om 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 deleteCredential(int pin, URI credentialId) { byte credID = this.getCredentialIDFromUri(pin, credentialId); ByteBuffer buf = ByteBuffer.allocate(10); buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.removeCredential, 0, 0, 5 }); buf.put(this.pinToByteArr(pin)); buf.put(credID);/*from w ww . j a v a2s . co m*/ buf.position(0); try { System.out.println("Removing credential with uri: " + credentialId); this.deleteBlob(pin, credentialId); if (credentialId.toString().startsWith(UProveCryptoEngineUserImpl.UProveCredential)) { URI reloadURI = URI.create(credentialId.toString() + ReloadStorageManager.URI_POSTFIX); if (reloadURI.toString().contains(":") && !reloadURI.toString().contains("_")) { reloadURI = URI.create(reloadURI.toString().replaceAll(":", "_")); //change all ':' to '_' } this.deleteBlob(pin, reloadURI); System.out.println("deleted the reload blob of the credential: " + reloadURI); } this.removeCredentialUri(pin, credentialId); if (printInput) System.out.println("Input for removeCredential: " + Arrays.toString(buf.array())); System.out.println("Trying to remove on-board credential with ID=" + credID); ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf)); System.out.println("response from RemoveCredential: " + response); return this.evaluateStatus(response); } catch (CardException e) { return SmartcardStatusCode.NOT_FOUND; } }
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; }// w ww .j a va2 s . co m 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 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 {//ww w . jav a 2 s . com 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 deleteBlob(int pin, URI uri) { byte[] uriBytes = null; uriBytes = this.uriToByteArr(uri); if (uriBytes.length > 199) { return SmartcardStatusCode.REQUEST_URI_TOO_LONG; }/*from ww w . j a va2 s . c o m*/ // BLOB CACHE! blobCache.remove(uri); blobUrisCache.remove(uri); 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.removeBlob, 0, 0, (byte) data.length }); buf.put(data); buf.position(0); try { if (printInput) System.out.println("Input for removeBlob: " + Arrays.toString(buf.array())); ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf)); System.out.println("Response from removeBlob: " + response); return this.evaluateStatus(response); } catch (CardException e) { e.printStackTrace(); return null; } }
From source file:eu.abc4trust.smartcard.HardwareSmartcard.java
/** * Returns the number of uris read, no of uris remaining to be read. *//* w ww . j a va 2 s . com*/ private byte[] getBlobUrisHelper(int pin, Set<URI> uris, byte nread) { ByteBuffer buf = ByteBuffer.allocate(14); buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.listBlobs, 0, 0, 0, 0, 5 }); buf.put(this.pinToByteArr(pin)); buf.put(new byte[] { nread, 0, 0 }); //first arg is how many URIs we read so far. buf.position(0); try { if (printInput) System.out.println("Input for listBlobs: " + Arrays.toString(buf.array())); ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf)); System.out.println("Response from listBlobs: " + response); if (this.evaluateStatus(response) != SmartcardStatusCode.OK) { return null; } byte[] data = response.getData(); System.out.println("data: " + Arrays.toString(data)); int index = 0; while (true) { if ((index + 2) == data.length) { //at the end, so the last two bytes is the updated number of read URIs and the number of unread URIs // System.out.println("data.length: " + data.length); // System.out.println("index: " + index); nread = data[index]; byte unread = data[index + 1]; System.out.println("nread: " + nread); System.out.println("unread: " + unread); return new byte[] { nread, unread }; } else { byte uriSize = data[index]; byte[] uri = new byte[uriSize]; System.arraycopy(data, index + 1, uri, 0, uriSize); uris.add(this.byteArrToUri(uri)); index += uriSize + 1; } } } catch (CardException e) { e.printStackTrace(); return null; } }
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."); }// ww w . j a v a 2 s .c om // 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 changePin(int pin, int newPin) { byte[] data = new byte[8]; System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4); System.arraycopy(this.pinToByteArr(newPin), 0, data, 4, 4); try {//from w w w .ja v a2 s . c om ByteBuffer buf = ByteBuffer.allocate(13); buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.changePin, 0, 0, 8 }); buf.put(data); buf.position(0); if (printInput) System.out.println("Input for changePin: " + Arrays.toString(buf.array())); ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf)); System.out.println("Response from changePin: " + response); return this.evaluateStatus(response); } catch (CardException e) { e.printStackTrace(); return SmartcardStatusCode.NOT_FOUND; } }