-
Notifications
You must be signed in to change notification settings - Fork 5
/
example2.html
131 lines (114 loc) · 3.99 KB
/
example2.html
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Example #2</title>
<!-- Babel Polyfill -->
<script src="https://fortifyapp.com/external/babel-polyfill/6.26.0/polyfill.min.js"></script>
<!-- Fetch Polyfill -->
<script
nomodule
src="https://fortifyapp.com/external/[email protected]/fetch.umd.js"
></script>
<!-- Crypto Polyfill -->
<script src="https://fortifyapp.com/external/asmCrypto/2.3.2/asmcrypto.all.es5.min.js"></script>
<script src="https://fortifyapp.com/external/elliptic/elliptic.min.js"></script>
<script
type="module"
src="https://fortifyapp.com/external/webcrypto-liner/1.2.3/webcrypto-liner.shim.min.mjs"
></script>
<script
nomodule
src="https://fortifyapp.com/external/webcrypto-liner/1.2.3/webcrypto-liner.shim.min.js"
></script>
<!-- WebCrypto Socket -->
<script src="https://fortifyapp.com/external/protobuf/6.8.0/protobuf.min.js"></script>
<script src="https://fortifyapp.com/external/webcrypto-local/client/1.7.1/webcrypto-socket.min.js"></script>
<!-- CMS -->
<script src="https://fortifyapp.com/external/pvtsutils/pvtsutils.js"></script>
<script src="https://fortifyapp.com/external/asn1js/asn1.min.js"></script>
<script src="https://fortifyapp.com/external/pkijs/pki.min.js"></script>
<script src="src/helper.js"></script>
</head>
<body>
<h2>Importing a Certificate</h2>
<div>
<h3>1: Select provider:</h3>
<select name="provider" id="provider" style="width: 300px">
</select>
</div>
<div>
<h3>2: Insert certificate PEM:</h3>
<textarea name="cert" id="cert" cols="100" rows="10"></textarea>
</div>
<div>
<h3>3: Create</h3>
<button id="btn" onclick="start()">Start</button>
</div>
<script>
async function main() {
self.ws = new WebcryptoSocket.SocketProvider({
storage: await WebcryptoSocket.BrowserStorage.create(),
});
ws.connect("127.0.0.1:31337")
.on("error", function (e) {
console.error(e);
})
.on("listening", async (e) => {
// Check if end-to-end session is approved
if (! await ws.isLoggedIn()) {
const pin = await ws.challenge();
// show PIN
setTimeout(() => {
alert("2key session PIN:" + pin);
}, 100)
// ask to approve session
await ws.login();
}
await FillProviderSelect($("provider"));
ws.cardReader
.on("insert", updateProvider)
.on("remove", updateProvider);
});
}
async function updateProvider() {
const $provider = $("provider");
$provider.innerHTML = "";
await FillProviderSelect($provider);
}
async function start() {
// disabled button
$("btn").disabled = true;
try {
const $provider = document.getElementById("provider");
const $cert = $("cert");
if (!$cert.value) {
throw new Error("Certificate PEM is empty");
}
const crypto = await ws.getCrypto($provider.value);
// Check provider login
if (! await crypto.isLoggedIn()) {
await crypto.login();
}
const der = PemToDer($cert.value);
// get algorithm for key
// NOTE: pkijs needs crypto engine
pkijs.setEngine("Fortify", crypto, crypto.subtle);
const asn1 = asn1js.fromBER(der);
asn1Cert = new pkijs.Certificate({ schema: asn1.result });
const key = await asn1Cert.getPublicKey();
const cert = await crypto.certStorage.importCert("raw", der, key.algorithm, ["verify"]);
// Add certificate to storage
const index = await crypto.certStorage.setItem(cert);
alert("Certificate was added successfully");
console.log("Certificate id:", index);
} finally {
$("btn").disabled = false;
}
}
main();
</script>
</body>
</html>