Compare commits

..
Author SHA1 Message Date
Dmitry Novotochinov 11ae795703 emit init event 2019-01-11 21:02:16 +03:00
Dmitry Novotochinov 7c0feb912c Call start() after SmartCard initialized 2019-01-11 20:35:29 +03:00
Dmitry Novotochinov cd432d19c2 bump version 2019-01-09 20:04:11 +03:00
Dmitry Novotochinov ab6df22059 update cap file 2019-01-09 20:01:51 +03:00
Dmitry Novotochinov 6b915dda5a add new methods 2019-01-09 19:57:06 +03:00
Dmitry Novotochinov 7a23dc233b bump version 2019-01-09 19:57:06 +03:00
Dmitry Novotochinov d2db19f49f add verifyPin and changePin methods 2019-01-09 19:57:06 +03:00
Dmitry Novotochinov b2cf1b0a7a fix NPE 2019-01-07 21:03:03 +03:00
Dmitry Novotochinov d4b4abb63b Update package.json
update license
2018-12-22 21:24:12 +03:00
4 changed files with 168 additions and 33 deletions
@@ -23,6 +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 SmartCard smartCard;
private final ReactApplicationContext reactContext;
@@ -41,6 +42,7 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
public void onHostResume() {
if (this.smartCard == null) {
this.smartCard = new SmartCard(getCurrentActivity(), reactContext);
smartCard.start();
}
}
@@ -55,12 +57,20 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
@ReactMethod
public void nfcIsSupported(final Promise promise) {
promise.resolve(smartCard.isNfcSupported());
if (smartCard != null) {
promise.resolve(smartCard.isNfcSupported());
} else {
promise.resolve(false);
}
}
@ReactMethod
public void nfcIsEnabled(final Promise promise) {
promise.resolve(smartCard.isNfcEnabled());
if (smartCard != null) {
promise.resolve(smartCard.isNfcEnabled());
} else {
promise.resolve(false);
}
}
@ReactMethod
@@ -72,10 +82,14 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
@ReactMethod
public void start(final Promise promise) {
if (smartCard.start()) {
promise.resolve(true);
if (smartCard != null) {
if (smartCard.start()) {
promise.resolve(true);
} else {
promise.reject("Error", "Not supported on this device");
}
} else {
promise.reject("Error", "Not supported on this device");
promise.reject("Error", "smartCard is not initialized yet");
}
}
@@ -157,11 +171,11 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
}
@ReactMethod
public void getApplicationInfo(final Promise promise) {
public void getApplicationInfo(final String pairingBase64, final Promise promise) {
new Thread(new Runnable() {
public void run() {
try {
promise.resolve(smartCard.getApplicationInfo());
promise.resolve(smartCard.getApplicationInfo(pairingBase64));
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
@@ -211,7 +225,7 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
new Thread(new Runnable() {
public void run() {
try {
smartCard.installApplet(ctx.getAssets(), "wallet.cap");
smartCard.installApplet(ctx.getAssets(), CAP_FILENAME);
promise.resolve(true);
} catch (IOException | APDUException | NoSuchAlgorithmException | InvalidKeySpecException e) {
Log.d(TAG, e.getMessage());
@@ -227,7 +241,7 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
new Thread(new Runnable() {
public void run() {
try {
SmartCardSecrets s = smartCard.installAppletAndInitCard(ctx.getAssets(), "wallet.cap");
SmartCardSecrets s = smartCard.installAppletAndInitCard(ctx.getAssets(), CAP_FILENAME);
WritableMap params = Arguments.createMap();
params.putString("pin", s.getPin());
@@ -247,8 +261,7 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
@ReactMethod
public void verifyPin(final String pairing, final String pin, final Promise promise) {
try {
smartCard.verifyPin(pairing, pin);
promise.resolve(true);
promise.resolve(smartCard.verifyPin(pairing, pin));
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
@@ -266,4 +279,48 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
}
}
@ReactMethod
public void unblockPin(final String pairing, final String puk, final String newPin, final Promise promise) {
try {
smartCard.unblockPin(pairing, puk, newPin);
promise.resolve(true);
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
}
}
@ReactMethod
public void unpair(final String pairing, final String pin, final Promise promise) {
try {
smartCard.unpair(pairing, pin);
promise.resolve(true);
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
}
}
@ReactMethod
public void delete(final Promise promise) {
try {
smartCard.delete();
promise.resolve(true);
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
}
}
@ReactMethod
public void unpairAndDelete(final String pairing, final String pin, final Promise promise) {
try {
smartCard.unpairAndDelete(pairing, pin);
promise.resolve(true);
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
}
}
}
@@ -19,10 +19,12 @@ import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import im.status.keycard.globalplatform.GlobalPlatformCommandSet;
import im.status.keycard.io.APDUException;
import im.status.keycard.io.CardChannel;
import im.status.keycard.io.CardListener;
import im.status.keycard.android.NFCCardManager;
import im.status.keycard.applet.ApplicationStatus;
import im.status.keycard.applet.BIP32KeyPair;
import im.status.keycard.applet.Mnemonic;
import im.status.keycard.applet.KeycardCommandSet;
@@ -40,6 +42,7 @@ 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 WHISPER_PATH = "m/43'/60'/1581'/0'/0";
@@ -52,6 +55,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
this.reactContext = reactContext;
this.nfcAdapter = NfcAdapter.getDefaultAdapter(activity.getBaseContext());
this.eventEmitter = new EventEmitter(reactContext);
eventEmitter.emit("keyCardDidInit", null);
}
public String getName() {
@@ -63,15 +67,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;
}
}
@@ -151,14 +162,6 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
return pairing.toBase64();
}
public void unpair(String base64) throws IOException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
Pairing pairing = new Pairing(base64);
cmdSet.setPairing(pairing);
cmdSet.autoUnpair();
}
public String generateMnemonic(String pairingBase64) throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK();
@@ -194,7 +197,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
log("seed loaded to card");
}
public WritableMap getApplicationInfo() throws IOException, APDUException {
public WritableMap getApplicationInfo(final String pairingBase64) throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
ApplicationInfo info = new ApplicationInfo(cmdSet.select().checkOK().getData());
@@ -215,6 +218,22 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
Log.i(TAG, "The card has no master key");
}
if (pairingBase64.length() > 0) {
Pairing pairing = new Pairing(pairingBase64);
cmdSet.setPairing(pairing);
cmdSet.autoOpenSecureChannel();
Log.i(TAG, "secure channel opened");
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());
cardInfo.putInt("pin-retry-counter", status.getPINRetryCount());
cardInfo.putInt("puk-retry-counter", status.getPUKRetryCount());
}
cardInfo.putBoolean("has-master-key?", info.hasMasterKey());
cardInfo.putString("instance-uid", Hex.toHexString(info.getInstanceUID()));
cardInfo.putString("secure-channel-pub-key", Hex.toHexString(info.getSecureChannelPubKey()));
@@ -367,7 +386,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
return init();
}
public KeycardCommandSet verifyPin(final String pairingBase64, final String pin) throws IOException, APDUException {
public int verifyPin(final String pairingBase64, final String pin) throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK();
@@ -380,14 +399,73 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
cmdSet.verifyPIN(pin).checkOK();
Log.i(TAG, "pin verified");
return cmdSet;
ApplicationStatus status = new ApplicationStatus(cmdSet.getStatus(KeycardCommandSet.GET_STATUS_P1_APPLICATION).checkOK().getData());
return status.getPINRetryCount();
}
public void changePin(final String pairingBase64, final String currentPin, final String newPin) throws IOException, APDUException {
KeycardCommandSet cmdSet = verifyPin(pairingBase64, currentPin);
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(currentPin).checkOK();
Log.i(TAG, "pin verified");
cmdSet.changePIN(0, newPin);
Log.i(TAG, "pin changed");
}
public void unblockPin(final String pairingBase64, final String puk, final String newPin) 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.unblockPIN(puk, newPin).checkOK();
Log.i(TAG, "pin unblocked");
}
public void unpair(final String pairingBase64, final String pin) 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");
cmdSet.autoUnpair();
Log.i(TAG, "card unpaired");
}
public void delete() throws IOException, APDUException {
GlobalPlatformCommandSet cmdSet = new GlobalPlatformCommandSet(this.cardChannel);
cmdSet.select().checkOK();
cmdSet.openSecureChannel();
Log.i(TAG, "secure channel opened");
cmdSet.deleteKeycardInstancesAndPackage();
Log.i(TAG, "instance and package deleted");
}
public void unpairAndDelete(final String pairingBase64, final String pin) throws IOException, APDUException {
unpair(pairingBase64, pin);
delete();
}
}
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "react-native-status-keycard",
"version": "2.1.1",
"version": "2.3.1",
"description": "React Native library to interact with Status Keycard using NFC connection (Android only)",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Dmitry Novotochinov",
"license": "MIT"
"license": "MPL 2.0"
}