aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure
diff options
context:
space:
mode:
authorNikita Mikhailov <nikita.s.mikhailov@gmail.com>2016-04-09 17:28:44 +0600
committerNikita Mikhailov <nikita.s.mikhailov@gmail.com>2016-04-14 22:48:01 +0600
commit409c38ba2ab79cac97a914d89f9cb9a14ebe08c1 (patch)
tree8e6b5a205c704c79e4d8a9ba5cb5c3ae5d7adde0 /OpenKeychain/src/main/java/org/sufficientlysecure
parent6ad2676e8c2659470572ddddc88a9d37e30ec5fa (diff)
downloadopen-keychain-409c38ba2ab79cac97a914d89f9cb9a14ebe08c1.tar.gz
open-keychain-409c38ba2ab79cac97a914d89f9cb9a14ebe08c1.tar.bz2
open-keychain-409c38ba2ab79cac97a914d89f9cb9a14ebe08c1.zip
OTG: UsbTransport minor refactoring
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java33
1 files changed, 22 insertions, 11 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java
index 86f4a687d..4b4f2c35c 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/smartcard/UsbTransport.java
@@ -50,7 +50,7 @@ public class UsbTransport implements Transport {
* @throws UsbTransportException
*/
private void iccPowerSet(boolean on) throws UsbTransportException {
- final byte[] iccPowerOn = {
+ final byte[] iccPowerCommand = {
(byte) (on ? 0x62 : 0x63),
0x00, 0x00, 0x00, 0x00,
0x00,
@@ -58,8 +58,13 @@ public class UsbTransport implements Transport {
0x00,
0x00, 0x00
};
- sendRaw(iccPowerOn);
- receive();
+
+ sendRaw(iccPowerCommand);
+ byte[] bytes;
+ do {
+ bytes = receive();
+ } while (isDataBlockNotReady(bytes));
+ checkDataBlockResponse(receive());
}
/**
@@ -174,19 +179,25 @@ public class UsbTransport implements Transport {
*/
@Override
public byte[] transceive(byte[] data) throws UsbTransportException {
- send(data);
+ sendXfrBlock(data);
byte[] bytes;
do {
bytes = receive();
- } while (isXfrBlockNotReady(bytes));
+ } while (isDataBlockNotReady(bytes));
- checkXfrBlockResult(bytes);
+ checkDataBlockResponse(bytes);
// Discard header
return Arrays.copyOfRange(bytes, 10, bytes.length);
}
- private void send(byte[] d) throws UsbTransportException {
- int l = d.length;
+ /**
+ * Transmits XfrBlock
+ * 6.1.4 PC_to_RDR_XfrBlock
+ * @param payload payload to transmit
+ * @throws UsbTransportException
+ */
+ private void sendXfrBlock(byte[] payload) throws UsbTransportException {
+ int l = payload.length;
byte[] data = Arrays.concatenate(new byte[]{
0x6f,
(byte) l, (byte) (l >> 8), (byte) (l >> 16), (byte) (l >> 24),
@@ -194,7 +205,7 @@ public class UsbTransport implements Transport {
mCounter++,
0x00,
0x00, 0x00},
- d);
+ payload);
int send = 0;
while (send < data.length) {
@@ -239,14 +250,14 @@ public class UsbTransport implements Transport {
return (byte) ((bytes[7] >> 6) & 0x03);
}
- private void checkXfrBlockResult(byte[] bytes) throws UsbTransportException {
+ private void checkDataBlockResponse(byte[] bytes) throws UsbTransportException {
final byte status = getStatus(bytes);
if (status != 0) {
throw new UsbTransportException("USB-CCID error: status " + status + " error code: " + Hex.toHexString(bytes, 8, 1));
}
}
- private boolean isXfrBlockNotReady(byte[] bytes) {
+ private boolean isDataBlockNotReady(byte[] bytes) {
return getStatus(bytes) == 2;
}