com.btchip.BTChipException Java Examples

The following examples show how to use com.btchip.BTChipException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX2ContactlessNoChange() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_1));
	BitcoinTransaction txin_2 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_2));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_2));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	BTChipDongle.BTChipInput input2 = dongle.getTrustedInput(txin_2, 0);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	try {
		dongle.finalizeInputFull(txout_1.serializeOutputs());		
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}				
}
 
Example #2
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX2ContactlessWrongChange() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_1));
	BitcoinTransaction txin_2 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_2_2));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_2));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	BTChipDongle.BTChipInput input2 = dongle.getTrustedInput(txin_2, 0);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1, input2 }, 
		txin_1.getOutputs().get(1).getScript());
	try {
		dongle.finalizeInputFull(txout_1.serializeOutputs(), "44'/0'/0'/1/1");		
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}				
}
 
Example #3
Source File: VarintUtils.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public static long read(ByteArrayInputStream in) throws BTChipException {
	long result = 0;
	int val1 = in.read() & 0xff;
	if (val1 < 0xfd) {
		result = val1;
	}
	else
	if (val1 == 0xfd) {
		result |= in.read() & 0xff;
		result |= ((in.read() & 0xff) << 8);
	}
	else
	if (val1 == 0xfe) {
		result |= in.read() & 0xff;
		result |= ((in.read() & 0xff) << 8);
		result |= ((in.read() & 0xff) << 16);
		result |= ((in.read() & 0xff) << 24);
	}
	else {
		throw new BTChipException("Unsupported varint encoding");
	}
	return result;
}
 
Example #4
Source File: JCardSIMTransport.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public byte[] exchange(byte[] command) throws BTChipException {
	try {
		if (debug) {
			System.out.println("=> " + ByteUtil.hexString(command));
		}
		byte[] result = simulator.transmitCommand(command);
		if (debug) {
			System.out.println("<= " + ByteUtil.hexString(result));
		}    		
		return result;
	}
	catch(Exception e) {
		throw new BTChipException("Simulator exception", e);
	}
}
 
Example #5
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX3Contactless() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_3));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_3));		
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 0);
	byte[] prevout = Arrays.copyOfRange(input1.getValue(), 4, 4 + 36);
	input1 = dongle.createInput(prevout, false);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		TXIN_3_REDEEM_SCRIPT);
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.NONE);		
	byte[] signature = dongle.untrustedHashSign("45'/2147483647/0/0", new byte[0]);
	signature = canonicalizeSignature(signature);
	byte[] originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(0).getScript(), 2, 2 + signature.length);				
	assertTrue(Arrays.equals(signature, originalSignature));
}
 
Example #6
Source File: KeyUtils.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] compressPublicKey(byte[] publicKey) throws BTChipException {
	switch(publicKey[0]) {
		case 0x04:
			if (publicKey.length != 65) {
				throw new BTChipException("Invalid public key");
			}
			break;
		case 0x02:
		case 0x03:
			if (publicKey.length != 33) {
				throw new BTChipException("Invalid public key");
			}
			return publicKey;
		default:
			throw new BTChipException("Invalid public key");
	}
	byte[] result = new byte[33];
	result[0] = (((publicKey[64] & 1) != 0) ? (byte)0x03 : (byte)0x02);
	System.arraycopy(publicKey, 1, result, 1, 32);
	return result;		
}
 
Example #7
Source File: KeyUtils.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] compressPublicKey(byte[] publicKey) throws BTChipException {
	switch(publicKey[0]) {
		case 0x04:
			if (publicKey.length != 65) {
				throw new BTChipException("Invalid public key");
			}
			break;
		case 0x02:
		case 0x03:
			if (publicKey.length != 33) {
				throw new BTChipException("Invalid public key");
			}
			return publicKey;
		default:
			throw new BTChipException("Invalid public key");
	}
	byte[] result = new byte[33];
	result[0] = (((publicKey[64] & 1) != 0) ? (byte)0x03 : (byte)0x02);
	System.arraycopy(publicKey, 1, result, 1, 32);
	return result;		
}
 
Example #8
Source File: LedgerTransportTEEProxy.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void close() throws BTChipException {
	
	if (service == null) {
		throw new BTChipException("Service is not available");
	}
	if (session == null) {
		return;
	}		
	try {
		service.close(session);
	}
	catch(Exception e) {
		throw new BTChipException("Exception calling service", e);
	}
	session = null;
}
 
Example #9
Source File: LedgerTransportTEEProxy.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public Future<byte[]> requestNVM() throws BTChipException {
	if (service == null) {
		throw new BTChipException("Service is not available");
	}		
	if (session == null) {
		throw new BTChipException("Session is not open");
	}	
	ServiceResult result;
	try {
		result = service.getStorage(session);
	}
	catch(Exception e) {
		throw new BTChipException("Exception calling service", e);
	}
	return FutureUtils.getDummyFuture(result.getResult());
}
 
