Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d53ea4792 | ||
|
|
8b46a646c5 | ||
|
|
a11a103620 | ||
|
|
02e6471c10 | ||
|
|
d2168418ee | ||
|
|
d74b800bfa | ||
|
|
52d43a71dd | ||
|
|
2594577503 | ||
|
|
e0b97ae471 | ||
|
|
8cf7cbff80 | ||
|
|
9679ae2ea5 | ||
|
|
b9f242aa11 | ||
|
|
d1098e969a |
@@ -1,6 +1,6 @@
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.reactlibrary">
|
||||
package="im.status.ethereum.keycard">
|
||||
|
||||
</manifest>
|
||||
|
||||
|
||||
|
||||
@@ -176,6 +176,20 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
|
||||
}).start();
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void factoryReset(final Promise promise) {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
promise.resolve(smartCard.factoryReset());
|
||||
} catch (IOException | APDUException e) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
promise.reject(e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void deriveKey(final String path, final String pin, final Promise promise) {
|
||||
new Thread(new Runnable() {
|
||||
@@ -341,6 +355,36 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
|
||||
}).start();
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void changePairingPassword(final String pin, final String pairingPassword, final Promise promise) {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
smartCard.changePairingPassword(pin, pairingPassword);
|
||||
promise.resolve(true);
|
||||
} catch (IOException | APDUException e) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
promise.reject(e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void changePUK(final String pin, final String puk, final Promise promise) {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
smartCard.changePUK(pin, puk);
|
||||
promise.resolve(true);
|
||||
} catch (IOException | APDUException e) {
|
||||
Log.d(TAG, e.getMessage());
|
||||
promise.reject(e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void changePin(final String currentPin, final String newPin, final Promise promise) {
|
||||
new Thread(new Runnable() {
|
||||
|
||||
@@ -8,7 +8,6 @@ import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.nfc.NfcAdapter;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.EventLog;
|
||||
import android.util.Log;
|
||||
|
||||
@@ -28,6 +27,7 @@ import java.util.Iterator;
|
||||
import im.status.keycard.applet.RecoverableSignature;
|
||||
import im.status.keycard.globalplatform.GlobalPlatformCommandSet;
|
||||
import im.status.keycard.io.APDUException;
|
||||
import im.status.keycard.io.APDUResponse;
|
||||
import im.status.keycard.io.CardChannel;
|
||||
import im.status.keycard.io.CardListener;
|
||||
import im.status.keycard.android.NFCCardManager;
|
||||
@@ -206,6 +206,22 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
|
||||
log("seed loaded to card");
|
||||
}
|
||||
|
||||
public boolean tryDefaultPairing(KeycardCommandSet cmdSet, String instanceUID, WritableMap cardInfo) throws IOException {
|
||||
try {
|
||||
cmdSet.autoPair("KeycardDefaultPairing");
|
||||
|
||||
Pairing pairing = cmdSet.getPairing();
|
||||
pairings.put(instanceUID, pairing.toBase64());
|
||||
cardInfo.putString("new-pairing", pairing.toBase64());
|
||||
|
||||
openSecureChannel(cmdSet);
|
||||
return true;
|
||||
} catch(APDUException e) {
|
||||
Log.i(TAG, "autoOpenSecureChannel failed: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public WritableMap getApplicationInfo() throws IOException, APDUException {
|
||||
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
|
||||
ApplicationInfo info = new ApplicationInfo(cmdSet.select().checkOK().getData());
|
||||
@@ -226,23 +242,25 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
|
||||
|
||||
Boolean isPaired = false;
|
||||
|
||||
if (pairings.containsKey(instanceUID)) {
|
||||
if (!pairings.containsKey(instanceUID)) {
|
||||
isPaired = tryDefaultPairing(cmdSet, instanceUID, cardInfo);
|
||||
} else {
|
||||
try {
|
||||
openSecureChannel(cmdSet);
|
||||
isPaired = true;
|
||||
} catch(APDUException e) {
|
||||
Log.i(TAG, "autoOpenSecureChannel failed: " + e.getMessage());
|
||||
isPaired = tryDefaultPairing(cmdSet, instanceUID, cardInfo);
|
||||
}
|
||||
}
|
||||
|
||||
if (isPaired) {
|
||||
ApplicationStatus status = new ApplicationStatus(cmdSet.getStatus(KeycardCommandSet.GET_STATUS_P1_APPLICATION).checkOK().getData());
|
||||
if (isPaired) {
|
||||
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());
|
||||
}
|
||||
|
||||
cardInfo.putBoolean("has-master-key?", info.hasMasterKey());
|
||||
@@ -257,6 +275,29 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
|
||||
return cardInfo;
|
||||
}
|
||||
|
||||
public WritableMap factoryReset() throws IOException, APDUException {
|
||||
GlobalPlatformCommandSet cmdSet = new GlobalPlatformCommandSet(this.cardChannel);
|
||||
cmdSet.select().checkOK();
|
||||
Log.i(TAG, "ISD selected");
|
||||
|
||||
cmdSet.openSecureChannel();
|
||||
Log.i(TAG, "SecureChannel opened");
|
||||
|
||||
cmdSet.deleteKeycardInstance().checkSW(APDUResponse.SW_OK, APDUResponse.SW_REFERENCED_DATA_NOT_FOUND);
|
||||
Log.i(TAG, "Keycard applet instance deleted");
|
||||
|
||||
cmdSet.installKeycardApplet().checkOK();
|
||||
Log.i(TAG, "Keycard applet instance re-installed");
|
||||
|
||||
ApplicationInfo info = new ApplicationInfo(new KeycardCommandSet(this.cardChannel).select().checkOK().getData());
|
||||
Log.i(TAG, "Selecting the newly installed Keycard applet succeeded");
|
||||
|
||||
WritableMap cardInfo = Arguments.createMap();
|
||||
cardInfo.putBoolean("initialized?", info.isInitializedCard());
|
||||
|
||||
return cardInfo;
|
||||
}
|
||||
|
||||
public void deriveKey(final String path, final String pin) throws IOException, APDUException {
|
||||
KeycardCommandSet cmdSet = authenticatedCommandSet(pin);
|
||||
|
||||
@@ -405,10 +446,24 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
|
||||
return 3;
|
||||
}
|
||||
|
||||
public void changePairingPassword(final String pin, final String pairingPassword) throws IOException, APDUException {
|
||||
KeycardCommandSet cmdSet = authenticatedCommandSet(pin);
|
||||
|
||||
cmdSet.changePairingPassword(pairingPassword);
|
||||
Log.i(TAG, "pairing password changed");
|
||||
}
|
||||
|
||||
public void changePUK(final String pin, final String puk) throws IOException, APDUException {
|
||||
KeycardCommandSet cmdSet = authenticatedCommandSet(pin);
|
||||
|
||||
cmdSet.changePUK(puk);
|
||||
Log.i(TAG, "puk changed");
|
||||
}
|
||||
|
||||
public void changePin(final String currentPin, final String newPin) throws IOException, APDUException {
|
||||
KeycardCommandSet cmdSet = authenticatedCommandSet(currentPin);
|
||||
|
||||
cmdSet.changePIN(0, newPin);
|
||||
cmdSet.changePIN(newPin);
|
||||
Log.i(TAG, "pin changed");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package im.status.ethereum.keycard;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Base64;
|
||||
|
||||
import static android.util.Base64.NO_PADDING;
|
||||
@@ -29,9 +28,8 @@ public class SmartCardSecrets {
|
||||
this.pairingPassword = pairingPassword;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static SmartCardSecrets generate(final String userPin) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
||||
String pairingPassword = randomToken(5);
|
||||
String pairingPassword = "KeycardDefaultPairing";
|
||||
long pinNumber = randomLong(PIN_BOUND);
|
||||
long pukNumber = randomLong(PUK_BOUND);
|
||||
|
||||
|
||||
+68
-11
@@ -20,11 +20,11 @@ class SmartCard {
|
||||
|
||||
func initialize(channel: CardChannel, pin: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
|
||||
let puk = self.randomPUK()
|
||||
let pairingPassword = self.randomPairingPassword();
|
||||
let pairingPassword = "KeycardDefaultPairing"
|
||||
|
||||
let cmdSet = KeycardCommandSet(cardChannel: channel)
|
||||
try cmdSet.select().checkOK()
|
||||
try cmdSet.initialize(pin: pin, puk: puk, pairingPassword: pairingPassword).checkOK();
|
||||
try cmdSet.initialize(pin: pin, puk: puk, pairingPassword: pairingPassword).checkOK()
|
||||
|
||||
resolve(["pin": pin, "puk": puk, "password": pairingPassword])
|
||||
}
|
||||
@@ -89,6 +89,29 @@ class SmartCard {
|
||||
resolve(true)
|
||||
}
|
||||
|
||||
func factoryReset(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
|
||||
let cmdSet: GlobalPlatformCommandSet = GlobalPlatformCommandSet(cardChannel: channel);
|
||||
try cmdSet.select().checkOK()
|
||||
os_log("ISD selected")
|
||||
|
||||
try cmdSet.openSecureChannel()
|
||||
os_log("SecureChannel opened")
|
||||
|
||||
try cmdSet.deleteKeycardInstance().checkSW(StatusWord.ok, StatusWord.referencedDataNotFound)
|
||||
os_log("Keycard applet instance deleted")
|
||||
|
||||
try cmdSet.installKeycardInstance().checkOK()
|
||||
os_log("Keycard applet instance re-installed")
|
||||
|
||||
let info = try ApplicationInfo(KeycardCommandSet(cardChannel: channel).select().checkOK().data)
|
||||
os_log("Selecting the newly installed Keycard applet succeeded")
|
||||
|
||||
var cardInfo = [String: Any]()
|
||||
cardInfo["initialized?"] = info.initializedCard
|
||||
|
||||
resolve(cardInfo)
|
||||
}
|
||||
|
||||
func getApplicationInfo(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
|
||||
let cmdSet = KeycardCommandSet(cardChannel: channel)
|
||||
let info = try ApplicationInfo(cmdSet.select().checkOK().data)
|
||||
@@ -106,19 +129,21 @@ class SmartCard {
|
||||
try openSecureChannel(cmdSet: cmdSet)
|
||||
isPaired = true
|
||||
} catch let error as CardError {
|
||||
os_log("autoOpenSecureChannel failed: %@", String(describing: error));
|
||||
isPaired = try tryDefaultPairing(cmdSet: cmdSet, cardInfo: &cardInfo)
|
||||
} catch let error as StatusWord {
|
||||
os_log("autoOpenSecureChannel failed: %@", String(describing: error));
|
||||
isPaired = try tryDefaultPairing(cmdSet: cmdSet, cardInfo: &cardInfo)
|
||||
}
|
||||
} else {
|
||||
isPaired = try tryDefaultPairing(cmdSet: cmdSet, cardInfo: &cardInfo)
|
||||
}
|
||||
|
||||
if (isPaired) {
|
||||
let status = try ApplicationStatus(cmdSet.getStatus(info: GetStatusP1.application.rawValue).checkOK().data);
|
||||
os_log("PIN retry counter: %d", status.pinRetryCount)
|
||||
os_log("PUK retry counter: %d", status.pukRetryCount)
|
||||
if (isPaired) {
|
||||
let status = try ApplicationStatus(cmdSet.getStatus(info: GetStatusP1.application.rawValue).checkOK().data);
|
||||
os_log("PIN retry counter: %d", status.pinRetryCount)
|
||||
os_log("PUK retry counter: %d", status.pukRetryCount)
|
||||
|
||||
cardInfo["pin-retry-counter"] = status.pinRetryCount
|
||||
cardInfo["puk-retry-counter"] = status.pukRetryCount
|
||||
}
|
||||
cardInfo["pin-retry-counter"] = status.pinRetryCount
|
||||
cardInfo["puk-retry-counter"] = status.pukRetryCount
|
||||
}
|
||||
|
||||
cardInfo["paired?"] = isPaired
|
||||
@@ -243,6 +268,20 @@ class SmartCard {
|
||||
resolve(3)
|
||||
}
|
||||
|
||||
func changePairingPassword(channel: CardChannel, pin: String, pairingPassword: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
|
||||
let cmdSet = try authenticatedCommandSet(channel: channel, pin: pin)
|
||||
try cmdSet.changePairingPassword(pairingPassword: pairingPassword).checkOK()
|
||||
os_log("pairing password changed")
|
||||
resolve(true)
|
||||
}
|
||||
|
||||
func changePUK(channel: CardChannel, pin: String, puk: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
|
||||
let cmdSet = try authenticatedCommandSet(channel: channel, pin: pin)
|
||||
try cmdSet.changePUK(puk: puk).checkOK()
|
||||
os_log("puk changed")
|
||||
resolve(true)
|
||||
}
|
||||
|
||||
func changePin(channel: CardChannel, currentPin: String, newPin: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
|
||||
let cmdSet = try authenticatedCommandSet(channel: channel, pin: currentPin)
|
||||
try cmdSet.changePIN(pin: newPin).checkOK()
|
||||
@@ -332,6 +371,24 @@ class SmartCard {
|
||||
return cmdSet
|
||||
}
|
||||
|
||||
func tryDefaultPairing(cmdSet: KeycardCommandSet, cardInfo: inout [String: Any]) throws -> Bool {
|
||||
do {
|
||||
try cmdSet.autoPair(password: "KeycardDefaultPairing")
|
||||
let pairing = Data(cmdSet.pairing!.bytes).base64EncodedString()
|
||||
self.pairings[bytesToHex(cmdSet.info!.instanceUID)] = pairing
|
||||
cardInfo["new-pairing"] = pairing
|
||||
|
||||
try openSecureChannel(cmdSet: cmdSet)
|
||||
return true
|
||||
} catch let error as CardError {
|
||||
os_log("autoOpenSecureChannel failed: %@", String(describing: error));
|
||||
} catch let error as StatusWord {
|
||||
os_log("autoOpenSecureChannel failed: %@", String(describing: error));
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func openSecureChannel(cmdSet: KeycardCommandSet) throws -> Void {
|
||||
if let pairingBase64 = self.pairings[bytesToHex(cmdSet.info!.instanceUID)] {
|
||||
cmdSet.pairing = try base64ToPairing(pairingBase64)
|
||||
|
||||
@@ -12,6 +12,7 @@ RCT_EXTERN_METHOD(generateMnemonic:(NSString *)words resolve:(RCTPromiseResolveB
|
||||
RCT_EXTERN_METHOD(generateAndLoadKey:(NSString *)mnemonic pin:(NSString *)pin resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(saveMnemonic:(NSString *)mnemonic pin:(NSString *)pin resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(getApplicationInfo:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(factoryReset:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(deriveKey:(NSString *)path pin:(NSString *)pin resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(exportKey:(NSString *)pin resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(exportKeyWithPath:(NSString *)pin path:(NSString *)path resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
@@ -23,6 +24,8 @@ RCT_EXTERN_METHOD(signPinless:(NSString *)hash resolve:(RCTPromiseResolveBlock)r
|
||||
RCT_EXTERN_METHOD(installApplet:(RCTPromiseResolveBlock)resolve reject: (RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(installAppletAndInitCard:(String *)pin resolve: (RCTPromiseResolveBlock)resolve reject: (RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(verifyPin:(NSString *)pin resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(changePairingPassword:(NSString *)pin pairingPassword:(NSString *)pairingPassword resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(changePUK:(NSString *)pin puk:(NSString *)puk resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(changePin:(NSString *)currentPin newPin:(NSString *)newPin resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(unblockPin:(NSString *)puk newPin:(NSString *)newPin resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
RCT_EXTERN_METHOD(unpair:(NSString *)pin resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
|
||||
|
||||
@@ -58,6 +58,11 @@ class StatusKeycard: RCTEventEmitter {
|
||||
keycardInvokation(reject) { [unowned self] channel in try self.smartCard.saveMnemonic(channel: channel, mnemonic: mnemonic, pin: pin, resolve: resolve, reject: reject) }
|
||||
}
|
||||
|
||||
@objc
|
||||
func factoryReset(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
||||
keycardInvokation(reject) { [unowned self] channel in try self.smartCard.factoryReset(channel: channel, resolve: resolve, reject: reject) }
|
||||
}
|
||||
|
||||
@objc
|
||||
func getApplicationInfo(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
||||
keycardInvokation(reject) { [unowned self] channel in try self.smartCard.getApplicationInfo(channel: channel, resolve: resolve, reject: reject) }
|
||||
@@ -118,6 +123,16 @@ class StatusKeycard: RCTEventEmitter {
|
||||
keycardInvokation(reject) { [unowned self] channel in try self.smartCard.verifyPin(channel: channel, pin: pin, resolve: resolve, reject: reject) }
|
||||
}
|
||||
|
||||
@objc
|
||||
func changePairingPassword(_ pin: String, pairingPassword: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
||||
keycardInvokation(reject) { [unowned self] channel in try self.smartCard.changePairingPassword(channel: channel, pin: pin, pairingPassword: pairingPassword, resolve: resolve, reject: reject) }
|
||||
}
|
||||
|
||||
@objc
|
||||
func changePUK(_ pin: String, puk: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
||||
keycardInvokation(reject) { [unowned self] channel in try self.smartCard.changePUK(channel: channel, pin: pin, puk: puk, resolve: resolve, reject: reject) }
|
||||
}
|
||||
|
||||
@objc
|
||||
func changePin(_ currentPin: String, newPin: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
|
||||
keycardInvokation(reject) { [unowned self] channel in try self.smartCard.changePin(channel: channel, currentPin: currentPin, newPin: newPin, resolve: resolve, reject: reject) }
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "react-native-status-keycard",
|
||||
"homepage": "https://keycard.status.im/",
|
||||
"version": "2.5.33",
|
||||
"description": "React Native library to interact with Status Keycard using NFC connection (Android only)",
|
||||
"version": "2.5.36",
|
||||
"description": "React Native library to interact with Status Keycard using NFC connection",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
|
||||
Reference in New Issue
Block a user