Repository navigation
Expand file tree
/
Copy pathActSigner.CryptoPro.js
More file actions
479 lines (452 loc) · 21.5 KB
/
Copy pathActSigner.CryptoPro.js
File metadata and controls
479 lines (452 loc) · 21.5 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Native Windows Script Host / JScript 5.x. All CAdESCOM stays in this process.
// Protocol alphabet: ASCII. Redirected stdin/stdout: UTF-16LE via cscript //U.
// No eval, JSON, file-system access, input paths, key export or PIN handling.
(function () {
var MAGIC = "ACTSIGNER/1";
var MAX_BYTES = 67108864;
var CERT_BYTES = 1048576;
var CHUNK_CHARS = 49152;
var MAX_RECORDS = 4096;
var MAX_DISCOVERY_CHARS = 16777216;
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var stage = "Protocol";
var selectedKind = 0;
var selectedThumb = "";
var hasWork = false;
function failure(code) {
var error = new Error(code);
error.bridgeCode = code;
error.number = 0;
return error;
}
function check(condition, code) {
if (!condition) throw failure(code);
}
function hex(number) {
var value = (number >>> 0).toString(16).toUpperCase();
while (value.length < 8) value = "0" + value;
return "0x" + value;
}
function safeError(error) {
var code = error && error.bridgeCode ? String(error.bridgeCode) : "NATIVE";
if (!/^[A-Z_]{1,32}$/.test(code)) code = "NATIVE";
var number = error && typeof error.number === "number" ? error.number : 0;
return { stage: stage, code: code, hr: hex(number) };
}
function readLine(maximum) {
var value = String(WScript.StdIn.ReadLine());
check(value.length <= maximum && /^[\x20-\x7E]*$/.test(value), "PROTOCOL");
return value;
}
function numberLine(maximum) {
var value = readLine(9);
check(/^(0|[1-9][0-9]{0,8})$/.test(value), "PROTOCOL");
var number = Number(value);
check(number <= maximum, "BINARY_LIMIT");
return number;
}
function normalizedThumbprint(value) {
var result = String(value).replace(/\s/g, "").toUpperCase();
check(/^[0-9A-F]{40}$/.test(result), "THUMBPRINT");
return result;
}
function inputThumbprint() {
var value = readLine(40);
check(/^[0-9A-F]{40}$/.test(value), "PROTOCOL");
return value;
}
function inputKind() {
var value = numberLine(100);
check(value === 2 || value === 100, "PROTOCOL");
return value;
}
function canonicalBase64(value, bytes) {
var length = 4 * Math.ceil(bytes / 3);
check(bytes >= 0 && value.length === length, "BINARY_LIMIT");
if (bytes === 0) return;
check(/^[A-Za-z0-9+/]+={0,2}$/.test(value), "PROTOCOL");
var padding = (3 - bytes % 3) % 3;
var actual = value.charAt(value.length - 1) === "=" ? 1 : 0;
if (value.charAt(value.length - 2) === "=") actual++;
check(actual === padding, "PROTOCOL");
if (padding === 1) check((alphabet.indexOf(value.charAt(value.length - 2)) & 3) === 0, "PROTOCOL");
if (padding === 2) check((alphabet.indexOf(value.charAt(value.length - 3)) & 15) === 0, "PROTOCOL");
}
function inputBlob(allowEmpty) {
var bytes = numberLine(MAX_BYTES);
check(allowEmpty === true || bytes > 0, "BINARY_LIMIT");
var remaining = 4 * Math.ceil(bytes / 3);
var chunks = [];
while (remaining > 0) {
var expected = Math.min(CHUNK_CHARS, remaining);
var chunk = readLine(CHUNK_CHARS);
check(chunk.length === expected, "PROTOCOL");
if (remaining > CHUNK_CHARS) {
check(/^[A-Za-z0-9+/]+$/.test(chunk), "PROTOCOL");
} else {
canonicalBase64(chunk, bytes - Math.floor(bytes / 36864) * 36864 || 36864);
}
chunks.push(chunk);
remaining -= expected;
}
return chunks.join("");
}
function exportedBlob(value, maximum, allowEmpty) {
check(typeof value === "string", "PROTOCOL");
var base64 = String(value).replace(/[ \t\r\n]/g, "");
check((allowEmpty === true || base64.length > 0) && base64.length <= 4 * Math.ceil(maximum / 3), "BINARY_LIMIT");
check(base64.length % 4 === 0, "PROTOCOL");
var padding = base64.charAt(base64.length - 1) === "=" ? 1 : 0;
if (base64.charAt(base64.length - 2) === "=") padding++;
var bytes = base64.length / 4 * 3 - padding;
check((allowEmpty === true || bytes > 0) && bytes <= maximum, "BINARY_LIMIT");
canonicalBase64(base64, bytes);
return { bytes: bytes, base64: base64 };
}
function line(value) { WScript.StdOut.WriteLine(String(value)); }
function outputBlob(blob) {
line(blob.bytes);
for (var offset = 0; offset < blob.base64.length; offset += CHUNK_CHARS) {
line(blob.base64.substr(offset, CHUNK_CHARS));
}
}
function beginResponse(request, status) {
line(MAGIC);
line(request.id);
line(status);
line(request.command);
}
function endResponse() { line("END"); }
function eligibility(certificate) {
stage = "Certificate.HasPrivateKey";
if (!certificate.HasPrivateKey()) return "NO_KEY";
stage = "Certificate.PublicKey";
var oid = String(certificate.PublicKey().Algorithm.Value);
if (oid !== "1.2.643.7.1.1.1.1" && oid !== "1.2.643.7.1.1.1.2") return "ALGORITHM";
stage = "Certificate.Validity";
var from = new Date(certificate.ValidFromDate).getTime();
var to = new Date(certificate.ValidToDate).getTime();
var now = new Date().getTime();
if (!isFinite(from) || !isFinite(to) || from > now || to <= now) return "VALIDITY";
return "";
}
function certificateRecord(kind, certificate) {
stage = "Certificate.Thumbprint";
var thumb = normalizedThumbprint(certificate.Thumbprint);
stage = "Certificate.Export";
var blob = exportedBlob(certificate.Export(0), CERT_BYTES);
return { kind: kind, thumb: thumb, blob: blob };
}
function withCertificate(kind, thumb, callback) {
var store = null;
var opened = false;
try {
stage = "Store.Create";
store = new ActiveXObject("CAdESCOM.Store");
stage = "Store.Open";
store.Open(kind, "My", 0);
opened = true;
stage = "Certificates.Find";
var matches = store.Certificates.Find(0, thumb, false);
var count = matches.Count;
check(typeof count === "number" && isFinite(count) && count >= 0 && count === Math.floor(count) && count <= MAX_RECORDS, "PROTOCOL");
check(count > 0, "CERT_NOT_FOUND");
var skipped = "";
for (var i = 1; i <= count; i++) {
stage = "Certificates.Item";
var certificate = matches.Item(i);
stage = "Certificate.Thumbprint";
check(normalizedThumbprint(certificate.Thumbprint) === thumb, "THUMBPRINT");
var reason = eligibility(certificate);
if (reason !== "") { skipped = reason; continue; }
return callback(certificate);
}
throw failure(skipped || "CERT_NOT_FOUND");
} finally {
if (opened) {
// A cleanup failure must not replace the original error stage.
try { store.Close(); } catch (closeError) { }
}
store = null;
}
}
function discover() {
var records = [];
var warnings = [];
var seen = {};
var totalCharacters = 0;
var kinds = [100, 2];
function warning(kind, error, count) {
if (warnings.length < 62) warnings.push({ kind: kind, stage: error.stage, code: error.code, hr: error.hr, count: count });
}
for (var k = 0; k < kinds.length; k++) {
var kind = kinds[k];
var store = null;
var opened = false;
var counters = { NO_KEY: 0, ALGORITHM: 0, VALIDITY: 0 };
try {
stage = "Store.Create";
store = new ActiveXObject("CAdESCOM.Store");
stage = "Store.Open";
store.Open(kind, "My", 0);
opened = true;
stage = "Certificates.Count";
var certificates = store.Certificates;
var count = certificates.Count;
check(typeof count === "number" && isFinite(count) && count >= 0 && count === Math.floor(count), "PROTOCOL");
check(count <= MAX_RECORDS, "DISCOVERY_LIMIT");
warning(kind, { stage: stage, code: "COUNT", hr: "0x00000000" }, count);
for (var i = 1; i <= count; i++) {
try {
stage = "Certificates.Item";
var certificate = certificates.Item(i);
var reason = eligibility(certificate);
if (reason !== "") { counters[reason]++; continue; }
var record = certificateRecord(kind, certificate);
if (seen[record.thumb] === true) continue;
totalCharacters += record.blob.base64.length;
check(records.length < MAX_RECORDS && totalCharacters <= MAX_DISCOVERY_CHARS, "DISCOVERY_LIMIT");
seen[record.thumb] = true;
records.push(record);
} catch (certificateError) {
if (certificateError.bridgeCode === "DISCOVERY_LIMIT") throw certificateError;
warning(kind, safeError(certificateError), 1);
}
}
} catch (storeError) {
if (storeError.bridgeCode === "DISCOVERY_LIMIT") throw storeError;
warning(kind, safeError(storeError), 1);
} finally {
if (opened) { try { store.Close(); } catch (closeError) { warning(kind, { stage: "Store.Close", code: "NATIVE", hr: hex(closeError.number) }, 1); } }
store = null;
}
var codes = ["NO_KEY", "ALGORITHM", "VALIDITY"];
for (var c = 0; c < codes.length; c++) {
if (counters[codes[c]] > 0) warning(kind, { stage: "Certificate.Filter", code: codes[c], hr: "0x00000000" }, counters[codes[c]]);
}
}
return { records: records, warnings: warnings };
}
function sign(request) {
check(selectedThumb !== "" && request.kind === selectedKind && request.thumb === selectedThumb, "SESSION");
return withCertificate(selectedKind, selectedThumb, function (certificate) {
stage = "Signer.Create";
var signer = new ActiveXObject("CAdESCOM.CPSigner");
stage = "Signer.Configure";
signer.Certificate = certificate;
signer.CheckCertificate = true;
signer.Options = 2;
stage = "Signer.SigningTime";
var attribute = new ActiveXObject("CAdESCOM.CPAttribute");
attribute.Name = 0;
attribute.Value = new Date().getVarDate();
signer.AuthenticatedAttributes2.Add(attribute);
stage = "SignedData.Create";
var data = new ActiveXObject("CAdESCOM.CadesSignedData");
stage = "SignedData.ContentEncoding";
data.ContentEncoding = request.pdf === "" ? 0 : 1;
stage = "SignedData.Content";
data.Content = request.pdf;
stage = "SignedData.SignCades";
return exportedBlob(data.SignCades(signer, 1, true, 0), MAX_BYTES);
});
}
function verify(request) {
stage = "SignedData.Create";
var data = new ActiveXObject("CAdESCOM.CadesSignedData");
data.ContentEncoding = 1;
data.Content = request.pdf;
stage = "SignedData.VerifyCades";
data.VerifyCades(request.signature, 1, true);
stage = "Signers.Count";
check(typeof data.Signers.Count === "number" && data.Signers.Count === 1, "SIGNER_COUNT");
stage = "Signer.Thumbprint";
var actual = normalizedThumbprint(data.Signers.Item(1).Certificate.Thumbprint);
check(actual === request.thumb, "SIGNER_MISMATCH");
return actual;
}
function verifyEmpty(request) {
// PowerShell first authenticates the exact zero-byte source and the
// selected embedded certificate with native CryptoAPI. This operation
// additionally performs full CAdES verification without a My-store lookup.
stage = "HashedData.Create";
var hash = new ActiveXObject("CAdESCOM.HashedData");
stage = "HashedData.Algorithm";
hash.Algorithm = request.hashAlgorithm;
stage = "HashedData.DataEncoding";
hash.DataEncoding = 0;
stage = "HashedData.Hash";
hash.Hash("");
stage = "HashedData.Value";
var value = hash.Value;
check(typeof value === "string", "PROTOCOL");
value = value.replace(/\s/g, "").toUpperCase();
check(/^[0-9A-F]+$/.test(value) && value.length === (request.hashAlgorithm === 101 ? 64 : 128), "PROTOCOL");
stage = "SignedData.Create";
var data = new ActiveXObject("CAdESCOM.CadesSignedData");
stage = "SignedData.VerifyHash";
data.VerifyHash(hash, request.signature, 1);
stage = "Signers.Count";
check(typeof data.Signers.Count === "number" && data.Signers.Count === 1, "SIGNER_COUNT");
stage = "Signer.Thumbprint";
var actual = normalizedThumbprint(data.Signers.Item(1).Certificate.Thumbprint);
check(actual === request.thumb, "SIGNER_MISMATCH");
return actual;
}
function inspect(request) {
stage = "SignedData.Create";
var data = new ActiveXObject("CAdESCOM.CadesSignedData");
data.ContentEncoding = 1;
data.Content = request.pdf;
try {
stage = "SignedData.VerifyCades";
// Accept existing PKCS7/CMS/CAdES without requiring a newly made BES.
// All signatures are verified before any identity is returned.
data.VerifyCades(request.signature, 65535, true);
stage = "SignedData.Content";
// Enforce exact external-content binding, including implementations
// which return embedded content when given an attached CMS.
var content = exportedBlob(data.Content, MAX_BYTES, true);
check(content.base64 === request.pdf, "CONTENT_BINDING");
stage = "Signers.Count";
var signers = data.Signers;
var count = signers.Count;
check(typeof count === "number" && isFinite(count) && count >= 1 && count === Math.floor(count) && count <= MAX_RECORDS, "SIGNER_COUNT");
var thumbprints = [];
for (var i = 1; i <= count; i++) {
stage = "Signer.Thumbprint";
thumbprints.push(normalizedThumbprint(signers.Item(i).Certificate.Thumbprint));
}
return { status: "valid", thumbprints: thumbprints, detail: null };
} catch (inspectionError) {
var detail = safeError(inspectionError);
// Only the explicit content-hash mismatch is an unrelated document.
// Malformed, unsupported, trust and all other failures stay unresolved.
var mismatch = detail.stage === "SignedData.VerifyCades" && detail.code === "NATIVE" && detail.hr === "0x80091007";
return { status: mismatch ? "different_content" : "unverifiable", thumbprints: [], detail: detail };
}
}
function preflight() {
stage = "Store.Create";
var store = new ActiveXObject("CAdESCOM.Store");
var opened = false;
try {
stage = "Store.Open";
store.Open(0, "My", 0);
opened = true;
stage = "Signer.Create";
var signer = new ActiveXObject("CAdESCOM.CPSigner");
stage = "SignedData.Create";
var data = new ActiveXObject("CAdESCOM.CadesSignedData");
signer = null;
data = null;
} finally {
if (opened) { try { store.Close(); } catch (closeError) { } }
store = null;
}
}
function readRequest() {
stage = "Protocol";
check(readLine(32) === MAGIC, "PROTOCOL");
var id = readLine(32);
check(/^[0-9a-f]{32}$/.test(id), "PROTOCOL");
var command = readLine(16);
check(/^(CHECK|DISCOVER|SELECT|SIGN|VERIFY|VERIFYEMPTY|INSPECT|CLOSE)$/.test(command), "PROTOCOL");
var request = { id: id, command: command };
if (command === "SELECT" || command === "SIGN") { request.kind = inputKind(); request.thumb = inputThumbprint(); }
if (command === "VERIFY" || command === "VERIFYEMPTY") request.thumb = inputThumbprint();
if (command === "VERIFYEMPTY") {
request.hashAlgorithm = numberLine(102);
check(request.hashAlgorithm === 101 || request.hashAlgorithm === 102, "PROTOCOL");
}
if (command === "SIGN" || command === "VERIFY" || command === "VERIFYEMPTY" || command === "INSPECT") request.pdf = inputBlob(true);
if (command === "VERIFYEMPTY") check(request.pdf === "", "PROTOCOL");
if (command === "VERIFY" || command === "VERIFYEMPTY" || command === "INSPECT") request.signature = inputBlob(false);
check(readLine(3) === "END", "PROTOCOL");
return request;
}
var exitCode = 0;
try {
check(WScript.Arguments.length === 0, "PROTOCOL");
while (true) {
var request = readRequest();
try {
var command = request.command;
if (command === "CLOSE") {
beginResponse(request, "OK");
endResponse();
break;
}
if (command === "SIGN") {
var signature = sign(request);
beginResponse(request, "OK");
outputBlob(signature);
} else {
check(!hasWork, "SESSION");
hasWork = true;
if (command === "CHECK") {
preflight();
beginResponse(request, "OK");
} else if (command === "DISCOVER") {
var result = discover();
beginResponse(request, "OK");
line(result.records.length);
for (var r = 0; r < result.records.length; r++) {
line(result.records[r].kind);
line(result.records[r].thumb);
outputBlob(result.records[r].blob);
}
line(result.warnings.length);
for (var w = 0; w < result.warnings.length; w++) {
line(result.warnings[w].kind);
line(result.warnings[w].stage);
line(result.warnings[w].code);
line(result.warnings[w].hr);
line(result.warnings[w].count);
}
} else if (command === "SELECT") {
var selected = withCertificate(request.kind, request.thumb, function (certificate) { return certificateRecord(request.kind, certificate); });
selectedKind = request.kind;
selectedThumb = request.thumb;
beginResponse(request, "OK");
line(selected.kind);
line(selected.thumb);
outputBlob(selected.blob);
} else if (command === "VERIFY" || command === "VERIFYEMPTY") {
var thumb = command === "VERIFYEMPTY" ? verifyEmpty(request) : verify(request);
beginResponse(request, "OK");
line(1);
line(thumb);
} else if (command === "INSPECT") {
var inspection = inspect(request);
beginResponse(request, "OK");
line(inspection.status);
line(inspection.thumbprints.length);
for (var t = 0; t < inspection.thumbprints.length; t++) line(inspection.thumbprints[t]);
if (inspection.detail !== null) {
line(inspection.detail.stage);
line(inspection.detail.code);
line(inspection.detail.hr);
}
}
}
endResponse();
// Release source snapshots before waiting for the next command.
request = null;
} catch (operationError) {
var detail = safeError(operationError);
beginResponse(request, "ERROR");
line(detail.stage);
line(detail.code);
line(detail.hr);
endResponse();
exitCode = 1;
break;
}
}
} catch (protocolError) {
// A damaged request cannot be correlated safely. No raw exception text.
exitCode = 2;
}
WScript.Quit(exitCode);
}());