SecurityToken: fix ccid class header parsing
This commit is contained in:
@@ -80,7 +80,7 @@ public class CcidTransceiver {
|
|||||||
try {
|
try {
|
||||||
atr = receiveRaw();
|
atr = receiveRaw();
|
||||||
break;
|
break;
|
||||||
} catch (UsbTransportException e) {
|
} catch (Exception e) {
|
||||||
// Try more startTime
|
// Try more startTime
|
||||||
if (System.currentTimeMillis() - startTime > TIMEOUT) {
|
if (System.currentTimeMillis() - startTime > TIMEOUT) {
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import android.util.Pair;
|
|||||||
import org.sufficientlysecure.keychain.Constants;
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.Transport;
|
import org.sufficientlysecure.keychain.securitytoken.Transport;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.T1TpduProtocol;
|
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.T1TpduProtocol;
|
||||||
|
import org.sufficientlysecure.keychain.util.Iso7816TLV;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -42,10 +43,9 @@ import java.nio.ByteOrder;
|
|||||||
* Implements small subset of these features
|
* Implements small subset of these features
|
||||||
*/
|
*/
|
||||||
public class UsbTransport implements Transport {
|
public class UsbTransport implements Transport {
|
||||||
private static final int USB_CLASS_SMARTCARD = 11;
|
|
||||||
private static final int PROTOCOLS_OFFSET = 6;
|
private static final int PROTOCOLS_OFFSET = 6;
|
||||||
private static final int FEATURES_OFFSET = 40;
|
private static final int FEATURES_OFFSET = 40;
|
||||||
private static final int MASK_T1_PROTO = 2;
|
private static final short MASK_T1_PROTO = 2;
|
||||||
|
|
||||||
// dwFeatures Masks
|
// dwFeatures Masks
|
||||||
private static final int MASK_TPDU = 0x10000;
|
private static final int MASK_TPDU = 0x10000;
|
||||||
@@ -77,7 +77,7 @@ public class UsbTransport implements Transport {
|
|||||||
private static UsbInterface getSmartCardInterface(UsbDevice device) {
|
private static UsbInterface getSmartCardInterface(UsbDevice device) {
|
||||||
for (int i = 0; i < device.getInterfaceCount(); i++) {
|
for (int i = 0; i < device.getInterfaceCount(); i++) {
|
||||||
UsbInterface anInterface = device.getInterface(i);
|
UsbInterface anInterface = device.getInterface(i);
|
||||||
if (anInterface.getInterfaceClass() == USB_CLASS_SMARTCARD) {
|
if (anInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CSCID) {
|
||||||
return anInterface;
|
return anInterface;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,13 +180,35 @@ public class UsbTransport implements Transport {
|
|||||||
|
|
||||||
private void configureProtocol() throws UsbTransportException {
|
private void configureProtocol() throws UsbTransportException {
|
||||||
byte[] desc = mConnection.getRawDescriptors();
|
byte[] desc = mConnection.getRawDescriptors();
|
||||||
if (desc.length < FEATURES_OFFSET + 4)
|
int dwProtocols = 0, dwFeatures = 0;
|
||||||
throw new UsbTransportException("Can't access dwFeatures for protocol selection");
|
boolean hasCcidDescriptor = false;
|
||||||
|
|
||||||
int dwProtocols = ByteBuffer.wrap(desc, FEATURES_OFFSET, 4).order(ByteOrder.LITTLE_ENDIAN)
|
ByteBuffer byteBuffer = ByteBuffer.wrap(desc).order(ByteOrder.LITTLE_ENDIAN);
|
||||||
.asIntBuffer().get();
|
|
||||||
int dwFeatures = ByteBuffer.wrap(desc, FEATURES_OFFSET, 4).order(ByteOrder.LITTLE_ENDIAN)
|
while (byteBuffer.hasRemaining()) {
|
||||||
.asIntBuffer().get();
|
byteBuffer.mark();
|
||||||
|
byte len = byteBuffer.get(), type = byteBuffer.get();
|
||||||
|
|
||||||
|
if (type == 0x21 && len == 0x36) {
|
||||||
|
byteBuffer.reset();
|
||||||
|
|
||||||
|
byteBuffer.position(byteBuffer.position() + PROTOCOLS_OFFSET);
|
||||||
|
dwProtocols = byteBuffer.getInt();
|
||||||
|
|
||||||
|
byteBuffer.reset();
|
||||||
|
|
||||||
|
byteBuffer.position(byteBuffer.position() + FEATURES_OFFSET);
|
||||||
|
dwFeatures = byteBuffer.getInt();
|
||||||
|
hasCcidDescriptor = true;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
byteBuffer.position(byteBuffer.position() + len - 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasCcidDescriptor) {
|
||||||
|
throw new UsbTransportException("CCID descriptor not found");
|
||||||
|
}
|
||||||
|
|
||||||
if ((dwProtocols & MASK_T1_PROTO) == 0) {
|
if ((dwProtocols & MASK_T1_PROTO) == 0) {
|
||||||
throw new UsbTransportException("T=0 protocol is not supported");
|
throw new UsbTransportException("T=0 protocol is not supported");
|
||||||
|
|||||||
@@ -15,12 +15,11 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block;
|
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu;
|
||||||
|
|
||||||
import org.bouncycastle.util.Arrays;
|
import org.bouncycastle.util.Arrays;
|
||||||
import org.bouncycastle.util.encoders.Hex;
|
import org.bouncycastle.util.encoders.Hex;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.BlockChecksumType;
|
|
||||||
|
|
||||||
public class Block {
|
public class Block {
|
||||||
protected static final int MAX_PAYLOAD_LEN = 254;
|
protected static final int MAX_PAYLOAD_LEN = 254;
|
||||||
@@ -78,7 +77,7 @@ public class Block {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getEdc() {
|
public byte[] getEdc() {
|
||||||
return Arrays.copyOfRange(mData, mData.length - mChecksumType.getLength(), mChecksumType.getLength());
|
return Arrays.copyOfRange(mData, mData.length - mChecksumType.getLength(), mData.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BlockChecksumType getChecksumType() {
|
public BlockChecksumType getChecksumType() {
|
||||||
@@ -15,10 +15,9 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block;
|
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.BlockChecksumType;
|
|
||||||
|
|
||||||
public class IBlock extends Block {
|
public class IBlock extends Block {
|
||||||
private static final byte BIT_SEQUENCE = 6;
|
private static final byte BIT_SEQUENCE = 6;
|
||||||
@@ -15,12 +15,11 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block;
|
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu;
|
||||||
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.BlockChecksumType;
|
|
||||||
|
|
||||||
public class RBlock extends Block {
|
public class RBlock extends Block {
|
||||||
private static final byte BIT_SEQUENCE = 5;
|
private static final byte BIT_SEQUENCE = 5;
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block;
|
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu;
|
||||||
|
|
||||||
public class SBlock extends Block {
|
public class SBlock extends Block {
|
||||||
public SBlock(Block baseBlock) {
|
public SBlock(Block baseBlock) {
|
||||||
@@ -25,10 +25,6 @@ import org.sufficientlysecure.keychain.Constants;
|
|||||||
import org.sufficientlysecure.keychain.securitytoken.usb.CcidTransceiver;
|
import org.sufficientlysecure.keychain.securitytoken.usb.CcidTransceiver;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.CcidTransportProtocol;
|
import org.sufficientlysecure.keychain.securitytoken.usb.CcidTransportProtocol;
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block.Block;
|
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block.IBlock;
|
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block.RBlock;
|
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block.SBlock;
|
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
|
||||||
public class T1TpduProtocol implements CcidTransportProtocol {
|
public class T1TpduProtocol implements CcidTransportProtocol {
|
||||||
@@ -107,7 +103,7 @@ public class T1TpduProtocol implements CcidTransportProtocol {
|
|||||||
byte[] responseApdu = responseBlock.getApdu();
|
byte[] responseApdu = responseBlock.getApdu();
|
||||||
|
|
||||||
while (((IBlock) responseBlock).getChaining()) {
|
while (((IBlock) responseBlock).getChaining()) {
|
||||||
Block ackBlock = newRBlock(((IBlock) responseBlock).getSequence());
|
Block ackBlock = newRBlock((byte) (1 - ((IBlock) responseBlock).getSequence()));
|
||||||
mTransceiver.sendXfrBlock(ackBlock.getRawData());
|
mTransceiver.sendXfrBlock(ackBlock.getRawData());
|
||||||
|
|
||||||
responseBlock = getBlockFromResponse(mTransceiver.receive());
|
responseBlock = getBlockFromResponse(mTransceiver.receive());
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2016 Nikita Mikhailov <nikita.s.mikhailov@gmail.com>
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.sufficientlysecure.keychain.securitytoken.usb.tpdu.block;
|
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.UsbTransportException;
|
|
||||||
import org.sufficientlysecure.keychain.securitytoken.usb.tpdu.BlockChecksumType;
|
|
||||||
|
|
||||||
public class BlockFactory {
|
|
||||||
private BlockChecksumType mChecksumType = BlockChecksumType.LRC;
|
|
||||||
|
|
||||||
public Block getBlockFromResponse(byte[] data) throws UsbTransportException {
|
|
||||||
final Block baseBlock = new Block(mChecksumType, data);
|
|
||||||
|
|
||||||
if ((baseBlock.getPcb() & 0b10000000) == 0b00000000) {
|
|
||||||
return new IBlock(baseBlock);
|
|
||||||
} else if ((baseBlock.getPcb() & 0b11000000) == 0b11000000) {
|
|
||||||
return new SBlock(baseBlock);
|
|
||||||
} else if ((baseBlock.getPcb() & 0b11000000) == 0b10000000) {
|
|
||||||
return new RBlock(baseBlock);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new UsbTransportException("TPDU Unknown block type");
|
|
||||||
}
|
|
||||||
|
|
||||||
public IBlock newIBlock(byte sequence, boolean chaining, byte[] apdu) throws UsbTransportException {
|
|
||||||
return new IBlock(mChecksumType, (byte) 0, sequence, chaining, apdu);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RBlock newRBlock(byte sequence) throws UsbTransportException {
|
|
||||||
return new RBlock(mChecksumType, (byte) 0, sequence);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setChecksumType(final BlockChecksumType checksumType) {
|
|
||||||
mChecksumType = checksumType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user