Example #10
Source File: RequestLoginActivity.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private void autoRegister() throws BTChipException {
    showInstructions(R.string.hw_wallet_registering);

    final BTChipPublicKey hdkey = mHwWallet.getDongle().getWalletPublicKey("");
    final byte[] pubkey = KeyUtils.compressPublicKey(hdkey.getPublicKey());
    Futures.addCallback(mService.signup(mHwWallet, null, "HW", pubkey, hdkey.getChainCode()),
        new FutureCallback<LoginData>() {
            @Override
            public void onSuccess(final LoginData result) {
                onLoginSuccess();
            }

            @Override
            public void onFailure(final Throwable t) {
                t.printStackTrace();
                finishOnUiThread();
            }
        });
}
 
Example #11
Source File: VarintUtils.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public static long read(ByteArrayInputStream in) throws BTChipException {
	long result = 0;
	int val1 = in.read() & 0xff;
	if (val1 < 0xfd) {
		result = val1;
	}
	else
	if (val1 == 0xfd) {
		result |= in.read() & 0xff;
		result |= ((in.read() & 0xff) << 8);
	}
	else
	if (val1 == 0xfe) {
		result |= in.read() & 0xff;
		result |= ((in.read() & 0xff) << 8);
		result |= ((in.read() & 0xff) << 16);
		result |= ((in.read() & 0xff) << 24);
	}
	else {
		throw new BTChipException("Unsupported varint encoding");
	}
	return result;
}
 
Example #12
Source File: BIP32Utils.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] splitPath(String path) throws BTChipException {
	if (path.length() == 0) {
		return new byte[] { 0 };
	}		
	String elements[] = path.split("/");
	if (elements.length > 10) {
		throw new BTChipException("Path too long");
	}
	ByteArrayOutputStream result = new ByteArrayOutputStream();
	result.write((byte)elements.length);
	for (String element : elements) {
		long elementValue;
		int hardenedIndex = element.indexOf('\'');
		if (hardenedIndex > 0) {
			elementValue = Long.parseLong(element.substring(0, hardenedIndex));
			elementValue |= 0x80000000;
		}
		else {
			elementValue = Long.parseLong(element);
		}
		BufferUtils.writeUint32BE(result, elementValue);
	}
	return result.toByteArray();
}
 
Example #13
Source File: BTChipTransportAndroidNFC.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Future<byte[]> exchange(byte[] command) throws BTChipException {
	if (!selected) {
		byte[] selectCommand = new byte[aid.length + 5];
		selectCommand[0] = (byte)0x00;
		selectCommand[1] = (byte)0xA4;
		selectCommand[2] = (byte)0x04;
		selectCommand[3] = (byte)0x00;
		selectCommand[4] = (byte)aid.length;
		System.arraycopy(aid, 0, selectCommand, 5, aid.length);
		try {
			exchangeInternal(selectCommand);
		}
		catch(Exception e) {				
		}
		selected = true;
	}
	return exchangeInternal(command);
}
 
Example #14
Source File: VarintUtils.java    From WalletCordova with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static long read(ByteArrayInputStream in) throws BTChipException {
	long result = 0;
	int val1 = in.read() & 0xff;
	if (val1 < 0xfd) {
		result = val1;
	}
	else
	if (val1 == 0xfd) {
		result |= in.read() & 0xff;
		result |= ((in.read() & 0xff) << 8);
	}
	else
	if (val1 == 0xfe) {
		result |= in.read() & 0xff;
		result |= ((in.read() & 0xff) << 8);
		result |= ((in.read() & 0xff) << 16);
		result |= ((in.read() & 0xff) << 24);
	}
	else {
		throw new BTChipException("Unsupported varint encoding");
	}
	return result;
}
 
Example #15
Source File: BIP32Utils.java    From WalletCordova with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static byte[] splitPath(String path) throws BTChipException {
	if (path.length() == 0) {
		return new byte[] { 0 };
	}		
	String elements[] = path.split("/");
	if (elements.length > 10) {
		throw new BTChipException("Path too long");
	}
	ByteArrayOutputStream result = new ByteArrayOutputStream();
	result.write((byte)elements.length);
	for (String element : elements) {
		long elementValue;
		int hardenedIndex = element.indexOf('\'');
		if (hardenedIndex > 0) {
			elementValue = Long.parseLong(element.substring(0, hardenedIndex));
			elementValue |= 0x80000000;
		}
		else {
			elementValue = Long.parseLong(element);
		}
		BufferUtils.writeUint32BE(result, elementValue);
	}
	return result.toByteArray();
}
 
