Compare commits

..
Author SHA1 Message Date
Michele Balistreri 4d77e475af bump version 2024-09-23 11:27:28 +02:00
Michele Balistreri cb6eb0a9c6 add missing empty emitter methods 2024-09-23 10:50:43 +02:00
Michele Balistreri 3953d66cbc add try statements 2024-09-19 10:16:07 +02:00
Michele Balistreri f2d5c2ca53 fix compilation 2024-09-19 10:09:05 +02:00
Michele Balistreri fe748a8dcd add verifyCard for iOS 2024-09-19 09:49:45 +02:00
Michele Balistreri 36887aeb86 typo 2024-09-16 13:38:06 +02:00
Michele Balistreri 8698594eff typo 2024-09-16 13:32:54 +02:00
Michele Balistreri a360897369 add verifyCard command (android) 2024-09-16 13:31:33 +02:00
Michele Balistreri 6de4b0deb0 implement v2 factory reset (#51)
implement v2 factory reset
2024-09-16 10:47:26 +02:00
Michele Balistreri 93dd64754e update sdk (#48)
* update sdk
2023-02-22 12:24:27 +01:00
Michele Balistreri 9c5b897991 Make compatible with newer XCode (#46)
xcode 14 compatibility
2022-09-19 14:51:16 +02:00
Michele Balistreri af61b0213a bump version 2021-11-17 14:02:23 +03:00
Michele Balistreri 31e057dc04 handle system busy error 2021-11-17 13:04:59 +03:00
Michele Balistreri 7548329c59 default pairing (#44)
use a default pairing password
2021-11-16 08:23:35 +03:00
Bitgamma 8cf7cbff80 add factory reset (#41)
add factory reset
2021-04-30 15:44:35 +03:00
8 changed files with 199 additions and 41 deletions
+1 -1
View File
@@ -46,5 +46,5 @@ 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.0.4'
implementation 'com.github.status-im.status-keycard-java:android:3.1.2'
}
@@ -56,6 +56,17 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
public void onHostDestroy() {
}
// Required for rn built in EventEmitter Calls.
@ReactMethod
public void addListener(String eventName) {
}
@ReactMethod
public void removeListeners(Integer count) {
}
@ReactMethod
public void nfcIsSupported(final Promise promise) {
if (smartCard != null) {
@@ -490,6 +501,20 @@ public class RNStatusKeycardModule extends ReactContextBaseJavaModule implements
}).start();
}
@ReactMethod
public void verifyCard(final String challenge, final Promise promise) {
new Thread(new Runnable() {
public void run() {
try {
promise.resolve(smartCard.verifyCard(challenge));
} catch (IOException | APDUException e) {
Log.d(TAG, e.getMessage());
promise.reject(e);
}
}
}).start();
}
// These three methods below are a nop on Android since NFC is always listening and we have a custom UI. They are needed in iOS to show the NFC dialog
@ReactMethod
public void startNFC(String prompt, final Promise promise) {
@@ -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;
@@ -26,6 +25,7 @@ import java.util.HashMap;
import java.util.Iterator;
import im.status.keycard.applet.RecoverableSignature;
import im.status.keycard.applet.Certificate;
import im.status.keycard.globalplatform.GlobalPlatformCommandSet;
import im.status.keycard.io.APDUException;
import im.status.keycard.io.APDUResponse;
@@ -207,6 +207,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());
@@ -227,23 +243,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());
@@ -258,7 +276,17 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
return cardInfo;
}
public WritableMap factoryReset() throws IOException, APDUException {
public WritableMap factoryResetPost() throws IOException, APDUException {
ApplicationInfo info = new ApplicationInfo(new KeycardCommandSet(this.cardChannel).select().checkOK().getData());
Log.i(TAG, "Selecting the factory reset Keycard applet succeeded");
WritableMap cardInfo = Arguments.createMap();
cardInfo.putBoolean("initialized?", info.isInitializedCard());
return cardInfo;
}
public WritableMap factoryResetFallback() throws IOException, APDUException {
GlobalPlatformCommandSet cmdSet = new GlobalPlatformCommandSet(this.cardChannel);
cmdSet.select().checkOK();
Log.i(TAG, "ISD selected");
@@ -270,15 +298,30 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
Log.i(TAG, "Keycard applet instance deleted");
cmdSet.installKeycardApplet().checkOK();
Log.i(TAG, "Keycard applet instance re-installed");
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");
return factoryResetPost();
}
WritableMap cardInfo = Arguments.createMap();
cardInfo.putBoolean("initialized?", info.isInitializedCard());
public WritableMap factoryReset() throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
APDUResponse resp = cmdSet.select();
return cardInfo;
if (!resp.isOK()) {
return factoryResetFallback();
}
ApplicationInfo info = new ApplicationInfo(resp.getData());
if (!info.hasFactoryResetCapability()) {
return factoryResetFallback();
}
if (!cmdSet.factoryReset().isOK()) {
return factoryResetFallback();
}
return factoryResetPost();
}
public void deriveKey(final String path, final String pin) throws IOException, APDUException {
@@ -586,6 +629,20 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
return sig;
}
public WritableMap verifyCard(final String challenge) throws IOException, APDUException {
KeycardCommandSet cmdSet = new KeycardCommandSet(this.cardChannel);
cmdSet.select().checkOK();
byte[] rawChallenge = Hex.decode(challenge);
byte[] data = cmdSet.identifyCard(rawChallenge).checkOK().getData();
byte[] caPubKey = Certificate.verifyIdentity(rawChallenge, data);
WritableMap out = Arguments.createMap();
out.putString("ca-public-key", Hex.toHexString(caPubKey));
out.putString("tlv-data", Hex.toHexString(data));
return out;
}
public void setPairings(ReadableMap newPairings) {
pairings.clear();
Iterator<Map.Entry<String,Object>> i = newPairings.getEntryIterator();
@@ -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);
+79 -17
View File
@@ -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,7 +89,17 @@ class SmartCard {
resolve(true)
}
func factoryReset(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
func factoryResetPost(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
let info = try ApplicationInfo(KeycardCommandSet(cardChannel: channel).select().checkOK().data)
os_log("Selecting the factory reset Keycard applet succeeded")
var cardInfo = [String: Any]()
cardInfo["initialized?"] = info.initializedCard
resolve(cardInfo)
}
func factoryResetFallback(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
let cmdSet: GlobalPlatformCommandSet = GlobalPlatformCommandSet(cardChannel: channel);
try cmdSet.select().checkOK()
os_log("ISD selected")
@@ -103,13 +113,45 @@ class SmartCard {
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")
try factoryResetPost(channel: channel, resolve: resolve, reject: reject)
}
var cardInfo = [String: Any]()
cardInfo["initialized?"] = info.initializedCard
func factoryReset(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
let cmdSet = KeycardCommandSet(cardChannel: channel)
var resp = try cmdSet.select()
if (resp.sw != 0x9000) {
try factoryResetFallback(channel: channel, resolve: resolve, reject: reject)
return
}
resolve(cardInfo)
let info = try ApplicationInfo(resp.data)
if (!info.hasFactoryResetCapability) {
try factoryResetFallback(channel: channel, resolve: resolve, reject: reject)
return
}
resp = try cmdSet.factoryReset()
if (resp.sw != 0x9000) {
try factoryResetFallback(channel: channel, resolve: resolve, reject: reject)
return
}
try factoryResetPost(channel: channel, resolve: resolve, reject: reject)
}
func verifyCard(channel: CardChannel, challenge: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws {
let cmdSet = KeycardCommandSet(cardChannel: channel)
try cmdSet.select().checkOK()
let rawChallenge = hexToBytes(challenge);
let data = try cmdSet.identifyCard(challenge: rawChallenge).checkOK().data
let caPubKey = try Certificate.verifyIdentity(hash: rawChallenge, tlvData: data);
resolve([
"ca-public-key": bytesToHex(caPubKey ?? []),
"tlv-data": bytesToHex(data)
])
}
func getApplicationInfo(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
@@ -129,19 +171,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
@@ -369,6 +413,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)
+1
View File
@@ -13,6 +13,7 @@ RCT_EXTERN_METHOD(generateAndLoadKey:(NSString *)mnemonic pin:(NSString *)pin re
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(verifyCard:(NSString *)challenge resolve:(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)
+17 -2
View File
@@ -9,8 +9,18 @@ class StatusKeycard: RCTEventEmitter {
var cardChannel: CardChannel? = nil
var nfcStartPrompt: String = "Hold your iPhone near a Status Keycard."
private var _keycardController: Any? = nil
@available(iOS 13.0, *)
private(set) lazy var keycardController: KeycardController? = nil
private var keycardController: KeycardController? {
get {
return _keycardController as? KeycardController
}
set(kc) {
_keycardController = kc
}
}
@objc
func nfcIsSupported(_ resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void {
@@ -62,6 +72,11 @@ class StatusKeycard: RCTEventEmitter {
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 verifyCard(_ challenge: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
keycardInvokation(reject) { [unowned self] channel in try self.smartCard.verifyCard(channel: channel, challenge: challenge, resolve: resolve, reject: reject) }
}
@objc
func getApplicationInfo(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
@@ -199,7 +214,7 @@ class StatusKeycard: RCTEventEmitter {
let nsError = error as NSError
if nsError.code == 200 && nsError.domain == "NFCError" {
self.sendEvent(withName: "keyCardOnNFCUserCancelled", body: nil)
} else if nsError.code == 201 && nsError.domain == "NFCError" {
} else if (nsError.code == 201 || nsError.code == 203) && (nsError.domain == "NFCError") {
self.sendEvent(withName: "keyCardOnNFCTimeout", body: nil)
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "react-native-status-keycard",
"homepage": "https://keycard.status.im/",
"version": "2.5.35",
"version": "2.6.0",
"description": "React Native library to interact with Status Keycard using NFC connection",
"main": "index.js",
"scripts": {