-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDSignatures.cs
97 lines (83 loc) · 3.52 KB
/
RDSignatures.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Xml;
namespace RDSignatures
{
public static class RDSignaturesClass
{
public static SignedXml SignXmlFileWithCertificate(string xml, X509Certificate2 certRoot, X509Certificate2 certChild, String CanocalizationMethod, String SignatureMethod)
{
String msgError = "0";
try
{
msgError = "0.5";
if (null == xml)
throw new ArgumentNullException();
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.LoadXml(xml);
SignedXml signedXml = new SignedXml(doc);
signedXml.SigningKey = certChild.PrivateKey;
signedXml.SignedInfo.CanonicalizationMethod = CanocalizationMethod; // "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"; //Exemplo: SignedXml.XmlDsigCanonicalizationWithCommentsUrl
signedXml.SignedInfo.SignatureMethod = SignatureMethod; // "http://www.w3.org/2000/09/xmldsig#rsa-sha1"; //Exemplo: SignedXml.XmlDsigRSASHA1Url
Reference reference = new Reference();
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
XmlDsigC14NTransform env2 = new XmlDsigC14NTransform();
reference.AddTransform(env);
reference.AddTransform(env2);
signedXml.AddReference(reference);
var KeyInfoX509 = new KeyInfoX509Data();
KeyInfoX509.AddCertificate(certChild);
KeyInfoX509.AddCertificate(certRoot);
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(KeyInfoX509);
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature();
return signedXml;
}
catch (Exception ex)
{
throw ex;
}
}
public static X509Certificate2 ReturnCertificate(String nameCertificate)
{
try
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store = new X509Store(StoreName.CertificateAuthority, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
foreach (X509Certificate2 certificate in store.Certificates)
{
if (certificate.Subject == nameCertificate)
return certificate;
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
public static X509Certificate2 ReturnCertificate(String prefix, String nameCertificate)
{
try
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
foreach (X509Certificate2 certificate in store.Certificates)
{
if ((certificate.SubjectName.Name.Contains(nameCertificate) && certificate.IssuerName.Name.Contains(prefix)))
return certificate;
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
}
}