Compare commits

...
Author SHA1 Message Date
Dmitry Novotochinov e3870c6669 add signPinless() 2019-04-09 17:57:30 +03:00
Dmitry Novotochinov d9ccdd6a7d bump version 2019-04-08 20:53:54 +03:00
Dmitry Novotochinov 7b1e6cce65 pass bip39 words list as an argument to generateMnemonic() 2019-04-08 20:53:17 +03:00
Dmitry Novotochinov b9a66144b3 use 2.1.0 sdk 2019-03-26 16:54:13 +03:00
Dmitry Novotochinov 8032e1c30f bump version 2019-03-16 22:13:43 +03:00
Dmitry Novotochinov 56069b4b07 use Hex.decode to get bytes 2019-03-16 22:13:43 +03:00
Dmitry Novotochinov 7f416b0380 return signature string (r+s+v) 2019-03-16 22:13:43 +03:00
Dmitry Novotochinov 8cf3cf5785 add sign() method 2019-03-16 22:13:43 +03:00
Dmitry Novotochinov a6da0a7889 bump version 2019-03-11 12:52:30 +03:00
Dmitry Novotochinov 97de3e6066 improve getKeys() speed 2019-03-11 12:52:18 +03:00
Dmitry Novotochinov d6378e434f bump version 2019-03-05 14:40:28 +03:00
Dmitry Novotochinov d7854a33bb handle IllegalArgumentException during autoOpenSecureChannel() 2019-03-05 14:40:15 +03:00
Dmitry Novotochinov e0448b0960 bump version 2019-02-27 16:41:18 +03:00
Dmitry Novotochinov c7a37ffef8 update wallet derivation path 2019-02-27 16:40:14 +03:00
Dmitry Novotochinov 9e6df66bfd bump version 2019-02-22 17:54:09 +02:00
Dmitry Novotochinov c8b803ab07 add paired? attribute to info 2019-02-22 17:53:35 +02:00
Dmitry Novotochinov 0bc6dcd93c remove logging 2019-02-20 21:13:09 +03:00
Dmitry Novotochinov f6fe495046 bump version 2019-02-20 18:54:07 +03:00
Dmitry Novotochinov 45ca4d49e5 do not add new line to the end of random token string 2019-02-20 18:53:13 +03:00
Dmitry Novotochinov 8b6fb829bd bump version 2019-02-10 13:33:07 +03:00
Dmitry Novotochinov a15dbca837 allow user to set PIN during init 2019-02-10 13:33:07 +03:00
Dmitry Novotochinov 04b0fb6fcb bump version 2019-02-01 14:01:10 +03:00
Dmitry Novotochinov 2d15fd6d50 upd version 2019-02-01 13:57:41 +03:00
Dmitry Novotochinov fcd50896c0 add instance-uid to return data 2019-02-01 13:48:10 +03:00
Dmitry Novotochinov 4c2aada5dc update cap file 2019-01-17 12:11:16 +03:00
Dmitry Novotochinov 2dffe6aae4 remove unused method 2019-01-11 21:24:53 +03:00
Dmitry Novotochinov 0dfa0ecee6 Call start() after SmartCard initialized 2019-01-11 21:17:31 +03:00
6 changed files with 171 additions and 75 deletions
+2 -2
View File
@@ -39,6 +39,6 @@ repositories {
dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'com.madgag.spongycastle:core:1.58.0.0'
implementation 'com.github.status-im.status-keycard-java:android:2.0.0'
implementation 'org.bouncycastle:bcprov-jdk15on:1.60'
implementation 'com.github.status-im.status-keycard-java:android:2.2.1'
}
@@ -23,7 +23,7 @@ import im.status.keycard.io.APDUException;
public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
private static final String TAG = "StatusKeycard";
private static final String CAP_FILENAME = "keycard.cap";
private static final String CAP_FILENAME = "keycard_v2.1.cap";
private SmartCard smartCard;
private final ReactApplicationContext reactContext;
@@ -42,6 +42,7 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
public void onHostResume() {
if (this.smartCard == null) {
this.smartCard = new SmartCard(getCurrentActivity(), reactContext);
smartCard.start();
}
}
@@ -80,24 +81,11 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
}
@ReactMethod
public void start(final Promise promise) {
if (smartCard != null) {
if (smartCard.start()) {
promise.resolve(true);
} else {
promise.reject("Error", "Not supported on this device");
}
} else {
promise.reject("Error", "smartCard is not initialized yet");
}
}
@ReactMethod
public void init(final Promise promise) {
public void init(final String pin, final Promise promise) {
new Thread(new Runnable() {
public void run() {
try {
SmartCardSecrets s = smartCard.init();
SmartCardSecrets s = smartCard.init(pin);
WritableMap params = Arguments.createMap();
params.putString("pin", s.getPin());
@@ -131,11 +119,11 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
}
@ReactMethod
public void generateMnemonic(final String pairing, final Promise promise) {
public void generateMnemonic(final String pairing, final String words, final Promise promise) {
new Thread(new Runnable() {
public void run() {
try {
promise.resolve(smartCard.generateMnemonic(pairing));
promise.resolve(smartCard.generateMnemonic(pairing, words));
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
@@ -218,6 +206,34 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
}).start();
}
@ReactMethod
public void sign(final String pairing, final String pin, final String hash, final Promise promise) {
new Thread(new Runnable() {
public void run() {
try {
promise.resolve(smartCard.sign(pairing, pin, hash));
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
}
}
}).start();
}
@ReactMethod
public void signPinless(final String hash, final Promise promise) {
new Thread(new Runnable() {
public void run() {
try {
promise.resolve(smartCard.signPinless(hash));
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
}
}
}).start();
}
@ReactMethod
public void installApplet(final Promise promise) {
final ReactContext ctx = this.reactContext;
@@ -235,12 +251,12 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
}
@ReactMethod
public void installAppletAndInitCard(final Promise promise) {
public void installAppletAndInitCard(final String pin, final Promise promise) {
final ReactContext ctx = this.reactContext;
new Thread(new Runnable() {
public void run() {
try {
SmartCardSecrets s = smartCard.installAppletAndInitCard(ctx.getAssets(), CAP_FILENAME);
SmartCardSecrets s = smartCard.installAppletAndInitCard(pin, ctx.getAssets(), CAP_FILENAME);
WritableMap params = Arguments.createMap();
params.putString("pin", s.getPin());
@@ -15,10 +15,14 @@ import android.util.Log;
import com.facebook.react.bridge.*;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.Scanner;
import im.status.keycard.applet.RecoverableSignature;
import im.status.keycard.globalplatform.GlobalPlatformCommandSet;
import im.status.keycard.io.APDUException;
import im.status.keycard.io.CardChannel;
@@ -32,7 +36,7 @@ import im.status.keycard.applet.Pairing;
import im.status.keycard.applet.ApplicationInfo;
import im.status.keycard.applet.KeyPath;
import org.spongycastle.util.encoders.Hex;
import org.bouncycastle.util.encoders.Hex;
public class SmartCard extends BroadcastReceiver implements CardListener {
private NFCCardManager cardManager;
@@ -42,10 +46,12 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
private CardChannel cardChannel;
private EventEmitter eventEmitter;
private static final String TAG = "SmartCard";
private Boolean started = false;
private static final String WALLET_PATH = "m/44'/0'/0'/0/0";
private static final String WALLET_PATH = "m/44'/60'/0'/0/0";
private static final String WHISPER_PATH = "m/43'/60'/1581'/0'/0";
private static final String ENCRYPTION_PATH = "m/43'/60'/1581'/1'/0";
private static final int WORDS_LIST_SIZE = 2048;
public SmartCard(Activity activity, ReactContext reactContext) {
this.cardManager = new NFCCardManager();
@@ -65,15 +71,22 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
}
public boolean start() {
this.cardManager.start();
if (this.nfcAdapter != null) {
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
activity.registerReceiver(this, filter);
nfcAdapter.enableReaderMode(activity, this.cardManager, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK, null);
return true;
if (!started) {
this.cardManager.start();
started = true;
if (this.nfcAdapter != null) {
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
activity.registerReceiver(this, filter);
nfcAdapter.enableReaderMode(activity, this.cardManager, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK, null);
return true;
} else {
log("not support in this device");
return false;
}
} else {
log("not support in this device");
return false;
return true;
}
}
@@ -85,7 +98,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
@Override
public void onDisconnected() {
eventEmitter.emit( "keyCardOnDisconnected", null);
eventEmitter.emit("keyCardOnDisconnected", null);
}
@Override
@@ -114,13 +127,13 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
}
}
public SmartCardSecrets init() throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
public SmartCardSecrets init(final String userPin) throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK();
eventEmitter.emit("keycardInstallationProgress", 0.90);
SmartCardSecrets s = SmartCardSecrets.generate();
SmartCardSecrets s = SmartCardSecrets.generate(userPin);
eventEmitter.emit("keycardInstallationProgress", 0.93);
@@ -133,7 +146,6 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
public String pair(String pairingPassword) throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
log("Pairing password: " + pairingPassword);
Log.i(TAG, "Applet selection successful");
// First thing to do is selecting the applet on the card.
@@ -146,14 +158,11 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
cmdSet.autoPair(pairingPassword);
Pairing pairing = cmdSet.getPairing();
log("Pairing index: " + pairing.getPairingIndex());
log("Pairing key: " + Hex.toHexString(pairing.getPairingKey()));
log("Pairing toBase64: " + pairing.toBase64());
return pairing.toBase64();
}
public String generateMnemonic(String pairingBase64) throws IOException, APDUException {
public String generateMnemonic(String pairingBase64, String words) throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK();
@@ -164,7 +173,16 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
Log.i(TAG, "secure channel opened");
Mnemonic mnemonic = new Mnemonic(cmdSet.generateMnemonic(KeycardCommandSet.GENERATE_MNEMONIC_12_WORDS).checkOK().getData());
mnemonic.fetchBIP39EnglishWordlist();
Scanner scanner = new Scanner(words);
ArrayList<String> list = new ArrayList<>();
while(scanner.hasNextLine()) {
list.add(scanner.nextLine());
}
scanner.close();
String [] wordsList = list.toArray(new String[WORDS_LIST_SIZE]);
mnemonic.setWordlist(wordsList);
return mnemonic.toMnemonicPhrase();
}
@@ -209,23 +227,31 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
Log.i(TAG, "The card has no master key");
}
Boolean isPaired = false;
if (pairingBase64.length() > 0) {
Pairing pairing = new Pairing(pairingBase64);
cmdSet.setPairing(pairing);
try {
Pairing pairing = new Pairing(pairingBase64);
cmdSet.setPairing(pairing);
cmdSet.autoOpenSecureChannel();
Log.i(TAG, "secure channel opened");
cmdSet.autoOpenSecureChannel();
Log.i(TAG, "secure channel opened");
isPaired = true;
ApplicationStatus status = new ApplicationStatus(cmdSet.getStatus(KeycardCommandSet.GET_STATUS_P1_APPLICATION).checkOK().getData());
ApplicationStatus status = new ApplicationStatus(cmdSet.getStatus(KeycardCommandSet.GET_STATUS_P1_APPLICATION).checkOK().getData());
Log.i(TAG, "PIN retry counter: " + status.getPINRetryCount());
Log.i(TAG, "PUK retry counter: " + status.getPUKRetryCount());
Log.i(TAG, "PIN retry counter: " + status.getPINRetryCount());
Log.i(TAG, "PUK retry counter: " + status.getPUKRetryCount());
cardInfo.putInt("pin-retry-counter", status.getPINRetryCount());
cardInfo.putInt("puk-retry-counter", status.getPUKRetryCount());
cardInfo.putInt("pin-retry-counter", status.getPINRetryCount());
cardInfo.putInt("puk-retry-counter", status.getPUKRetryCount());
} catch (IOException | IllegalArgumentException e) {
Log.i(TAG, "autoOpenSecureChannel failed: " + e.getMessage());
}
}
cardInfo.putBoolean("has-master-key?", info.hasMasterKey());
cardInfo.putBoolean("paired?", isPaired);
cardInfo.putString("instance-uid", Hex.toHexString(info.getInstanceUID()));
cardInfo.putString("secure-channel-pub-key", Hex.toHexString(info.getSecureChannelPubKey()));
cardInfo.putString("app-version", info.getAppVersionString());
@@ -251,10 +277,8 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
KeyPath currentPath = new KeyPath(cmdSet.getStatus(KeycardCommandSet.GET_STATUS_P1_KEY_PATH).checkOK().getData());
Log.i(TAG, "Current key path: " + currentPath);
if (!currentPath.toString().equals("m/44'/0'/0'/0/0")) {
cmdSet.deriveKey(path).checkOK();
Log.i(TAG, "Derived m/44'/0'/0'/0/0");
}
cmdSet.deriveKey(path).checkOK();
Log.i(TAG, "Derived " + path);
}
public String exportKey(final String pairingBase64, final String pin) throws IOException, APDUException {
@@ -288,25 +312,13 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
cmdSet.verifyPIN(pin).checkOK();
Log.i(TAG, "pin verified");
cmdSet.deriveKey(WALLET_PATH).checkOK();
Log.i(TAG, "Derived " + WALLET_PATH);
byte[] tlv = cmdSet.exportCurrentKey(true).checkOK().getData();
byte[] tlv = cmdSet.exportKey(WALLET_PATH, true, true).checkOK().getData();
BIP32KeyPair walletKeyPair = BIP32KeyPair.fromTLV(tlv);
cmdSet.deriveKey(WHISPER_PATH).checkOK();
Log.i(TAG, "Derived " + WHISPER_PATH);
byte[] tlv2 = cmdSet.exportCurrentKey(false).checkOK().getData();
byte[] tlv2 = cmdSet.exportKey(WHISPER_PATH, false, false).checkOK().getData();
BIP32KeyPair whisperKeyPair = BIP32KeyPair.fromTLV(tlv2);
cmdSet.deriveKey(ENCRYPTION_PATH).checkOK();
Log.i(TAG, "Derived " + ENCRYPTION_PATH);
byte[] tlv3 = cmdSet.exportCurrentKey(false).checkOK().getData();
byte[] tlv3 = cmdSet.exportKey(ENCRYPTION_PATH, false, false).checkOK().getData();
BIP32KeyPair encryptionKeyPair = BIP32KeyPair.fromTLV(tlv3);
WritableMap data = Arguments.createMap();
@@ -355,12 +367,15 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
byte[] tlv3 = cmdSet.exportCurrentKey(false).checkOK().getData();
BIP32KeyPair encryptionKeyPair = BIP32KeyPair.fromTLV(tlv3);
ApplicationInfo info = new ApplicationInfo(cmdSet.select().checkOK().getData());
WritableMap data = Arguments.createMap();
data.putString("wallet-address", Hex.toHexString(walletKeyPair.toEthereumAddress()));
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()));
return data;
}
@@ -370,11 +385,11 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
installer.start();
}
public SmartCardSecrets installAppletAndInitCard(AssetManager assets, String capPath) throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
public SmartCardSecrets installAppletAndInitCard(final String userPin, AssetManager assets, String capPath) throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
Installer installer = new Installer(this.cardChannel, assets, capPath, eventEmitter);
installer.start();
return init();
return init(userPin);
}
public int verifyPin(final String pairingBase64, final String pin) throws IOException, APDUException {
@@ -459,4 +474,61 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
delete();
}
public String sign(final String pairingBase64, final String pin, final String message) throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK();
Pairing pairing = new Pairing(pairingBase64);
cmdSet.setPairing(pairing);
cmdSet.autoOpenSecureChannel();
Log.i(TAG, "secure channel opened");
cmdSet.verifyPIN(pin).checkOK();
Log.i(TAG, "pin verified");
byte[] hash = Hex.decode(message);
RecoverableSignature signature = new RecoverableSignature(hash, cmdSet.sign(hash).checkOK().getData());
Log.i(TAG, "Signed hash: " + Hex.toHexString(hash));
Log.i(TAG, "Recovery ID: " + signature.getRecId());
Log.i(TAG, "R: " + Hex.toHexString(signature.getR()));
Log.i(TAG, "S: " + Hex.toHexString(signature.getS()));
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(signature.getR());
out.write(signature.getS());
out.write(signature.getRecId());
String sig = Hex.toHexString(out.toByteArray());
Log.i(TAG, "Signature: " + sig);
return sig;
}
public String signPinless(final String message) throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK();
byte[] hash = Hex.decode(message);
RecoverableSignature signature = new RecoverableSignature(hash, cmdSet.signPinless(hash).checkOK().getData());
Log.i(TAG, "Signed hash: " + Hex.toHexString(hash));
Log.i(TAG, "Recovery ID: " + signature.getRecId());
Log.i(TAG, "R: " + Hex.toHexString(signature.getR()));
Log.i(TAG, "S: " + Hex.toHexString(signature.getS()));
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(signature.getR());
out.write(signature.getS());
out.write(signature.getRecId());
String sig = Hex.toHexString(out.toByteArray());
Log.i(TAG, "Signature: " + sig);
return sig;
}
}
@@ -3,6 +3,7 @@ package im.status.ethereum.keycard;
import android.support.annotation.NonNull;
import android.util.Base64;
import static android.util.Base64.NO_PADDING;
import static android.util.Base64.NO_WRAP;
import java.security.SecureRandom;
@@ -28,11 +29,18 @@ public class SmartCardSecrets {
}
@NonNull
public static SmartCardSecrets generate() throws NoSuchAlgorithmException, InvalidKeySpecException {
public static SmartCardSecrets generate(final String userPin) throws NoSuchAlgorithmException, InvalidKeySpecException {
String pairingPassword = randomToken(12);
long pinNumber = randomLong(PIN_BOUND);
long pukNumber = randomLong(PUK_BOUND);
String pin = String.format("%06d", pinNumber);
String pin;
if (userPin.isEmpty()) {
pin = String.format("%06d", pinNumber);
} else {
pin = userPin;
}
String puk = String.format("%012d", pukNumber);
return new SmartCardSecrets(pin, puk, pairingPassword);
@@ -56,7 +64,7 @@ public class SmartCardSecrets {
}
public static String randomToken(int length) {
return Base64.encodeToString(randomBytes(length), NO_PADDING);
return Base64.encodeToString(randomBytes(length), (NO_PADDING | NO_WRAP));
}
public static byte[] randomBytes(int length) {
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "react-native-status-keycard",
"version": "2.3.0",
"version": "2.5.0",
"description": "React Native library to interact with Status Keycard using NFC connection (Android only)",
"main": "index.js",
"scripts": {