Example #16
Source File: BTChipTransportAndroidNFC.java    From WalletCordova with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public byte[] exchange(byte[] command) throws BTChipException {
	if (!selected) {
		byte[] selectCommand = new byte[aid.length + 5];
		selectCommand[0] = (byte)0x00;
		selectCommand[1] = (byte)0xA4;
		selectCommand[2] = (byte)0x04;
		selectCommand[3] = (byte)0x00;
		selectCommand[4] = (byte)aid.length;
		System.arraycopy(aid, 0, selectCommand, 5, aid.length);
		try {
			exchangeInternal(selectCommand);
		}
		catch(Exception e) {				
		}
		selected = true;
	}
	return exchangeInternal(command);
}
 
Example #17
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX1ContactlessUntrustedInput() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	byte[] prevout = Arrays.copyOfRange(input1.getValue(), 4, 4 + 36);
	input1 = dongle.createInput(prevout, false);
	try {
		dongle.startUntrustedTransaction(
			true, 
			0, 
			new BTChipDongle.BTChipInput[] { input1 }, 
			txin_1.getOutputs().get(1).getScript());
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}		
}
 
Example #18
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX1ContactlessNoPIN() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	try {
		dongle.startUntrustedTransaction(
			true, 
			0, 
			new BTChipDongle.BTChipInput[] { input1 }, 
			txin_1.getOutputs().get(1).getScript());
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
	}		
}
 
Example #19
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testTX1ContactlessSticky() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	// Keycard validation is done while still in the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	byte[] signature = dongle.untrustedHashSign("44'/0'/0'/0/0", pin);
	signature = canonicalizeSignature(signature);
	byte[] originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(0).getScript(), 1, 1 + signature.length);		
	assertTrue(Arrays.equals(signature, originalSignature));
}
 
Example #20
Source File: AbstractTest.java    From ledger-javacard with GNU Affero General Public License v3.0 6 votes vote down vote up
protected byte[] canonicalizeSignature(byte[] signature) throws BTChipException {
	try {
		ASN1Sequence seq = (ASN1Sequence)ASN1Primitive.fromByteArray(signature);
		BigInteger r = ((DERInteger)seq.getObjectAt(0)).getValue();
		BigInteger s = ((DERInteger)seq.getObjectAt(1)).getValue();
		if (s.compareTo(HALF_ORDER) > 0) {
			s = ORDER.subtract(s);
		}
		else {
			return signature;
		}
			ASN1EncodableVector v = new ASN1EncodableVector();
 			v.add(new DERInteger(r));
 			v.add(new DERInteger(s)); 
 			return new DERSequence(v).getEncoded("DER");			
	}
	catch(Exception e) {
		throw new BTChipException("Error canonicalizing signature", e);
	}
}
 
Example #21
Source File: AbstractTest.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
protected BTChipDongle prepareDongleRestoreTestnet(boolean debug) throws BTChipException {
	BTChipDongle dongle = getDongle(debug);
	dongle.setup(
		new BTChipDongle.OperationMode[] { BTChipDongle.OperationMode.WALLET },
		new BTChipDongle.Feature[] { BTChipDongle.Feature.RFC6979, BTChipDongle.Feature.NO_2FA_P2SH},
		TESTNET_VERSION,
		TESTNET_P2SH_VERSION,
		DEFAULT_PIN,
		null,
		BTChipConstants.QWERTY_KEYMAP,
		DEFAULT_SEED,
		null);
	return dongle;
}
 
Example #22
Source File: TestSetUserKeycard.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testSetUserKeycard() throws BTChipException {
	byte[] newKeycard = new byte[16];
	for (byte i=0; i<16; i++) {
		newKeycard[i] = (byte)(i + (byte)0x20);
	}
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	KeycardHelper keycardHelperNew = new KeycardHelper(newKeycard);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	dongle.verifyPin(DEFAULT_PIN);
	byte[] challenge = dongle.setUserKeycard(DEFAULT_KEYCARD_ADDRESS_SIZE, newKeycard);
	dongle.confirmUserKeycard(keycardHelper.getPIN(challenge));
	challenge = dongle.setUserKeycard(DEFAULT_KEYCARD_ADDRESS_SIZE, DEFAULT_KEYCARD);
	dongle.confirmUserKeycard(keycardHelperNew.getPIN(challenge));
}
 
Example #23
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX1ContactlessDisconnect() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	reset();
	// Card is removed from the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	byte[] signature = null;
	try {
		dongle.untrustedHashSign("44'/0'/0'/0/0", pin);
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_CONDITIONS_NOT_SATISFIED);
	}
}
 
Example #24
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX1ContactlessDisconnectReconnect() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	reset();
	// Card is removed from the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	// Reinitialize the transient parser
	dongle.startUntrustedTransaction(
		false, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	dongle.finalizeInputFull(txout_1.serializeOutputs());		
	byte[] signature = dongle.untrustedHashSign("44'/0'/0'/0/0", pin);
	signature = canonicalizeSignature(signature);
	byte[] originalSignature = Arrays.copyOfRange(txout_1.getInputs().get(0).getScript(), 1, 1 + signature.length);		
	assertTrue(Arrays.equals(signature, originalSignature));
}
 
