首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xml-crypto node.js库签名的xml会产生错误

使用xml-crypto node.js库签名的xml会产生错误
EN

Stack Overflow用户
提问于 2021-07-21 07:27:57
回答 1查看 59关注 0票数 0

我正在使用xml-crypto node.js包对xml进行签名。当我在SAML2.0中使用带签名的xml文档时,我得到一个错误: org.opensaml.xmlsec.signature.support.SignatureException:签名密码验证不成功我的方法完整的代码:

代码语言:javascript
复制
import { Injectable, Optional } from "@nestjs/common";
import { SignedXml } from "xml-crypto";
import * as p12 from "p12-pem";

  async getAuthRequest(): Promise<any> {
    const date = new Date();

    const xml = `<?xml version="1.0" encoding="utf-8" ?>
<saml2p:AuthnRequest Destination="${this.config.url}SingleSignOnService" AssertionConsumerServiceURL="https://url/artifactResolves" IssueInstant="${date.toISOString()}" Version="2.0" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact" xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:eidas="http://eidas.europa.eu/saml-extensions">
    <saml2:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">https://izzinakarte.pl</saml2:Issuer>
    <saml2p:NameIDPolicy  AllowCreate="true" Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"/>
    <saml2p:RequestedAuthnContext Comparison="minimum">
        <saml2:AuthnContextClassRef xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">http://eidas.europa.eu/LoA/high</saml2:AuthnContextClassRef>
    </saml2p:RequestedAuthnContext>
</saml2p:AuthnRequest>`;

    const {pemKey, pemCertificate, commonName} = p12.getPemFromP12("certs/sign.p12", this.config.signPassword);

    const sign = new SignedXml();
    sign.addReference(
      "//*[local-name(.)='AuthnRequest']",
      [
        "http://www.w3.org/2000/09/xmldsig#enveloped-signature",
        "http://www.w3.org/2001/10/xml-exc-c14n#",
      ],
      "http://www.w3.org/2001/04/xmlenc#sha256"
    );
    sign.signingKey = pemKey
        .replace("-----BEGIN RSA PRIVATE KEY-----", "-----BEGIN RSA PRIVATE KEY-----\n")
        .replace("-----END RSA PRIVATE KEY-----", "\n-----END RSA PRIVATE KEY-----")

    // @ts-ignore
    sign.keyInfoProvider = new KeyInfoProvider(pemCertificate);
    sign.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
    sign.signatureAlgorithm =
      "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";

    sign.computeSignature(xml, {
      prefix: "ds",
      location: { reference: "//*[local-name(.)='Issuer']", action: "after" },
    });

    const signedXml = sign.getSignedXml().replace("Id=", "ID=");
    const buff = Buffer.from(signedXml);
    const base64data = buff.toString("base64");

    return base64data;
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-28 18:42:52

问题解决了。我必须显式地插入标识符,这样就不会插入_0。

代码语言:javascript
复制
<saml2p:AuthnRequest ID="ID-${Guid.raw()}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68462410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档