PeculiarVentures / PKI.js

PKI.js is a pure JavaScript library implementing the formats that are used in PKI applications (signing, encryption, certificate requests, OCSP and TSP requests/responses). It is built on WebCrypto (Web Cryptography API) and requires no plug-ins.
http://pkijs.org
Other
1.3k stars 204 forks source link

Error on subjectUniqueID creation in Certificate #179

Closed microshine closed 6 years ago

microshine commented 6 years ago

Cannot encode SubjectUniqueID for Certificate

Certificate schema has subjectUniqueID and empty extensions

TypeError: Cannot read property 'toSchema' of undefined
    at Certificate.encodeTBS (node_modules/pkijs/src/Certificate.js:443:29)
    at Certificate.toSchema (node_modules/pkijs/src/Certificate.js:488:21)

IssuerUniqueID works fine

There are 2 subjectUniqueID implementations in encodeTBS method https://github.com/PeculiarVentures/PKI.js/blob/master/src/Certificate.js#L423-L445

test app

const certificate = new Certificate();

const alg: RsaHashedKeyGenParams = {
  name: "RSASSA-PKCS1-v1_5",
  hash: "SHA-256",
  publicExponent: new Uint8Array([1, 0, 1]),
  modulusLength: 2048,
};

const keys = await crypto.subtle.generateKey(alg, false, ["sign", "verify"]);

certificate.version = 2;
certificate.serialNumber = new Integer({ value: 1 });

certificate.notBefore.value = new Date();
certificate.notAfter.value = new Date();
certificate.notAfter.value.setFullYear(certificate.notAfter.value.getFullYear() + 1);

certificate.subjectUniqueID = Convert.FromHex("0102030405060708090001020304050607080900");

await certificate.subjectPublicKeyInfo.importKey(keys.publicKey);

await certificate.sign(keys.privateKey, (keys.privateKey.algorithm as any).hash.name);

const certificateBuffer = certificate.toSchema(true).toBER(false); // Error is here
YuryStrozhevsky commented 6 years ago

Fixed.

microshine commented 6 years ago

@YuryStrozhevsky Thank you

When new version will be published? Current version in npm is 2.1.59

YuryStrozhevsky commented 6 years ago

@microshine Published.