J
Jesus Suarez
Hi all.
I have a problem when signing the xml using x509 certificate created with
private and public keys.
Here is my code example
getting certificate:
Dim cert As X509Certificate2 = Nothing
Dim store As New X509Store("My", StoreLocation.LocalMachine)
Try
store.Open(OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly)
Dim CertCol As X509Certificate2Collection = store.Certificates
Dim c As X509Certificate2
For Each c In CertCol
If c.Subject.Contains(CertificateSubject) Then
cert = c
Exit For
End If
Next c
If cert Is Nothing Then
Throw New CryptographicException("The certificate could not be found.")
End If
Finally
store.Close()
End Try
Return cert
signing xml:
Dim MSCert As X509Certificate2 = cert 'GetCertificateBySubject(conf_Cer)
If MSCert Is Nothing Then Throw New Exception("Cert is nothing")
Dim signedXml As New SignedXml(Doc)
signedXml.SigningKey = MSCert.PrivateKey
Dim reference As New Reference()
reference.Uri = ""
Dim trns As XmlDsigC14NTransform = New XmlDsigC14NTransform()
reference.AddTransform(trns)
Dim env As New XmlDsigEnvelopedSignatureTransform()
reference.AddTransform(env)
signedXml.AddReference(reference)
Dim keyInfo As New KeyInfo()
keyInfo.AddClause(New KeyInfoX509Data(MSCert))
signedXml.KeyInfo = keyInfo
signedXml.ComputeSignature()
Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, True))
The keyset does not exist exception happens here:
Dim MSCert As X509Certificate2 = cert 'GetCertificateBySubject(conf_Cer)
If MSCert Is Nothing Then Throw New Exception("Cert is nothing")
Dim signedXml As New SignedXml(Doc)
signedXml.SigningKey = MSCert.PrivateKey
I have a problem when signing the xml using x509 certificate created with
private and public keys.
Here is my code example
getting certificate:
Dim cert As X509Certificate2 = Nothing
Dim store As New X509Store("My", StoreLocation.LocalMachine)
Try
store.Open(OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly)
Dim CertCol As X509Certificate2Collection = store.Certificates
Dim c As X509Certificate2
For Each c In CertCol
If c.Subject.Contains(CertificateSubject) Then
cert = c
Exit For
End If
Next c
If cert Is Nothing Then
Throw New CryptographicException("The certificate could not be found.")
End If
Finally
store.Close()
End Try
Return cert
signing xml:
Dim MSCert As X509Certificate2 = cert 'GetCertificateBySubject(conf_Cer)
If MSCert Is Nothing Then Throw New Exception("Cert is nothing")
Dim signedXml As New SignedXml(Doc)
signedXml.SigningKey = MSCert.PrivateKey
Dim reference As New Reference()
reference.Uri = ""
Dim trns As XmlDsigC14NTransform = New XmlDsigC14NTransform()
reference.AddTransform(trns)
Dim env As New XmlDsigEnvelopedSignatureTransform()
reference.AddTransform(env)
signedXml.AddReference(reference)
Dim keyInfo As New KeyInfo()
keyInfo.AddClause(New KeyInfoX509Data(MSCert))
signedXml.KeyInfo = keyInfo
signedXml.ComputeSignature()
Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, True))
The keyset does not exist exception happens here:
Dim MSCert As X509Certificate2 = cert 'GetCertificateBySubject(conf_Cer)
If MSCert Is Nothing Then Throw New Exception("Cert is nothing")
Dim signedXml As New SignedXml(Doc)
signedXml.SigningKey = MSCert.PrivateKey