5

OpenSSLによって生成された秘密RSAキーを使用して、C# .NET 3.5 で XML ファイルに署名しようとしています。

手順は次のとおりです。chilkat フレームワーク (www.example-code.com/csharp/cert_usePrivateKeyFromPEM.asp) を使用して、RSA キーをPEM形式から XML 形式に変換しました。

XML キーを使用して、私が好むネイティブ .NET 関数を使用できるようになりました。そこで、MSDNで説明されている方法を使用しました。

最終的に、私のソースコードは次のようになります。

RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();

//Load the private key from xml file
XmlDocument xmlPrivateKey = new XmlDocument();
xmlPrivateKey.Load("PrivateKey.xml");
rsaProvider.FromXmlString(xmlPrivateKey.InnerXml);

 // Create a SignedXml object.
 SignedXml signedXml = new SignedXml(Doc);

 // Add the key to the SignedXml document.
 signedXml.SigningKey = Key;

 // Create a reference to be signed.
 Reference reference = new Reference();
 reference.Uri = "";

 // Add an enveloped transformation to the reference.
 XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
 reference.AddTransform(env);

 // Add the reference to the SignedXml object.
 signedXml.AddReference(reference);

 // Compute the signature.
 signedXml.ComputeSignature();

 // Get the XML representation of the signature and save
 // it to an XmlElement object.
 XmlElement xmlDigitalSignature = signedXml.GetXml();

 // Append the element to the XML document.
 Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, true));

この関数で取得した署名付き XML は問題ないように見えます。次のように、ファイルの末尾に XML 要素があります。

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
  <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
  <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
  <Reference URI="">
    <Transforms>
      <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
    </Transforms>
    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
    <DigestValue>qoGPSbe4oR9e2XKN6MzP+7XlXYI=</DigestValue>
  </Reference>
</SignedInfo>
<SignatureValue>iPQ6IET400CXfchWJcP22p2gK6RpEc9mkSgfoA94fL5UM6+AB5+IO6BbjsNt31q6MB8hR6lAIcnjzHzc5SeXvFP8Py2bqHTYJvcSA6KcKCQl1LiDNt12UwWiKpSkus2p0LdAeeZJNy9aDxjC/blUaZEr4uPFt0kGCD7h1NQM2SY=</SignatureValue>

問題は、この URL で xmlsec を使用して署名を検証しようとしたときです: http://www.aleksey.com/xmlsec/xmldsig-verifier.html。署名が無効であるというメッセージが表示されます。

コード内のエラーを何日も探していましたが、見つけることができません。PEM から XML ファイルへの変換に問題があるのではないかと考え始めていますが、これをテストする方法がわかりません。さらに、キーに変換したり、.NET で PEM ファイルを直接使用したりする他の方法は見つかりませんでした。

.NET で有効な署名を取得できた人はいますか?

4

5 に答える 5

3

はい、私はそれを行うことができました。問題はあなたの参照にあると思います。uri は、署名の対象となる要素の ID を指す必要があります。とにかく、以下のコードを確認してください。正しい方向に向けられることを願っています。

/クラウス

/// <summary>
    /// Signs an XmlDocument with an xml signature using the signing certificate given as argument to the method.
    /// </summary>
    /// <param name="doc">The XmlDocument to be signed</param>
    /// <param name="id">The is of the topmost element in the xmldocument</param>
    /// <param name="cert">The certificate used to sign the document</param>
    public static void SignDocument(XmlDocument doc, string id, X509Certificate2 cert)
    {
        SignedXml signedXml = new SignedXml(doc);
        signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
        signedXml.SigningKey = cert.PrivateKey;

        // Retrieve the value of the "ID" attribute on the root assertion element.
        Reference reference = new Reference("#" + id);

        reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
        reference.AddTransform(new XmlDsigExcC14NTransform());

        signedXml.AddReference(reference);

        // Include the public key of the certificate in the assertion.
        signedXml.KeyInfo = new KeyInfo();
        signedXml.KeyInfo.AddClause(new KeyInfoX509Data(cert, X509IncludeOption.WholeChain));

        signedXml.ComputeSignature();
        // Append the computed signature. The signature must be placed as the sibling of the Issuer element.
        XmlNodeList nodes = doc.DocumentElement.GetElementsByTagName("Issuer", Saml20Constants.ASSERTION);
        // doc.DocumentElement.InsertAfter(doc.ImportNode(signedXml.GetXml(), true), nodes[0]);            
        nodes[0].ParentNode.InsertAfter(doc.ImportNode(signedXml.GetXml(), true), nodes[0]);
    }
于 2009-12-01T10:05:08.127 に答える
1

私は今日同じ問題を抱えています.net xml C14変換がxmlsecに実装されているXML標準に従っていないためではないかと同僚の1人が疑っています。私は自分で変換を試み、それが本当に問題であるかどうかをお知らせします.

于 2013-08-09T06:45:14.097 に答える
1

あなたはxmlのすべてに署名しているように私には思えます:reference.Uri = "";

署名をドキュメントに挿入したり、間違った方法で挿入したりすると、署名が壊れる可能性があります。

また、名前空間と空白にも注意してください。その一部は署名されているため、後で署名されたドキュメントで作業するときに問題が発生する可能性があります。

于 2009-12-01T10:05:28.310 に答える
1

交換してみる

dataObject.Data = Doc.ChildNodes;

これとともに:

dataObject.Data = Doc.GetElementsByTagName("YourRootNodeNameHere");
于 2009-12-08T15:46:32.197 に答える