From cb7f8904d8861996c89afdb6a07650ac2d116405 Mon Sep 17 00:00:00 2001 From: microshine Date: Sat, 28 Aug 2021 00:08:54 +0300 Subject: [PATCH] feat: Improve signing certificate getting --- src/SignedData.js | 49 ++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/SignedData.js b/src/SignedData.js index e5347871a..e41edd345 100644 --- a/src/SignedData.js +++ b/src/SignedData.js @@ -493,38 +493,31 @@ export default class SignedData } else // Find by SubjectKeyIdentifier { - sequence = sequence.then(() => - Promise.all(Array.from(this.certificates.filter(certificate => (certificate instanceof Certificate)), certificate => - crypto.digest({ name: "sha-1" }, new Uint8Array(certificate.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex))) - ).then(results => - { - for(const [index, certificate] of this.certificates.entries()) - { - if((certificate instanceof Certificate) === false) + sequence = (async () => { + try { + const sid = this.signerInfos[signer].sid; + const keyId = sid.idBlock.isConstructed + ? sid.valueBlock.value[0].valueBlock.valueHex // EXPLICIT OCTET STRING + : sid.valueBlock.valueHex; // IMPLICIT OCTET STRING + + for (const certificate of this.certificates) { + if (!(certificate instanceof Certificate)) { continue; - - if(isEqualBuffer(results[index], this.signerInfos[signer].sid.valueBlock.valueHex)) + } + + const digest = await crypto.digest({ name: "sha-1" }, new Uint8Array(certificate.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex)); + if(isEqualBuffer(digest, keyId)) { signerCertificate = certificate; - return Promise.resolve(); + break; } } - if(extendedMode) - { - return Promise.reject({ - date: checkDate, - code: 3, - message: "Unable to find signer certificate", - signatureVerified: null, - signerCertificate: null, - signerCertificateVerified: null - }); + if (!signerCertificate) { + throw new Error("Signing certificate not found"); } - - return Promise.reject("Unable to find signer certificate"); - }, () => - { + } + catch (e) { if(extendedMode) { return Promise.reject({ @@ -537,9 +530,9 @@ export default class SignedData }); } - return Promise.reject("Unable to find signer certificate"); - }) - ); + throw "Unable to find signer certificate"; + } + })(); } //endregion