SignedXml은 잘못된 서명을 생성합니다
-
05-07-2019 - |
문제
.NET에서 XMLDSig 지원을 적절하게, 특히 SignEdXml 클래스를 제대로 작동 시키려고 노력했습니다. 저는 제 3 자 서비스를 구현하고 있으며 최근에 모든 메시지에 디지털 서명을 요구하기 시작했습니다 ...
내 문제는 유효한 서명을 생성 할 수 없다는 것입니다. 제 3 자 서비스와 내가 찾은 온라인 서명 검증자는 서명을 유효하지 않은 것으로보고합니다. 검증 서비스 (http://www.aleksey.com/xmlsec/xmldsig-verifier.html)는 Digest와 데이터 사이에 불일치가 있다고보고했으며 지금까지 내가 잘못하고있는 일을 알아낼 수 없었습니다.
관련 코드는 다음과 같습니다. 누군가 내 실수를 발견 할 수 있기를 바랍니다.
public static XDocument SignDocument(XDocument originalDocument, X509Certificate2 certificate)
{
var document = new XmlDocument();
document.LoadXml(originalDocument.ToString(SaveOptions.DisableFormatting));
if (document.DocumentElement == null)
throw new InvalidOperationException("Invalid XML document; no root element found.");
var signedDocument = new SignedXml(document);
Reference signatureReference = GetSignatureReference();
KeyInfo certificateKeyInfo = GetCertificateKeyInfo(certificate);
var dataObject = new DataObject("", "text/xml", "utf-8", document.DocumentElement);
signedDocument.AddReference(signatureReference);
signedDocument.AddObject(dataObject);
signedDocument.SigningKey = certificate.PrivateKey;
signedDocument.KeyInfo = certificateKeyInfo;
signedDocument.ComputeSignature();
return XDocument.Parse(signedDocument.GetXml().OuterXml, LoadOptions.PreserveWhitespace);
}
private static Reference GetSignatureReference()
{
var signatureReference = new Reference("");
signatureReference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
return signatureReference;
}
private static KeyInfo GetCertificateKeyInfo(X509Certificate certificate)
{
var certificateKeyInfo = new KeyInfo();
certificateKeyInfo.AddClause(new KeyInfoX509Data(certificate));
return certificateKeyInfo;
}
해결책
누구나 관심이있는 경우 문제를 해결하고 내 블로그에 썼습니다.http://thomasjo.com/blog/2009/08/04/xmldsig-in-the-net-framework.html
제휴하지 않습니다 StackOverflow