Skip to content

Commit

Permalink
Add fixes for getIssuer and getSerial (#15)
Browse files Browse the repository at this point in the history
* Add search for commonName in getIssuer
* Properly return serial number from _cert in getSerial
  • Loading branch information
RyanHopkins7 committed Sep 14, 2023
1 parent 9da9eb9 commit 79e7d31
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions lib/certUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ class Certificate {
}

getCommonName() {
return Certificate.#searchForCommonName(this._cert.subject.typesAndValues);
}

static #searchForCommonName(attributes) {
const X509_COMMON_NAME_KEY = "2.5.4.3";

let commonName = ""; // Default in case no subject is found

// Search the subject's attributes for the common name of the certificate
const subjectAttributes = this._cert.subject.typesAndValues;
for (let index = 0; index < subjectAttributes.length; index++) {
const attribute = subjectAttributes[index];
// Search the attributes for the common name of the certificate
for (let index = 0; index < attributes.length; index++) {
const attribute = attributes[index];
if (attribute.type === X509_COMMON_NAME_KEY) {
commonName = attribute.value.valueBlock.value;
break;
Expand Down Expand Up @@ -79,11 +82,11 @@ class Certificate {
}

getIssuer() {
return this._cert.issuer.typesAndValues[0].value.valueBlock.value;
return Certificate.#searchForCommonName(this._cert.issuer.typesAndValues);
}

getSerial() {
return this._cert.subject.typesAndValues[0].value.valueBlock.value;
return this._cert.serialNumber.valueBlock.toString();
}

getVersion() {
Expand Down Expand Up @@ -480,7 +483,7 @@ const certMap = new Map();
class CertManager {
static addCert(certBuf) {
const cert = new Certificate(certBuf);
const serial = cert.getSerial();
const serial = cert.getIssuer();
certMap.set(serial, cert);

return true;
Expand Down
4 changes: 2 additions & 2 deletions test/certUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ describe("cert utils", function() {
it("returns correct serial for attestation", function() {
const cert = new Certificate(h.certs.yubiKeyAttestation);
const serial = cert.getSerial();
assert.strictEqual(serial, "Yubico U2F EE Serial 1432534688");
assert.strictEqual(serial, "1432534688");
});
it("returns correct serial for root", function() {
const cert = new Certificate(h.certs.yubicoRoot);
const serial = cert.getSerial();
assert.strictEqual(
serial,
"Yubico U2F Root CA Serial 457200631",
"457200631",
);
});
});
Expand Down

0 comments on commit 79e7d31

Please sign in to comment.