Code with Finding: |
class SecureMessagingWrapper {
/**
* Does the actual decoding of a response apdu.
* Based on Section E.3 of TR-PKI, especially the examples.
*
* @param rapdu buffer containing the apdu data.
* @param len length of the apdu data.
*
* @return a byte array containing the unwrapped apdu buffer.
*/
private byte[] unwrapResponseAPDU(byte[] rapdu, int len)
throws GeneralSecurityException, IOException {
if (rapdu == null || rapdu.length < 2 || len < 2) {
throw new IllegalArgumentException("Invalid type");
}
cipher.init(Cipher.DECRYPT_MODE, ksEnc, ZERO_IV_PARAM_SPEC);
DataInputStream in = new DataInputStream(new ByteArrayInputStream(rapdu));
byte[] data = new byte[0];
short sw = 0;
boolean finished = false;
while (!finished) {
int tag = in.readByte();
switch (tag) {
case (byte)0x87: data = readDO87(in); break;
case (byte)0x99: sw = readDO99(in); break;
case (byte)0x8E: readDO8E(in, rapdu); finished = true; break;
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(data, 0, data.length);
out.write((sw & 0x0000FF00) >> 8);
out.write(sw & 0x000000FF);
return out.toByteArray();
}
}
|