Skip to content

Commit

Permalink
x509: add keyUsage extension
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Jun 25, 2020
1 parent 0416948 commit a83e5a0
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions lib/encoding/x509.js
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,92 @@ class UniformResourceIdentifier extends asn1.IA5String {};
class IPAddress extends asn1.OctString {};
class RegisteredID extends asn1.OID {};

/**
* KeyUsage
*/

// KeyUsage ::= BIT STRING {
// digitalSignature (0),
// nonRepudiation (1), -- recent editions of X.509 have
// -- renamed this bit to contentCommitment
// keyEncipherment (2),
// dataEncipherment (3),
// keyAgreement (4),
// keyCertSign (5),
// cRLSign (6),
// encipherOnly (7),
// decipherOnly (8) }

class KeyUsage extends asn1.BitString {
constructor() {
super();
this.value = Buffer.alloc(2);
}

getBitByProperty(property) {
const properties = {
'digitalSignature': 0,
'nonRepudiation': 1,
'keyEncipherment': 2,
'dataEncipherment': 3,
'keyAgreement': 4,
'keyCertSign': 5,
'cRLSign': 6,
'encipherOnly': 7,
'decipherOnly': 8
};

return properties[property];
}

getPropertyByBit(bit) {
const bits = [
'digitalSignature',
'nonRepudiation',
'keyEncipherment',
'dataEncipherment',
'keyAgreement',
'keyCertSign',
'cRLSign',
'encipherOnly',
'decipherOnly'
];

return bits[bit];
}

getJSON() {
const purpose = [];
for (let i = 0; i <= this.bits; i++) {
if (this.getBit(i))
purpose.push(this.getPropertyByBit(i));
}

return purpose;
}

fromJSON(json) {
assert(Array.isArray(json));
for (const property of json) {
const bit = this.getBitByProperty(property);

if (bit + 1 > this.bits)
this.bits = bit + 1;

this.setBit(bit, true);
}

if (this.bits < 9)
this.value = this.value.slice(0, -1);

return this;
}

static fromJSON(json) {
return new this().fromJSON(json);
}
}

/**
* Entity
*/
Expand Down Expand Up @@ -1296,6 +1382,8 @@ function identifierToClass(oid) {
return RSAPublicKey;
case 'SubjectAltName':
return SubjectAltName;
case 'KeyUsage':
return KeyUsage;
default:
return null;
}
Expand All @@ -1320,4 +1408,5 @@ exports.DigestInfo = DigestInfo;
exports.BasicConstraints = BasicConstraints;
exports.RSAPublicKey = RSAPublicKey;
exports.SubjectAltName = SubjectAltName;
exports.KeyUsage = KeyUsage;
exports.Entity = Entity;

0 comments on commit a83e5a0

Please sign in to comment.