-
Notifications
You must be signed in to change notification settings - Fork 5
/
example4.html
224 lines (197 loc) · 6.03 KB
/
example4.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
218
219
220
221
222
223
224
<!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 #4</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>
<style>
th {
text-align: left;
}
td {
padding: 2px 4px;
}
</style>
</head>
<body>
<h2>Enumerate providers and their contents</h2>
<p></p>
<div>
<h3>1: Select provider:</h3>
<select name="provider" id="provider" style="width: 300px">
</select>
<button id="refresh">Refresh</button>
</div>
<div>
<h3>Providers:</h3>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>ATR</th>
</tr>
</thead>
<tbody id="providers"></tbody>
</table>
</div>
<div>
<h3>Items</h3>
<table>
<thead>
<tr>
<th>Index</th>
<th>Type</th>
<th>Name</th>
</tr>
</thead>
<tbody id="items"></tbody>
</table>
</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 FillProviderSelectEx($("provider"));
await FillProviders();
ws.cardReader
.on("insert", updateProvider)
.on("remove", updateProvider);
});
}
async function updateProvider() {
const $provider = $("provider");
$provider.innerHTML = "";
await FillProviderSelectEx($provider)
await FillProviders();
}
async function FillProviderSelectEx(domSelect) {
await FillProviderSelect(domSelect);
await FillItems(domSelect.value);
}
async function FillItems(providerId) {
const $items = $("items");
// clear table items
$items.innerHTML = "";
// get crypto for selected provider
const crypto = await ws.getCrypto(providerId);
// Check provider login
if (! await crypto.isLoggedIn()) {
// Request provider for PIN window
await crypto.login();
}
// Get certificate items
let indexes = await crypto.certStorage.keys();
for (const index of indexes) {
try {
const item = await crypto.certStorage.getItem(index);
const $raw = CreateRaw(item, index);
$items.appendChild($raw);
} catch (e) {
console.error(`Cannot get ${index} from CertificateStorage`)
console.error(e);
}
}
// Get key items
indexes = await crypto.keyStorage.keys();
for (const index of indexes) {
try {
const item = await crypto.keyStorage.getItem(index);
const $raw = CreateRaw(item, index);
$items.appendChild($raw);
} catch (e) {
console.error(`Cannot get ${index} from KeyStorage`)
console.error(e);
}
}
}
async function FillProviders() {
const $providers = $("providers");
$providers.innerHTML = "";
const info = await ws.info();
for (const provider of info.providers) {
$providers.appendChild(CreateProvidersRaw(provider));
}
}
function CreateRaw(item, index) {
const $tr = document.createElement("tr");
$tr.appendChild(CreateCell(index));
$tr.appendChild(CreateCell(item.type));
switch (item.type) {
case "public":
case "private":
$tr.appendChild(CreateCell(item.algorithm.name));
break;
case "x509":
case "request":
$tr.appendChild(CreateCell(GetCommonName(item.subjectName)));
break;
}
return $tr;
}
function CreateCell(data) {
const $td = document.createElement("td");
$td.textContent = data;
return $td;
}
function CreateProvidersRaw(item) {
const $tr = document.createElement("tr");
$tr.appendChild(CreateCell(item.id));
$tr.appendChild(CreateCell(item.name));
$tr.appendChild(CreateCell(item.atr || "none"));
return $tr;
}
$("provider").onchange = () => FillItems($("provider").value);
$("refresh").onclick = async () => {
// get crypto for selected provider
const crypto = await ws.getCrypto($("provider").value);
await crypto.reset();
await FillItems($("provider").value);
}
main();
</script>
</body>
</html>