forked from rudonick/crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gostCert.js
1301 lines (1208 loc) · 54.6 KB
/
gostCert.js
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file Provides facilities for handling certificates, CRLs, etc.
* @version 1.76
* @copyright 2014-2016, Rudolf Nickolaev. All rights reserved.
*/
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
(function (root, factory) {
/*
* Module imports and exports
*
*/ // <editor-fold defaultstate="collapsed">
if (typeof define === 'function' && define.amd) {
define(['gostCrypto', 'gostASN1'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('gostCrypto'), require('gostASN1'));
} else {
root.GostCert = factory(root.gostCrypto, root.GostASN1);
}
// </editor-fold>
}(this, function (gostCrypto) {
/*
* Common algorithms
*
*/ // <editor-fold defaultstate="collapsed">
var root = this;
var Promise = root.Promise;
var Object = root.Object;
var CryptoOperationData = root.ArrayBuffer;
var Date = root.Date;
// Crypto subtle
var subtle = gostCrypto.subtle;
// Coding
var coding = gostCrypto.coding;
// ASN.1 syntax
var asn1 = gostCrypto.asn1;
// Providers
var providers = gostCrypto.security.providers;
// Expand javascript object
function expand(r) {
for (var i = 1, n = arguments.length; i < n; i++) {
var item = arguments[i];
if (typeof item === 'object')
for (var name in item)
if (item.hasOwnProperty(name))
r[name] = item[name];
}
return r;
}
// Extend javascript class
function extend(Class) {
var F = function () {
};
F.prototype = Class.prototype;
var r = new F, args = [r];
for (var i = 1; i < arguments.length; i++)
args.push(arguments[i]);
return expand.apply(this, args);
}
// Today date + n days
function today(n) {
var date = new Date();
date.setHours(0, 0, 0, 0);
if (n)
date.setDate(date.getDate() + n);
return date;
}
// Self resolver
function call(callback) {
try {
callback();
} catch (e) {
}
}
// Serial number
function genSerialNumber() {
var seed = new Uint8Array(4);
gostCrypto.getRandomValues(seed);
seed[0] = seed[0] & 0x7f;
return coding.Int16.encode(seed);
}
// True if equal numbers
var equalNumbers = (function () {
// Convert number to bigendian hex string
var hex = function (s) {
var t = typeof s;
return t === 'undefined' || s === '' ? '0' :
t === 'number' || s instanceof Number ? s.toString(16).toLowerCase() :
s.replace('0x', '').toLowerCase();
};
// Zero left padding
var lpad = function (s, size) {
return (new Array(size + 1).join('0') + s).slice(-size);
};
return function (s1, s2) {
s1 = hex(s1);
s2 = hex(s2);
var len = Math.max(s1.length, s2.length);
return lpad(s1, len) === lpad(s2, len);
};
})();
// Check equal names
function equalNames(name1, name2) {
for (var key in name1)
if (name1[key] !== name2[key])
return false;
for (var key in name2)
if (name1[key] !== name2[key])
return false;
return true;
}
// Check the buffers to equal
function equalBuffers(r1, r2) {
var s1 = new Uint8Array(r1),
s2 = new Uint8Array(r2);
if (s1.length !== s2.length)
return false;
for (var i = 0, n = s1.length; i < n; i++)
if (s1[i] !== s2[i])
return false;
return true;
}
// Match certificate
function matchCertificate(cert, selector) {
var skid = cert.extensions && cert.extensions.subjectKeyIdentifier;
return (cert && selector &&
(!selector.issuer || equalNames(cert.issuer, selector.issuer)) &&
(!selector.serialNumber || equalNumbers(cert.serialNumber, selector.serialNumber)) &&
(!selector.subjectKeyIdentifier || equalBuffers(skid, selector.subjectKeyIdentifier)) &&
(!selector.subject || equalNames(cert.subject, selector.subject)) &&
(!selector.date || (cert.notBefore.getTime() <= selector.date.getTime() &&
cert.notAfter.getTime() > selector.date.getTime())));
}
// Create authority certificate selector
function authoritySelector(cert, extensions, date) {
var selector = {subject: cert.issuer, date: date},
akid = extensions && extensions.authorityKeyIdentifier;
if (akid) {
selector.subjectKeyIdentifier = akid.keyIdentifier;
if (akid.authorityCertIssuer && akid.authorityCertIssuer[0] &&
akid.authorityCertSerialNumber) {
selector.issuer = akid.authorityCertIssuer[0];
selector.serialNumber = akid.authorityCertSerialNumber;
}
}
return selector;
}
// Find certificates
function selectCertificates(certs, selector) {
var result = [];
for (var i = 0, n = certs.length; i < n; i++)
if (matchCertificate(certs[i], selector))
result.push(certs[i]);
return result;
}
// Match CRL
function matchCRL(crl, selector) {
return ((!selector.issuer || equalNames(crl.issuer, selector.issuer)) &&
(!selector.date || (crl.thisUpdate.getTime() < selector.date.getTime())));
}
// Find certificates
function selectCRLs(crls, selector) {
var result = [];
for (var i = 0, n = crls.length; i < n; i++)
if (matchCRL(crls[i], selector))
result.push(crls[i]);
return result;
}
// Define provider for key algorithm
function keyProvider(algorithm) {
var id = algorithm.id;
for (var name in providers) {
var provider = providers[name];
if (provider.publicKey.id === id)
return provider;
}
}
function defineProperty(object, name, descriptor, enumerable) {
if (typeof descriptor !== 'object')
descriptor = {value: descriptor};
if (enumerable !== undefined)
descriptor.enumerable = enumerable;
Object.defineProperty(object, name, descriptor);
}
function defineProperties(object, properties, enumerable) {
for (var name in properties)
defineProperty(object, name, properties[name], enumerable);
}
// Extend javascript class
function extend(Super, Class, propertiesObject, propertiesClass) {
// If constructor not defined
if (typeof Class !== 'function') {
propertiesClass = propertiesObject;
propertiesObject = Class;
Class = function () {
Super.apply(this, arguments);
};
}
// Create prototype properties
Class.prototype = Object.create(Super.prototype, {
constructor: {
value: Class
},
superclass: {
value: Super.prototype
}
});
if (propertiesObject)
defineProperties(Class.prototype, propertiesObject, true);
// Inherites super class properties
if (Super !== Object)
for (var name in Super)
Class[name] = Super[name];
Class.super = Super;
if (propertiesClass)
defineProperties(Class, propertiesClass, true);
return Class;
}
// </editor-fold>
/**
* Provides facilities for handling certificates, CRLs, etc.
*
* @class GostCert
*/
function GostCert() {
}
/**
* Certificate templates
* <ul>
* <li>providerName - provider name for key generation, default 'CP-01'</li>
* <li>subject - template of subject name {countryName: 'RU', commonName: 'Anonymous'}</li>
* <li>caKeyUsage - default key usages for a CA certificates
* ['digitalSignature', 'nonRepudiation', 'keyEncipherment',
* 'dataEncipherment', 'keyAgreement', 'keyCertSign', 'cRLSign']</li>
* <li>caExtKeyUsage - default extended key usages for a CA certificates
* ['serverAuth', 'clientAuth', 'codeSigning', 'emailProtection', 'ipsecEndSystem',
* 'ipsecTunnel', 'ipsecUser', 'timeStamping', 'OCSPSigning']</li>
* <li>userKeyUsage - default key usages for a user certificate
* ['digitalSignature', 'nonRepudiation', 'keyEncipherment', 'dataEncipherment', 'keyAgreement'],
* <li>userExtKeyUsage - default extended key usages for user certificate
* ['clientAuth', 'emailProtection']</li>
* <li>days - validity of the certificate in days, default 7305</li>
* </ul>
*
* @memberOf GostCert
* @instance
*/
var options = {// <editor-fold defaultstate="collapsed">
providerName: 'CP-01',
subject: {countryName: 'RU', commonName: 'Anonymous'},
caKeyUsage: ['digitalSignature', 'nonRepudiation', 'keyEncipherment',
'dataEncipherment', 'keyAgreement', 'keyCertSign', 'cRLSign'],
caExtKeyUsage: ['serverAuth', 'clientAuth', 'codeSigning', 'emailProtection', 'ipsecEndSystem',
'ipsecTunnel', 'ipsecUser', 'timeStamping', 'OCSPSigning'],
userKeyUsage: ['digitalSignature', 'nonRepudiation', 'keyEncipherment', 'dataEncipherment', 'keyAgreement'],
userExtKeyUsage: ['clientAuth', 'emailProtection'],
days: 7305 // </editor-fold>
};
GostCert.prototype.options = options;
/**
* This class encapsulates X.509 Version 3 certificates.<br><br>
*
* Constructs an X.509 certificate from the given DER encoding or ASN.1 Certificate object.
*
* @class GostCert.X509
* @extends GostASN1.Certificate
* @param {(FormatedData|GostASN1.Certificate)} cert The certificate
*/
var X509 = function (cert) // <editor-fold defaultstate="collapsed">
{
try {
// Try to use decode X.509 certificate
asn1.Certificate.call(this, cert, true);
} catch (e) {
try {
// Try to decode certification request
cert = new asn1.CertificationRequest(cert, true);
} catch (e) {
}
// Create new certificate structure
cert = cert || {};
asn1.Certificate.call(this, {
version: 2,
serialNumber: cert.serialNumber || genSerialNumber(),
signature: cert.signature || {id: 'noSignature'},
issuer: cert.subject || options.subject,
notBefore: cert.notBefore || today(),
notAfter: cert.notAfter ||
today(cert.days || options.days),
subject: cert.subject || options.subject,
subjectPublicKeyInfo: cert.subjectPublicKeyInfo || {
algorithm: {id: 'noSignature'},
subjectPublicKey: new CryptoOperationData(0)
},
extensions: (cert.attributes && (cert.attributes.extensionRequest ||
cert.attributes.msCertExtensions)) || cert.extensions,
signatureAlgorithm: {id: 'noSignature'},
signatureValue: new CryptoOperationData(0)
});
}
}; // </editor-fold>
extend(asn1.Certificate, X509, {
/**
* Generate the contents of this certificate and sign it.<br><br>
*
* If issuerCertificate is not defined self signed certificate generated
*
* @memberOf GostCert.X509
* @instance
* @param {GostASN1.PrivateKeyInfo} issuerPrivateKey The issuer's private key
* @param {GostCert.X509} issuerCertificate The issuer's certificate or undefined for self-signed certificate
* @returns {Promise} Promise to return self object after sign the certificate
*/
sign: function (issuerPrivateKey, issuerCertificate) // <editor-fold defaultstate="collapsed">
{
var self = this, spki = self.subjectPublicKeyInfo;
return new Promise(call).then(function () {
// Need generated key
if (!spki || !spki.algorithm || spki.algorithm === 'noSignature')
throw new Error('Key pair was not generated for the certificate');
// Check issuer private key
if (!issuerPrivateKey)
throw new Error('The private key of the issuer is not defined');
// Certificate can be self signed
issuerCertificate = issuerCertificate || self;
// Calculate subject key indentifier
return subtle.digest('SHA-1', spki.subjectPublicKey);
}).then(function (digest) {
// Signature algorithm
var provider = issuerCertificate.getProvider() || providers[options.providerName];
if (!self.signature || self.signature.id === 'noSignature')
self.signature = provider.signature;
self.signatureAlgorithm = self.signature;
// Set issuer
self.issuer = issuerCertificate.subject;
// Set default extensions
if (!self.extensions)
self.extensions = {};
var exts = self.extensions, ae = issuerCertificate.extensions;
if (self === issuerCertificate) { // Self-signed CA certificate
// Set key usage
exts.keyUsage = exts.keyUsage || options.caKeyUsage;
exts.extKeyUsage = exts.extKeyUsage || options.caExtKeyUsage;
// Set basic constraints
exts.basicConstraints = exts.basicConstraints || {cA: true};
} else {
// Check key usage and validity
if (!issuerCertificate.checkUsage('keyCertSign', self.notBefore))
throw new Error('The issuer\'s certificate is not valid for signing a certificate');
// Set key usage
exts.keyUsage = exts.keyUsage || options.userKeyUsage;
exts.extKeyUsage = exts.extKeyUsage || options.userExtKeyUsage;
// Set basic constraints
exts.basicConstraints = exts.basicConstraints || {
cA: exts.keyUsage.indexOf('keyCertSign') >= 0
};
// Check path length constraint
if (exts.basicConstraints.cA) {
var pathLen = ae && ae.basicConstraints && ae.pathLenConstraint;
if (pathLen !== undefined) {
if (pathLen > 0)
exts.basicConstraints.pathLenConstraint = pathLen - 1;
else
throw new Error('Path length constraint exceeded');
}
}
}
// Subject key identifier 160 bit from public key hash
exts.subjectKeyIdentifier = digest;
// Authority key identifier
if (ae && ae.subjectKeyIdentifier)
exts.authorityKeyIdentifier = {
keyIdentifier: ae.subjectKeyIdentifier,
authorityCertIssuer: [issuerCertificate.issuer],
authorityCertSerialNumber: issuerCertificate.serialNumber};
// Import the private key
return subtle.importKey('pkcs8', issuerPrivateKey.encode(), issuerPrivateKey.privateKeyAlgorithm, false, ['sign']);
}).then(function (key) {
// Sign certificate
return subtle.sign(self.signatureAlgorithm, key, self.tbsCertificate.encode());
}).then(function (signatureValue) {
// Siganture value
self.signatureValue = signatureValue;
return self;
});
}, // </editor-fold>
/**
* Generate key pair for certificate
*
* @memberOf GostCert.X509
* @instance
* @param {(AlgorithmIdentifier|string)} keyAlgorithm The key algorithm or name of provider
* @returns {Promise} Promise to return {@link GostASN1.PrivateKeyInfo} after self-signed certificate generation
*/
generate: function (keyAlgorithm) // <editor-fold defaultstate="collapsed">
{
var self = this, privateKey, provider;
if (keyAlgorithm)
provider = providers[keyAlgorithm];
else
provider = this.getProvider() || providers[options.providerName];
if (provider)
keyAlgorithm = expand(provider.publicKey, {privateKey: provider.privateKey});
return new Promise(call).then(function () {
// Generate key pair
return subtle.generateKey(keyAlgorithm, 'true', ['sign', 'verify']);
}).then(function (keyPair) {
privateKey = keyPair.privateKey;
// Export public key
return subtle.exportKey('spki', keyPair.publicKey);
}).then(function (spki) {
self.subjectPublicKeyInfo = new asn1.SubjectPublicKeyInfo(spki);
return subtle.exportKey('pkcs8', privateKey);
}).then(function (pkcs8) {
return new asn1.PrivateKeyInfo(pkcs8);
});
}, // </editor-fold>
/**
* Gets the public key.
*
* @memberOf GostCert.X509
* @instance
* @returns {Promise} Promise to return {@link Key}
*/
getPublicKey: function () // <editor-fold defaultstate="collapsed">
{
var spki = this.subjectPublicKeyInfo,
keyUsages = (spki.algorithm.id === 'rsaEncryption') ? ['verify'] :
['verify', 'deriveKey', 'deriveBits'];
return subtle.importKey('spki', spki.encode(), spki.algorithm, 'false', keyUsages);
}, // </editor-fold>
/**
* Get appropriate crypto provider for public key
*
* @memberOf GostCert.X509
* @instance
* @returns Object Set of crypto provider algorithms
*/
getProvider: function () // <editor-fold defaultstate="collapsed">
{
return keyProvider(this.subjectPublicKeyInfo.algorithm);
}, // </editor-fold>
/**
* Verifies this certificate.<br><br>
*
* More precisely:<br><br>
* <ul>
* <li>Verifies that the current VM date/time is within the validity period of the certificate.</li>
* <li>If an unrecognized critical extension is present, the certificate is rejected.</li>
* <li>If the issuer certificate has been set, verifies that the signing certificate is a
* CA certificate, and that the signature is correct. The signing certificate is considered
* to be a CA certificate unless one of the following two conditions hold: The signing
* certificate contains a basicConstraints extension, and the CA flag is false; or the
* signing certificate contains a keyUsage extension, the keyUsage extension is marked
* critical, and the keyCertSign bit is false.</li>
* <li>If the issuer CRL has been set, verifies that the certificate has not been revoked.</li>
* </ul>
*
* @memberOf GostCert.X509
* @instance
* @param {GostCert.X509} issuerCertificate The issuer X.509 certificate
* @param {GostCert.CRL} issuerCRL The issuer CRL
* @param {Date} date Validation date. Default current date
* @returns {Promise} Promise to return self object if the certificate is valid
*/
verify: function (issuerCertificate, issuerCRL, date) // <editor-fold defaultstate="collapsed">
{
var self = this, exts = self.extensions;
return new Promise(call).then(function () {
// Current date
date = date || today();
if (self.notBefore.getTime() > date.getTime() ||
self.notAfter.getTime() <= date.getTime())
throw new Error('The certificate has not yet started or expired');
// A unrecognized critical extensions
for (var name in exts) {
var value = exts[name];
if (value.critical &&
['authorityKeyIdentifier', 'subjectKeyIdentifier', 'keyUsage', 'certificatePolicies',
'policyMappings', 'basicConstraints', 'nameConstraints', 'policyConstraints',
'extKeyUsage'].indexOf(name) < 0)
throw new Error('The critical extension \'' + name + '\' is unrecognized');
}
// The certificate can be self-signed
var selector = authoritySelector(self, exts, self.notBefore);
if (!issuerCertificate && matchCertificate(self, selector))
issuerCertificate = self;
// Check issuer
if (issuerCertificate) {
if (!matchCertificate(issuerCertificate, selector) ||
!issuerCertificate.checkUsage('keyCertSign', self.notBefore))
throw new Error('The issuer\'s certificate is not valid');
// Check certificate signature
return issuerCertificate.verifySignature(self.tbsCertificate.encode(),
self.signatureValue, self.signatureAlgorithm);
}
return true;
}).then(function (result) {
if (!result)
throw new Error('The certificate has invalid signature');
// Check CRL
if (issuerCRL) {
if (!matchCRL(issuerCRL, {issuer: self.issuer, date: date}))
throw new Error('The issuer\'s CRL is not valid');
if (issuerCRL.isRevoked(self.serialNumber))
throw new Error('The certificate is revoked');
}
return self;
});
}, // </editor-fold>
/**
* Verify a signature made with this certificate's public key.
*
* @memberOf GostCert.X509
* @instance
* @param {CryptoOperationData} data The signed document.
* @param {CryptoOperationData} signature The signature
* @param {AlgorithmIdentifier} algorithm The algorithm ID used for the signature.
* @returns {Promise} Promise to return true if the signature is verified, and false otherwise
*/
verifySignature: function (data, signature, algorithm) // <editor-fold defaultstate="collapsed">
{
return this.getPublicKey().then(function (publicKey) {
return subtle.verify(algorithm, publicKey, signature, data);
});
}, // </editor-fold>
/**
* Check key usage and date validation
*
* @memberOf GostCert.X509
* @instance
* @param {DOMString} operation The operation
* @param {Date} date Operation date. Default current date
* @returns {boolean}
*/
checkUsage: function (operation, date) // <editor-fold defaultstate="collapsed">
{
var self = this, exts = self.extensions;
date = date || today();
return (self.notBefore.getTime() <= date.getTime() && self.notAfter.getTime() > date.getTime()) &&
(!exts || !((['keyCertSign', 'cRLSign'].indexOf(operation) > 0 && exts.basicConstraints && !exts.basicConstraints.cA) ||
((exts.keyUsage && exts.keyUsage.indexOf(operation) < 0) && (exts.extKeyUsage && exts.extKeyUsage.indexOf(operation) < 0))));
} // </editor-fold>
});
/**
* This class encapsulates X.509 Version 3 certificates.
*
* @memberOf GostCert
* @type GostCert.X509
*/
GostCert.prototype.X509 = X509;
/**
* This class encapsulates a X.509 certificate revocation list (CRL) of RevokedCertificate objects.<br><br>
*
* Note: the methods and constructors that input a CRL do not automatically verify it.
* You need to explicitly call the verify method.
*
* @class GostCert.CRL
* @extends GostASN1.CertificateList
* @param {(FormatedData|GostASN1.CertificateList)} crl
*/
var CRL = function (crl) // <editor-fold defaultstate="collapsed">
{
// Call super
CRL.super.call(this, crl);
// Initialize defaults
if (!this.version)
this.version = 1;
if (!this.revokedCertificates)
this.revokedCertificates = [];
if (!this.thisUpdate)
this.thisUpdate = today();
}; // </editor-fold>
extend(asn1.CertificateList, CRL, {
/**
* Signs this CRL. The issuer's private key has to be set. The default random number generator is used, if needed.<br><br>
*
* Note: Making any modifications to the contents of the CRL after signing invalidates the signature.
* The sign method must be invoked again after any modifications for a valid signature to be computed.
*
* @memberOf GostCert.CRL
* @instance
* @param {GostASN1.PrivateKeyInfo} issuerPrivateKey the issuer's private signing key
* @param {GostCert.X509} issuerCertificate the issuer's certificate
* @returns {Promise} Promise to return self object after sign the CRL
*/
sign: function (issuerPrivateKey, issuerCertificate) // <editor-fold defaultstate="collapsed">
{
var self = this;
return new Promise(call).then(function () {
// Check issuer private key
if (!issuerPrivateKey)
throw new Error('The issuer\'s private key is not defined');
// Check issuer certificate
if (!issuerCertificate)
throw new Error('The issuer\'s certificate is not defined');
// Check issuer name
if (!self.issuer)
self.issuer = issuerCertificate.issuer;
else if (!equalNames(self.issuer, issuerCertificate.issuer))
throw new Error('The CRL prototype and authority certificate have different issuers');
// Check key usage and validity
if (!issuerCertificate.checkUsage('cRLSign', self.thisUpdate))
throw new Error('The issuer\'s certificate is not valid for signing a CRL');
// Signature algorithm
var provider = issuerCertificate.getProvider() || providers[options.providerName];
if (!self.signature)
self.signature = provider.signature;
self.signatureAlgorithm = self.signature;
// Set issuer
self.issuer = issuerCertificate.subject;
// Set default extensions
if (!self.crlExtensions)
self.crlExtensions = {};
var exts = self.crlExtensions,
ae = issuerCertificate.extensions;
if (ae && ae.subjectKeyIdentifier)
exts.authorityKeyIdentifier = {
keyIdentifier: ae.subjectKeyIdentifier,
authorityCertIssuer: [issuerCertificate.issuer],
authorityCertSerialNumber: issuerCertificate.serialNumber};
exts.cRLNumber = exts.cRLNumber || 0;
// Import the private key
return subtle.importKey('pkcs8', issuerPrivateKey.encode(),
issuerPrivateKey.privateKeyAlgorithm, false, ['sign']);
}).then(function (key) {
// Sign CRL
return subtle.sign(self.signatureAlgorithm, key, self.tbsCertList.encode());
}).then(function (signatureValue) {
// Siganture value
self.signatureValue = signatureValue;
return self;
});
}, // </editor-fold>
/**
* Verify the CRL. Checks the date and signature if issuer's certifiate has been defined.
*
* @memberOf GostCert.CRL
* @instance
* @param {GostCert.X509} issuerCertificate the issuer's certificate
* @param {Date} date Validation date. Default current date
* @returns {Promise} Promise to return self object if the certificate is valid
*/
verify: function (issuerCertificate, date) // <editor-fold defaultstate="collapsed">
{
var self = this, exts = self.crlExtensions;
return new Promise(call).then(function () {
// Current date
date = date || today();
if (!self.thisUpdate.getTime() > date.getTime())
throw new Error('The CRL has not yet started');
// Check issuer
if (issuerCertificate) {
if (!matchCertificate(issuerCertificate, authoritySelector(self, exts, self.thisUpdate)) ||
!issuerCertificate.checkUsage('cRLSign', self.thisUpdate))
throw new Error('The issuer\'s certificate is not valid');
if (!self.signatureValue || !self.signatureAlgorithm)
throw new Error('The has no signature');
// Check CRL signature
return issuerCertificate.verifySignature(self.tbsCertList.encode(),
self.signatureValue, self.signatureAlgorithm);
}
}).then(function (result) {
if (!result)
throw new Error('The CRL has invalid signature');
return self;
});
}, // </editor-fold>
/**
* Checks whether this certificate serial number is on the list.
*
* @memberOf GostCert.CRL
* @instance
* @param {Number} serialNumber the issuer's certificate
* @param {Date} date Validation date. Default current date
* @returns {boolean} True if the certificate is valid, and false otherwise
*/
isRevoked: function (serialNumber, date) // <editor-fold defaultstate="collapsed">
{
var rc = this.revokedCertificates;
date = date || today();
for (var i = 0; i < rc.length; i++) {
// Check date and serial number
if (date.getTime() >= rc[i].revocationDate.getTime() &&
equalNumbers(rc[i].userCertificate, serialNumber))
return true;
}
return false;
} // </editor-fold>
});
/**
* This class encapsulates a X.509 certificate revocation list (CRL) of RevokedCertificate objects.
*
* @memberOf GostCert
* @type GostCert.CRL
*/
GostCert.prototype.CRL = CRL;
/**
* A class that encapsulates a DER-encoded PKCS #10 certificate request. The request contains
* the subject's name and public key, and it is signed with the subject's private key.
* The public key contained in the request is used to verify the signature.
* The signature on the request is verified automatically when the request is read.
* Note that the subject's private key is used only to produce a signature when the request is output,
* and is not actually stored with the request.
*
* @class GostCert.Request
* @extends GostASN1.CertificationRequest
* @param {(FormatedData|GostASN1.CertificationRequest)} req
*/
function Request(req) // <editor-fold defaultstate="collapsed">
{
try {
// Try to use encode
asn1.CertificationRequest.call(this, req, true);
} catch (e) {
// Create new certificate structure
req = req || {};
asn1.CertificationRequest.call(this, {
version: 0,
subject: req.subject || options.subject,
subjectPublicKeyInfo: req.subjectPublicKeyInfo || {
algorithm: {id: 'noSignature'},
subjectPublicKey: new CryptoOperationData(0)
},
attributes: req.attributes || {
extensionRequest: {
keyUsage: options.userKeyUsage,
extKeyUsage: options.userExtKeyUsage
}
},
signatureAlgorithm: {id: 'noSignature'},
signatureValue: new CryptoOperationData(0)
});
}
} // </editor-fold>
extend(asn1.CertificationRequest, Request, {
/**
* Generate key pair and sign request
*
* @memberOf GostCert.Request
* @instance
* @param {(AlgorithmIdentifier|string)} keyAlgorithm The name of provider or algorithm
* @returns {Promise} Promise to return {@link GostASN1.PrivateKeyInfo} after request generation
*/
generate: function (keyAlgorithm) // <editor-fold defaultstate="collapsed">
{
var self = this, privateKey, provider;
if (keyAlgorithm)
provider = providers[keyAlgorithm];
else
provider = this.getProvider() || providers[options.providerName];
if (provider)
keyAlgorithm = expand(provider.publicKey, {privateKey: provider.privateKey});
return new Promise(call).then(function () {
// Generate key pair
return subtle.generateKey(keyAlgorithm, 'true', ['sign', 'verify']);
}).then(function (keyPair) {
privateKey = keyPair.privateKey;
// Export public key
return subtle.exportKey('spki', keyPair.publicKey);
}).then(function (spki) {
self.subjectPublicKeyInfo = new asn1.SubjectPublicKeyInfo(spki);
return subtle.exportKey('pkcs8', privateKey);
}).then(function (pkcs8) {
privateKey = new asn1.PrivateKeyInfo(pkcs8);
// Sign request
return self.sign(privateKey);
}).then(function () {
return privateKey;
});
}, // </editor-fold>
/**
* Get appropriate crypto provider for public key
*
* @memberOf GostCert.Request
* @instance
* @returns Object Set of crypto provider algorithms
*/
getProvider: function () // <editor-fold defaultstate="collapsed">
{
return keyProvider(this.subjectPublicKeyInfo.algorithm);
}, // </editor-fold>
/**
* Generate the contents of this request and sign it.<br><br>
*
* @memberOf GostCert.Request
* @instance
* @param {GostASN1.PrivateKeyInfo} privateKey The subject's private key
* @returns Promise to return self object after sign the request
*/
sign: function (privateKey) // <editor-fold defaultstate="collapsed">
{
var self = this, spki = self.subjectPublicKeyInfo;
return new Promise(call).then(function () {
// Need generated key
if (!spki || !spki.algorithm || spki.algorithm === 'noSignature')
throw new Error('Key pair was not generated for the certificate');
// Check issuer private key
if (!privateKey)
throw new Error('The private key is not defined');
// Signature algorithm
var provider = keyProvider(spki.algorithm) || providers[options.providerName];
self.signatureAlgorithm = provider.signature;
// Import the private key
return subtle.importKey('pkcs8', privateKey.encode(),
privateKey.privateKeyAlgorithm, false, ['sign']);
}).then(function (key) {
// Sign the certification request
return subtle.sign(self.signatureAlgorithm, key, self.requestInfo.encode());
}).then(function (signatureValue) {
// Siganture value
self.signatureValue = signatureValue;
return self;
});
}, // </editor-fold>
/**
* Verify the Certification Request. Checks the signature on the public key in the request.
*
* @memberOf GostCert.Request
* @instance
* @returns {Promise} Promise to return self object if the certificate is valid
*/
verify: function () // <editor-fold defaultstate="collapsed">
{
var self = this, spki = self.subjectPublicKeyInfo;
return new Promise(call).then(function () {
// Import key
return subtle.importKey('spki', spki.encode(), spki.algorithm, 'false', ['verify']);
}).then(function (publicKey) {
// Verify signature
return subtle.verify(self.signatureAlgorithm, publicKey, self.signatureValue,
self.requestInfo.encode());
}).then(function (result) {
if (!result)
throw new Error('The certification request has invalid signature');
return self;
});
} // </editor-fold>
});
/**
* A class that encapsulates a DER-encoded PKCS #10 certificate request.
*
* @memberOf GostCert
* @type GostCert.Request
*/
GostCert.prototype.Request = Request;
/**
* A class for retrieving Certificates and CRLs from a repository.<br><br>
*
* Once the CertStore has been created, it can be used to retrieve Certificates
* and CRLs by calling its getCertificates and getCRLs methods. Unlike a KeyStore,
* which provides access to a cache of private keys and trusted certificates,
* a CertStore is designed to provide access to a potentially vast repository
* of untrusted certificates and CRLs.
*
* @class GostCert.CertStore
* @param {GostCert.X509[]} certificates Certificates
* @param {GostCert.CRL[]} crls CLRs
*/
function CertStore(certificates, crls) // <editor-fold defaultstate="collapsed">
{
this.certificates = certificates || [];
this.crls = crls || [];
} // </editor-fold>
extend(Object, CertStore, {
/**
* Returns a Array of Certificates that match the specified selector.
*
* @memberOf GostCert.CertStore
* @instance
* @param {GostCert.CertSelector} selector Certificate filter selector
* @returns {GostCert.X509[]} Selected certificates
*/
getCertificates: function (selector) // <editor-fold defaultstate="collapsed">
{
return selectCertificates(this.certificates, selector);
}, // </editor-fold>
/**
* Returns a Collection of CRLs that match the specified selector.
*
* @memberOf GostCert.CertStore
* @instance
* @param {GostCert.CertSelector} selector CRL filter selector
* @returns {GostCert.CRL[]} selected CRLs
*/
getCRLs: function (selector) // <editor-fold defaultstate="collapsed">
{
return selectCRLs(this.certificates, selector);
}, // </editor-fold>
/**
* Loads this CertStore from the given PKCS#7 formated input stream.