-
Notifications
You must be signed in to change notification settings - Fork 5
/
example6.html
217 lines (190 loc) · 6.85 KB
/
example6.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<!-- Data signing/verify -->
<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 #6</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="https://www.unpkg.com/@peculiar/[email protected]/build/x509.js"></script>
<script src="src/helper.js"></script>
<style>
label {
display: inline-block;
width: 128px;
}
select {
display: inline-block;
width: 256px;
}
input[type=text] {
display: inline-block;
width: 256px;
}
.group {
padding: 8px 16px;
}
</style>
</head>
<body>
<h2>Certificate chain building</h2>
<p>
Builds a certificate chain for the selected certificate. This example builds extracts certificates from the
provider and builds a chain on the client side using <a
href="https://github.com/PeculiarVentures/x509/blob/master/README.md">@peculiar/x509</a> library.
</p>
<div class="group">
<label for="providers">Providers:</label>
<select id="providers"></select>
</div>
<div class="group">
<label for="certificates">Certificates:</label>
<select id="certificates"></select>
</div>
<div class="group">
<label for="withoutKey">Show certificates without private key</label>
<input type="checkbox" id="withoutKey">
</div>
<div class="group">
<button id="build">Build</button>
</div>
<pre id="chain">
</pre>
<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 FillData();
ws.cardReader
.on("insert", updateProvider)
.on("remove", updateProvider);
});
}
async function updateProvider() {
const $providers = document.getElementById("providers");
$providers.innerHTML = "";
await FillData();
}
async function FillData() {
await FillProviderSelect($("providers"));
const providerID = $("providers").value;
if (providerID) {
const crypto = await ws.getCrypto(providerID);
fillCertificateSelect(crypto, $certificates, !$withoutKey.checked);
}
}
$("providers").onchange = async () => {
const providerID = $("providers").value;
const provider = await ws.getCrypto(providerID);
fillCertificateSelect(provider, $certificates, !$withoutKey.checked);
}
const $providers = $("providers");
const $build = $("build");
const $chain = $("chain");
const $certificates = $("certificates");
const $withoutKey = $("withoutKey");
$withoutKey.onchange = () => {
(async () => {
const providerID = $providers.value;
const provider = await ws.getCrypto(providerID)
fillCertificateSelect(provider, $certificates, !$withoutKey.checked);
})();
}
$build.onclick = async () => {
$build.disabled = true;
try {
$chain.textContent = "";
const provider = await ws.getCrypto($("providers").value);
// Get all certificate blobs from the provider
const certMap = new Map();
const certIds = await provider.certStorage.keys();
console.log("Certificates IDs:", certIds);
for (const certId of certIds) {
const [type, ,] = certId.split("-");
const cert = await provider.certStorage.getValue(certId);
if (cert && type === "x509") {
certMap.set(certId, new x509.X509Certificate(cert));
}
}
// Log certificates
for (const [certId, cert] of certMap) {
console.log(`Certificate: ${certId}`);
console.log(` Subject: ${cert.subject}`);
console.log(` Issuer: ${cert.issuer}`);
console.log(` Serial: ${cert.serialNumber}`);
console.log(` Validity: ${cert.notBefore.toISOString()} - ${cert.notAfter.toISOString()}`);
console.log(cert.toString('pem'));
}
// Get selected certificate
const certId = $('certificates').value;
const cert = certMap.get(certId);
if (!cert) {
throw new Error("Selected certificate not found");
}
// Build certificate chain
const chainBuilder = new x509.X509ChainBuilder({
certificates: [
...certMap.values(), // add all certificates from the provider
// <- add additional certificates if needed
],
});
const chain = await chainBuilder.build(cert);
// Print chain
const chainStr = [
"Certificate chain:",
" Length: " + chain.length,
"",
];
chain.map((cert, i) => {
chainStr.push(` [${i}]`);
chainStr.push(` Subject: ${cert.subject}`);
chainStr.push(` Issuer: ${cert.issuer}`);
chainStr.push(` Serial: ${cert.serialNumber}`);
chainStr.push(` Validity: ${cert.notBefore.toISOString()} - ${cert.notAfter.toISOString()}`);
chainStr.push("");
});
$chain.textContent = chainStr.join("\n");
} catch (e) {
console.error(e);
}
finally {
$build.disabled = false;
}
}
main();
</script>
</body>
</html>