@@ -62,6 +62,7 @@ public class SmartCard extends BroadcastReceiver implements CardListener {
62
62
private static final String WALLET_PATH = "m/44'/60'/0'/0/0" ;
63
63
private static final String WHISPER_PATH = "m/43'/60'/1581'/0'/0" ;
64
64
private static final String ENCRYPTION_PATH = "m/43'/60'/1581'/1'/0" ;
65
+ private static final String TAG_LOST = "Tag was lost." ;
65
66
private static final int WORDS_LIST_SIZE = 2048 ;
66
67
67
68
public SmartCard (ReactContext reactContext ) {
@@ -177,7 +178,7 @@ public boolean isNfcEnabled() {
177
178
}
178
179
179
180
public SmartCardSecrets init (final String userPin ) throws IOException , APDUException , NoSuchAlgorithmException , InvalidKeySpecException {
180
- KeycardCommandSet cmdSet = new KeycardCommandSet ( this . cardChannel );
181
+ KeycardCommandSet cmdSet = commandSet ( );
181
182
cmdSet .select ().checkOK ();
182
183
183
184
SmartCardSecrets s = SmartCardSecrets .generate (userPin );
@@ -186,7 +187,7 @@ public SmartCardSecrets init(final String userPin) throws IOException, APDUExcep
186
187
}
187
188
188
189
public String pair (String pairingPassword ) throws IOException , APDUException {
189
- KeycardCommandSet cmdSet = new KeycardCommandSet ( this . cardChannel );
190
+ KeycardCommandSet cmdSet = commandSet ( );
190
191
Log .i (TAG , "Applet selection successful" );
191
192
192
193
// First thing to do is selecting the applet on the card.
@@ -282,7 +283,7 @@ private boolean verifyAuthenticity(KeycardCommandSet cmdSet, String instanceUID)
282
283
}
283
284
284
285
public WritableMap getApplicationInfo () throws IOException , APDUException {
285
- KeycardCommandSet cmdSet = new KeycardCommandSet ( this . cardChannel );
286
+ KeycardCommandSet cmdSet = commandSet ( );
286
287
ApplicationInfo info = new ApplicationInfo (cmdSet .select ().checkOK ().getData ());
287
288
288
289
Log .i (TAG , "Card initialized? " + info .isInitializedCard ());
@@ -348,7 +349,7 @@ public WritableMap getApplicationInfo() throws IOException, APDUException {
348
349
}
349
350
350
351
public WritableMap factoryResetPost () throws IOException , APDUException {
351
- ApplicationInfo info = new ApplicationInfo (new KeycardCommandSet ( this . cardChannel ).select ().checkOK ().getData ());
352
+ ApplicationInfo info = new ApplicationInfo (commandSet ( ).select ().checkOK ().getData ());
352
353
Log .i (TAG , "Selecting the factory reset Keycard applet succeeded" );
353
354
354
355
WritableMap cardInfo = Arguments .createMap ();
@@ -358,7 +359,7 @@ public WritableMap factoryResetPost() throws IOException, APDUException {
358
359
}
359
360
360
361
public WritableMap factoryResetFallback () throws IOException , APDUException {
361
- GlobalPlatformCommandSet cmdSet = new GlobalPlatformCommandSet ( this . cardChannel );
362
+ GlobalPlatformCommandSet cmdSet = gpCommandSet ( );
362
363
cmdSet .select ().checkOK ();
363
364
Log .i (TAG , "ISD selected" );
364
365
@@ -375,7 +376,7 @@ public WritableMap factoryResetFallback() throws IOException, APDUException {
375
376
}
376
377
377
378
public WritableMap factoryReset () throws IOException , APDUException {
378
- KeycardCommandSet cmdSet = new KeycardCommandSet ( this . cardChannel );
379
+ KeycardCommandSet cmdSet = commandSet ( );
379
380
APDUResponse resp = cmdSet .select ();
380
381
381
382
if (!resp .isOK ()) {
@@ -661,7 +662,7 @@ public String signWithPath(final String pin, final String path, final String mes
661
662
}
662
663
663
664
public String signPinless (final String message ) throws IOException , APDUException {
664
- CashCommandSet cmdSet = new CashCommandSet ( this . cardChannel );
665
+ CashCommandSet cmdSet = cashCommandSet ( );
665
666
cmdSet .select ().checkOK ();
666
667
667
668
byte [] hash = Hex .decode (message );
@@ -685,7 +686,7 @@ public String signPinless(final String message) throws IOException, APDUExceptio
685
686
}
686
687
687
688
public String getCardName () throws IOException , APDUException {
688
- KeycardCommandSet cmdSet = new KeycardCommandSet ( this . cardChannel );
689
+ KeycardCommandSet cmdSet = commandSet ( );
689
690
cmdSet .select ().checkOK ();
690
691
return getCardNameOrDefault (cmdSet );
691
692
}
@@ -698,7 +699,7 @@ public void setCardName(final String pin, final String name) throws IOException,
698
699
}
699
700
700
701
public WritableMap verifyCard (final String challenge ) throws IOException , APDUException {
701
- KeycardCommandSet cmdSet = new KeycardCommandSet ( this . cardChannel );
702
+ KeycardCommandSet cmdSet = commandSet ( );
702
703
cmdSet .select ().checkOK ();
703
704
byte [] rawChallenge = Hex .decode (challenge );
704
705
byte [] data = cmdSet .identifyCard (rawChallenge ).checkOK ().getData ();
@@ -742,13 +743,43 @@ private KeycardCommandSet authenticatedCommandSet(String pin) throws IOException
742
743
}
743
744
744
745
private KeycardCommandSet securedCommandSet () throws IOException , APDUException {
745
- KeycardCommandSet cmdSet = new KeycardCommandSet ( this . cardChannel );
746
+ KeycardCommandSet cmdSet = commandSet ( );
746
747
cmdSet .select ().checkOK ();
747
748
openSecureChannel (cmdSet );
748
749
749
750
return cmdSet ;
750
751
}
751
752
753
+ private KeycardCommandSet commandSet () throws IOException {
754
+ synchronized (lock ) {
755
+ if (this .cardChannel != null ) {
756
+ return new KeycardCommandSet (this .cardChannel );
757
+ }
758
+ }
759
+
760
+ throw new IOException (TAG_LOST );
761
+ }
762
+
763
+ private CashCommandSet cashCommandSet () throws IOException {
764
+ synchronized (lock ) {
765
+ if (this .cardChannel != null ) {
766
+ return new CashCommandSet (this .cardChannel );
767
+ }
768
+ }
769
+
770
+ throw new IOException (TAG_LOST );
771
+ }
772
+
773
+ private GlobalPlatformCommandSet gpCommandSet () throws IOException {
774
+ synchronized (lock ) {
775
+ if (this .cardChannel != null ) {
776
+ return new GlobalPlatformCommandSet (this .cardChannel );
777
+ }
778
+ }
779
+
780
+ throw new IOException (TAG_LOST );
781
+ }
782
+
752
783
private String getCardNameOrDefault (KeycardCommandSet cmdSet ) throws IOException , APDUException {
753
784
byte [] data = cmdSet .getData (KeycardCommandSet .STORE_DATA_P1_PUBLIC ).checkOK ().getData ();
754
785
0 commit comments