/
TestSignSimple.cs
236 lines (202 loc) · 11.2 KB
/
TestSignSimple.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using iText.Kernel.Pdf;
using iText.Signatures;
using NUnit.Framework;
using Org.BouncyCastle.Asn1.X509;
using BcX509 = Org.BouncyCastle.X509;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using static iText.Signatures.PdfSigner;
using iText.SigningExamples.Simple;
using System.Security.Cryptography.Pkcs;
using iText.Bouncycastle.Cert;
using iText.Bouncycastle.X509;
using iText.Commons.Bouncycastle.Cert;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Asn1;
namespace iText.SigningExamples.CngUtimaco
{
class TestSignSimple
{
[SetUp]
public void Init()
{
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadWrite))
{
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection ecdsaSigningCertificates = certificates.Find(X509FindType.FindBySubjectName, "Utimaco CNG ECDSA Signing Test", false);
if (ecdsaSigningCertificates.Count == 0)
{
CngProvider provider = new CngProvider("Utimaco CryptoServer Key Storage Provider");
CngKey key = CngKey.Open("DEMOecdsaKEY", provider);
ECDsaCng ecdsaKey = new ECDsaCng(key);
CertificateRequest request = new CertificateRequest("CN = Utimaco CNG ECDSA Signing Test", ecdsaKey, HashAlgorithmName.SHA512);
X509Certificate2 certificate = request.CreateSelfSigned(System.DateTimeOffset.Now, System.DateTimeOffset.Now.AddYears(2));
certificate.FriendlyName = "Utimaco CNG ECDSA Signing Test";
System.Console.WriteLine("Utimaco CNG ECDSA Signing Test Certificate generated:\n****\n{0}\n****", certificate);
store.Add(certificate);
}
X509Certificate2Collection rsaSigningCertificates = certificates.Find(X509FindType.FindBySubjectName, "Utimaco CNG RSA Signing Test", false);
if (rsaSigningCertificates.Count == 0)
{
CngProvider provider = new CngProvider("Utimaco CryptoServer Key Storage Provider");
CngKey key = CngKey.Open("DEMOrsaKEY", provider);
RSACng rsaKey = new RSACng(key);
CertificateRequest request = new CertificateRequest("CN = Utimaco CNG RSA Signing Test", rsaKey, HashAlgorithmName.SHA512, RSASignaturePadding.Pss);
X509Certificate2 certificate = request.CreateSelfSigned(System.DateTimeOffset.Now, System.DateTimeOffset.Now.AddYears(2));
certificate.FriendlyName = "Utimaco CNG RSA Signing Test";
System.Console.WriteLine("Utimaco CNG RSA Signing Test Certificate generated:\n****\n{0}\n****", certificate);
store.Add(certificate);
}
}
}
[Test]
public void TestCngSignEcdsaSimple()
{
string testFileName = @"..\..\..\resources\circles.pdf";
X509Certificate2 certificate;
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadOnly))
{
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection signingcertificates = certificates.Find(X509FindType.FindBySubjectName, "Utimaco CNG ECDSA Signing Test", false);
certificate = signingcertificates[0];
}
using (PdfReader pdfReader = new PdfReader(testFileName))
using (FileStream result = File.Create("circles-cng-signed-ecdsa-simple-custom.pdf"))
{
PdfSigner pdfSigner = new PdfSigner(pdfReader, result, new StampingProperties().UseAppendMode());
X509Certificate2ECDsaSignature signature = new X509Certificate2ECDsaSignature(certificate);
IX509Certificate[] certificateWrappers = new IX509Certificate[signature.GetChain().Length];
for (int i = 0; i < certificateWrappers.Length; ++i) {
certificateWrappers[i] = new X509CertificateBC(signature.GetChain()[i]);
}
pdfSigner.SignDetached(signature, certificateWrappers, null, null, null, 0, CryptoStandard.CMS);
}
}
[Test]
public void TestCngSignEcdsaSimpleGeneric()
{
string testFileName = @"..\..\..\resources\circles.pdf";
X509Certificate2 certificate;
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadOnly))
{
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection signingcertificates = certificates.Find(X509FindType.FindBySubjectName, "Utimaco CNG ECDSA Signing Test", false);
certificate = signingcertificates[0];
}
BcX509.X509Certificate bcCertificate = new BcX509.X509Certificate(X509CertificateStructure.GetInstance(certificate.RawData));
IX509Certificate[] chain = { new X509CertificateBC(bcCertificate) };
using (PdfReader pdfReader = new PdfReader(testFileName))
using (FileStream result = File.Create("circles-cng-signed-ecdsa-simple-generic.pdf"))
{
PdfSigner pdfSigner = new PdfSigner(pdfReader, result, new StampingProperties().UseAppendMode());
X509Certificate2Signature signature = new X509Certificate2Signature(certificate, "SHA512");
pdfSigner.SignDetached(signature, chain, null, null, null, 0, CryptoStandard.CMS);
}
}
[Test]
public void TestCngSignRsaSimpleGeneric()
{
string testFileName = @"..\..\..\resources\circles.pdf";
X509Certificate2 certificate;
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadOnly))
{
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection signingcertificates = certificates.Find(X509FindType.FindBySubjectName, "Utimaco CNG RSA Signing Test", false);
certificate = signingcertificates[0];
}
BcX509.X509Certificate bcCertificate = new BcX509.X509Certificate(X509CertificateStructure.GetInstance(certificate.RawData));
IX509Certificate[] chain = { new X509CertificateBC(bcCertificate) };
using (PdfReader pdfReader = new PdfReader(testFileName))
using (FileStream result = File.Create("circles-cng-signed-rsa-simple-generic.pdf"))
{
PdfSigner pdfSigner = new PdfSigner(pdfReader, result, new StampingProperties().UseAppendMode());
X509Certificate2Signature signature = new X509Certificate2Signature(certificate, "SHA512");
pdfSigner.SignDetached(signature, chain, null, null, null, 0, CryptoStandard.CMS);
}
}
[Test]
public void TestCngSignRsaSsaPssSimpleGeneric()
{
string testFileName = @"..\..\..\resources\circles.pdf";
X509Certificate2 certificate;
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadOnly))
{
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection signingcertificates = certificates.Find(X509FindType.FindBySubjectName, "Utimaco CNG RSA Signing Test", false);
certificate = signingcertificates[0];
}
BcX509.X509Certificate bcCertificate = new BcX509.X509Certificate(X509CertificateStructure.GetInstance(certificate.RawData));
IX509Certificate[] chain = { new X509CertificateBC(bcCertificate) };
using (PdfReader pdfReader = new PdfReader(testFileName))
using (FileStream result = File.Create("circles-cng-signed-rsassapss-simple-generic.pdf"))
{
PdfSigner pdfSigner = new PdfSigner(pdfReader, result, new StampingProperties().UseAppendMode());
X509Certificate2Signature signature = new X509Certificate2Signature(certificate, "SHA512");
signature.UsePssForRsaSsa = true;
pdfSigner.SignDetached(signature, chain, null, null, null, 0, CryptoStandard.CMS);
}
}
[Test]
public void TestCngSignEcdsaSimpleGenericContainer()
{
string testFileName = @"..\..\..\resources\circles.pdf";
X509Certificate2 certificate;
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser, OpenFlags.ReadOnly))
{
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2Collection signingcertificates = certificates.Find(X509FindType.FindBySubjectName, "Utimaco CNG ECDSA Signing Test", false);
certificate = signingcertificates[0];
}
X509Certificate2SignatureContainer signature = new X509Certificate2SignatureContainer(certificate, signer => {
signer.DigestAlgorithm = Oid.FromFriendlyName("SHA512", OidGroup.HashAlgorithm);
signer.SignedAttributes.Add(new Pkcs9SigningTime());
});
using (PdfReader pdfReader = new PdfReader(testFileName))
using (FileStream result = File.Create("circles-cng-signed-ecdsa-simple-generic-container.pdf"))
{
PdfSigner pdfSigner = new PdfSigner(pdfReader, result, new StampingProperties().UseAppendMode());
pdfSigner.SignExternalContainer(signature, 8192);
}
}
}
class X509Certificate2ECDsaSignature : IExternalSignature
{
public X509Certificate2ECDsaSignature(X509Certificate2 certificate)
{
this.certificate = certificate;
}
public Org.BouncyCastle.X509.X509Certificate[] GetChain()
{
var bcCertificate = new Org.BouncyCastle.X509.X509Certificate(Org.BouncyCastle.Asn1.X509.X509CertificateStructure.GetInstance(certificate.RawData));
return new Org.BouncyCastle.X509.X509Certificate[] { bcCertificate };
}
public string GetSignatureAlgorithmName()
{
return "ECDSA";
}
public ISignatureMechanismParams GetSignatureMechanismParameters()
{
return null;
}
public string GetDigestAlgorithmName()
{
return "SHA512";
}
public byte[] Sign(byte[] message)
{
using (ECDsa ecdsa = certificate.GetECDsaPrivateKey())
{
return PlainToDer(ecdsa.SignData(message, HashAlgorithmName.SHA512));
}
}
byte[] PlainToDer(byte[] plain)
{
int valueLength = plain.Length / 2;
BigInteger r = new BigInteger(1, plain, 0, valueLength);
BigInteger s = new BigInteger(1, plain, valueLength, valueLength);
return new DerSequence(new DerInteger(r), new DerInteger(s)).GetEncoded(Asn1Encodable.Der);
}
X509Certificate2 certificate;
}
}