forked from kjur/jsrsasign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x509-1.1.js
1234 lines (1131 loc) · 42.7 KB
/
x509-1.1.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
/*! x509-1.1.9.js (c) 2012-2016 Kenji Urushima | kjur.github.com/jsrsasign/license
*/
/*
* x509.js - X509 class to read subject public key from certificate.
*
* Copyright (c) 2010-2016 Kenji Urushima ([email protected])
*
* This software is licensed under the terms of the MIT License.
* http://kjur.github.com/jsrsasign/license
*
* The above copyright and license notice shall be
* included in all copies or substantial portions of the Software.
*/
/**
* @fileOverview
* @name x509-1.1.js
* @author Kenji Urushima [email protected]
* @version x509 1.1.9 (2016-May-10)
* @since jsrsasign 1.x.x
* @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
*/
/*
* Depends:
* base64.js
* rsa.js
* asn1hex.js
*/
/**
* hexadecimal X.509 certificate ASN.1 parser class.<br/>
* @class hexadecimal X.509 certificate ASN.1 parser class
* @property {RSAKey} subjectPublicKeyRSA Tom Wu's RSAKey object
* @property {String} subjectPublicKeyRSA_hN hexadecimal string for modulus of RSA public key
* @property {String} subjectPublicKeyRSA_hE hexadecimal string for public exponent of RSA public key
* @property {String} hex hexacedimal string for X.509 certificate.
* @author Kenji Urushima
* @version 1.0.1 (08 May 2012)
* @see <a href="http://kjur.github.com/jsrsasigns/">'jsrsasign'(RSA Sign JavaScript Library) home page http://kjur.github.com/jsrsasign/</a>
* @description
* X509 class provides following functionality:
* <ul>
* <li>parse X.509 certificate ASN.1 structure</li>
* <li>get basic fields, extensions, signature algorithms and signature values</li>
* <li>read PEM certificate</li>
* </ul>
*
* <ul>
* <li><b>TO GET FIELDS</b>
* <ul>
* <li>serial - {@link X509#getSerialNumberHex}</li>
* <li>issuer - {@link X509#getIssuerHex}</li>
* <li>issuer - {@link X509#getIssuerString}</li>
* <li>notBefore - {@link X509#getNotBefore}</li>
* <li>notAfter - {@link X509#getNotAfter}</li>
* <li>subject - {@link X509#getSubjectHex}</li>
* <li>subject - {@link X509#getSubjectString}</li>
* <li>subjectPublicKeyInfo - {@link X509.getSubjectPublicKeyPosFromCertHex}</li>
* <li>subjectPublicKeyInfo - {@link X509.getSubjectPublicKeyInfoPosFromCertHex}</li>
* <li>subjectPublicKeyInfo - {@link X509.getPublicKeyFromCertPEM}</li>
* <li>signature algorithm - {@link X509.getSignatureAlgorithmName}</li>
* <li>signature value - {@link X509.getSignatureValueHex}</li>
* </ul>
* </li>
* <li><b>TO GET EXTENSIONS</b>
* <ul>
* <li>basicConstraints - {@link X509.getExtBasicConstraints}</li>
* <li>keyUsage - {@link X509.getExtKeyUsageBin}</li>
* <li>keyUsage - {@link X509.getExtKeyUsageString}</li>
* <li>subjectKeyIdentifier - {@link X509.getExtSubjectKeyIdentifier}</li>
* <li>authorityKeyIdentifier - {@link X509.getExtAuthorityKeyIdentifier}</li>
* <li>extKeyUsage - {@link X509.getExtExtKeyUsageName}</li>
* <li>subjectAltName - {@link X509.getExtSubjectAltName}</li>
* <li>cRLDistributionPoints - {@link X509.getExtCRLDistributionPointsURI}</li>
* <li>authorityInfoAccess - {@link X509.getExtAIAInfo}</li>
* </ul>
* </li>
* <li><b>UTILITIES</b>
* <ul>
* <li>reading PEM certificate - {@link X509#readCertPEM}</li>
* <li>get all certificate information - {@link X509#getInfo}</li>
* <li>get Base64 from PEM certificate - {@link X509.pemToBase64}</li>
* <li>get hexadecimal string from PEM certificate - {@link X509.pemToHex}</li>
* </ul>
* </li>
* </ul>
*/
function X509() {
this.subjectPublicKeyRSA = null;
this.subjectPublicKeyRSA_hN = null;
this.subjectPublicKeyRSA_hE = null;
this.hex = null;
// ===== get basic fields from hex =====================================
/**
* get hexadecimal string of serialNumber field of certificate.<br/>
* @name getSerialNumberHex
* @memberOf X509#
* @function
* @return {String} hexadecimal string of certificate serial number
* @example
* var x = new X509();
* x.readCertPEM(sCertPEM);
* var sn = x.getSerialNumberHex(); // return string like "01ad..."
*/
this.getSerialNumberHex = function() {
return ASN1HEX.getDecendantHexVByNthList(this.hex, 0, [0, 1]);
};
/**
* get signature algorithm name in basic field
* @name getSignatureAlgorithmField
* @memberOf X509#
* @function
* @return {String} signature algorithm name (ex. SHA1withRSA, SHA256withECDSA)
* @since x509 1.1.8
* @description
* This method will get a name of signature algorithm field of certificate:
* @example
* var x = new X509();
* x.readCertPEM(sCertPEM);
* algName = x.getSignatureAlgorithmField();
*/
this.getSignatureAlgorithmField = function() {
var sigAlgOidHex = ASN1HEX.getDecendantHexVByNthList(this.hex, 0, [0, 2, 0]);
var sigAlgOidInt = KJUR.asn1.ASN1Util.oidHexToInt(sigAlgOidHex);
var sigAlgName = KJUR.asn1.x509.OID.oid2name(sigAlgOidInt);
return sigAlgName;
};
/**
* get hexadecimal string of issuer field TLV of certificate.<br/>
* @name getIssuerHex
* @memberOf X509#
* @function
* @return {String} hexadecial string of issuer DN ASN.1
* @example
* var x = new X509();
* x.readCertPEM(sCertPEM);
* var issuer = x.getIssuerHex(); // return string like "3013..."
*/
this.getIssuerHex = function() {
return ASN1HEX.getDecendantHexTLVByNthList(this.hex, 0, [0, 3]);
};
/**
* get string of issuer field of certificate.<br/>
* @name getIssuerString
* @memberOf X509#
* @function
* @return {String} issuer DN string
* @example
* var x = new X509();
* x.readCertPEM(sCertPEM);
* var issuer = x.getIssuerString(); // return string like "/C=US/O=TEST"
*/
this.getIssuerString = function() {
return X509.hex2dn(ASN1HEX.getDecendantHexTLVByNthList(this.hex, 0, [0, 3]));
};
/**
* get hexadecimal string of subject field of certificate.<br/>
* @name getSubjectHex
* @memberOf X509#
* @function
* @return {String} hexadecial string of subject DN ASN.1
* @example
* var x = new X509();
* x.readCertPEM(sCertPEM);
* var subject = x.getSubjectHex(); // return string like "3013..."
*/
this.getSubjectHex = function() {
return ASN1HEX.getDecendantHexTLVByNthList(this.hex, 0, [0, 5]);
};
/**
* get string of subject field of certificate.<br/>
* @name getSubjectString
* @memberOf X509#
* @function
* @return {String} subject DN string
* @example
* var x = new X509();
* x.readCertPEM(sCertPEM);
* var subject = x.getSubjectString(); // return string like "/C=US/O=TEST"
*/
this.getSubjectString = function() {
return X509.hex2dn(ASN1HEX.getDecendantHexTLVByNthList(this.hex, 0, [0, 5]));
};
/**
* get notBefore field string of certificate.<br/>
* @name getNotBefore
* @memberOf X509#
* @function
* @return {String} not before time value (ex. "151231235959Z")
* @example
* var x = new X509();
* x.readCertPEM(sCertPEM);
* var notBefore = x.getNotBefore(); // return string like "151231235959Z"
*/
this.getNotBefore = function() {
var s = ASN1HEX.getDecendantHexVByNthList(this.hex, 0, [0, 4, 0]);
s = s.replace(/(..)/g, "%$1");
s = decodeURIComponent(s);
return s;
};
/**
* get notAfter field string of certificate.<br/>
* @name getNotAfter
* @memberOf X509#
* @function
* @return {String} not after time value (ex. "151231235959Z")
* @example
* var x = new X509();
* x.readCertPEM(sCertPEM);
* var notAfter = x.getNotAfter(); // return string like "151231235959Z"
*/
this.getNotAfter = function() {
var s = ASN1HEX.getDecendantHexVByNthList(this.hex, 0, [0, 4, 1]);
s = s.replace(/(..)/g, "%$1");
s = decodeURIComponent(s);
return s;
};
// ===== read certificate public key ==========================
// ===== read certificate =====================================
/**
* read PEM formatted X.509 certificate from string.<br/>
* @name readCertPEM
* @memberOf X509#
* @function
* @param {String} sCertPEM string for PEM formatted X.509 certificate
* @example
* x = new X509();
* x.readCertPEM(sCertPEM); // read certificate
*/
this.readCertPEM = function(sCertPEM) {
var hCert = X509.pemToHex(sCertPEM);
var a = X509.getPublicKeyHexArrayFromCertHex(hCert);
var rsa = new RSAKey();
rsa.setPublic(a[0], a[1]);
this.subjectPublicKeyRSA = rsa;
this.subjectPublicKeyRSA_hN = a[0];
this.subjectPublicKeyRSA_hE = a[1];
this.hex = hCert;
};
this.readCertPEMWithoutRSAInit = function(sCertPEM) {
var hCert = X509.pemToHex(sCertPEM);
var a = X509.getPublicKeyHexArrayFromCertHex(hCert);
this.subjectPublicKeyRSA.setPublic(a[0], a[1]);
this.subjectPublicKeyRSA_hN = a[0];
this.subjectPublicKeyRSA_hE = a[1];
this.hex = hCert;
};
/**
* get certificate information as string.<br/>
* @name getInfo
* @memberOf X509#
* @function
* @return {String} certificate information string
* @since jsrsasign 5.0.10 x509 1.1.8
* @example
* x = new X509();
* x.readCertPEM(certPEM);
* console.log(x.getInfo());
* // this shows as following
* Basic Fields
* serial number: 02ac5c266a0b409b8f0b79f2ae462577
* signature algorithm: SHA1withRSA
* issuer: /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert High Assurance EV Root CA
* notBefore: 061110000000Z
* notAfter: 311110000000Z
* subject: /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert High Assurance EV Root CA
* subject public key info:
* key algorithm: RSA
* n=c6cce573e6fbd4bb...
* e=10001
* X509v3 Extensions:
* keyUsage CRITICAL:
* digitalSignature,keyCertSign,cRLSign
* basicConstraints CRITICAL:
* cA=true
* subjectKeyIdentifier :
* b13ec36903f8bf4701d498261a0802ef63642bc3
* authorityKeyIdentifier :
* kid=b13ec36903f8bf4701d498261a0802ef63642bc3
* signature algorithm: SHA1withRSA
* signature: 1c1a0697dcd79c9f...
*/
this.getInfo = function() {
var s = "Basic Fields\n";
s += " serial number: " + this.getSerialNumberHex() + "\n";
s += " signature algorithm: " + this.getSignatureAlgorithmField() + "\n";
s += " issuer: " + this.getIssuerString() + "\n";
s += " notBefore: " + this.getNotBefore() + "\n";
s += " notAfter: " + this.getNotAfter() + "\n";
s += " subject: " + this.getSubjectString() + "\n";
s += " subject public key info: " + "\n";
// subject public key info
var pSPKI = X509.getSubjectPublicKeyInfoPosFromCertHex(this.hex);
var hSPKI = ASN1HEX.getHexOfTLV_AtObj(this.hex, pSPKI);
var keyObj = KEYUTIL.getKey(hSPKI, null, "pkcs8pub");
//s += " " + JSON.stringify(keyObj) + "\n";
if (keyObj instanceof RSAKey) {
s += " key algorithm: RSA\n";
s += " n=" + keyObj.n.toString(16).substr(0, 16) + "...\n";
s += " e=" + keyObj.e.toString(16) + "\n";
}
s += "X509v3 Extensions:\n";
var aExt = X509.getV3ExtInfoListOfCertHex(this.hex);
for (var i = 0; i < aExt.length; i++) {
var info = aExt[i];
// show extension name and critical flag
var extName = KJUR.asn1.x509.OID.oid2name(info["oid"]);
if (extName === '') extName = info["oid"];
var critical = '';
if (info["critical"] === true) critical = "CRITICAL";
s += " " + extName + " " + critical + ":\n";
// show extension value if supported
if (extName === "basicConstraints") {
var bc = X509.getExtBasicConstraints(this.hex);
if (bc.cA === undefined) {
s += " {}\n";
} else {
s += " cA=true";
if (bc.pathLen !== undefined)
s += ", pathLen=" + bc.pathLen;
s += "\n";
}
} else if (extName === "keyUsage") {
s += " " + X509.getExtKeyUsageString(this.hex) + "\n";
} else if (extName === "subjectKeyIdentifier") {
s += " " + X509.getExtSubjectKeyIdentifier(this.hex) + "\n";
} else if (extName === "authorityKeyIdentifier") {
var akid = X509.getExtAuthorityKeyIdentifier(this.hex);
if (akid.kid !== undefined)
s += " kid=" + akid.kid + "\n";
} else if (extName === "extKeyUsage") {
var eku = X509.getExtExtKeyUsageName(this.hex);
s += " " + eku.join(", ") + "\n";
} else if (extName === "subjectAltName") {
var san = X509.getExtSubjectAltName(this.hex);
s += " " + san.join(", ") + "\n";
} else if (extName === "cRLDistributionPoints") {
var cdp = X509.getExtCRLDistributionPointsURI(this.hex);
s += " " + cdp + "\n";
} else if (extName === "authorityInfoAccess") {
var aia = X509.getExtAIAInfo(this.hex);
if (aia.ocsp !== undefined)
s += " ocsp: " + aia.ocsp.join(",") + "\n";
if (aia.caissuer !== undefined)
s += " caissuer: " + aia.caissuer.join(",") + "\n";
}
}
s += "signature algorithm: " + X509.getSignatureAlgorithmName(this.hex) + "\n";
s += "signature: " + X509.getSignatureValueHex(this.hex).substr(0, 16) + "...\n";
return s;
};
};
/**
* get Base64 string from PEM certificate string
* @name pemToBase64
* @memberOf X509
* @function
* @param {String} sCertPEM PEM formatted RSA/ECDSA/DSA X.509 certificate
* @return {String} Base64 string of PEM certificate
* @example
* b64 = X509.pemToBase64(certPEM);
*/
X509.pemToBase64 = function(sCertPEM) {
var s = sCertPEM;
s = s.replace("-----BEGIN CERTIFICATE-----", "");
s = s.replace("-----END CERTIFICATE-----", "");
s = s.replace(/[ \n]+/g, "");
return s;
};
/**
* get a hexa decimal string from PEM certificate string
* @name pemToHex
* @memberOf X509
* @function
* @param {String} sCertPEM PEM formatted RSA/ECDSA/DSA X.509 certificate
* @return {String} hexadecimal string of PEM certificate
* @example
* hex = X509.pemToHex(certPEM);
*/
X509.pemToHex = function(sCertPEM) {
var b64Cert = X509.pemToBase64(sCertPEM);
var hCert = b64tohex(b64Cert);
return hCert;
};
// NOTE: Without BITSTRING encapsulation.
X509.getSubjectPublicKeyPosFromCertHex = function(hCert) {
var pInfo = X509.getSubjectPublicKeyInfoPosFromCertHex(hCert);
if (pInfo == -1) return -1;
var a = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, pInfo);
if (a.length != 2) return -1;
var pBitString = a[1];
if (hCert.substring(pBitString, pBitString + 2) != '03') return -1;
var pBitStringV = ASN1HEX.getStartPosOfV_AtObj(hCert, pBitString);
if (hCert.substring(pBitStringV, pBitStringV + 2) != '00') return -1;
return pBitStringV + 2;
};
// NOTE: privateKeyUsagePeriod field of X509v2 not supported.
// NOTE: v1 and v3 supported
X509.getSubjectPublicKeyInfoPosFromCertHex = function(hCert) {
var pTbsCert = ASN1HEX.getStartPosOfV_AtObj(hCert, 0);
var a = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, pTbsCert);
if (a.length < 1) return -1;
if (hCert.substring(a[0], a[0] + 10) == "a003020102") { // v3
if (a.length < 6) return -1;
return a[6];
} else {
if (a.length < 5) return -1;
return a[5];
}
};
X509.getPublicKeyHexArrayFromCertHex = function(hCert) {
var p = X509.getSubjectPublicKeyPosFromCertHex(hCert);
var a = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, p);
if (a.length != 2) return [];
var hN = ASN1HEX.getHexOfV_AtObj(hCert, a[0]);
var hE = ASN1HEX.getHexOfV_AtObj(hCert, a[1]);
if (hN != null && hE != null) {
return [hN, hE];
} else {
return [];
}
};
X509.getHexTbsCertificateFromCert = function(hCert) {
var pTbsCert = ASN1HEX.getStartPosOfV_AtObj(hCert, 0);
return pTbsCert;
};
X509.getPublicKeyHexArrayFromCertPEM = function(sCertPEM) {
var hCert = X509.pemToHex(sCertPEM);
var a = X509.getPublicKeyHexArrayFromCertHex(hCert);
return a;
};
X509.hex2dn = function(hDN) {
var s = "";
var a = ASN1HEX.getPosArrayOfChildren_AtObj(hDN, 0);
for (var i = 0; i < a.length; i++) {
var hRDN = ASN1HEX.getHexOfTLV_AtObj(hDN, a[i]);
s = s + "/" + X509.hex2rdn(hRDN);
}
return s;
};
X509.hex2rdn = function(hRDN) {
var hType = ASN1HEX.getDecendantHexTLVByNthList(hRDN, 0, [0, 0]);
var hValue = ASN1HEX.getDecendantHexVByNthList(hRDN, 0, [0, 1]);
var type = "";
try { type = X509.DN_ATTRHEX[hType]; } catch (ex) { type = hType; }
hValue = hValue.replace(/(..)/g, "%$1");
var value = decodeURIComponent(hValue);
return type + "=" + value;
};
X509.DN_ATTRHEX = {
"0603550406": "C",
"060355040a": "O",
"060355040b": "OU",
"0603550403": "CN",
"0603550405": "SN",
"0603550408": "ST",
"0603550407": "L",
"0603550409": "streetAddress",
"060355040f": "businessCategory",
"0603550411": "postalCode",
"060b2b0601040182373c020102": "jurisdictionOfIncorporationSP",
"060b2b0601040182373c020103": "jurisdictionOfIncorporationC",
};
/**
* get RSAKey/ECDSA public key object from PEM certificate string
* @name getPublicKeyFromCertPEM
* @memberOf X509
* @function
* @param {String} sCertPEM PEM formatted RSA/ECDSA/DSA X.509 certificate
* @return returns RSAKey/KJUR.crypto.{ECDSA,DSA} object of public key
* @since x509 1.1.1
* @description
* NOTE: DSA is also supported since x509 1.1.2.
*/
X509.getPublicKeyFromCertPEM = function(sCertPEM) {
var info = X509.getPublicKeyInfoPropOfCertPEM(sCertPEM);
if (info.algoid == "2a864886f70d010101") { // RSA
var aRSA = KEYUTIL.parsePublicRawRSAKeyHex(info.keyhex);
var key = new RSAKey();
key.setPublic(aRSA.n, aRSA.e);
return key;
} else if (info.algoid == "2a8648ce3d0201") { // ECC
var curveName = KJUR.crypto.OID.oidhex2name[info.algparam];
var key = new KJUR.crypto.ECDSA({'curve': curveName, 'info': info.keyhex});
key.setPublicKeyHex(info.keyhex);
return key;
} else if (info.algoid == "2a8648ce380401") { // DSA 1.2.840.10040.4.1
var p = ASN1HEX.getVbyList(info.algparam, 0, [0], "02");
var q = ASN1HEX.getVbyList(info.algparam, 0, [1], "02");
var g = ASN1HEX.getVbyList(info.algparam, 0, [2], "02");
var y = ASN1HEX.getHexOfV_AtObj(info.keyhex, 0);
y = y.substr(2);
var key = new KJUR.crypto.DSA();
key.setPublic(new BigInteger(p, 16),
new BigInteger(q, 16),
new BigInteger(g, 16),
new BigInteger(y, 16));
return key;
} else {
throw "unsupported key";
}
};
/**
* get public key information from PEM certificate
* @name getPublicKeyInfoPropOfCertPEM
* @memberOf X509
* @function
* @param {String} sCertPEM string of PEM formatted certificate
* @return {Hash} hash of information for public key
* @since x509 1.1.1
* @description
* Resulted associative array has following properties:<br/>
* <ul>
* <li>algoid - hexadecimal string of OID of asymmetric key algorithm</li>
* <li>algparam - hexadecimal string of OID of ECC curve name or null</li>
* <li>keyhex - hexadecimal string of key in the certificate</li>
* </ul>
* NOTE: X509v1 certificate is also supported since x509.js 1.1.9.
*/
X509.getPublicKeyInfoPropOfCertPEM = function(sCertPEM) {
var result = {};
result.algparam = null;
var hCert = X509.pemToHex(sCertPEM);
// 1. Certificate ASN.1
var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, 0);
if (a1.length != 3)
throw "malformed X.509 certificate PEM (code:001)"; // not 3 item of seq Cert
// 2. tbsCertificate
if (hCert.substr(a1[0], 2) != "30")
throw "malformed X.509 certificate PEM (code:002)"; // tbsCert not seq
var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, a1[0]);
// 3. subjectPublicKeyInfo
var idx_spi = 6; // subjectPublicKeyInfo index in tbsCert for v3 cert
if (hCert.substr(a2[0], 2) !== "a0") idx_spi = 5;
if (a2.length < idx_spi + 1)
throw "malformed X.509 certificate PEM (code:003)"; // no subjPubKeyInfo
var a3 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, a2[idx_spi]);
if (a3.length != 2)
throw "malformed X.509 certificate PEM (code:004)"; // not AlgId and PubKey
// 4. AlgId
var a4 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, a3[0]);
if (a4.length != 2)
throw "malformed X.509 certificate PEM (code:005)"; // not 2 item in AlgId
result.algoid = ASN1HEX.getHexOfV_AtObj(hCert, a4[0]);
if (hCert.substr(a4[1], 2) == "06") { // EC
result.algparam = ASN1HEX.getHexOfV_AtObj(hCert, a4[1]);
} else if (hCert.substr(a4[1], 2) == "30") { // DSA
result.algparam = ASN1HEX.getHexOfTLV_AtObj(hCert, a4[1]);
}
// 5. Public Key Hex
if (hCert.substr(a3[1], 2) != "03")
throw "malformed X.509 certificate PEM (code:006)"; // not bitstring
var unusedBitAndKeyHex = ASN1HEX.getHexOfV_AtObj(hCert, a3[1]);
result.keyhex = unusedBitAndKeyHex.substr(2);
return result;
};
/**
* get position of subjectPublicKeyInfo field from HEX certificate
* @name getPublicKeyInfoPosOfCertHEX
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of certificate
* @return {Integer} position in hexadecimal string
* @since x509 1.1.4
* @description
* get position for SubjectPublicKeyInfo field in the hexadecimal string of
* certificate.
*/
X509.getPublicKeyInfoPosOfCertHEX = function(hCert) {
// 1. Certificate ASN.1
var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, 0);
if (a1.length != 3)
throw "malformed X.509 certificate PEM (code:001)"; // not 3 item of seq Cert
// 2. tbsCertificate
if (hCert.substr(a1[0], 2) != "30")
throw "malformed X.509 certificate PEM (code:002)"; // tbsCert not seq
var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, a1[0]);
// 3. subjectPublicKeyInfo
if (a2.length < 7)
throw "malformed X.509 certificate PEM (code:003)"; // no subjPubKeyInfo
return a2[6];
};
/**
* get array of X.509 V3 extension value information in hex string of certificate
* @name getV3ExtInfoListOfCertHex
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @return {Array} array of result object by {@link X509.getV3ExtInfoListOfCertHex}
* @since x509 1.1.5
* @description
* This method will get all extension information of a X.509 certificate.
* Items of resulting array has following properties:
* <ul>
* <li>posTLV - index of ASN.1 TLV for the extension. same as 'pos' argument.</li>
* <li>oid - dot noted string of extension oid (ex. 2.5.29.14)</li>
* <li>critical - critical flag value for this extension</li>
* <li>posV - index of ASN.1 TLV for the extension value.
* This is a position of a content of ENCAPSULATED OCTET STRING.</li>
* </ul>
* @example
* hCert = X509.pemToHex(certGithubPEM);
* a = X509.getV3ExtInfoListOfCertHex(hCert);
* // Then a will be an array of like following:
* [{posTLV: 1952, oid: "2.5.29.35", critical: false, posV: 1968},
* {posTLV: 1974, oid: "2.5.29.19", critical: true, posV: 1986}, ...]
*/
X509.getV3ExtInfoListOfCertHex = function(hCert) {
// 1. Certificate ASN.1
var a1 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, 0);
if (a1.length != 3)
throw "malformed X.509 certificate PEM (code:001)"; // not 3 item of seq Cert
// 2. tbsCertificate
if (hCert.substr(a1[0], 2) != "30")
throw "malformed X.509 certificate PEM (code:002)"; // tbsCert not seq
var a2 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, a1[0]);
// 3. v3Extension EXPLICIT Tag [3]
// ver, seri, alg, iss, validity, subj, spki, (iui,) (sui,) ext
if (a2.length < 8)
throw "malformed X.509 certificate PEM (code:003)"; // tbsCert num field too short
if (hCert.substr(a2[7], 2) != "a3")
throw "malformed X.509 certificate PEM (code:004)"; // not [3] tag
var a3 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, a2[7]);
if (a3.length != 1)
throw "malformed X.509 certificate PEM (code:005)"; // [3]tag numChild!=1
// 4. v3Extension SEQUENCE
if (hCert.substr(a3[0], 2) != "30")
throw "malformed X.509 certificate PEM (code:006)"; // not SEQ
var a4 = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, a3[0]);
// 5. v3Extension item position
var numExt = a4.length;
var aInfo = new Array(numExt);
for (var i = 0; i < numExt; i++) {
aInfo[i] = X509.getV3ExtItemInfo_AtObj(hCert, a4[i]);
}
return aInfo;
};
/**
* get X.509 V3 extension value information at the specified position
* @name getV3ExtItemInfo_AtObj
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @param {Integer} pos index of hexadecimal string for the extension
* @return {Object} properties for the extension
* @since x509 1.1.5
* @description
* This method will get some information of a X.509 V extension
* which is referred by an index of hexadecimal string of X.509
* certificate.
* Resulting object has following properties:
* <ul>
* <li>posTLV - index of ASN.1 TLV for the extension. same as 'pos' argument.</li>
* <li>oid - dot noted string of extension oid (ex. 2.5.29.14)</li>
* <li>critical - critical flag value for this extension</li>
* <li>posV - index of ASN.1 TLV for the extension value.
* This is a position of a content of ENCAPSULATED OCTET STRING.</li>
* </ul>
* This method is used by {@link X509.getV3ExtInfoListOfCertHex} internally.
*/
X509.getV3ExtItemInfo_AtObj = function(hCert, pos) {
var info = {};
// posTLV - extension TLV
info.posTLV = pos;
var a = ASN1HEX.getPosArrayOfChildren_AtObj(hCert, pos);
if (a.length != 2 && a.length != 3)
throw "malformed X.509v3 Ext (code:001)"; // oid,(critical,)val
// oid - extension OID
if (hCert.substr(a[0], 2) != "06")
throw "malformed X.509v3 Ext (code:002)"; // not OID "06"
var valueHex = ASN1HEX.getHexOfV_AtObj(hCert, a[0]);
info.oid = ASN1HEX.hextooidstr(valueHex);
// critical - extension critical flag
info.critical = false; // critical false by default
if (a.length == 3) info.critical = true;
// posV - content TLV position of encapsulated
// octet string of V3 extension value.
var posExtV = a[a.length - 1];
if (hCert.substr(posExtV, 2) != "04")
throw "malformed X.509v3 Ext (code:003)"; // not EncapOctet "04"
info.posV = ASN1HEX.getStartPosOfV_AtObj(hCert, posExtV);
return info;
};
/**
* get X.509 V3 extension value ASN.1 TLV for specified oid or name
* @name getHexOfTLV_V3ExtValue
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @param {String} oidOrName oid or name for extension (ex. 'keyUsage' or '2.5.29.15')
* @return {String} hexadecimal string of extension ASN.1 TLV
* @since x509 1.1.6
* @description
* This method will get X.509v3 extension value of ASN.1 TLV
* which is specifyed by extension name or oid.
* If there is no such extension in the certificate, it returns null.
* @example
* hExtValue = X509.getHexOfTLV_V3ExtValue(hCert, "keyUsage");
* // hExtValue will be such like '030205a0'.
*/
X509.getHexOfTLV_V3ExtValue = function(hCert, oidOrName) {
var pos = X509.getPosOfTLV_V3ExtValue(hCert, oidOrName);
if (pos == -1) return null;
return ASN1HEX.getHexOfTLV_AtObj(hCert, pos);
};
/**
* get X.509 V3 extension value ASN.1 V for specified oid or name
* @name getHexOfV_V3ExtValue
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @param {String} oidOrName oid or name for extension (ex. 'keyUsage' or '2.5.29.15')
* @return {String} hexadecimal string of extension ASN.1 TLV
* @since x509 1.1.6
* @description
* This method will get X.509v3 extension value of ASN.1 value
* which is specifyed by extension name or oid.
* If there is no such extension in the certificate, it returns null.
* Available extension names and oids are defined
* in the {@link KJUR.asn1.x509.OID} class.
* @example
* hExtValue = X509.getHexOfV_V3ExtValue(hCert, "keyUsage");
* // hExtValue will be such like '05a0'.
*/
X509.getHexOfV_V3ExtValue = function(hCert, oidOrName) {
var pos = X509.getPosOfTLV_V3ExtValue(hCert, oidOrName);
if (pos == -1) return null;
return ASN1HEX.getHexOfV_AtObj(hCert, pos);
};
/**
* get index in the certificate hexa string for specified oid or name specified extension
* @name getPosOfTLV_V3ExtValue
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @param {String} oidOrName oid or name for extension (ex. 'keyUsage' or '2.5.29.15')
* @return {Integer} index in the hexadecimal string of certficate for specified extension
* @since x509 1.1.6
* @description
* This method will get X.509v3 extension value of ASN.1 V(value)
* which is specifyed by extension name or oid.
* If there is no such extension in the certificate,
* it returns -1.
* Available extension names and oids are defined
* in the {@link KJUR.asn1.x509.OID} class.
* @example
* idx = X509.getPosOfV_V3ExtValue(hCert, "keyUsage");
* // The 'idx' will be index in the string for keyUsage value ASN.1 TLV.
*/
X509.getPosOfTLV_V3ExtValue = function(hCert, oidOrName) {
var oid = oidOrName;
if (! oidOrName.match(/^[0-9.]+$/)) oid = KJUR.asn1.x509.OID.name2oid(oidOrName);
if (oid == '') return -1;
var infoList = X509.getV3ExtInfoListOfCertHex(hCert);
for (var i = 0; i < infoList.length; i++) {
var info = infoList[i];
if (info.oid == oid) return info.posV;
}
return -1;
};
/* ======================================================================
* Specific V3 Extensions
* ====================================================================== */
/**
* get BasicConstraints extension value as object in the certificate
* @name getExtBasicConstraints
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @return {Object} associative array which may have "cA" and "pathLen" parameters
* @since x509 1.1.7
* @description
* This method will get basic constraints extension value as object with following paramters.
* <ul>
* <li>cA - CA flag whether CA or not</li>
* <li>pathLen - maximum intermediate certificate length</li>
* </ul>
* There are use cases for return values:
* <ul>
* <li>{cA:true, pathLen:3} - cA flag is true and pathLen is 3</li>
* <li>{cA:true} - cA flag is true and no pathLen</li>
* <li>{} - basic constraints has no value in case of end entity certificate</li>
* <li>null - there is no basic constraints extension</li>
* </ul>
* @example
* obj = X509.getExtBasicConstraints(hCert);
*/
X509.getExtBasicConstraints = function(hCert) {
var hBC = X509.getHexOfV_V3ExtValue(hCert, "basicConstraints");
if (hBC === null) return null;
if (hBC === '') return {};
if (hBC === '0101ff') return { "cA": true };
if (hBC.substr(0, 8) === '0101ff02') {
var pathLexHex = ASN1HEX.getHexOfV_AtObj(hBC, 6);
var pathLen = parseInt(pathLexHex, 16);
return { "cA": true, "pathLen": pathLen };
}
throw "unknown error";
};
X509.KEYUSAGE_NAME = [
"digitalSignature",
"nonRepudiation",
"keyEncipherment",
"dataEncipherment",
"keyAgreement",
"keyCertSign",
"cRLSign",
"encipherOnly",
"decipherOnly"
];
/**
* get KeyUsage extension value as binary string in the certificate
* @name getExtKeyUsageBin
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @return {String} binary string of key usage bits (ex. '101')
* @since x509 1.1.6
* @description
* This method will get key usage extension value
* as binary string such like '101'.
* Key usage bits definition is in the RFC 5280.
* If there is no key usage extension in the certificate,
* it returns empty string (i.e. '').
* @example
* bKeyUsage = X509.getExtKeyUsageBin(hCert);
* // bKeyUsage will be such like '101'.
* // 1 - digitalSignature
* // 0 - nonRepudiation
* // 1 - keyEncipherment
*/
X509.getExtKeyUsageBin = function(hCert) {
var hKeyUsage = X509.getHexOfV_V3ExtValue(hCert, "keyUsage");
if (hKeyUsage == '') return '';
if (hKeyUsage.length % 2 != 0 || hKeyUsage.length <= 2)
throw "malformed key usage value";
var unusedBits = parseInt(hKeyUsage.substr(0, 2));
var bKeyUsage = parseInt(hKeyUsage.substr(2), 16).toString(2);
return bKeyUsage.substr(0, bKeyUsage.length - unusedBits);
};
/**
* get KeyUsage extension value as names in the certificate
* @name getExtKeyUsageString
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @return {String} comma separated string of key usage
* @since x509 1.1.6
* @description
* This method will get key usage extension value
* as comma separated string of usage names.
* If there is no key usage extension in the certificate,
* it returns empty string (i.e. '').
* @example
* sKeyUsage = X509.getExtKeyUsageString(hCert);
* // sKeyUsage will be such like 'digitalSignature,keyEncipherment'.
*/
X509.getExtKeyUsageString = function(hCert) {
var bKeyUsage = X509.getExtKeyUsageBin(hCert);
var a = new Array();
for (var i = 0; i < bKeyUsage.length; i++) {
if (bKeyUsage.substr(i, 1) == "1") a.push(X509.KEYUSAGE_NAME[i]);
}
return a.join(",");
};
/**
* get subjectKeyIdentifier value as hexadecimal string in the certificate
* @name getExtSubjectKeyIdentifier
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @return {String} hexadecimal string of subject key identifier or null
* @since jsrsasign 5.0.10 x509 1.1.8
* @description
* This method will get subject key identifier extension value
* as hexadecimal string.
* If there is no its extension in the certificate,
* it returns null.
* @example
* skid = X509.getExtSubjectKeyIdentifier(hCert);
*/
X509.getExtSubjectKeyIdentifier = function(hCert) {
var hSKID = X509.getHexOfV_V3ExtValue(hCert, "subjectKeyIdentifier");
return hSKID;
};
/**
* get authorityKeyIdentifier value as JSON object in the certificate
* @name getExtAuthorityKeyIdentifier
* @memberOf X509
* @function
* @param {String} hCert hexadecimal string of X.509 certificate binary
* @return {Object} JSON object of authority key identifier or null
* @since jsrsasign 5.0.10 x509 1.1.8
* @description
* This method will get authority key identifier extension value
* as JSON object.
* If there is no its extension in the certificate,
* it returns null.
* <br>
* NOTE: Currently this method only supports keyIdentifier so that
* authorityCertIssuer and authorityCertSerialNumber will not
* be return in the JSON object.
* @example
* akid = X509.getExtAuthorityKeyIdentifier(hCert);
* // returns following JSON object
* { kid: "1234abcd..." }
*/
X509.getExtAuthorityKeyIdentifier = function(hCert) {
var result = {};
var hAKID = X509.getHexOfTLV_V3ExtValue(hCert, "authorityKeyIdentifier");
if (hAKID === null) return null;
var a = ASN1HEX.getPosArrayOfChildren_AtObj(hAKID, 0);
for (var i = 0; i < a.length; i++) {
if (hAKID.substr(a[i], 2) === "80")
result.kid = ASN1HEX.getHexOfV_AtObj(hAKID, a[i]);
}