List of usage examples for javax.smartcardio ResponseAPDU getData
public byte[] getData()
From source file:eu.abc4trust.smartcard.HardwareSmartcard.java
/** * Returns the number of uris read, no of uris remaining to be read. *//*ww w . j a v a2 s . c om*/ 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 int init(int newPin, SystemParameters pseuParams, RSAKeyPair rootKey, short deviceId) { if (this.wasInit()) { return -1; }// w ww . j a v a 2 s . c o 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
@SuppressWarnings("unused") private List<Byte> listCounters(int pin) { ByteBuffer buf = ByteBuffer.allocate(10); buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.listCounters, 0, 0, 4 }); buf.put(this.pinToByteArr(pin)); buf.put(new byte[] { 0 }); buf.position(0);/*from w ww.jav a 2 s . c o m*/ try { if (printInput) System.out.println("Input for listCounters: " + Arrays.toString(buf.array())); ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf)); System.out.println("Response from listCounters: " + response); if (this.evaluateStatus(response) == SmartcardStatusCode.OK) { List<Byte> counters = new ArrayList<Byte>(); byte[] counterIDs = response.getData(); for (byte counterID : counterIDs) { counters.add(counterID); } return counters; } } 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."); }/*from w w w.jav a 2s.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
public List<Byte> listCredentialIDs(int pin) { ByteBuffer buf = ByteBuffer.allocate(10); buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.listCredentials, 0, 0, 4 }); buf.put(this.pinToByteArr(pin)); buf.put((byte) 0); buf.position(0);// www. ja v a2 s .co m try { if (printInput) System.out.println("Input for listCredentials: " + Arrays.toString(buf.array())); ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf)); System.out.println("Response from listCredentials: " + response); if (this.evaluateStatus(response) == SmartcardStatusCode.OK) { List<Byte> credentialIDs = new ArrayList<Byte>(); byte[] creds = response.getData(); for (byte cred : creds) { credentialIDs.add(cred); } return credentialIDs; } } catch (CardException e) { e.printStackTrace(); return null; } return null; }
From source file:eu.abc4trust.smartcard.HardwareSmartcard.java
@Override public short getDeviceID(int pin) { try {/* w ww.jav a 2s .co m*/ ResponseAPDU response = this.transmitCommand( new CommandAPDU(this.ABC4TRUSTCMD, this.getDeviceID, 0, 0, this.pinToByteArr(pin), 2)); System.out.println("Response from getdeviceID: " + response); if (this.evaluateStatus(response) == SmartcardStatusCode.OK) { return ByteBuffer.wrap(response.getData()).getShort(); } } catch (CardException e) { e.printStackTrace(); } return -1; }
From source file:eu.abc4trust.smartcard.HardwareSmartcard.java
public String getVersion() { try {//ww w . j a v a 2 s. c om 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 RSAVerificationKey readAuthenticationKey(int pin, int keyID) { byte[] data = new byte[5]; System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4); data[4] = (byte) keyID; ByteBuffer buffer = ByteBuffer.allocate(14); buffer.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readAuthenticationKey, 0, 0, 0, 0, 5 }); buffer.put(data);// ww w.j a v a 2 s . co m buffer.put(new byte[] { 0, 0 }); buffer.position(0); try { ResponseAPDU response = this.transmitCommand(new CommandAPDU(buffer)); System.out.println("Response from readAuthenticationKey: " + response); if (this.evaluateStatus(response) == SmartcardStatusCode.OK) { RSAVerificationKey vkey = new RSAVerificationKey(); vkey.n = new BigInteger(1, response.getData()); return vkey; } 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);/* w w w .ja v a 2 s .com*/ 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 SmartcardBackup backupAttendanceData(int pin, String password) { SmartcardBackup backup = new SmartcardBackup(); ByteBuffer buf = ByteBuffer.allocate(21); buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.backupDevice, 0, 0, 0, 0, 0x0C }); buf.put(this.pinToByteArr(pin)); byte[] password_bytes = Utils.passwordToByteArr(password); if (password_bytes == null) { return null; }/*from w w w . j av a 2s .co m*/ buf.put(password_bytes); buf.put(new byte[] { 0, 0 }); buf.position(0); try { //First we backup the device-specific stuff. pin, puk and deviceSecret is encrypted and //deviceID and deviceURI is stored in plain text ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf)); System.out.println("Response from backupDevice: " + response); if (this.evaluateStatus(response) != SmartcardStatusCode.OK) { return null; } backup.macDevice = response.getData(); backup.deviceID = this.getDeviceID(pin); backup.deviceUri = this.getDeviceURI(pin); //Then we backup the counters. counterID, index and cursor is encrypted, but //the threshold and keyID is hidden. Thus we need to save those along with the counterID //in cleartext. We assume that the key is put on the card in the initialization phase. // buf = ByteBuffer.allocate(18); // buf.put(new byte[]{(byte)this.ABC4TRUSTCMD, this.backupCounters, 0, 0, 0x0C}); // buf.put(this.pinToByteArr(pin)); // buf.put(password_bytes); // buf.put((byte)0); // buf.position(0); // response = this.transmitCommand(new CommandAPDU(buf)); // System.out.println("Response from backupCounters: " + response); // if(this.evaluateStatus(response) == SmartcardStatusCode.OK){ // backup.macCounters = response.getData(); // }else{ // backup.macCounters = null; // } // // List<Byte> credentials = this.listCredentialIDs(pin); // for(Byte credID : credentials){ // byte[] credInfo = this.readCredential(pin, credID); // byte status = credInfo[5]; // System.out.println("backing up credential: "+this.getCredentialUriFromID(pin, credID)+" with status: " + status); // if(status != 2){ // //Credential is either just created, and thus not backed up, // //OR done presenting (limited amount of presentations), thus not backupable. // continue; // } // buf = ByteBuffer.allocate(22); // buf.put(new byte[]{(byte)this.ABC4TRUSTCMD, (byte) this.backupCredential, 0, 0, 0, 0, 0x0D}); // buf.put(this.pinToByteArr(pin)); // buf.put(password_bytes); // buf.put(credID); // buf.put(new byte[]{0, 0}); // buf.position(0); // response = this.transmitCommand(new CommandAPDU(buf)); // System.out.println("Response from backupCredentials: " + response); // if(this.evaluateStatus(response) != SmartcardStatusCode.OK){ // return null; // } // backup.macCredentials.put(credID, response.getData()); // } //Create AES key using the PIN and Password of the user final byte[] IV = new byte[16]; new SecureRandom().nextBytes(IV); Cipher cipher = getAESKey(salt, password, IV, true); backup.IV = IV; //finally we backup the blobstore Map<URI, SmartcardBlob> blobs = this.getBlobs(pin); Map<URI, byte[]> encBlobs = new HashMap<URI, byte[]>(); try { for (URI uri : blobs.keySet()) { String uriString = uri.toString(); if (uriString.startsWith(IdemixCryptoEngineUserImpl.IdmxCredential) || uriString.startsWith(UProveCryptoEngineUserImpl.UProveCredential)) { continue; } byte[] blob = blobs.get(uri).blob; encBlobs.put(uri, cipher.doFinal(blob)); } } catch (Exception e) { throw new RuntimeException("Could not encrypt blobstore", e); } backup.blobstore = encBlobs; } catch (CardException e) { e.printStackTrace(); return null; } return backup; }