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

hmtdttj4  于 2024-01-08  发布在  其他
关注(0)|答案(1)|浏览(196)

我试图veryfy一个CMS签名创建与开放的SSL像这样:
第一个月
下面是我使用pkijs的代码:

  1. import * as pkijs from "../src/shared/vendor/pkijs/index.es.js";
  2. import * as pvtsutils from "../src/shared/vendor/pvtsutils/index.es.js";
  3. function decodePEM(pem: string, tag = "[A-Z0-9 ]+"): ArrayBuffer[] {
  4. const pattern = new RegExp(
  5. `-{5}BEGIN ${tag}-{5}([a-zA-Z0-9=+\\/\\n\\r]+)-{5}END ${tag}-{5}`,
  6. "g",
  7. );
  8. const res: ArrayBuffer[] = [];
  9. let matches: RegExpExecArray | null = null;
  10. // eslint-disable-next-line no-cond-assign
  11. while ((matches = pattern.exec(pem))) {
  12. const base64 = matches[1]
  13. .replace(/\r/g, "")
  14. .replace(/\n/g, "");
  15. res.push(pvtsutils.Convert.FromBase64(base64));
  16. }
  17. return res;
  18. }
  19. const buffer = pvtsutils.BufferSourceConverter.toArrayBuffer(await Deno.readFile("./domain.pem"));
  20. const pem = pvtsutils.Convert.ToBinary(buffer);
  21. const certificate = pkijs.Certificate.fromBER(decodePEM(pem, "CERTIFICATE")[0]) as pkijs.Certificate;
  22. //const publicKey = await certificate.getPublicKey();
  23. //console.log(publicKey);
  24. //console.log(certificate.signatureAlgorithm);
  25. const cms = pkijs.ContentInfo.fromBER(await Deno.readFile("./signature"));
  26. if (cms.contentType !== pkijs.ContentInfo.SIGNED_DATA) {
  27. throw new Error("CMS is not Signed Data");
  28. }
  29. const signedData = new pkijs.SignedData({ schema: cms.content });
  30. // Verify Signed Data signature
  31. const ok = await signedData.verify({
  32. signer: 0,
  33. checkChain: true,
  34. trustedCerts: [certificate],
  35. });
  36. console.log(ok);

字符串
证书被正确读取和解析,以及SignedData,但它在signedData.verify失败,并出现以下错误:

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


我哪里做错了?

yx2lnoni

yx2lnoni1#

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

  1. // Verify Signed Data signature
  2. const ok = await signedData.verify({
  3. signer: 0,
  4. checkChain: true,
  5. trustedCerts: [certificate],
  6. data: await Deno.readFile("./README.md")
  7. });

字符串

相关问题