-
Notifications
You must be signed in to change notification settings - Fork 13
/
Installer.java
83 lines (57 loc) · 2.91 KB
/
Installer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package im.status.ethereum.keycard;
import android.content.res.AssetManager;
import android.util.Log;
import im.status.keycard.globalplatform.GlobalPlatformCommandSet;
import im.status.keycard.globalplatform.LoadCallback;
import im.status.keycard.io.APDUException;
import im.status.keycard.io.CardChannel;
import org.bouncycastle.util.encoders.Hex;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
public class Installer {
private CardChannel plainChannel;
private AssetManager assets;
private String capPath;
private EventEmitter eventEmitter;
private GlobalPlatformCommandSet cmdSet;
private static final String TAG = "SmartCardInstaller";
public Installer(CardChannel channel, AssetManager assets, String capPath, EventEmitter eventEmitter) {
this.plainChannel = channel;
this.assets = assets;
this.capPath = capPath;
this.eventEmitter = eventEmitter;
}
public void start() throws IOException, APDUException, NoSuchAlgorithmException, InvalidKeySpecException {
Log.i(TAG, "installation started...");
long startTime = System.currentTimeMillis();
eventEmitter.emit("keycardInstallationProgress", 0.05);
Log.i(TAG, "select ISD...");
cmdSet = new GlobalPlatformCommandSet(this.plainChannel);
cmdSet.select().checkOK();
Log.i(TAG, "opening secure channel...");
cmdSet.openSecureChannel();
Log.i(TAG, "deleting old version (if present)...");
cmdSet.deleteKeycardInstancesAndPackage();
eventEmitter.emit("keycardInstallationProgress", 0.1);
Log.i(TAG, "loading package...");
cmdSet.loadKeycardPackage(this.assets.open(this.capPath), new LoadCallback() {
public void blockLoaded(int loadedBlock, int blockCount) {
Log.i(TAG, String.format("load %d/%d...", loadedBlock, blockCount));
eventEmitter.emit("keycardInstallationProgress", 0.1 + (0.6 * loadedBlock / blockCount));
}
});
Log.i(TAG, "installing NDEF applet...");
cmdSet.installNDEFApplet(Hex.decode("0024d40f12616e64726f69642e636f6d3a706b67696d2e7374617475732e657468657265756d")).checkOK();
eventEmitter.emit("keycardInstallationProgress", 0.72);
eventEmitter.emitWithDelay("keycardInstallationProgress", 0.77, 3100);
eventEmitter.emitWithDelay("keycardInstallationProgress", 0.82, 6200);
eventEmitter.emitWithDelay("keycardInstallationProgress", 0.85, 8500);
Log.i(TAG, "installing Keycard applet...");
cmdSet.installKeycardApplet().checkOK();
eventEmitter.removeCallbacksAndMessages();
eventEmitter.emit("keycardInstallationProgress", 0.88);
long duration = System.currentTimeMillis() - startTime;
Log.i(TAG, String.format("\n\ninstallation completed in %d seconds", duration / 1000));
}
}