Debian Codegraph @

external/corefx/src/System.Security.Cryptography.Pkcs/tests/EnvelopedCms/DecryptTests.cs

raw
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Security.Cryptography.Xml;
6 using System.Security.Cryptography.X509Certificates;
7 using Xunit;
8
9 using Test.Cryptography;
10 using System.Security.Cryptography.Pkcs.Tests;
11
12 namespace System.Security.Cryptography.Pkcs.EnvelopedCmsTests.Tests
13 {
14 public static partial class DecryptTests
15 {
16 public static bool SupportsCngCertificates { get; } = (!PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer);
17 public static bool SupportsIndefiniteLengthEncoding { get; } = !PlatformDetection.IsFullFramework;
18
19 [ConditionalFact(nameof(SupportsCngCertificates))]
20 [OuterLoop(/* Leaks key on disk if interrupted */)]
21 public static void Decrypt_IssuerAndSerial()
22 {
23 byte[] content = { 5, 112, 233, 43 };
24 ContentInfo contentInfo = new ContentInfo(content);
25 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransfer1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber);
26 }
27
28 [ConditionalFact(nameof(SupportsCngCertificates))]
29 [OuterLoop(/* Leaks key on disk if interrupted */)]
30 public static void Decrypt_Ski()
31 {
32 byte[] content = { 6, 3, 128, 33, 44 };
33 ContentInfo contentInfo = new ContentInfo(content);
34 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransfer1, contentInfo, Oids.Aes256, SubjectIdentifierType.SubjectKeyIdentifier);
35 }
36
37 [Fact]
38 [OuterLoop(/* Leaks key on disk if interrupted */)]
39 public static void Decrypt_Capi()
40 {
41 byte[] content = { 5, 77, 32, 33, 2, 34 };
42 ContentInfo contentInfo = new ContentInfo(content);
43 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber);
44 }
45
46 [ConditionalFact(nameof(SupportsCngCertificates))]
47 [OuterLoop(/* Leaks key on disk if interrupted */)]
48 public static void Decrypt_256()
49 {
50 byte[] content = { 5, 77, 32, 33, 2, 34 };
51 ContentInfo contentInfo = new ContentInfo(content);
52 TestSimpleDecrypt_RoundTrip(Certificates.RSASha256KeyTransfer1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber);
53 }
54
55 [ConditionalFact(nameof(SupportsCngCertificates))]
56 [OuterLoop(/* Leaks key on disk if interrupted */)]
57 public static void Decrypt_384()
58 {
59 byte[] content = { 5, 77, 32, 33, 2, 34 };
60 ContentInfo contentInfo = new ContentInfo(content);
61 TestSimpleDecrypt_RoundTrip(Certificates.RSASha384KeyTransfer1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber);
62 }
63
64 [ConditionalFact(nameof(SupportsCngCertificates))]
65 [OuterLoop(/* Leaks key on disk if interrupted */)]
66 public static void Decrypt_512()
67 {
68 byte[] content = { 5, 77, 32, 33, 2, 34 };
69 ContentInfo contentInfo = new ContentInfo(content);
70 TestSimpleDecrypt_RoundTrip(Certificates.RSASha512KeyTransfer1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber);
71 }
72
73 [ConditionalFact(nameof(SupportsCngCertificates))]
74 [OuterLoop("Leaks key on disk if interrupted")]
75 public static void Decrypt_512_FixedValue()
76 {
77 byte[] content = { 5, 77, 32, 33, 2, 34 };
78 byte[] message = (
79 "3082012506092A864886F70D010703A0820116308201120201003181CE3081CB" +
80 "02010030343020311E301C060355040313155253415368613531324B65795472" +
81 "616E736665723102102F5D9D58A5F41B844650AA233E68F105300D06092A8648" +
82 "86F70D01010105000481803163AA33F8F5E033DC03AE98CCEE158199589FC420" +
83 "19200DCC1D202309CCCAF79CC0278B9502B5709F1311E522DA325338136D3F1E" +
84 "A271FAEA978CC656A3CB94B1C6A8D7AFC836C3193DB693E8B8767472C2C23125" +
85 "BA11E7D0623E4C8B848826BBF99EB411CB88B4731740D1AD834F0E4076BAD0D4" +
86 "BA695CFE8CDB2DE3E77196303C06092A864886F70D010701301D060960864801" +
87 "650304012A0410280AC7A629BFC9FD6FB24F8A42F094B48010B78CDFECFF32A8" +
88 "E86D448989382A93E7"
89 ).HexToByteArray();
90
91 VerifySimpleDecrypt(message, Certificates.RSASha512KeyTransfer1, new ContentInfo(content));
92 }
93
94 [ConditionalFact(nameof(SupportsCngCertificates))]
95 [OuterLoop("Leaks key on disk if interrupted")]
96 public static void Decrypt_512_NoData_FixedValue()
97 {
98 // This is the Decrypt_512_FixedData test re-encoded to remove the
99 // encryptedContentInfo.encryptedContent optional value.
100 byte[] content = Array.Empty<byte>();
101 byte[] message = (
102 "3082011306092A864886F70D010703A0820104308201000201003181CE3081CB" +
103 "02010030343020311E301C060355040313155253415368613531324B65795472" +
104 "616E736665723102102F5D9D58A5F41B844650AA233E68F105300D06092A8648" +
105 "86F70D01010105000481803163AA33F8F5E033DC03AE98CCEE158199589FC420" +
106 "19200DCC1D202309CCCAF79CC0278B9502B5709F1311E522DA325338136D3F1E" +
107 "A271FAEA978CC656A3CB94B1C6A8D7AFC836C3193DB693E8B8767472C2C23125" +
108 "BA11E7D0623E4C8B848826BBF99EB411CB88B4731740D1AD834F0E4076BAD0D4" +
109 "BA695CFE8CDB2DE3E77196302A06092A864886F70D010701301D060960864801" +
110 "650304012A0410280AC7A629BFC9FD6FB24F8A42F094B4"
111 ).HexToByteArray();
112
113 if (PlatformDetection.IsFullFramework)
114 {
115 // On NetFx when Array.Empty should be returned an array of 6 zeros is
116 // returned instead.
117 content = new byte[6];
118 }
119
120 VerifySimpleDecrypt(message, Certificates.RSASha512KeyTransfer1, new ContentInfo(content));
121 }
122
123 [ConditionalFact(nameof(SupportsCngCertificates))]
124 [OuterLoop("Leaks key on disk if interrupted")]
125 public static void Decrypt_512_CekDoesNotDecrypt_FixedValue()
126 {
127 // This is the Decrypt_512_NoData_FixedValue test except that the last
128 // byte of the recipient encrypted key has been changed from 0x96 to 0x95
129 // (the sequence 7195 identifies the changed byte)
130 byte[] content = Array.Empty<byte>();
131 byte[] message = (
132 "3082011306092A864886F70D010703A0820104308201000201003181CE3081CB" +
133 "02010030343020311E301C060355040313155253415368613531324B65795472" +
134 "616E736665723102102F5D9D58A5F41B844650AA233E68F105300D06092A8648" +
135 "86F70D01010105000481803163AA33F8F5E033DC03AE98CCEE158199589FC420" +
136 "19200DCC1D202309CCCAF79CC0278B9502B5709F1311E522DA325338136D3F1E" +
137 "A271FAEA978CC656A3CB94B1C6A8D7AFC836C3193DB693E8B8767472C2C23125" +
138 "BA11E7D0623E4C8B848826BBF99EB411CB88B4731740D1AD834F0E4076BAD0D4" +
139 "BA695CFE8CDB2DE3E77195302A06092A864886F70D010701301D060960864801" +
140 "650304012A0410280AC7A629BFC9FD6FB24F8A42F094B4"
141 ).HexToByteArray();
142
143 Assert.ThrowsAny<CryptographicException>(
144 () => VerifySimpleDecrypt(message, Certificates.RSASha512KeyTransfer1, new ContentInfo(content)));
145 }
146
147 [Fact]
148 [OuterLoop(/* Leaks key on disk if interrupted */)]
149 public static void Decrypt_SignedWithinEnveloped()
150 {
151 byte[] content =
152 ("3082032506092a864886f70d010702a082031630820312020101310b300906052b0e03021a0500301206092a864886f70d01"
153 + "0701a0050403010203a08202103082020c30820179a00302010202105d2ffff863babc9b4d3c80ab178a4cca300906052b0e"
154 + "03021d0500301e311c301a060355040313135253414b65795472616e736665724361706931301e170d313530343135303730"
155 + "3030305a170d3235303431353037303030305a301e311c301a060355040313135253414b65795472616e7366657243617069"
156 + "3130819f300d06092a864886f70d010101050003818d0030818902818100aa272700586c0cc41b05c65c7d846f5a2bc27b03"
157 + "e301c37d9bff6d75b6eb6671ba9596c5c63ba2b1af5c318d9ca39e7400d10c238ac72630579211b86570d1a1d44ec86aa8f6"
158 + "c9d2b4e283ea3535923f398a312a23eaeacd8d34faaca965cd910b37da4093ef76c13b337c1afab7d1d07e317b41a336baa4"
159 + "111299f99424408d0203010001a3533051304f0603551d0104483046801015432db116b35d07e4ba89edb2469d7aa120301e"
160 + "311c301a060355040313135253414b65795472616e73666572436170693182105d2ffff863babc9b4d3c80ab178a4cca3009"
161 + "06052b0e03021d05000381810081e5535d8eceef265acbc82f6c5f8bc9d84319265f3ccf23369fa533c8dc1938952c593166"
162 + "2d9ecd8b1e7b81749e48468167e2fce3d019fa70d54646975b6dc2a3ba72d5a5274c1866da6d7a5df47938e034a075d11957"
163 + "d653b5c78e5291e4401045576f6d4eda81bef3c369af56121e49a083c8d1adb09f291822e99a4296463181d73081d4020101"
164 + "3032301e311c301a060355040313135253414b65795472616e73666572436170693102105d2ffff863babc9b4d3c80ab178a"
165 + "4cca300906052b0e03021a0500300d06092a864886f70d010101050004818031a718ea1483c88494661e1d3dedfea0a3d97e"
166 + "eb64c3e093a628b257c0cfc183ecf11697ac84f2af882b8de0c793572af38dc15d1b6f3d8f2392ba1cc71210e177c146fd16"
167 + "b77a583b6411e801d7a2640d612f2fe99d87e9718e0e505a7ab9536d71dbde329da21816ce7da1416a74a3e0a112b86b33af"
168 + "336a2ba6ae2443d0ab").HexToByteArray();
169
170 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Signed), content);
171 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber);
172 }
173
174 [Fact]
175 [OuterLoop(/* Leaks key on disk if interrupted */)]
176 public static void Decrypt_EnvelopedWithinEnveloped()
177 {
178 byte[] content =
179 ("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253"
180 + "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004818013"
181 + "dc0eb2984a445d04a1f6246b8fe41f1d24507548d449d454d5bb5e0638d75ed101bf78c0155a5d208eb746755fbccbc86923"
182 + "8443760a9ae94770d6373e0197be23a6a891f0c522ca96b3e8008bf23547474b7e24e7f32e8134df3862d84f4dea2470548e"
183 + "c774dd74f149a56cdd966e141122900d00ad9d10ea1848541294a1302b06092a864886f70d010701301406082a864886f70d"
184 + "030704089c8119f6cf6b174c8008bcea3a10d0737eb9").HexToByteArray();
185
186
187 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7SignedEnveloped), content);
188 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber);
189 }
190
191 [ConditionalFact(nameof(SupportsCngCertificates))]
192 [OuterLoop(/* Leaks key on disk if interrupted */)]
193 public static void DecryptMultipleRecipients()
194 {
195 // Force Decrypt() to try multiple recipients. Ensure that a failure to find a matching cert in one doesn't cause it to quit early.
196
197 CertLoader[] certLoaders = new CertLoader[]
198 {
199 Certificates.RSAKeyTransfer1,
200 Certificates.RSAKeyTransfer2,
201 Certificates.RSAKeyTransfer3,
202 };
203
204 byte[] content = { 6, 3, 128, 33, 44 };
205 EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(content), new AlgorithmIdentifier(new Oid(Oids.Aes256)));
206 CmsRecipientCollection recipients = new CmsRecipientCollection();
207 foreach (CertLoader certLoader in certLoaders)
208 {
209 recipients.Add(new CmsRecipient(certLoader.GetCertificate()));
210 }
211 ecms.Encrypt(recipients);
212 byte[] encodedMessage = ecms.Encode();
213
214 ecms = new EnvelopedCms();
215 ecms.Decode(encodedMessage);
216
217 // How do we know that Decrypt() tries receipients in the order they appear in ecms.RecipientInfos? Because we wrote the implementation.
218 // Not that some future implementation can't ever change it but it's the best guess we have.
219 RecipientInfo me = ecms.RecipientInfos[2];
220
221 CertLoader matchingCertLoader = null;
222 for (int index = 0; index < recipients.Count; index++)
223 {
224 if (recipients[index].Certificate.Issuer == ((X509IssuerSerial)(me.RecipientIdentifier.Value)).IssuerName)
225 {
226 matchingCertLoader = certLoaders[index];
227 break;
228 }
229 }
230 Assert.NotNull(matchingCertLoader);
231
232 using (X509Certificate2 cert = matchingCertLoader.TryGetCertificateWithPrivateKey())
233 {
234 if (cert == null)
235 return; // Sorry - CertLoader is not configured to load certs with private keys - we've tested as much as we can.
236 X509Certificate2Collection extraStore = new X509Certificate2Collection();
237 extraStore.Add(cert);
238 ecms.Decrypt(extraStore);
239 }
240
241 ContentInfo contentInfo = ecms.ContentInfo;
242 Assert.Equal<byte>(content, contentInfo.Content);
243 }
244
245 [Fact]
246 public static void EncryptToNegativeSerialNumber()
247 {
248 CertLoader negativeSerial = Certificates.NegativeSerialNumber;
249
250 const string expectedSerial = "FD319CB1514B06AF49E00522277E43C8";
251
252 byte[] content = { 1, 2, 3 };
253 ContentInfo contentInfo = new ContentInfo(content);
254 EnvelopedCms cms = new EnvelopedCms(contentInfo);
255
256 using (X509Certificate2 cert = negativeSerial.GetCertificate())
257 {
258 Assert.Equal(expectedSerial, cert.SerialNumber);
259
260 CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, cert);
261 cms.Encrypt(recipient);
262 }
263
264 EnvelopedCms cms2 = new EnvelopedCms();
265 cms2.Decode(cms.Encode());
266
267 RecipientInfoCollection recipients = cms2.RecipientInfos;
268 Assert.Equal(1, recipients.Count);
269
270 RecipientInfo recipientInfo = recipients[0];
271 Assert.Equal(SubjectIdentifierType.IssuerAndSerialNumber, recipientInfo.RecipientIdentifier.Type);
272
273 X509IssuerSerial issuerSerial = (X509IssuerSerial)recipientInfo.RecipientIdentifier.Value;
274 Assert.Equal(expectedSerial, issuerSerial.SerialNumber);
275
276 using (X509Certificate2 cert = negativeSerial.TryGetCertificateWithPrivateKey())
277 {
278 Assert.Equal(expectedSerial, cert.SerialNumber);
279
280 cms2.Decrypt(new X509Certificate2Collection(cert));
281 }
282
283 Assert.Equal(content, cms2.ContentInfo.Content);
284 }
285
286 [Fact]
287 [OuterLoop(/* Leaks key on disk if interrupted */)]
288 public static void TestDecryptSimpleAes128_IssuerAndSerial()
289 {
290 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSAKeyTransfer1.GetCertificate()
291 // and of type IssuerAndSerialNumber. The symmetric algorithm is Aes128
292 byte[] encryptedMessage =
293 ("3082011F06092A864886F70D010703A08201103082010C0201003181C83081C5020100302E301A311830160"
294 + "603550403130F5253414B65795472616E7366657231021031D935FB63E8CFAB48A0BF7B397B67C0300D0609"
295 + "2A864886F70D0101073000048180862175CD3B2932235A67C6A025F75CDA1A43B53E785370895BA9AC8D0DD"
296 + "318EB36DFAE275B16ABD497FEBBFCF2D4B3F38C75B91DC40941A2CC1F7F47E701EEA2D5A770C485565F8726"
297 + "DC0D59DDE17AA6DB0F9384C919FC8BC6CB561A980A9AE6095486FDF9F52249FB466B3676E4AEFE4035C15DC"
298 + "EE769F25E4660D4BE664E7F303C06092A864886F70D010701301D060960864801650304010204100A068EE9"
299 + "03E085EA5A03D1D8B4B73DD88010740E5DE9B798AA062B449F104D0F5D35").HexToByteArray();
300
301 byte[] expectedContent = { 1, 2, 3, 4 };
302 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
303 CertLoader certLoader = Certificates.RSAKeyTransfer1;
304
305 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
306 }
307
308 [Fact]
309 [OuterLoop(/* Leaks key on disk if interrupted */)]
310 public static void TestDecryptSimpleAes192_IssuerAndSerial()
311 {
312 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSAKeyTransfer1.GetCertificate()
313 // and of type IssuerAndSerialNumber. The symmetric algorithm used is Aes192
314 byte[] encryptedMessage =
315 ("3082011F06092A864886F70D010703A08201103082010C0201003181C83081C5020100302E301A311830160"
316 + "603550403130F5253414B65795472616E7366657231021031D935FB63E8CFAB48A0BF7B397B67C0300D0609"
317 + "2A864886F70D010107300004818029B82454B4C301F277D7872A14695A41ED24FD37AC4C9942F9EE96774E0"
318 + "C6ACC18E756993A38AB215E5702CD34F244E52402DA432E8B79DF748405135E8A6D8CB78D88D9E4C142565C"
319 + "06F9FAFB32F5A9A4074E10FCCB0758A708CA758C12A17A4961969FCB3B2A6E6C9EB49F5E688D107E1B1DF3D"
320 + "531BC684B944FCE6BD4550C303C06092A864886F70D010701301D06096086480165030401160410FD7CBBF5"
321 + "6101854387E584C1B6EF3B08801034BD11C68228CB683E0A43AB5D27A8A4").HexToByteArray();
322
323 byte[] expectedContent = { 1, 2, 3, 4 };
324 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
325 CertLoader certLoader = Certificates.RSAKeyTransfer1;
326
327 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
328 }
329
330 [Fact]
331 [OuterLoop(/* Leaks key on disk if interrupted */)]
332 public static void TestDecryptSimpleAes256_IssuerAndSerial()
333 {
334 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSAKeyTransfer1.GetCertificate()
335 // and of type IssuerAndSerialNumber. The symmetric algorithm used is Aes256
336 byte[] encryptedMessage =
337 ("3082011F06092A864886F70D010703A08201103082010C0201003181C83081C5020100302E301A311830160"
338 + "603550403130F5253414B65795472616E7366657231021031D935FB63E8CFAB48A0BF7B397B67C0300D0609"
339 + "2A864886F70D01010730000481800215BF7505BCD5D083F8EFDA01A4F91D61DE3967779B2F5E4360593D4CB"
340 + "96474E36198531A5E20E417B04C5C7E3263C3301DF8FA888FFBECC796500D382858379059C986285AFD605C"
341 + "B5DE125487CCA658DF261C836720E2E14440DA60E2F12D6D5E3992A0DB59973929DF6FC23D8E891F97CA956"
342 + "2A7AD160B502FA3C10477AA303C06092A864886F70D010701301D060960864801650304012A04101287FE80"
343 + "93F3C517AE86AFB95E599D7E80101823D88F47191857BE0743C4C730E39E").HexToByteArray();
344
345 byte[] expectedContent = { 1, 2, 3, 4 };
346 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
347 CertLoader certLoader = Certificates.RSAKeyTransfer1;
348
349 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
350 }
351
352 [Fact]
353 [OuterLoop(/* Leaks key on disk if interrupted */)]
354 public static void TestDecryptSimpleTripleDes_IssuerAndSerial()
355 {
356 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSAKeyTransfer1.GetCertificate()
357 // and of type IssuerAndSerialNumber. The symmetric algorithm used is 3DES-CBC
358 byte[] encryptedMessage =
359 ("3082010C06092A864886F70D010703A081FE3081FB0201003181C83081C5020100302E301A3118301606035"
360 + "50403130F5253414B65795472616E7366657231021031D935FB63E8CFAB48A0BF7B397B67C0300D06092A86"
361 + "4886F70D0101010500048180062F6F16637C8F35B73924AD85BA47D99DBB4800CB8F0C4094F6896050B7C1F"
362 + "11CE79BEE55A638EAAE70F2C32C01FC24B8D09D9D574CB7373788C8BC3A4748124154338C74B644A2A11750"
363 + "9E97D1B3535FAE70E4E7C8F2F866232CBFC6448E89CF9D72B948EDCF9C9FC9C153BCC7104680282A4BBBC1E"
364 + "E367F094F627EE45FCD302B06092A864886F70D010701301406082A864886F70D030704081E3F12D42E4041"
365 + "58800877A4A100165DD0F2").HexToByteArray();
366
367 byte[] expectedContent = { 1, 2, 3, 4 };
368 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
369 CertLoader certLoader = Certificates.RSAKeyTransfer1;
370
371 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
372 }
373
374 [Fact]
375 [OuterLoop(/* Leaks key on disk if interrupted */)]
376 public static void TestDecryptSimpleAes256_Ski()
377 {
378 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSAKeyTransfer1.GetCertificate()
379 // and of type SubjectKeyIdentifier. The symmetric algorithm used is Aes256
380 byte[] encryptedMessage =
381 ("3082010306092A864886F70D010703A081F53081F20201023181AE3081AB0201028014F2008AA9FA3742E83"
382 + "70CB1674CE1D1582921DCC3300D06092A864886F70D010101050004818055F258073615B95426A7021E1B30"
383 + "9CFE8DD135B58D29F174B9FE19AE80CFC84621BCE3DBD63A5422AF30A6FAA3E2DFC05CB1AB5AB4FBA6C84EB"
384 + "1C2E17D5BE5C4959DBE8F96BF1A9701F55B697843032EEC7AFEC58A36815168F017DCFD70C74AD05C48B5E4"
385 + "D9DDEE409FDC9DC3326B6C5BA9F433A9E031FF9B09473176637F50303C06092A864886F70D010701301D060"
386 + "960864801650304012A0410314DA87435ED110DFE4F52FA70CEF7B080104DDA6C617338DEBDD10913A9141B"
387 + "EE52").HexToByteArray();
388
389 byte[] expectedContent = { 1, 2, 3, 4 };
390 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
391 CertLoader certLoader = Certificates.RSAKeyTransfer1;
392
393 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
394 }
395
396 [Fact]
397 [OuterLoop(/* Leaks key on disk if interrupted */)]
398 public static void TestDecryptSimpleAes256_RsaTransferCapi()
399 {
400 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSAKeyTransferCapi1.GetCertificate()
401 // and of type IssuerAndSerialNumber. The symmetric algorithm used is Aes256
402 byte[] encryptedMessage =
403 ("3082012306092A864886F70D010703A0820114308201100201003181CC3081C90201003032301E311C301A0"
404 + "60355040313135253414B65795472616E73666572436170693102105D2FFFF863BABC9B4D3C80AB178A4CCA"
405 + "300D06092A864886F70D01010730000481804F3F4A6707B329AB9A7343C62F20D5C1EAF4E74ECBB2DC66D1C"
406 + "642FC4AA3E40FC4C13547C6C9F73D525EE2FE4147B2043B8FEBF8604C0E4091C657B48DFD83A322F0879580"
407 + "FA002C9B27AD1FCF9B8AF24EDDA927BB6728D11530B3F96EBFC859ED6B9F7B009F992171FACB587A7D05E8B"
408 + "467B3A1DACC08B2F3341413A7E96576303C06092A864886F70D010701301D060960864801650304012A0410"
409 + "6F911E14D9D991DAB93C0B7738D1EC208010044264D201501735F73052FFCA4B2A95").HexToByteArray();
410
411 byte[] expectedContent = { 1, 2, 3, 4 };
412 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
413 CertLoader certLoader = Certificates.RSAKeyTransferCapi1;
414
415 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
416 }
417
418 [Fact]
419 [OuterLoop(/* Leaks key on disk if interrupted */)]
420 public static void TestDecryptSimpleAes256_RsaSha256()
421 {
422 // Message encrypted on framework for a recipient using the certificate returned by
423 // Certificates.RSASha256KeyTransfer1.GetCertificate() and of type IssuerAndSerialNumber. The symmetric algorithm used is Aes256
424 byte[] encryptedMessage =
425 ("3082012506092A864886F70D010703A0820116308201120201003181CE3081CB02010030343020311E301C0"
426 + "60355040313155253415368613235364B65795472616E7366657231021072C6C7734916468C4D608253DA01"
427 + "7676300D06092A864886F70D01010730000481805C32FA32EBDCFFC3595166EEDACFC9E9D60842105B581E1"
428 + "8B85DE1409F4C999995637153480438530955EE4481A3B27B866FF4E106A525CDFFC6941BDD01EFECCC6CCC"
429 + "82A3D7F743F7543AB20A61A7831FE4DFB24A1652B072B3758FE4B2588D3B94A29575B6422DC5EF52E432565"
430 + "36CA25A11BB92817D61FEAFBDDDEC6EE331303C06092A864886F70D010701301D060960864801650304012A"
431 + "041021D59FDB89C13A3EC3766EF32FB333D080105AE8DEB71DF50DD85F66FEA63C8113F4").HexToByteArray();
432
433 byte[] expectedContent = { 1, 2, 3, 4 };
434 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
435 CertLoader certLoader = Certificates.RSASha256KeyTransfer1;
436
437 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
438 }
439
440 [Fact]
441 [OuterLoop(/* Leaks key on disk if interrupted */)]
442 public static void TestDecryptSimpleAes256_RsaSha384()
443 {
444 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSASha384KeyTransfer1.GetCertificate()
445 // and of type IssuerAndSerialNumber. The symmetric algorithm used is Aes256
446 byte[] encryptedMessage =
447 ("3082012506092A864886F70D010703A0820116308201120201003181CE3081CB02010030343020311E301C0"
448 + "60355040313155253415368613338344B65795472616E736665723102103C724FB7A0159A9345CAAC9E3DF5"
449 + "F136300D06092A864886F70D010107300004818011C1B85914331C005EA89E30D00364821B29BC0C459A22D"
450 + "917494A1092CDBDA2022792E46C5E88BAD0EE3FD4927B856722311F9B17934FB29CAB8FE595C2AB2B20096B"
451 + "9E2FC6F9D7B92125F571CBFC945C892EE4764D9B63369350FD2DAEFE455B367F48E100CB461F112808E792A"
452 + "8AA49B66C79E511508A877530BBAA896696303C06092A864886F70D010701301D060960864801650304012A"
453 + "0410D653E25E06BFF2EEB0BED4A90D00FE2680106B7EF143912ABA5C24F5E2C151E59D7D").HexToByteArray();
454
455 byte[] expectedContent = { 1, 2, 3, 4 };
456 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
457 CertLoader certLoader = Certificates.RSASha384KeyTransfer1;
458
459 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
460 }
461
462 [Fact]
463 [OuterLoop(/* Leaks key on disk if interrupted */)]
464 public static void TestDecryptSimpleAes256_RsaSha512()
465 {
466 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSASha512KeyTransfer1.GetCertificate()
467 // and of type IssuerAndSerialNumber. The symmetric algorithm used is Aes256
468 byte[] encryptedMessage =
469 ("3082012506092A864886F70D010703A0820116308201120201003181CE3081CB02010030343020311E301C0"
470 + "60355040313155253415368613531324B65795472616E736665723102102F5D9D58A5F41B844650AA233E68"
471 + "F105300D06092A864886F70D01010730000481802156D42FF5ED2F0338302E7298EF79BA1D04E20E68B079D"
472 + "B3239120E1FC03FEDA8B544F59142AACAFBC5E58205E8A0D124AAD17B5DCAA39BFC6BA634E820DE623BFDB6"
473 + "582BC48AF1B3DEF6849A57D2033586AF01079D67C9AB3AA9F6B51754BCC479A19581D4045EBE23145370219"
474 + "98ECB6F5E1BCF8D6BED6A75FE957A40077D303C06092A864886F70D010701301D060960864801650304012A"
475 + "04100B696608E489E7C35914D0A3DB9EB27F80103D362181B54721FB2CB7CE461CB31030").HexToByteArray();
476
477 byte[] expectedContent = { 1, 2, 3, 4 };
478 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
479 CertLoader certLoader = Certificates.RSASha512KeyTransfer1;
480
481 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
482 }
483
484 [Fact]
485 [OuterLoop(/* Leaks key on disk if interrupted */)]
486 public static void TestDecryptSimple_ExplicitSki()
487 {
488 // Message encrypted on framework for a recipient using the certificate returned by Certificates.RSAKeyTransfer_ExplicitSki.GetCertificate()
489 // and of type SubjectKeyIdentifier. The symmetric algorithm used is Aes256
490 byte[] encryptedMessage =
491 ("3082018806092A864886F70D010703A082017930820175020102318201303082012C020102801401952851C"
492 + "55DB594B0C6167F5863C5B6B67AEFE6300D06092A864886F70D010101050004820100269EAF029262C87125"
493 + "314DD3FB02302FA212EB3CC06F73DF1474382BBA2A92845F39FF5A7F5020482849C36B4BC6BC82F7AF0E2E3"
494 + "9143548CC32B93B72EF0659C6895F77E6B5839962678532392185C9431658B34D1ABD31F64F4C4A9B348A77"
495 + "56783D60244519ADDD33560405E9377A91617127C2EECF2BAE53AB930FC13AFD25723FB60DB763286EDF6F1"
496 + "187D8124B6A569AA2BD19294A7D551A0D90F8436274690231520A2254C19EA9BF877FC99566059A29CDF503"
497 + "6BEA1D517916BA2F20AC9F1D8F164B6E8ACDD52BA8B2650EBBCC2ED9103561E11AF422D10DF7405404195FA"
498 + "EF79A1FDC680F3A3DC395E3E9C0B10394DF35AE134E6CB719E35152F8E5303C06092A864886F70D01070130"
499 + "1D060960864801650304012A041085072D8771A2A2BB403E3236A7C60C2A80105C71A04E73C57FE75C1DEDD"
500 + "94B57FD01").HexToByteArray();
501
502 byte[] expectedContent = { 1, 2, 3, 4 };
503 ContentInfo expectedContentInfo = new ContentInfo(expectedContent);
504 CertLoader certLoader = Certificates.RSAKeyTransfer_ExplicitSki;
505
506 VerifySimpleDecrypt(encryptedMessage, certLoader, expectedContentInfo);
507 }
508
509 [Fact]
510 [OuterLoop(/* Leaks key on disk if interrupted */)]
511 public static void TestDecryptSimple_Pkcs7Signed_GeneratedOnWindows()
512 {
513 byte[] expectedContent =
514 ("3082032506092a864886f70d010702a082031630820312020101310b300906052b0e03021a0500301206092a864886f70d01"
515 + "0701a0050403010203a08202103082020c30820179a00302010202105d2ffff863babc9b4d3c80ab178a4cca300906052b0e"
516 + "03021d0500301e311c301a060355040313135253414b65795472616e736665724361706931301e170d313530343135303730"
517 + "3030305a170d3235303431353037303030305a301e311c301a060355040313135253414b65795472616e7366657243617069"
518 + "3130819f300d06092a864886f70d010101050003818d0030818902818100aa272700586c0cc41b05c65c7d846f5a2bc27b03"
519 + "e301c37d9bff6d75b6eb6671ba9596c5c63ba2b1af5c318d9ca39e7400d10c238ac72630579211b86570d1a1d44ec86aa8f6"
520 + "c9d2b4e283ea3535923f398a312a23eaeacd8d34faaca965cd910b37da4093ef76c13b337c1afab7d1d07e317b41a336baa4"
521 + "111299f99424408d0203010001a3533051304f0603551d0104483046801015432db116b35d07e4ba89edb2469d7aa120301e"
522 + "311c301a060355040313135253414b65795472616e73666572436170693182105d2ffff863babc9b4d3c80ab178a4cca3009"
523 + "06052b0e03021d05000381810081e5535d8eceef265acbc82f6c5f8bc9d84319265f3ccf23369fa533c8dc1938952c593166"
524 + "2d9ecd8b1e7b81749e48468167e2fce3d019fa70d54646975b6dc2a3ba72d5a5274c1866da6d7a5df47938e034a075d11957"
525 + "d653b5c78e5291e4401045576f6d4eda81bef3c369af56121e49a083c8d1adb09f291822e99a4296463181d73081d4020101"
526 + "3032301e311c301a060355040313135253414b65795472616e73666572436170693102105d2ffff863babc9b4d3c80ab178a"
527 + "4cca300906052b0e03021a0500300d06092a864886f70d010101050004818031a718ea1483c88494661e1d3dedfea0a3d97e"
528 + "eb64c3e093a628b257c0cfc183ecf11697ac84f2af882b8de0c793572af38dc15d1b6f3d8f2392ba1cc71210e177c146fd16"
529 + "b77a583b6411e801d7a2640d612f2fe99d87e9718e0e505a7ab9536d71dbde329da21816ce7da1416a74a3e0a112b86b33af"
530 + "336a2ba6ae2443d0ab").HexToByteArray();
531
532 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Signed), expectedContent);
533
534 byte[] encodedMessage = Convert.FromBase64String(
535 "MIIERwYJKoZIhvcNAQcDoIIEODCCBDQCAQAxgcwwgckCAQAwMjAeMRwwGgYDVQQDExNSU0FLZXlU" +
536 "cmFuc2ZlckNhcGkxAhBdL//4Y7q8m008gKsXikzKMA0GCSqGSIb3DQEBAQUABIGAngdmU69zGsgJ" +
537 "mx+hXmTjlefr1oazKRK8VGOeqNMm++J3yHxwz68CLoN4FIEyS/HE3NQE6qb3M80HOpk5fmVVMw7Z" +
538 "3mrsZlPLOEjJIxEFqAC/JFEzvyE/BL+1OvwRoHpxHsAvZNlz5f9g18wQVE7X5TkkbOJV/6F2disK" +
539 "H0jik68wggNeBgkqhkiG9w0BBwIwHQYJYIZIAWUDBAEqBBDr3I8NYAnetX+2h9D/nVAggIIDMOlW" +
540 "7mDuuScuXhCXgZaPy0/zEWy/sYDzxhj1G1X2qBwhB7m6Ez6giibAEYwfRNYaOiVIitIJAUU9LSKg" +
541 "n0FL1o0eCcgvo04w+zoaBH8pFFk78kuR+T73kflz+O3Eno1pIFpy+2cz2B0QvQSC7lYikbZ4J+/C" +
542 "7F/uqRoK7sdafNdyUnKhDL+vvP6ToGf9C4g0TjoFEC2ycyJxIBh1F57pqjht6HMQcYm+/fQoBtkt" +
543 "NrvZxJPlBhbQad/9pSCd0G6NDoPnDuFAicaxGVa7yI2BbvGTCc6NSnbdCzv2EgvsI10Yko+XO4/i" +
544 "oPnk9pquZBzC3p61XRKbBDrd5RsbkvPDXUHJKD5NQ3W3z9Bnc3bjNyilgDSIB01dE4AcWzdg+RGb" +
545 "TA7iAbAQKp2zjxb/prmxw1mhO9g6OkDovSTqmQQt7MlHFYFcX9wH8yEe+huIechmiy7famofluJX" +
546 "vBIz4m3JozlodyNX0nu9QwW58WWcFu6OyoPjFhlB+tLIHUElq9/AAEgwwgfsAj6jEQaHiFG+CYSJ" +
547 "RjX9+DHFJXMDyzW+eJw8Z/mvbZzzKF553xlAGpfUHHq4CywTyVTHn4nu9HPOeFzoirj1lzFvqmQd" +
548 "Dgp3T8NOPrns9ZIUBmdNNs/vUxNZqEeN4d0nD5lBG4aZnjsxr4i25rR3Jpe3kKrFtJQ74efkRM37" +
549 "1ntz9HGiA95G41fuaMw7lgOOfTL+AENNvwRGRCAhinajvQLDkFEuX5ErTtmBxJWU3ZET46u/vRiK" +
550 "NiRteFiN0hLv1jy+RJK+d+B/QEH3FeVMm3Tz5ll2LfO2nn/QR51hth7qFsvtFpwQqkkhMac6dMf/" +
551 "bb62pZY15U3y2x5jSn+MZVrNbL4ZK/JO5JFomqKVRjLH1/IZ+kuFNaaTrKFWB4U2gxrMdcjMCZvx" +
552 "ylbuf01Ajxo74KhPY9sIRztG4BU8ZaE69Ke50wJTE3ulm+g6hzNECdQS/yjuA8AUReGRj2NH/U4M" +
553 "lEUiR/rMHB/Mq/Vj6lsRFEVxHJSzek6jvrQ4UzVgWGrH4KnP3Rca8CfBTQX79RcZPu+0kI3KI+4H" +
554 "0Q+UBbb72FHnWsw3uqPEyA==");
555
556 CertLoader certLoader = Certificates.RSAKeyTransferCapi1;
557 VerifySimpleDecrypt(encodedMessage, certLoader, expectedContentInfo);
558 }
559
560 [Fact]
561 [OuterLoop(/* Leaks key on disk if interrupted */)]
562 public static void TestDecryptSimple_Pkcs7Signed_GeneratedOnLinux()
563 {
564 byte[] expectedContent =
565 ("3082032506092a864886f70d010702a082031630820312020101310b300906052b0e03021a0500301206092a864886f70d01"
566 + "0701a0050403010203a08202103082020c30820179a00302010202105d2ffff863babc9b4d3c80ab178a4cca300906052b0e"
567 + "03021d0500301e311c301a060355040313135253414b65795472616e736665724361706931301e170d313530343135303730"
568 + "3030305a170d3235303431353037303030305a301e311c301a060355040313135253414b65795472616e7366657243617069"
569 + "3130819f300d06092a864886f70d010101050003818d0030818902818100aa272700586c0cc41b05c65c7d846f5a2bc27b03"
570 + "e301c37d9bff6d75b6eb6671ba9596c5c63ba2b1af5c318d9ca39e7400d10c238ac72630579211b86570d1a1d44ec86aa8f6"
571 + "c9d2b4e283ea3535923f398a312a23eaeacd8d34faaca965cd910b37da4093ef76c13b337c1afab7d1d07e317b41a336baa4"
572 + "111299f99424408d0203010001a3533051304f0603551d0104483046801015432db116b35d07e4ba89edb2469d7aa120301e"
573 + "311c301a060355040313135253414b65795472616e73666572436170693182105d2ffff863babc9b4d3c80ab178a4cca3009"
574 + "06052b0e03021d05000381810081e5535d8eceef265acbc82f6c5f8bc9d84319265f3ccf23369fa533c8dc1938952c593166"
575 + "2d9ecd8b1e7b81749e48468167e2fce3d019fa70d54646975b6dc2a3ba72d5a5274c1866da6d7a5df47938e034a075d11957"
576 + "d653b5c78e5291e4401045576f6d4eda81bef3c369af56121e49a083c8d1adb09f291822e99a4296463181d73081d4020101"
577 + "3032301e311c301a060355040313135253414b65795472616e73666572436170693102105d2ffff863babc9b4d3c80ab178a"
578 + "4cca300906052b0e03021a0500300d06092a864886f70d010101050004818031a718ea1483c88494661e1d3dedfea0a3d97e"
579 + "eb64c3e093a628b257c0cfc183ecf11697ac84f2af882b8de0c793572af38dc15d1b6f3d8f2392ba1cc71210e177c146fd16"
580 + "b77a583b6411e801d7a2640d612f2fe99d87e9718e0e505a7ab9536d71dbde329da21816ce7da1416a74a3e0a112b86b33af"
581 + "336a2ba6ae2443d0ab").HexToByteArray();
582
583 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Signed), expectedContent);
584
585 byte[] encodedMessage = Convert.FromBase64String(
586 "MIIERwYJKoZIhvcNAQcDoIIEODCCBDQCAQAxgcwwgckCAQAwMjAeMRwwGgYDVQQDExNSU0FLZXlU" +
587 "cmFuc2ZlckNhcGkxAhBdL//4Y7q8m008gKsXikzKMA0GCSqGSIb3DQEBAQUABIGAfsH5F4ZlwIcH" +
588 "nzqn8sOn+ZwK884en7HZrQbgEfedy5ti0ituKn70yTDz4iuNJtpguukCfCAsOT/n3eQn+T6etIa9" +
589 "byRGQst3F6QtgjAzdb5P1N6c5Mpz1o6k0mbNP/f0FqAaNtuAOnYwlEMwWOz9x4eEYpOhhlc6agRG" +
590 "qWItNVgwggNeBgkqhkiG9w0BBwIwHQYJYIZIAWUDBAEqBBCA8qaTiT+6IkrO8Ks9WOBggIIDMIkr" +
591 "7OGlPd3CfzO2CfJA3Sis/1ulDm1ioMCS9V5kLHVx4GWDETORG/YTPA54luxJkvIdU5VwO86+QuXf" +
592 "rgaly38XHTLZv+RBxUwCaWI0pgvaykEki5CnDu4j9uRQxqz6iU5bY6SKxG3bwcnUzGhWFdQVcNJn" +
593 "xzuMEcbrix5zfqmoeqemaYyqVqgMPNOnc5kmMOqHrha76qMdbYOSpwx81zYwstBlg+S9+AgOMR+W" +
594 "qrV9aXJTovjiJEVHPEPJFx0v/wCthhXso51mSU091Cs8qVfvokzlzXcK2dPF5d8EdmZCcmHsoq35" +
595 "/DfAL4DkfKucwiP9W7rT1a2BmVMquFTMI+KXyDvNhMVasjhKq5eM2G+oUDc3kGa3akaPZ+hNEHA+" +
596 "BNAS7iIpRft2GMNfTqpkBqnS6pB0+SSf02/JVkcFuHXZ9oZJsvZRm8M1i4WdVauBJ34rInQBdhaO" +
597 "yaFDx69tBvolclYnMzvdHLiP2TZbiR6kM0vqD1DGjEHNDE+m/jxL7HXcNotW84J9CWlDnm9zaNhL" +
598 "sB4PJNiNjKhkAsO+2HaNWlEPrmgmWKvNi/Qyrz1qUryqz2/2HGrFDqmjTeEf1+yy35N3Pqv5uvAj" +
599 "f/ySihknnAh77nI0yOPy0Uto+hbO+xraeujrEifaII8izcz6UG6LHNPxOyscne7HNcqPSAFLsNFJ" +
600 "1oOlKO0SwhPkGQsk4W5tjVfvLvJiPNcL7SY/eof4vVsRRwX6Op5WUjhJIagY1Vij+4hOcn5TqdmU" +
601 "OZDh/FC3x4DI556BeMfbWxHNlGvptROQwQ6BasfdiVWCWzMHwLpz27Y47vKbMQ+8TL9668ilT83f" +
602 "6eo6mHZ590pzuDB+gFrjEK44ov0rvHBK5jHwnSObQvChN0ElizWBdMSUbx9SkcnReH6Fd29SSXdu" +
603 "RaVspnhmFNXWg7qGYHpEChnIGSr/WIKETZ84f7FRCxCNSYoQtrHs0SskiEGJYEbB6KDMFimEZ4YN" +
604 "b4cV8VLC9Pxa1Qe1Oa05FBzG2DAP2PfObeKR34afF5wo6vIZfQE0WWoPo9YS326vz1iA5rE0F6qw" +
605 "zCNmZl8+rW6x73MTEcWhvg==");
606
607 CertLoader certLoader = Certificates.RSAKeyTransferCapi1;
608 VerifySimpleDecrypt(encodedMessage, certLoader, expectedContentInfo);
609 }
610
611 [Fact]
612 public static void DecryptEnvelopedOctetStringWithDefiniteLength()
613 {
614 // enveloped content consists of 5 bytes: <id: 1 byte><length: 1 byte><content: 3 bytes>
615 // <id>:
616 // 04 - Octet string
617 // 30 - Sequence
618 // <length>: 03 => length is 3 (encoded with definite length)
619 // <content>: 010203
620 // Note: we expect invalid ASN.1 sequence
621 byte[] content = "0403010203".HexToByteArray();
622 byte[] expectedContent = "3003010203".HexToByteArray();
623
624 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content);
625 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), expectedContent);
626
627 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber, expectedContentInfo);
628 }
629
630 [Fact]
631 public static void DecryptEnvelopedOctetStringWithInefficientlyEncodedLength()
632 {
633 // enveloped content consists of 5 or 6 bytes: <id: 1 byte><length: 1 or 2 bytes><content: 3 bytes>
634 // <id>:
635 // 04 - Octet string
636 // 30 - Sequence
637 // <length>: 03 => length is 3 (encoded with definite length)
638 // 81 03 => length is 3 (encoded with inefficiently encoded length)
639 // <content>: 010203
640 // Note: we expect invalid ASN.1 sequence
641 byte[] content = "048103010203".HexToByteArray();
642 byte[] expectedContent = "3003010203".HexToByteArray();
643
644 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content);
645 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), expectedContent);
646
647 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber, expectedContentInfo);
648 }
649
650 [Fact]
651 [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "netfx does not allow it")]
652 public static void DecryptEnvelopedEmptyArray()
653 {
654 byte[] content = Array.Empty<byte>();
655 byte[] expectedContent = "3000".HexToByteArray();
656
657 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content);
658 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), expectedContent);
659
660 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber, expectedContentInfo);
661 }
662
663 [Fact]
664 public static void DecryptEnvelopedEmptyOctetString()
665 {
666 byte[] content = "0400".HexToByteArray();
667 byte[] expectedContent = "3000".HexToByteArray();
668
669 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content);
670 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), expectedContent);
671
672 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber, expectedContentInfo);
673 }
674
675 [Fact]
676 public static void DecryptEnvelopedOctetStringWithExtraData()
677 {
678 byte[] content = "04010203".HexToByteArray();
679 byte[] expectedContent = "300102".HexToByteArray();
680
681 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content);
682 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), expectedContent);
683
684 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber, expectedContentInfo);
685 }
686
687 [Fact]
688 public static void DecryptEnvelopedDataWithNonPkcs7Oid()
689 {
690 byte[] content = "3003010203".HexToByteArray();
691 string nonPkcs7Oid = "0.0";
692
693 ContentInfo contentInfo = new ContentInfo(new Oid(nonPkcs7Oid), content);
694
695 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber);
696 }
697
698 [ConditionalFact(nameof(SupportsIndefiniteLengthEncoding))]
699 public static void DecryptEnvelopedEmptyOctetStringWithIndefiniteLength()
700 {
701 byte[] content = "30800000".HexToByteArray();
702 byte[] expectedContent = "3000".HexToByteArray();
703
704 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content);
705 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), expectedContent);
706
707 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber, expectedContentInfo);
708 }
709
710 [ConditionalFact(nameof(SupportsIndefiniteLengthEncoding))]
711 public static void DecryptEnvelopedOctetStringWithIndefiniteLength()
712 {
713 byte[] content = "308004000000".HexToByteArray();
714 byte[] expectedContent = "30020400".HexToByteArray();
715
716 ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content);
717 ContentInfo expectedContentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), expectedContent);
718
719 TestSimpleDecrypt_RoundTrip(Certificates.RSAKeyTransferCapi1, contentInfo, Oids.Aes256, SubjectIdentifierType.IssuerAndSerialNumber, expectedContentInfo);
720 }
721
722 private static void TestSimpleDecrypt_RoundTrip(CertLoader certLoader, ContentInfo contentInfo, string algorithmOidValue, SubjectIdentifierType type, ContentInfo expectedContentInfo = null)
723 {
724 // Deep-copy the contentInfo since the real ContentInfo doesn't do this. This defends against a bad implementation changing
725 // our "expectedContentInfo" to match what it produces.
726 expectedContentInfo = expectedContentInfo ?? new ContentInfo(new Oid(contentInfo.ContentType), (byte[])(contentInfo.Content.Clone()));
727
728 string certSubjectName;
729 byte[] encodedMessage;
730 byte[] originalCopy = (byte[])(contentInfo.Content.Clone());
731 using (X509Certificate2 certificate = certLoader.GetCertificate())
732 {
733 certSubjectName = certificate.Subject;
734 AlgorithmIdentifier alg = new AlgorithmIdentifier(new Oid(algorithmOidValue));
735 EnvelopedCms ecms = new EnvelopedCms(contentInfo, alg);
736 CmsRecipient cmsRecipient = new CmsRecipient(type, certificate);
737 ecms.Encrypt(cmsRecipient);
738 Assert.Equal(originalCopy.ByteArrayToHex(), ecms.ContentInfo.Content.ByteArrayToHex());
739 encodedMessage = ecms.Encode();
740 }
741
742 // We don't pass "certificate" down because it's expected that the certificate used for encrypting doesn't have a private key (part of the purpose of this test is
743 // to ensure that you don't need the recipient's private key to encrypt.) The decrypt phase will have to locate the matching cert with the private key.
744 VerifySimpleDecrypt(encodedMessage, certLoader, expectedContentInfo);
745 }
746
747 private static void VerifySimpleDecrypt(byte[] encodedMessage, CertLoader certLoader, ContentInfo expectedContent)
748 {
749 EnvelopedCms ecms = new EnvelopedCms();
750 ecms.Decode(encodedMessage);
751
752 using (X509Certificate2 cert = certLoader.TryGetCertificateWithPrivateKey())
753 {
754 if (cert == null)
755 return; // Sorry - CertLoader is not configured to load certs with private keys - we've tested as much as we can.
756
757 X509Certificate2Collection extraStore = new X509Certificate2Collection(cert);
758 ecms.Decrypt(extraStore);
759 ContentInfo contentInfo = ecms.ContentInfo;
760 Assert.Equal(expectedContent.ContentType.Value, contentInfo.ContentType.Value);
761 Assert.Equal(expectedContent.Content.ByteArrayToHex(), contentInfo.Content.ByteArrayToHex());
762 }
763 }
764 }
765 }
766
767