| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.X509SCTExtension = exports.X509SubjectKeyIDExtension = exports.X509AuthorityKeyIDExtension = exports.X509SubjectAlternativeNameExtension = exports.X509KeyUsageExtension = exports.X509BasicConstraintsExtension = exports.X509Extension = void 0; |
| 4 |
const stream_1 = require("../stream"); |
| 5 |
const sct_1 = require("./sct"); |
| 6 |
// https://www.rfc-editor.org/rfc/rfc5280#section-4.1 |
| 7 |
class X509Extension { |
| 8 |
root; |
| 9 |
constructor(asn1) { |
| 10 |
this.root = asn1; |
| 11 |
} |
| 12 |
get oid() { |
| 13 |
return this.root.subs[0].toOID(); |
| 14 |
} |
| 15 |
get critical() { |
| 16 |
// The critical field is optional and will be the second element of the |
| 17 |
// extension sequence if present. Default to false if not present. |
| 18 |
return this.root.subs.length === 3 ? this.root.subs[1].toBoolean() : false; |
| 19 |
} |
| 20 |
get value() { |
| 21 |
return this.extnValueObj.value; |
| 22 |
} |
| 23 |
get valueObj() { |
| 24 |
return this.extnValueObj; |
| 25 |
} |
| 26 |
get extnValueObj() { |
| 27 |
// The extnValue field will be the last element of the extension sequence |
| 28 |
return this.root.subs[this.root.subs.length - 1]; |
| 29 |
} |
| 30 |
} |
| 31 |
exports.X509Extension = X509Extension; |
| 32 |
// https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.9 |
| 33 |
class X509BasicConstraintsExtension extends X509Extension { |
| 34 |
get isCA() { |
| 35 |
return this.sequence.subs[0]?.toBoolean() ?? false; |
| 36 |
} |
| 37 |
get pathLenConstraint() { |
| 38 |
return this.sequence.subs.length > 1 |
| 39 |
? this.sequence.subs[1].toInteger() |
| 40 |
: undefined; |
| 41 |
} |
| 42 |
// The extnValue field contains a single sequence wrapping the isCA and |
| 43 |
// pathLenConstraint. |
| 44 |
get sequence() { |
| 45 |
return this.extnValueObj.subs[0]; |
| 46 |
} |
| 47 |
} |
| 48 |
exports.X509BasicConstraintsExtension = X509BasicConstraintsExtension; |
| 49 |
// https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.3 |
| 50 |
class X509KeyUsageExtension extends X509Extension { |
| 51 |
get digitalSignature() { |
| 52 |
return this.bitString[0] === 1; |
| 53 |
} |
| 54 |
get keyCertSign() { |
| 55 |
return this.bitString[5] === 1; |
| 56 |
} |
| 57 |
get crlSign() { |
| 58 |
return this.bitString[6] === 1; |
| 59 |
} |
| 60 |
// The extnValue field contains a single bit string which is a bit mask |
| 61 |
// indicating which key usages are enabled. |
| 62 |
get bitString() { |
| 63 |
return this.extnValueObj.subs[0].toBitString(); |
| 64 |
} |
| 65 |
} |
| 66 |
exports.X509KeyUsageExtension = X509KeyUsageExtension; |
| 67 |
// https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.6 |
| 68 |
class X509SubjectAlternativeNameExtension extends X509Extension { |
| 69 |
get rfc822Name() { |
| 70 |
return this.findGeneralName(0x01)?.value.toString('ascii'); |
| 71 |
} |
| 72 |
get uri() { |
| 73 |
return this.findGeneralName(0x06)?.value.toString('ascii'); |
| 74 |
} |
| 75 |
// Retrieve the value of an otherName with the given OID. |
| 76 |
otherName(oid) { |
| 77 |
const otherName = this.findGeneralName(0x00); |
| 78 |
if (otherName === undefined) { |
| 79 |
return undefined; |
| 80 |
} |
| 81 |
// The otherName is a sequence containing an OID and a value. |
| 82 |
// Need to check that the OID matches the one we're looking for. |
| 83 |
const otherNameOID = otherName.subs[0].toOID(); |
| 84 |
if (otherNameOID !== oid) { |
| 85 |
return undefined; |
| 86 |
} |
| 87 |
// The otherNameValue is a sequence containing the actual value. |
| 88 |
const otherNameValue = otherName.subs[1]; |
| 89 |
return otherNameValue.subs[0].value.toString('ascii'); |
| 90 |
} |
| 91 |
findGeneralName(tag) { |
| 92 |
return this.generalNames.find((gn) => gn.tag.isContextSpecific(tag)); |
| 93 |
} |
| 94 |
// The extnValue field contains a sequence of GeneralNames. |
| 95 |
get generalNames() { |
| 96 |
return this.extnValueObj.subs[0].subs; |
| 97 |
} |
| 98 |
} |
| 99 |
exports.X509SubjectAlternativeNameExtension = X509SubjectAlternativeNameExtension; |
| 100 |
// https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.1 |
| 101 |
class X509AuthorityKeyIDExtension extends X509Extension { |
| 102 |
get keyIdentifier() { |
| 103 |
return this.findSequenceMember(0x00)?.value; |
| 104 |
} |
| 105 |
findSequenceMember(tag) { |
| 106 |
return this.sequence.subs.find((el) => el.tag.isContextSpecific(tag)); |
| 107 |
} |
| 108 |
// The extnValue field contains a single sequence wrapping the keyIdentifier |
| 109 |
get sequence() { |
| 110 |
return this.extnValueObj.subs[0]; |
| 111 |
} |
| 112 |
} |
| 113 |
exports.X509AuthorityKeyIDExtension = X509AuthorityKeyIDExtension; |
| 114 |
// https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.2 |
| 115 |
class X509SubjectKeyIDExtension extends X509Extension { |
| 116 |
get keyIdentifier() { |
| 117 |
return this.extnValueObj.subs[0].value; |
| 118 |
} |
| 119 |
} |
| 120 |
exports.X509SubjectKeyIDExtension = X509SubjectKeyIDExtension; |
| 121 |
// https://www.rfc-editor.org/rfc/rfc6962#section-3.3 |
| 122 |
class X509SCTExtension extends X509Extension { |
| 123 |
constructor(asn1) { |
| 124 |
super(asn1); |
| 125 |
} |
| 126 |
get signedCertificateTimestamps() { |
| 127 |
const buf = this.extnValueObj.subs[0].value; |
| 128 |
const stream = new stream_1.ByteStream(buf); |
| 129 |
// The overall list length is encoded in the first two bytes -- note this |
| 130 |
// is the length of the list in bytes, NOT the number of SCTs in the list |
| 131 |
const end = stream.getUint16() + 2; |
| 132 |
const sctList = []; |
| 133 |
while (stream.position < end) { |
| 134 |
// Read the length of the next SCT |
| 135 |
const sctLength = stream.getUint16(); |
| 136 |
// Slice out the bytes for the next SCT and parse it |
| 137 |
const sct = stream.getBlock(sctLength); |
| 138 |
sctList.push(sct_1.SignedCertificateTimestamp.parse(sct)); |
| 139 |
} |
| 140 |
if (stream.position !== end) { |
| 141 |
throw new Error('SCT list length does not match actual length'); |
| 142 |
} |
| 143 |
return sctList; |
| 144 |
} |
| 145 |
} |
| 146 |
exports.X509SCTExtension = X509SCTExtension; |