-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
certs.js
1456 lines (1319 loc) · 43 KB
/
certs.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
/**
* Detects iOS developer and distribution certificates and the WWDC certificate.
*
* @module certs
*
* @copyright
* Copyright (c) 2014-2016 by Appcelerator, Inc. All Rights Reserved.
*
* Copyright (c) 2010-2014 Digital Bazaar, Inc.
* {@link https://github.com/digitalbazaar/forge}
*
* @license
* Licensed under the terms of the Apache Public License.
* Please see the LICENSE included with this distribution for details.
*/
const
appc = require('node-appc'),
async = require('async'),
env = require('./env'),
magik = require('./utilities').magik,
__ = appc.i18n(__dirname).__;
const certRegExp = /^(?:((?:Apple|iOS) Development)|((?:iOS|Apple|iPhone) Distribution)): (.+)$/;
var cache = null,
watchers = {},
watchResults = null,
watchInterval = 60000,
watchTimer = null;
exports.detect = detect;
exports.watch = watch;
exports.unwatch = unwatch;
/**
* Detects installed certificates.
*
* @param {Object} [options] - An object containing various settings.
* @param {Boolean} [options.bypassCache=false] - When true, re-detects all certificates.
* @param {Boolean} [options.validOnly=true] - When true, only returns non-expired, valid certificates.
* @param {Function} [callback(err, results)] - A function to call with the certificate information.
*
* @emits module:certs#detected
* @emits module:certs#error
*
* @returns {Handle}
*/
function detect(options, callback) {
return magik(options, callback, function (emitter, options, callback) {
var validOnly = options.validOnly === undefined || options.validOnly === true;
if (cache && !options.bypassCache) {
emitter.emit('detected', cache);
return callback(null, cache);
}
function getCerts(cb) {
// detect the development environment
env.detect(options, function (err, env) {
var results = {
certs: {
keychains: {},
wwdr: false
},
issues: []
};
// if we don't have the security executable, we cannot detect certs
if (!env.executables.security) {
return cb(null, results);
}
appc.subprocess.run(env.executables.security, 'list-keychains', function (code, out, err) {
if (code) {
return cb(results);
}
function parseCerts(src, dest, prefix) {
var p = 0,
q = src.indexOf('-----END'),
pem, cert, validity, expired, invalid, commonName;
while (p !== -1 && q !== -1) {
pem = src.substring(p, q + 25);
cert = pem2cert(pem);
expired = cert.validity.notAfter < now,
invalid = expired || cert.validity.notBefore > now;
commonName = cert.subject.getField('CN').value;
let certName;
if (!prefix) {
const fullname = appc.encoding.decodeOctalUTF8(commonName);
if (fullname === 'Apple Worldwide Developer Relations Certification Authority') {
certName = commonName;
} else {
const match = fullname.match(certRegExp);
if (match) {
certName = match[3]
}
}
} else {
certName = appc.encoding.decodeOctalUTF8(commonName.substring(prefix.length)).trim();
}
if (!validOnly || !invalid) {
const teamId = cert.subject.attributes.find(attr => attr.name === 'organizationalUnitName');
dest.push({
name: certName,
fullname: appc.encoding.decodeOctalUTF8(commonName).trim(),
pem: pem,
before: cert.validity.notBefore,
after: cert.validity.notAfter,
expired: expired,
invalid: invalid,
teamId: teamId && teamId.value
});
}
p = src.indexOf('-----BEGIN', q + 25);
q = src.indexOf('-----END', p);
}
}
var now = new Date,
tasks = [];
// parse out the keychains and add tasks to find certs for each keychain
out.split('\n').forEach(function (line) {
var m = line.match(/[^"]*"([^"]*)"/);
if (!m) return;
var keychain = m[1].trim(),
dest = results.certs.keychains[keychain] = {
developer: [],
distribution: []
};
// find all the developer certificates in this keychain
tasks.push(function (next) {
appc.subprocess.run(env.executables.security, ['find-certificate', '-c', 'iPhone Developer:', '-a', '-p', keychain], function (code, out, err) {
if (!code) {
parseCerts(out, dest.developer, 'iPhone Developer:');
}
next();
});
});
// find all the developer certificates in this keychain
tasks.push(function (next) {
appc.subprocess.run(env.executables.security, ['find-certificate', '-c', 'Development:', '-a', '-p', keychain], function (code, out, err) {
if (!code) {
parseCerts(out, dest.developer);
}
next();
});
});
// find all the distribution certificates in this keychain
tasks.push(function (next) {
appc.subprocess.run(env.executables.security, ['find-certificate', '-c', 'Distribution:', '-a', '-p', keychain], function (code, out, err) {
if (!code) {
parseCerts(out, dest.distribution);
}
next();
});
});
// find all the wwdr certificates in this keychain
tasks.push(function (next) {
// if we already found it, then skip the remaining keychains
if (results.certs.wwdr) return next();
appc.subprocess.run(env.executables.security, ['find-certificate', '-c', 'Apple Worldwide Developer Relations Certification Authority', '-a', '-p', keychain], function (code, out, err) {
if (!code) {
var tmp = [];
parseCerts(out, tmp);
results.certs.wwdr = results.certs.wwdr || (tmp.length && tmp[0].invalid === false);
}
next();
});
});
});
// process all cert tasks
async.parallel(tasks, function () {
cb(results);
});
});
});
}
// get all keychains and certs
getCerts(function (results) {
detectIssues(results);
cache = results;
emitter.emit('detected', results);
callback(null, results);
});
});
};
function detectIssues(dest) {
dest.issues = [];
if (!dest.certs.wwdr) {
dest.issues.push({
id: 'IOS_NO_WWDR_CERT_FOUND',
type: 'error',
message: __('Apple’s World Wide Developer Relations (WWDR) intermediate certificate is not installed.') + '\n' +
__('This will prevent you from building apps for iOS devices or package for distribution.')
});
}
if (!Object.keys(dest.certs.keychains).length) {
// I don't think this is even possible
dest.issues.push({
id: 'IOS_NO_KEYCHAINS_FOUND',
type: 'warning',
message: __('Unable to find any keychains found.')
});
}
var validDevCerts = 0,
validDistCerts = 0;
Object.keys(dest.certs.keychains).forEach(function (keychain) {
validDevCerts += (dest.certs.keychains[keychain].developer || []).filter(function (c) {
return !c.invalid;
}).length;
validDistCerts += (dest.certs.keychains[keychain].distribution || []).filter(function (c) {
return !c.invalid;
}).length;
});
if (!validDevCerts) {
dest.issues.push({
id: 'IOS_NO_VALID_DEV_CERTS_FOUND',
type: 'warning',
message: __('Unable to find any valid iOS developer certificates.') + '\n' +
__('This will prevent you from building apps for iOS devices.')
});
}
if (!validDistCerts) {
dest.issues.push({
id: 'IOS_NO_VALID_DIST_CERTS_FOUND',
type: 'warning',
message: __('Unable to find any valid iOS production distribution certificates.') + '\n' +
__('This will prevent you from packaging apps for distribution.')
});
}
}
/**
* Watches for new and changed certificates.
*
* @param {Object} [options] - An object containing various settings
* @param {Boolean} [options.watchInterval=60000] - The number of milliseconds to wait before checking for cert updates
* @param {Function} [callback(err, results)] - A function to call with the certificate information
*
* @returns {Function} A function that unwatches changes.
*/
function watch(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
} else if (!options) {
options = {};
}
watchers[callback] = (watchers[callback] || 0) + 1;
watchInterval = ~~options.watchInterval || 60000;
// check if already watching or already watching
if (watchers[callback] === 1 && !watchTimer) {
options.bypassCache = true;
function check() {
detect(options, function (err, results) {
if (!err && (!watchResults || JSON.stringify(watchResults) !== JSON.stringify(results))) {
watchResults = results;
return callback(null, results);
}
watchTimer = setTimeout(check, watchInterval);
});
}
watchTimer = setTimeout(check, watchInterval);
}
return function () {
unwatch(callback);
};
};
/**
* Stops watching for certificate changes.
*/
function unwatch(callback) {
if (!watchers[callback]) return;
if (--watchers[callback] <= 0) {
delete watchers[callback];
}
if (!Object.keys(watchers).length) {
clearTimeout(watchTimer);
watchTimer = null;
}
};
/*
* Everything from this point onward is from the forge project (aka node-forge).
* https://github.com/digitalbazaar/forge
*
* New BSD License (3-clause)
* Copyright (c) 2010, Digital Bazaar, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of Digital Bazaar, Inc. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 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 DIGITAL BAZAAR 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.
*/
var typeRegExp = /^(?:X509 |TRUSTED )?CERTIFICATE$/,
rMessage = /\s*-----BEGIN ([A-Z0-9- ]+)-----\r?\n?([\x21-\x7e\s]+?(?:\r?\n\r?\n))?([:A-Za-z0-9+\/=\s]+?)-----END \1-----/g,
rHeader = /([\x21-\x7e]+):\s*([\x21-\x7e\s^:]+)/,
rCRLF = /\r?\n/,
whitespaceRegExp = /\s/,
leadingSpaceRegExp = /^\s+/,
asn1Class = {
UNIVERSAL: 0x00,
APPLICATION: 0x40,
CONTEXT_SPECIFIC: 0x80,
PRIVATE: 0xC0
},
asn1Type = {
NONE: 0,
BOOLEAN: 1,
INTEGER: 2,
BITSTRING: 3,
OCTETSTRING: 4,
NULL: 5,
OID: 6,
ODESC: 7,
EXTERNAL: 8,
REAL: 9,
ENUMERATED: 10,
EMBEDDED: 11,
UTF8: 12,
ROID: 13,
SEQUENCE: 16,
SET: 17,
PRINTABLESTRING: 19,
IA5STRING: 22,
UTCTIME: 23,
GENERALIZEDTIME: 24,
BMPSTRING: 30
},
x509CertificateValidator = {
name: 'Certificate',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
value: [ {
name: 'Certificate.TBSCertificate',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
captureAsn1: 'tbsCertificate',
value: [ {
name: 'Certificate.TBSCertificate.version',
tagClass: asn1Class.CONTEXT_SPECIFIC,
type: 0,
constructed: true,
optional: true,
value: [ {
name: 'Certificate.TBSCertificate.version.integer',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.INTEGER,
constructed: false,
capture: 'certVersion'
} ]
}, {
name: 'Certificate.TBSCertificate.serialNumber',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.INTEGER,
constructed: false,
capture: 'certSerialNumber'
}, {
name: 'Certificate.TBSCertificate.signature',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
value: [ {
name: 'Certificate.TBSCertificate.signature.algorithm',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.OID,
constructed: false,
capture: 'certinfoSignatureOid'
}, {
name: 'Certificate.TBSCertificate.signature.parameters',
tagClass: asn1Class.UNIVERSAL,
optional: true,
captureAsn1: 'certinfoSignatureParams'
} ]
}, {
name: 'Certificate.TBSCertificate.issuer',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
captureAsn1: 'certIssuer'
}, {
name: 'Certificate.TBSCertificate.validity',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
// Note: UTC and generalized times may both appear so the capture
// names are based on their detected order, the names used below
// are only for the common case, which validity time really means
// "notBefore" and which means "notAfter" will be determined by order
value: [ {
// notBefore (Time) (UTC time case)
name: 'Certificate.TBSCertificate.validity.notBefore (utc)',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.UTCTIME,
constructed: false,
optional: true,
capture: 'certValidity1UTCTime'
}, {
// notBefore (Time) (generalized time case)
name: 'Certificate.TBSCertificate.validity.notBefore (generalized)',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.GENERALIZEDTIME,
constructed: false,
optional: true,
capture: 'certValidity2GeneralizedTime'
}, {
// notAfter (Time) (only UTC time is supported)
name: 'Certificate.TBSCertificate.validity.notAfter (utc)',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.UTCTIME,
constructed: false,
optional: true,
capture: 'certValidity3UTCTime'
}, {
// notAfter (Time) (only UTC time is supported)
name: 'Certificate.TBSCertificate.validity.notAfter (generalized)',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.GENERALIZEDTIME,
constructed: false,
optional: true,
capture: 'certValidity4GeneralizedTime'
} ]
}, {
// Name (subject) (RDNSequence)
name: 'Certificate.TBSCertificate.subject',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
captureAsn1: 'certSubject'
}, {
name: 'SubjectPublicKeyInfo',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
captureAsn1: 'subjectPublicKeyInfo',
value: [ {
name: 'SubjectPublicKeyInfo.AlgorithmIdentifier',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
value: [ {
name: 'AlgorithmIdentifier.algorithm',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.OID,
constructed: false,
capture: 'publicKeyOid'
} ]
}, {
// subjectPublicKey
name: 'SubjectPublicKeyInfo.subjectPublicKey',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.BITSTRING,
constructed: false,
value: [ {
// RSAPublicKey
name: 'SubjectPublicKeyInfo.subjectPublicKey.RSAPublicKey',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
optional: true,
captureAsn1: 'rsaPublicKey'
} ]
} ]
}, {
// issuerUniqueID (optional)
name: 'Certificate.TBSCertificate.issuerUniqueID',
tagClass: asn1Class.CONTEXT_SPECIFIC,
type: 1,
constructed: true,
optional: true,
value: [ {
name: 'Certificate.TBSCertificate.issuerUniqueID.id',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.BITSTRING,
constructed: false,
capture: 'certIssuerUniqueId'
} ]
}, {
// subjectUniqueID (optional)
name: 'Certificate.TBSCertificate.subjectUniqueID',
tagClass: asn1Class.CONTEXT_SPECIFIC,
type: 2,
constructed: true,
optional: true,
value: [ {
name: 'Certificate.TBSCertificate.subjectUniqueID.id',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.BITSTRING,
constructed: false,
capture: 'certSubjectUniqueId'
} ]
}, {
// Extensions (optional)
name: 'Certificate.TBSCertificate.extensions',
tagClass: asn1Class.CONTEXT_SPECIFIC,
type: 3,
constructed: true,
captureAsn1: 'certExtensions',
optional: true
} ]
}, {
// AlgorithmIdentifier (signature algorithm)
name: 'Certificate.signatureAlgorithm',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.SEQUENCE,
constructed: true,
value: [ {
// algorithm
name: 'Certificate.signatureAlgorithm.algorithm',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.OID,
constructed: false,
capture: 'certSignatureOid'
}, {
name: 'Certificate.TBSCertificate.signature.parameters',
tagClass: asn1Class.UNIVERSAL,
optional: true,
captureAsn1: 'certSignatureParams'
} ]
}, {
// SignatureValue
name: 'Certificate.signatureValue',
tagClass: asn1Class.UNIVERSAL,
type: asn1Type.BITSTRING,
constructed: false,
capture: 'certSignature'
} ]
},
oids = {
// algorithm OIDs
'1.2.840.113549.1.1.1': 'rsaEncryption',
'rsaEncryption': '1.2.840.113549.1.1.1',
// Note: md2 & md4 not implemented
//'1.2.840.113549.1.1.2': 'md2WithRSAEncryption',
//'md2WithRSAEncryption': '1.2.840.113549.1.1.2',
//'1.2.840.113549.1.1.3': 'md4WithRSAEncryption',
//'md4WithRSAEncryption': '1.2.840.113549.1.1.3',
'1.2.840.113549.1.1.4': 'md5WithRSAEncryption',
'md5WithRSAEncryption': '1.2.840.113549.1.1.4',
'1.2.840.113549.1.1.5': 'sha1WithRSAEncryption',
'sha1WithRSAEncryption': '1.2.840.113549.1.1.5',
'1.2.840.113549.1.1.7': 'RSAES-OAEP',
'RSAES-OAEP': '1.2.840.113549.1.1.7',
'1.2.840.113549.1.1.8': 'mgf1',
'mgf1': '1.2.840.113549.1.1.8',
'1.2.840.113549.1.1.9': 'pSpecified',
'pSpecified': '1.2.840.113549.1.1.9',
'1.2.840.113549.1.1.10': 'RSASSA-PSS',
'RSASSA-PSS': '1.2.840.113549.1.1.10',
'1.2.840.113549.1.1.11': 'sha256WithRSAEncryption',
'sha256WithRSAEncryption': '1.2.840.113549.1.1.11',
'1.2.840.113549.1.1.12': 'sha384WithRSAEncryption',
'sha384WithRSAEncryption': '1.2.840.113549.1.1.12',
'1.2.840.113549.1.1.13': 'sha512WithRSAEncryption',
'sha512WithRSAEncryption': '1.2.840.113549.1.1.13',
'1.3.14.3.2.7': 'desCBC',
'desCBC': '1.3.14.3.2.7',
'1.3.14.3.2.26': 'sha1',
'sha1': '1.3.14.3.2.26',
'2.16.840.1.101.3.4.2.1': 'sha256',
'sha256': '2.16.840.1.101.3.4.2.1',
'2.16.840.1.101.3.4.2.2': 'sha384',
'sha384': '2.16.840.1.101.3.4.2.2',
'2.16.840.1.101.3.4.2.3': 'sha512',
'sha512': '2.16.840.1.101.3.4.2.3',
'1.2.840.113549.2.5': 'md5',
'md5': '1.2.840.113549.2.5',
// pkcs#7 content types
'1.2.840.113549.1.7.1': 'data',
'data': '1.2.840.113549.1.7.1',
'1.2.840.113549.1.7.2': 'signedData',
'signedData': '1.2.840.113549.1.7.2',
'1.2.840.113549.1.7.3': 'envelopedData',
'envelopedData': '1.2.840.113549.1.7.3',
'1.2.840.113549.1.7.4': 'signedAndEnvelopedData',
'signedAndEnvelopedData': '1.2.840.113549.1.7.4',
'1.2.840.113549.1.7.5': 'digestedData',
'digestedData': '1.2.840.113549.1.7.5',
'1.2.840.113549.1.7.6': 'encryptedData',
'encryptedData': '1.2.840.113549.1.7.6',
// pkcs#9 oids
'1.2.840.113549.1.9.1': 'emailAddress',
'emailAddress': '1.2.840.113549.1.9.1',
'1.2.840.113549.1.9.2': 'unstructuredName',
'unstructuredName': '1.2.840.113549.1.9.2',
'1.2.840.113549.1.9.3': 'contentType',
'contentType': '1.2.840.113549.1.9.3',
'1.2.840.113549.1.9.4': 'messageDigest',
'messageDigest': '1.2.840.113549.1.9.4',
'1.2.840.113549.1.9.5': 'signingTime',
'signingTime': '1.2.840.113549.1.9.5',
'1.2.840.113549.1.9.6': 'counterSignature',
'counterSignature': '1.2.840.113549.1.9.6',
'1.2.840.113549.1.9.7': 'challengePassword',
'challengePassword': '1.2.840.113549.1.9.7',
'1.2.840.113549.1.9.8': 'unstructuredAddress',
'unstructuredAddress': '1.2.840.113549.1.9.8',
'1.2.840.113549.1.9.20': 'friendlyName',
'friendlyName': '1.2.840.113549.1.9.20',
'1.2.840.113549.1.9.21': 'localKeyId',
'localKeyId': '1.2.840.113549.1.9.21',
'1.2.840.113549.1.9.22.1': 'x509Certificate',
'x509Certificate': '1.2.840.113549.1.9.22.1',
// pkcs#12 safe bags
'1.2.840.113549.1.12.10.1.1': 'keyBag',
'keyBag': '1.2.840.113549.1.12.10.1.1',
'1.2.840.113549.1.12.10.1.2': 'pkcs8ShroudedKeyBag',
'pkcs8ShroudedKeyBag': '1.2.840.113549.1.12.10.1.2',
'1.2.840.113549.1.12.10.1.3': 'certBag',
'certBag': '1.2.840.113549.1.12.10.1.3',
'1.2.840.113549.1.12.10.1.4': 'crlBag',
'crlBag': '1.2.840.113549.1.12.10.1.4',
'1.2.840.113549.1.12.10.1.5': 'secretBag',
'secretBag': '1.2.840.113549.1.12.10.1.5',
'1.2.840.113549.1.12.10.1.6': 'safeContentsBag',
'safeContentsBag': '1.2.840.113549.1.12.10.1.6',
// password-based-encryption for pkcs#12
'1.2.840.113549.1.5.13': 'pkcs5PBES2',
'pkcs5PBES2': '1.2.840.113549.1.5.13',
'1.2.840.113549.1.5.12': 'pkcs5PBKDF2',
'pkcs5PBKDF2': '1.2.840.113549.1.5.12',
'1.2.840.113549.1.12.1.1': 'pbeWithSHAAnd128BitRC4',
'pbeWithSHAAnd128BitRC4': '1.2.840.113549.1.12.1.1',
'1.2.840.113549.1.12.1.2': 'pbeWithSHAAnd40BitRC4',
'pbeWithSHAAnd40BitRC4': '1.2.840.113549.1.12.1.2',
'1.2.840.113549.1.12.1.3': 'pbeWithSHAAnd3-KeyTripleDES-CBC',
'pbeWithSHAAnd3-KeyTripleDES-CBC': '1.2.840.113549.1.12.1.3',
'1.2.840.113549.1.12.1.4': 'pbeWithSHAAnd2-KeyTripleDES-CBC',
'pbeWithSHAAnd2-KeyTripleDES-CBC': '1.2.840.113549.1.12.1.4',
'1.2.840.113549.1.12.1.5': 'pbeWithSHAAnd128BitRC2-CBC',
'pbeWithSHAAnd128BitRC2-CBC': '1.2.840.113549.1.12.1.5',
'1.2.840.113549.1.12.1.6': 'pbewithSHAAnd40BitRC2-CBC',
'pbewithSHAAnd40BitRC2-CBC': '1.2.840.113549.1.12.1.6',
// symmetric key algorithm oids
'1.2.840.113549.3.7': 'des-EDE3-CBC',
'des-EDE3-CBC': '1.2.840.113549.3.7',
'2.16.840.1.101.3.4.1.2': 'aes128-CBC',
'aes128-CBC': '2.16.840.1.101.3.4.1.2',
'2.16.840.1.101.3.4.1.22': 'aes192-CBC',
'aes192-CBC': '2.16.840.1.101.3.4.1.22',
'2.16.840.1.101.3.4.1.42': 'aes256-CBC',
'aes256-CBC': '2.16.840.1.101.3.4.1.42',
// certificate issuer/subject OIDs
'2.5.4.3': 'commonName',
'commonName': '2.5.4.3',
'2.5.4.5': 'serialName',
'serialName': '2.5.4.5',
'2.5.4.6': 'countryName',
'countryName': '2.5.4.6',
'2.5.4.7': 'localityName',
'localityName': '2.5.4.7',
'2.5.4.8': 'stateOrProvinceName',
'stateOrProvinceName': '2.5.4.8',
'2.5.4.10': 'organizationName',
'organizationName': '2.5.4.10',
'2.5.4.11': 'organizationalUnitName',
'organizationalUnitName': '2.5.4.11',
// X.509 extension OIDs
'2.16.840.1.113730.1.1': 'nsCertType',
'nsCertType': '2.16.840.1.113730.1.1',
'2.5.29.1': 'authorityKeyIdentifier', // deprecated, use .35
'2.5.29.2': 'keyAttributes', // obsolete use .37 or .15
'2.5.29.3': 'certificatePolicies', // deprecated, use .32
'2.5.29.4': 'keyUsageRestriction', // obsolete use .37 or .15
'2.5.29.5': 'policyMapping', // deprecated use .33
'2.5.29.6': 'subtreesConstraint', // obsolete use .30
'2.5.29.7': 'subjectAltName', // deprecated use .17
'2.5.29.8': 'issuerAltName', // deprecated use .18
'2.5.29.9': 'subjectDirectoryAttributes',
'2.5.29.10': 'basicConstraints', // deprecated use .19
'2.5.29.11': 'nameConstraints', // deprecated use .30
'2.5.29.12': 'policyConstraints', // deprecated use .36
'2.5.29.13': 'basicConstraints', // deprecated use .19
'2.5.29.14': 'subjectKeyIdentifier',
'subjectKeyIdentifier': '2.5.29.14',
'2.5.29.15': 'keyUsage',
'keyUsage': '2.5.29.15',
'2.5.29.16': 'privateKeyUsagePeriod',
'2.5.29.17': 'subjectAltName',
'subjectAltName': '2.5.29.17',
'2.5.29.18': 'issuerAltName',
'issuerAltName': '2.5.29.18',
'2.5.29.19': 'basicConstraints',
'basicConstraints': '2.5.29.19',
'2.5.29.20': 'cRLNumber',
'2.5.29.21': 'cRLReason',
'2.5.29.22': 'expirationDate',
'2.5.29.23': 'instructionCode',
'2.5.29.24': 'invalidityDate',
'2.5.29.25': 'cRLDistributionPoints', // deprecated use .31
'2.5.29.26': 'issuingDistributionPoint', // deprecated use .28
'2.5.29.27': 'deltaCRLIndicator',
'2.5.29.28': 'issuingDistributionPoint',
'2.5.29.29': 'certificateIssuer',
'2.5.29.30': 'nameConstraints',
'2.5.29.31': 'cRLDistributionPoints',
'2.5.29.32': 'certificatePolicies',
'2.5.29.33': 'policyMappings',
'2.5.29.34': 'policyConstraints', // deprecated use .36
'2.5.29.35': 'authorityKeyIdentifier',
'2.5.29.36': 'policyConstraints',
'2.5.29.37': 'extKeyUsage',
'extKeyUsage': '2.5.29.37',
'2.5.29.46': 'freshestCRL',
'2.5.29.54': 'inhibitAnyPolicy',
// extKeyUsage purposes
'1.3.6.1.5.5.7.3.1': 'serverAuth',
'serverAuth': '1.3.6.1.5.5.7.3.1',
'1.3.6.1.5.5.7.3.2': 'clientAuth',
'clientAuth': '1.3.6.1.5.5.7.3.2',
'1.3.6.1.5.5.7.3.3': 'codeSigning',
'codeSigning': '1.3.6.1.5.5.7.3.3',
'1.3.6.1.5.5.7.3.4': 'emailProtection',
'emailProtection': '1.3.6.1.5.5.7.3.4',
'1.3.6.1.5.5.7.3.8': 'timeStamping',
'timeStamping': '1.3.6.1.5.5.7.3.8'
},
shortNames = {
'CN': oids['commonName'],
'commonName': 'CN',
'C': oids['countryName'],
'countryName': 'C',
'L': oids['localityName'],
'localityName': 'L',
'ST': oids['stateOrProvinceName'],
'stateOrProvinceName': 'ST',
'O': oids['organizationName'],
'organizationName': 'O',
'OU': oids['organizationalUnitName'],
'organizationalUnitName': 'OU',
'E': oids['emailAddress'],
'emailAddress': 'E'
};
function pem2cert(pem) {
var msg = decodePem(pem)[0];
if (msg.type !== 'CERTIFICATE' && msg.type !== 'X509 CERTIFICATE' && msg.type !== 'TRUSTED CERTIFICATE') {
throw new Error(__('Could not convert certificate from PEM; PEM header type is "%s", but must be "CERTIFICATE", "X509 CERTIFICATE", or "TRUSTED CERTIFICATE".', msg.type));
}
if (msg.procType && msg.procType.type === 'ENCRYPTED') {
throw new Error(__('Could not convert certificate from PEM; PEM is encrypted.'));
}
return asn2cert(der2asn(new ByteStringBuffer(msg.body)));
}
function decodePem(str) {
var rval = [],
match, msg, lines, li, line, nl, next, header, values, vi;
while (true) {
match = rMessage.exec(str);
if (!match) {
break;
}
rval.push(msg = {
type: match[1],
procType: null,
contentDomain: null,
dekInfo: null,
headers: [],
body: Buffer.from(match[3], 'base64').toString('binary')
});
// no headers
if (!match[2]) {
continue;
}
// parse headers
lines = match[2].split(rCRLF);
for (li = 0; match && li < lines.length; ++li) {
// get line, trim any rhs whitespace
line = lines[li].replace(/\s+$/, '');
// RFC2822 unfold any following folded lines
for (nl = li + 1; nl < lines.length; ++nl) {
next = lines[nl];
if (!whitespaceRegExp.test(next[0])) {
break;
}
line += next;
li = nl;
}
// parse header
match = line.match(rHeader);
if (match) {
header = {name: match[1], values: []};
values = match[2].split(',');
for (vi = 0; vi < values.length; ++vi) {
header.values.push(values[vi].replace(leadingSpaceRegExp, ''));
}
// Proc-Type must be the first header
if (!msg.procType) {
if (header.name !== 'Proc-Type') {
throw new Error(__('Invalid PEM formatted message. The first encapsulated header must be "Proc-Type".'));
} else if (header.values.length !== 2) {
throw new Error(__('Invalid PEM formatted message. The "Proc-Type" header must have two subfields.'));
}
msg.procType = { version: values[0], type: values[1] };
// special-case Content-Domain
} else if (!msg.contentDomain && header.name === 'Content-Domain') {
msg.contentDomain = values[0] || '';
// special-case DEK-Info
} else if (!msg.dekInfo && header.name === 'DEK-Info') {
if (header.values.length === 0) {
throw new Error(__('Invalid PEM formatted message. The "DEK-Info" header must have at least one subfield.'));
}
msg.dekInfo = { algorithm: values[0], parameters: values[1] || null };
} else {
msg.headers.push(header);
}
}
}
if (msg.procType === 'ENCRYPTED' && !msg.dekInfo) {
throw new Error(__('Invalid PEM formatted message. The "DEK-Info" header must be present if "Proc-Type" is "ENCRYPTED".'));
}
}
if (rval.length === 0) {
throw new Error(__('Invalid PEM formatted message.'));
}
return rval;
}
function ByteStringBuffer(str) {
this.data = str;
this.read = 0;
}
ByteStringBuffer.prototype.length = function length() {
return this.data.length - this.read;
};
ByteStringBuffer.prototype.getByte = function getByte() {
return this.data.charCodeAt(this.read++);
};
ByteStringBuffer.prototype.getInt = function getInt(n) {
var rval = 0;
do {
rval = (rval << 8) + this.data.charCodeAt(this.read++);
n -= 8;
} while (n > 0);
return rval;
};
ByteStringBuffer.prototype.bytes = function bytes(count) {
return count === undefined ?
this.data.slice(this.read) :
this.data.slice(this.read, this.read + count);
};
ByteStringBuffer.prototype.getBytes = function getBytes(count) {
var rval;
if (count) {
// read count bytes
count = Math.min(this.length(), count);
rval = this.data.slice(this.read, this.read + count);
this.read += count;
} else if (count === 0) {
rval = '';
} else {
// read all bytes, optimize to only copy when needed
rval = this.read === 0 ? this.data : this.data.slice(this.read);
this.clear();
}
return rval;
};
ByteStringBuffer.prototype.getInt16 = function getInt16() {
var rval = (this.data.charCodeAt(this.read) << 8 ^ this.data.charCodeAt(this.read + 1));
this.read += 2;
return rval;
};
ByteStringBuffer.prototype.clear = function clear() {
this.data = '';
this.read = 0;
return this;
};
function der2asn(bytes) {
// minimum length for ASN.1 DER structure is 2
if (bytes.length() < 2) {
throw new Error(__('Too few bytes to parse DER; expected at least 2, got %d', bytes.length()));
}
// get the first byte
var b1 = bytes.getByte(),
// get the tag class
tagClass = (b1 & 0xC0),
// get the type (bits 1-5)
type = b1 & 0x1F,
_getValueLength = function _getValueLength(b) {
var b2 = b.getByte();
if (b2 === 0x80) {
return undefined;
}
// see if the length is "short form" or "long form" (bit 8 set)
// if "long form", the number of bytes the length is specified in bits 7 through 1
// and each length byte is in big-endian base-256
return b2 & 0x80 ? b.getInt((b2 & 0x7F) << 3) : b2;
},
// get the value length
length = _getValueLength(bytes),
// prepare to get value
value,
// constructed flag is bit 6 (32 = 0x20) of the first byte
constructed = ((b1 & 0x20) === 0x20),
composed = constructed;
// ensure there are enough bytes to get the value
if (bytes.length() < length) {
throw new Error(__('Too few bytes to read ASN.1 value. %d < %d', bytes.length(), length));
}
// determine if the value is composed of other ASN.1 objects (if its
// constructed it will be and if its a BITSTRING it may be)
if (!composed && tagClass === asn1Class.UNIVERSAL && type === asn1Type.BITSTRING && length > 1) {
/* The first octet gives the number of bits by which the length of the
bit string is less than the next multiple of eight (this is called
the "number of unused bits").