Example #25
Source File: LedgerHelper.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static byte[] wrapCommandAPDU(int channel, byte[] command, int packetSize) throws BTChipException {
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	if (packetSize < 3) {
		throw new BTChipException("Can't handle Ledger framing with less than 3 bytes for the report");
	}
	int sequenceIdx = 0;
	int offset = 0;
	output.write(channel >> 8);
	output.write(channel);
	output.write(TAG_APDU);
	output.write(sequenceIdx >> 8);
	output.write(sequenceIdx);
	sequenceIdx++;
	output.write(command.length >> 8);
	output.write(command.length);
	int blockSize = (command.length > packetSize - 7 ? packetSize - 7 : command.length);
	output.write(command, offset, blockSize);
	offset += blockSize;
	while (offset != command.length) {
		output.write(channel >> 8);
		output.write(channel);
		output.write(TAG_APDU);
		output.write(sequenceIdx >> 8);
		output.write(sequenceIdx);
		sequenceIdx++;
		blockSize = (command.length - offset > packetSize - 5 ? packetSize - 5 : command.length - offset);
		output.write(command, offset, blockSize);
		offset += blockSize;			
	}
	if ((output.size() % packetSize) != 0) {
		byte[] padding = new byte[packetSize - (output.size() % packetSize)];
		output.write(padding, 0, padding.length);
	}
	return output.toByteArray();		
}
 
Example #26
Source File: BufferUtils.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public static void writeBuffer(ByteArrayOutputStream buffer, byte[] data) throws BTChipException {
	try {
		buffer.write(data);
	}
	catch(IOException e) {
		throw new BTChipException("Internal error", e);
	}
}
 
Example #27
Source File: BufferUtils.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void writeBuffer(ByteArrayOutputStream buffer, byte[] data) throws BTChipException {
	try {
		buffer.write(data);
	}
	catch(IOException e) {
		throw new BTChipException("Internal error", e);
	}
}
 
Example #28
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX1ContactlessDisconnectReconnectFakeOutputAmount() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	reset();
	// Card is removed from the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	// Reinitialize the transient parser
	dongle.startUntrustedTransaction(
		false, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	byte[] fullOutput = txout_1.serializeOutputs();
	fullOutput[4]++;
	try {
		dongle.finalizeInputFull(fullOutput);		
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}		
}
 
Example #29
Source File: BTChipTransportAndroidNFC.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void close() throws BTChipException {
	try {
		if (card.isConnected()) {
			card.close();
		}			
	}
	catch(Exception e) {			
	}
}
 
Example #30
Source File: TestTransaction.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testTX1ContactlessDisconnectReconnectFakeOutputDestination() throws BTChipException {
	KeycardHelper keycardHelper = new KeycardHelper(DEFAULT_KEYCARD);
	BTChipDongle dongle = prepareDongleRestoreTestnet(true);
	simulator.changeProtocol("T=CL,TYPE_A,T1");
	dongle.verifyPin(DEFAULT_PIN);
	BitcoinTransaction txin_1 = new BitcoinTransaction(new ByteArrayInputStream(TXIN_1));
	BitcoinTransaction txout_1 = new BitcoinTransaction(new ByteArrayInputStream(TXOUT_1));
	BTChipDongle.BTChipInput input1 = dongle.getTrustedInput(txin_1, 1);
	dongle.startUntrustedTransaction(
		true, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	BTChipDongle.BTChipOutput output = dongle.finalizeInputFull(txout_1.serializeOutputs());		
	assertEquals(output.getUserConfirmation(), BTChipDongle.UserConfirmation.KEYCARD);		
	reset();
	// Card is removed from the field
	byte[] keycardIndexes = ((BTChipDongle.BTChipOutputKeycard)output).getKeycardIndexes();
	assertEquals(keycardIndexes.length, DEFAULT_KEYCARD_ADDRESS_SIZE);
	byte[] pin = keycardHelper.getPIN(TXOUT_1_ADDRESS, keycardIndexes);
	// Reinitialize the transient parser
	dongle.startUntrustedTransaction(
		false, 
		0, 
		new BTChipDongle.BTChipInput[] { input1 }, 
		txin_1.getOutputs().get(1).getScript());
	byte[] fullOutput = txout_1.serializeOutputs();
	fullOutput[fullOutput.length - 5] ^= (byte)0x42;
	try {
		dongle.finalizeInputFull(fullOutput);		
		fail();
	}
	catch(BTChipException e) {
		assertEquals(e.getSW(), ISO7816.SW_WRONG_DATA);
	}		
}