尝试使用 pkijs 验证 CMS 签名时出错

问题描述 投票:0回答:1

我正在尝试验证使用开放 ssl 创建的 CMS 签名,如下所示:

$ openssl cms -sign -signer domain.pem -inkey domain.key -binary -in README.md -outform der -out signature

这是我使用

pkijs
的代码:

import * as pkijs from "../src/shared/vendor/pkijs/index.es.js";
import * as pvtsutils from "../src/shared/vendor/pvtsutils/index.es.js";

function decodePEM(pem: string, tag = "[A-Z0-9 ]+"): ArrayBuffer[] {
  const pattern = new RegExp(
    `-{5}BEGIN ${tag}-{5}([a-zA-Z0-9=+\\/\\n\\r]+)-{5}END ${tag}-{5}`,
    "g",
  );

  const res: ArrayBuffer[] = [];
  let matches: RegExpExecArray | null = null;
  // eslint-disable-next-line no-cond-assign
  while ((matches = pattern.exec(pem))) {
    const base64 = matches[1]
      .replace(/\r/g, "")
      .replace(/\n/g, "");
    res.push(pvtsutils.Convert.FromBase64(base64));
  }

  return res;
}

const buffer = pvtsutils.BufferSourceConverter.toArrayBuffer(await Deno.readFile("./domain.pem"));
const pem = pvtsutils.Convert.ToBinary(buffer);
const certificate = pkijs.Certificate.fromBER(decodePEM(pem, "CERTIFICATE")[0]) as pkijs.Certificate;

//const publicKey = await certificate.getPublicKey();

//console.log(publicKey);
//console.log(certificate.signatureAlgorithm);


const cms = pkijs.ContentInfo.fromBER(await Deno.readFile("./signature"));
if (cms.contentType !== pkijs.ContentInfo.SIGNED_DATA) {
  throw new Error("CMS is not Signed Data");
}

const signedData = new pkijs.SignedData({ schema: cms.content });

// Verify Signed Data signature
const ok = await signedData.verify({
  signer: 0,
  checkChain: true,
  trustedCerts: [certificate],
});

console.log(ok);

证书被正确读取和解析以及

SignedData
,但在
signedData.verify
处失败,并出现以下错误:

error: Uncaught (in promise) SignedDataVerifyError: Missed detached data input array
                    throw new SignedDataVerifyError({

我哪里做错了?

typescript ssl openssl pki
1个回答
0
投票

好吧,我的错...我只是忘记提供数据来验证签名...

// Verify Signed Data signature
const ok = await signedData.verify({
  signer: 0,
  checkChain: true,
  trustedCerts: [certificate],
  data: await Deno.readFile("./README.md")
});
© www.soinside.com 2019 - 2024. All rights reserved.