1 |
3a515b92
|
cagy
|
// from https://github.com/Rantanen/node-dtls/blob/25a7dc861bda38cfeac93a723500eea4f0ac2e86/Certificate.js
|
2 |
|
|
// thanks to @Rantanen
|
3 |
|
|
|
4 |
|
|
'use strict'
|
5 |
|
|
|
6 |
|
|
var asn = require('asn1.js')
|
7 |
|
|
|
8 |
|
|
var Time = asn.define('Time', function () {
|
9 |
|
|
this.choice({
|
10 |
|
|
utcTime: this.utctime(),
|
11 |
|
|
generalTime: this.gentime()
|
12 |
|
|
})
|
13 |
|
|
})
|
14 |
|
|
|
15 |
|
|
var AttributeTypeValue = asn.define('AttributeTypeValue', function () {
|
16 |
|
|
this.seq().obj(
|
17 |
|
|
this.key('type').objid(),
|
18 |
|
|
this.key('value').any()
|
19 |
|
|
)
|
20 |
|
|
})
|
21 |
|
|
|
22 |
|
|
var AlgorithmIdentifier = asn.define('AlgorithmIdentifier', function () {
|
23 |
|
|
this.seq().obj(
|
24 |
|
|
this.key('algorithm').objid(),
|
25 |
|
|
this.key('parameters').optional(),
|
26 |
|
|
this.key('curve').objid().optional()
|
27 |
|
|
)
|
28 |
|
|
})
|
29 |
|
|
|
30 |
|
|
var SubjectPublicKeyInfo = asn.define('SubjectPublicKeyInfo', function () {
|
31 |
|
|
this.seq().obj(
|
32 |
|
|
this.key('algorithm').use(AlgorithmIdentifier),
|
33 |
|
|
this.key('subjectPublicKey').bitstr()
|
34 |
|
|
)
|
35 |
|
|
})
|
36 |
|
|
|
37 |
|
|
var RelativeDistinguishedName = asn.define('RelativeDistinguishedName', function () {
|
38 |
|
|
this.setof(AttributeTypeValue)
|
39 |
|
|
})
|
40 |
|
|
|
41 |
|
|
var RDNSequence = asn.define('RDNSequence', function () {
|
42 |
|
|
this.seqof(RelativeDistinguishedName)
|
43 |
|
|
})
|
44 |
|
|
|
45 |
|
|
var Name = asn.define('Name', function () {
|
46 |
|
|
this.choice({
|
47 |
|
|
rdnSequence: this.use(RDNSequence)
|
48 |
|
|
})
|
49 |
|
|
})
|
50 |
|
|
|
51 |
|
|
var Validity = asn.define('Validity', function () {
|
52 |
|
|
this.seq().obj(
|
53 |
|
|
this.key('notBefore').use(Time),
|
54 |
|
|
this.key('notAfter').use(Time)
|
55 |
|
|
)
|
56 |
|
|
})
|
57 |
|
|
|
58 |
|
|
var Extension = asn.define('Extension', function () {
|
59 |
|
|
this.seq().obj(
|
60 |
|
|
this.key('extnID').objid(),
|
61 |
|
|
this.key('critical').bool().def(false),
|
62 |
|
|
this.key('extnValue').octstr()
|
63 |
|
|
)
|
64 |
|
|
})
|
65 |
|
|
|
66 |
|
|
var TBSCertificate = asn.define('TBSCertificate', function () {
|
67 |
|
|
this.seq().obj(
|
68 |
|
|
this.key('version').explicit(0).int().optional(),
|
69 |
|
|
this.key('serialNumber').int(),
|
70 |
|
|
this.key('signature').use(AlgorithmIdentifier),
|
71 |
|
|
this.key('issuer').use(Name),
|
72 |
|
|
this.key('validity').use(Validity),
|
73 |
|
|
this.key('subject').use(Name),
|
74 |
|
|
this.key('subjectPublicKeyInfo').use(SubjectPublicKeyInfo),
|
75 |
|
|
this.key('issuerUniqueID').implicit(1).bitstr().optional(),
|
76 |
|
|
this.key('subjectUniqueID').implicit(2).bitstr().optional(),
|
77 |
|
|
this.key('extensions').explicit(3).seqof(Extension).optional()
|
78 |
|
|
)
|
79 |
|
|
})
|
80 |
|
|
|
81 |
|
|
var X509Certificate = asn.define('X509Certificate', function () {
|
82 |
|
|
this.seq().obj(
|
83 |
|
|
this.key('tbsCertificate').use(TBSCertificate),
|
84 |
|
|
this.key('signatureAlgorithm').use(AlgorithmIdentifier),
|
85 |
|
|
this.key('signatureValue').bitstr()
|
86 |
|
|
)
|
87 |
|
|
})
|
88 |
|
|
|
89 |
|
|
module.exports = X509Certificate
|