From cf1c4b6a1e97716be8702ca9b0a5ce0a8cb9093b Mon Sep 17 00:00:00 2001 From: PravinBagde Date: Sun, 11 Jun 2023 19:38:04 +0530 Subject: [PATCH] use HexUtils instead of bouncycastle --- android/build.gradle | 1 - .../im/status/ethereum/keycard/Installer.java | 3 +- .../im/status/ethereum/keycard/SmartCard.java | 121 +++++++++--------- 3 files changed, 62 insertions(+), 63 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 26e7703..9e351d2 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -44,7 +44,6 @@ repositories { dependencies { implementation 'com.facebook.react:react-native:+' - implementation 'org.bouncycastle:bcprov-jdk15on:1.60' implementation 'org.apache.commons:commons-lang3:3.9' implementation 'com.github.status-im.status-keycard-java:android:3.1.1' } diff --git a/android/src/main/java/im/status/ethereum/keycard/Installer.java b/android/src/main/java/im/status/ethereum/keycard/Installer.java index 57cc4c4..945ac2b 100644 --- a/android/src/main/java/im/status/ethereum/keycard/Installer.java +++ b/android/src/main/java/im/status/ethereum/keycard/Installer.java @@ -7,7 +7,6 @@ import im.status.keycard.globalplatform.GlobalPlatformCommandSet; import im.status.keycard.globalplatform.LoadCallback; import im.status.keycard.io.APDUException; import im.status.keycard.io.CardChannel; -import org.bouncycastle.util.encoders.Hex; import java.io.IOException; import java.security.NoSuchAlgorithmException; @@ -60,7 +59,7 @@ public class Installer { Log.i(TAG, "installing NDEF applet..."); - cmdSet.installNDEFApplet(Hex.decode("0024d40f12616e64726f69642e636f6d3a706b67696d2e7374617475732e657468657265756d")).checkOK(); + cmdSet.installNDEFApplet(HexUtils.hexStringToByteArray("0024d40f12616e64726f69642e636f6d3a706b67696d2e7374617475732e657468657265756d")).checkOK(); eventEmitter.emit("keycardInstallationProgress", 0.72); diff --git a/android/src/main/java/im/status/ethereum/keycard/SmartCard.java b/android/src/main/java/im/status/ethereum/keycard/SmartCard.java index ebd4403..dbf7509 100644 --- a/android/src/main/java/im/status/ethereum/keycard/SmartCard.java +++ b/android/src/main/java/im/status/ethereum/keycard/SmartCard.java @@ -40,7 +40,8 @@ import im.status.keycard.applet.Pairing; import im.status.keycard.applet.ApplicationInfo; import im.status.keycard.applet.KeyPath; -import org.bouncycastle.util.encoders.Hex; +import im.status.keycard.HexUtils; + public class SmartCard extends BroadcastReceiver implements CardListener { private NFCCardManager cardManager; @@ -165,10 +166,10 @@ public class SmartCard extends BroadcastReceiver implements CardListener { // First thing to do is selecting the applet on the card. ApplicationInfo info = new ApplicationInfo(cmdSet.select().checkOK().getData()); - String instanceUID = Hex.toHexString(info.getInstanceUID()); + String instanceUID = HexUtils.byteArrayToHexString(info.getInstanceUID()); Log.i(TAG, "Instance UID: " + instanceUID); - Log.i(TAG, "Key UID: " + Hex.toHexString(info.getKeyUID())); - Log.i(TAG, "Secure channel public key: " + Hex.toHexString(info.getSecureChannelPubKey())); + Log.i(TAG, "Key UID: " + HexUtils.byteArrayToHexString(info.getKeyUID())); + Log.i(TAG, "Secure channel public key: " + HexUtils.byteArrayToHexString(info.getSecureChannelPubKey())); Log.i(TAG, "Application version: " + info.getAppVersionString()); Log.i(TAG, "Free pairing slots: " + info.getFreePairingSlots()); @@ -232,11 +233,11 @@ public class SmartCard extends BroadcastReceiver implements CardListener { cardInfo.putBoolean("initialized?", info.isInitializedCard()); if (info.isInitializedCard()) { - String instanceUID = Hex.toHexString(info.getInstanceUID()); + String instanceUID = HexUtils.byteArrayToHexString(info.getInstanceUID()); Log.i(TAG, "Instance UID: " + instanceUID); - Log.i(TAG, "Key UID: " + Hex.toHexString(info.getKeyUID())); - Log.i(TAG, "Secure channel public key: " + Hex.toHexString(info.getSecureChannelPubKey())); + Log.i(TAG, "Key UID: " + HexUtils.byteArrayToHexString(info.getKeyUID())); + Log.i(TAG, "Secure channel public key: " + HexUtils.byteArrayToHexString(info.getSecureChannelPubKey())); Log.i(TAG, "Application version: " + info.getAppVersionString()); Log.i(TAG, "Free pairing slots: " + info.getFreePairingSlots()); @@ -265,9 +266,9 @@ public class SmartCard extends BroadcastReceiver implements CardListener { cardInfo.putBoolean("has-master-key?", info.hasMasterKey()); cardInfo.putBoolean("paired?", isPaired); - cardInfo.putString("instance-uid", Hex.toHexString(info.getInstanceUID())); - cardInfo.putString("key-uid", Hex.toHexString(info.getKeyUID())); - cardInfo.putString("secure-channel-pub-key", Hex.toHexString(info.getSecureChannelPubKey())); + cardInfo.putString("instance-uid", HexUtils.byteArrayToHexString(info.getInstanceUID())); + cardInfo.putString("key-uid", HexUtils.byteArrayToHexString(info.getKeyUID())); + cardInfo.putString("secure-channel-pub-key", HexUtils.byteArrayToHexString(info.getSecureChannelPubKey())); cardInfo.putString("app-version", info.getAppVersionString()); cardInfo.putInt("free-pairing-slots", info.getFreePairingSlots()); } @@ -315,7 +316,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { byte[] key = cmdSet.exportCurrentKey(true).checkOK().getData(); - return Hex.toHexString(key); + return HexUtils.byteArrayToHexString(key); } public String exportKeyWithPath(final String pin, final String path) throws IOException, APDUException { @@ -323,7 +324,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { byte[] key = BIP32KeyPair.fromTLV(cmdSet.exportKey(path, false, true).checkOK().getData()).getPublicKey(); - return Hex.toHexString(key); + return HexUtils.byteArrayToHexString(key); } public WritableMap getKeys(final String pin) throws IOException, APDUException { @@ -338,12 +339,12 @@ public class SmartCard extends BroadcastReceiver implements CardListener { ApplicationInfo info = cmdSet.getApplicationInfo(); WritableMap data = Arguments.createMap(); - data.putString("whisper-address", Hex.toHexString(whisperKeyPair.toEthereumAddress())); - data.putString("whisper-public-key", Hex.toHexString(whisperKeyPair.getPublicKey())); - data.putString("whisper-private-key", Hex.toHexString(whisperKeyPair.getPrivateKey())); - data.putString("encryption-public-key", Hex.toHexString(encryptionKeyPair.getPublicKey())); - data.putString("instance-uid", Hex.toHexString(info.getInstanceUID())); - data.putString("key-uid", Hex.toHexString(info.getKeyUID())); + data.putString("whisper-address", HexUtils.byteArrayToHexString(whisperKeyPair.toEthereumAddress())); + data.putString("whisper-public-key", HexUtils.byteArrayToHexString(whisperKeyPair.getPublicKey())); + data.putString("whisper-private-key", HexUtils.byteArrayToHexString(whisperKeyPair.getPrivateKey())); + data.putString("encryption-public-key", HexUtils.byteArrayToHexString(encryptionKeyPair.getPublicKey())); + data.putString("instance-uid", HexUtils.byteArrayToHexString(info.getInstanceUID())); + data.putString("key-uid", HexUtils.byteArrayToHexString(info.getKeyUID())); return data; } @@ -369,18 +370,18 @@ public class SmartCard extends BroadcastReceiver implements CardListener { ApplicationInfo info = cmdSet.getApplicationInfo(); WritableMap data = Arguments.createMap(); - data.putString("address", Hex.toHexString(masterPair.toEthereumAddress())); - data.putString("public-key", Hex.toHexString(masterPair.getPublicKey())); - data.putString("wallet-root-address", Hex.toHexString(keyPair.toEthereumAddress())); - data.putString("wallet-root-public-key", Hex.toHexString(keyPair.getPublicKey())); - data.putString("wallet-address", Hex.toHexString(walletKeyPair.toEthereumAddress())); - data.putString("wallet-public-key", Hex.toHexString(walletKeyPair.getPublicKey())); - data.putString("whisper-address", Hex.toHexString(whisperKeyPair.toEthereumAddress())); - data.putString("whisper-public-key", Hex.toHexString(whisperKeyPair.getPublicKey())); - data.putString("whisper-private-key", Hex.toHexString(whisperKeyPair.getPrivateKey())); - data.putString("encryption-public-key", Hex.toHexString(encryptionKeyPair.getPublicKey())); - data.putString("instance-uid", Hex.toHexString(info.getInstanceUID())); - data.putString("key-uid", Hex.toHexString(info.getKeyUID())); + data.putString("address", HexUtils.byteArrayToHexString(masterPair.toEthereumAddress())); + data.putString("public-key", HexUtils.byteArrayToHexString(masterPair.getPublicKey())); + data.putString("wallet-root-address", HexUtils.byteArrayToHexString(keyPair.toEthereumAddress())); + data.putString("wallet-root-public-key", HexUtils.byteArrayToHexString(keyPair.getPublicKey())); + data.putString("wallet-address", HexUtils.byteArrayToHexString(walletKeyPair.toEthereumAddress())); + data.putString("wallet-public-key", HexUtils.byteArrayToHexString(walletKeyPair.getPublicKey())); + data.putString("whisper-address", HexUtils.byteArrayToHexString(whisperKeyPair.toEthereumAddress())); + data.putString("whisper-public-key", HexUtils.byteArrayToHexString(whisperKeyPair.getPublicKey())); + data.putString("whisper-private-key", HexUtils.byteArrayToHexString(whisperKeyPair.getPrivateKey())); + data.putString("encryption-public-key", HexUtils.byteArrayToHexString(encryptionKeyPair.getPublicKey())); + data.putString("instance-uid", HexUtils.byteArrayToHexString(info.getInstanceUID())); + data.putString("key-uid", HexUtils.byteArrayToHexString(info.getKeyUID())); return data; } @@ -413,18 +414,18 @@ public class SmartCard extends BroadcastReceiver implements CardListener { ApplicationInfo info = new ApplicationInfo(cmdSet.select().checkOK().getData()); WritableMap data = Arguments.createMap(); - data.putString("address", Hex.toHexString(keyPair.toEthereumAddress())); - data.putString("public-key", Hex.toHexString(keyPair.getPublicKey())); - data.putString("wallet-root-address", Hex.toHexString(rootKeyPair.toEthereumAddress())); - data.putString("wallet-root-public-key", Hex.toHexString(rootKeyPair.getPublicKey())); - data.putString("wallet-address", Hex.toHexString(walletKeyPair.toEthereumAddress())); - data.putString("wallet-public-key", Hex.toHexString(walletKeyPair.getPublicKey())); - data.putString("whisper-address", Hex.toHexString(whisperKeyPair.toEthereumAddress())); - data.putString("whisper-public-key", Hex.toHexString(whisperKeyPair.getPublicKey())); - data.putString("whisper-private-key", Hex.toHexString(whisperKeyPair.getPrivateKey())); - data.putString("encryption-public-key", Hex.toHexString(encryptionKeyPair.getPublicKey())); - data.putString("instance-uid", Hex.toHexString(info.getInstanceUID())); - data.putString("key-uid", Hex.toHexString(info.getKeyUID())); + data.putString("address", HexUtils.byteArrayToHexString(keyPair.toEthereumAddress())); + data.putString("public-key", HexUtils.byteArrayToHexString(keyPair.getPublicKey())); + data.putString("wallet-root-address", HexUtils.byteArrayToHexString(rootKeyPair.toEthereumAddress())); + data.putString("wallet-root-public-key", HexUtils.byteArrayToHexString(rootKeyPair.getPublicKey())); + data.putString("wallet-address", HexUtils.byteArrayToHexString(walletKeyPair.toEthereumAddress())); + data.putString("wallet-public-key", HexUtils.byteArrayToHexString(walletKeyPair.getPublicKey())); + data.putString("whisper-address", HexUtils.byteArrayToHexString(whisperKeyPair.toEthereumAddress())); + data.putString("whisper-public-key", HexUtils.byteArrayToHexString(whisperKeyPair.getPublicKey())); + data.putString("whisper-private-key", HexUtils.byteArrayToHexString(whisperKeyPair.getPrivateKey())); + data.putString("encryption-public-key", HexUtils.byteArrayToHexString(encryptionKeyPair.getPublicKey())); + data.putString("instance-uid", HexUtils.byteArrayToHexString(info.getInstanceUID())); + data.putString("key-uid", HexUtils.byteArrayToHexString(info.getKeyUID())); return data; } @@ -479,7 +480,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { cmdSet.autoUnpair(); Log.i(TAG, "card unpaired"); - String instanceUID = Hex.toHexString(cmdSet.getApplicationInfo().getInstanceUID()); + String instanceUID = HexUtils.byteArrayToHexString(cmdSet.getApplicationInfo().getInstanceUID()); pairings.remove(instanceUID); } @@ -513,7 +514,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { cmdSet.autoUnpair(); Log.i(TAG, "card unpaired"); - String instanceUID = Hex.toHexString(cmdSet.getApplicationInfo().getInstanceUID()); + String instanceUID = HexUtils.byteArrayToHexString(cmdSet.getApplicationInfo().getInstanceUID()); pairings.remove(instanceUID); } @@ -525,13 +526,13 @@ public class SmartCard extends BroadcastReceiver implements CardListener { public String sign(final String pin, final String message) throws IOException, APDUException { KeycardCommandSet cmdSet = authenticatedCommandSet(pin); - byte[] hash = Hex.decode(message); + byte[] hash = HexUtils.hexStringToByteArray(message); RecoverableSignature signature = new RecoverableSignature(hash, cmdSet.sign(hash).checkOK().getData()); - Log.i(TAG, "Signed hash: " + Hex.toHexString(hash)); + Log.i(TAG, "Signed hash: " + HexUtils.byteArrayToHexString(hash)); Log.i(TAG, "Recovery ID: " + signature.getRecId()); - Log.i(TAG, "R: " + Hex.toHexString(signature.getR())); - Log.i(TAG, "S: " + Hex.toHexString(signature.getS())); + Log.i(TAG, "R: " + HexUtils.byteArrayToHexString(signature.getR())); + Log.i(TAG, "S: " + HexUtils.byteArrayToHexString(signature.getS())); ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -539,7 +540,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { out.write(signature.getS()); out.write(signature.getRecId()); - String sig = Hex.toHexString(out.toByteArray()); + String sig = HexUtils.byteArrayToHexString(out.toByteArray()); Log.i(TAG, "Signature: " + sig); return sig; @@ -548,7 +549,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { public String signWithPath(final String pin, final String path, final String message) throws IOException, APDUException { KeycardCommandSet cmdSet = authenticatedCommandSet(pin); - byte[] hash = Hex.decode(message); + byte[] hash = HexUtils.hexStringToByteArray(message); RecoverableSignature signature; @@ -562,10 +563,10 @@ public class SmartCard extends BroadcastReceiver implements CardListener { signature = new RecoverableSignature(hash, cmdSet.signWithPath(hash, path, false).checkOK().getData()); } - Log.i(TAG, "Signed hash: " + Hex.toHexString(hash)); + Log.i(TAG, "Signed hash: " + HexUtils.byteArrayToHexString(hash)); Log.i(TAG, "Recovery ID: " + signature.getRecId()); - Log.i(TAG, "R: " + Hex.toHexString(signature.getR())); - Log.i(TAG, "S: " + Hex.toHexString(signature.getS())); + Log.i(TAG, "R: " + HexUtils.byteArrayToHexString(signature.getR())); + Log.i(TAG, "S: " + HexUtils.byteArrayToHexString(signature.getS())); ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -573,7 +574,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { out.write(signature.getS()); out.write(signature.getRecId()); - String sig = Hex.toHexString(out.toByteArray()); + String sig = HexUtils.byteArrayToHexString(out.toByteArray()); Log.i(TAG, "Signature: " + sig); return sig; @@ -583,13 +584,13 @@ public class SmartCard extends BroadcastReceiver implements CardListener { CashCommandSet cmdSet = new CashCommandSet(this.cardChannel); cmdSet.select().checkOK(); - byte[] hash = Hex.decode(message); + byte[] hash = HexUtils.hexStringToByteArray(message); RecoverableSignature signature = new RecoverableSignature(hash, cmdSet.sign(hash).checkOK().getData()); - Log.i(TAG, "Signed hash: " + Hex.toHexString(hash)); + Log.i(TAG, "Signed hash: " + HexUtils.byteArrayToHexString(hash)); Log.i(TAG, "Recovery ID: " + signature.getRecId()); - Log.i(TAG, "R: " + Hex.toHexString(signature.getR())); - Log.i(TAG, "S: " + Hex.toHexString(signature.getS())); + Log.i(TAG, "R: " + HexUtils.byteArrayToHexString(signature.getR())); + Log.i(TAG, "S: " + HexUtils.byteArrayToHexString(signature.getS())); ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -597,7 +598,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { out.write(signature.getS()); out.write(signature.getRecId()); - String sig = Hex.toHexString(out.toByteArray()); + String sig = HexUtils.byteArrayToHexString(out.toByteArray()); Log.i(TAG, "Signature: " + sig); return sig; @@ -630,7 +631,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener { } private void openSecureChannel(KeycardCommandSet cmdSet) throws IOException, APDUException { - String instanceUID = Hex.toHexString(cmdSet.getApplicationInfo().getInstanceUID()); + String instanceUID = HexUtils.byteArrayToHexString(cmdSet.getApplicationInfo().getInstanceUID()); String pairingBase64 = pairings.get(instanceUID); if (pairingBase64 == null) {