Skip to content

Commit

Permalink
Ident support (#53)
Browse files Browse the repository at this point in the history
add IDENT support
  • Loading branch information
bitgamma authored Sep 23, 2024
1 parent 6de4b0d commit 7da38ff
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public void onHostPause() {
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) {
Expand Down Expand Up @@ -490,6 +501,20 @@ public void run() {
}).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) {
Expand Down
15 changes: 15 additions & 0 deletions android/src/main/java/im/status/ethereum/keycard/SmartCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -628,6 +629,20 @@ public String signPinless(final String message) throws IOException, APDUExceptio
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();
Expand Down
23 changes: 18 additions & 5 deletions ios/SmartCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,45 @@ class SmartCard {
try cmdSet.installKeycardInstance().checkOK()
os_log("Keycard applet instance re-installed")

factoryResetPost(channel, resolve, reject)
try factoryResetPost(channel: channel, resolve: resolve, reject: reject)
}

func factoryReset(channel: CardChannel, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) throws -> Void {
let cmdSet = KeycardCommandSet(cardChannel: channel)
var resp = try cmdSet.select()

if (resp.sw != 0x9000) {
factoryResetFallback(channel, resolve, reject)
try factoryResetFallback(channel: channel, resolve: resolve, reject: reject)
return
}

let info = try ApplicationInfo(resp.data)
if (!info.hasFactoryResetCapability) {
factoryResetFallback(channel, resolve, reject)
try factoryResetFallback(channel: channel, resolve: resolve, reject: reject)
return
}

resp = try cmdSet.factoryReset()

if (resp.sw != 0x9000) {
factoryResetFallback(channel, resolve, reject)
try factoryResetFallback(channel: channel, resolve: resolve, reject: reject)
return
}

factoryResetPost(channel, resolve, reject)
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 {
Expand Down
1 change: 1 addition & 0 deletions ios/StatusKeycard.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ @interface RCT_EXTERN_REMAP_MODULE(RNStatusKeycard, StatusKeycard, RCTEventEmitt
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)
Expand Down
5 changes: 5 additions & 0 deletions ios/StatusKeycard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,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 {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-status-keycard",
"homepage": "https://keycard.status.im/",
"version": "2.5.40",
"version": "2.6.0",
"description": "React Native library to interact with Status Keycard using NFC connection",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 7da38ff

Please sign in to comment.