Debian Codegraph @

external/corefx/src/System.Security.Cryptography.Pkcs/tests/EnvelopedCms/GeneralTests.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.Linq;
6 using System.Runtime.InteropServices;
7 using System.Security.Cryptography.Pkcs.Tests;
8 using System.Security.Cryptography.Xml;
9 using System.Security.Cryptography.X509Certificates;
10 using Xunit;
11
12 using Test.Cryptography;
13
14 namespace System.Security.Cryptography.Pkcs.EnvelopedCmsTests.Tests
15 {
16 public static partial class GeneralTests
17 {
18 public static bool SupportsDiffieHellman { get; } = KeyAgreeRecipientInfoTests.SupportsDiffieHellman;
19
20 [Fact]
21 public static void DecodeVersion0_RoundTrip()
22 {
23 ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
24 EnvelopedCms ecms = new EnvelopedCms(contentInfo);
25 using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
26 {
27 CmsRecipient cmsRecipient = new CmsRecipient(cert);
28 ecms.Encrypt(cmsRecipient);
29 }
30 byte[] encodedMessage = ecms.Encode();
31
32 VerifyVersion0(encodedMessage);
33 }
34
35 [Fact]
36 public static void DecodeVersion0_FixedValue()
37 {
38 byte[] encodedMessage =
39 ("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253"
40 + "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004818013"
41 + "dc0eb2984a445d04a1f6246b8fe41f1d24507548d449d454d5bb5e0638d75ed101bf78c0155a5d208eb746755fbccbc86923"
42 + "8443760a9ae94770d6373e0197be23a6a891f0c522ca96b3e8008bf23547474b7e24e7f32e8134df3862d84f4dea2470548e"
43 + "c774dd74f149a56cdd966e141122900d00ad9d10ea1848541294a1302b06092a864886f70d010701301406082a864886f70d"
44 + "030704089c8119f6cf6b174c8008bcea3a10d0737eb9").HexToByteArray();
45
46 VerifyVersion0(encodedMessage);
47 }
48
49 private static void VerifyVersion0(byte[] encodedMessage)
50 {
51 EnvelopedCms ecms = new EnvelopedCms();
52 ecms.Decode(encodedMessage);
53 int version = ecms.Version;
54 Assert.Equal(0, version);
55 }
56
57 [ConditionalFact(nameof(SupportsDiffieHellman))]
58 public static void DecodeRecipients3_RoundTrip()
59 {
60 ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
61 EnvelopedCms ecms = new EnvelopedCms(contentInfo);
62 CmsRecipientCollection recipients = new CmsRecipientCollection();
63 foreach (X509Certificate2 cert in s_certs)
64 {
65 recipients.Add(new CmsRecipient(cert));
66 }
67 ecms.Encrypt(recipients);
68 byte[] encodedMessage = ecms.Encode();
69
70 VerifyRecipients3(encodedMessage);
71 }
72
73 [Fact]
74 public static void DecodeRecipients3_FixedValue()
75 {
76 byte[] encodedMessage =
77 ("3082032b06092a864886f70d010703a082031c30820318020102318202e43081c5020100302e301a31183016060355040313"
78 + "0f5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004"
79 + "81805d4b13a055c512c2367db4ac4ad4470415ef1113ee78f6b22d114873759ddc1135027f59a8583d24527ceee38b34be52"
80 + "22400e37a265d5b4be67df685a21db2a1512d46d857c9c9ac8d801807131118efe68b8f89bfb81c06171cf12756e679bd518"
81 + "3501193a86bb3b3893a34d6907698e2391701a0ddcd8fe337734db83a54c3081c5020100302e301a31183016060355040313"
82 + "0f5253414b65795472616e736665723202102bce9f9ece39f98044f0cd2faa9a14e7300d06092a864886f70d010101050004"
83 + "818030e318a393ec982869cccec4d7ea24106f996892abf35c4faa0f88b0d2f5d97371f761ef2e60dfe46b9c63bc004a433a"
84 + "16504adeda4edb3f37d3da5e602d97d8a049bca07c0e092c1f43682d8b8ba0c8f495ba0265231f68624e74d263efee580629"
85 + "b97e4faf8d66c053f9bf214ed76a2e81b03b0771baacd0b07ee775d06244a1820150020103a08195a18192300906072a8648"
86 + "ce3e02010381840002818054287a01a44b38468f629e28d11b9f80de6cb0ab3dc0828cf41ff077d256beafaf06ae111e5235"
87 + "d90c57a37a22fb10fd22d11fb47f0c278d5b23c5914475452dcac8cfb6bff3f326450ab7a666c183f89f96d966336464cc2f"
88 + "39f61263996c3e56b9e782b8264a4e8cd57e5576174dca5d02bc7f33f7fdfe71af1ff1f11b287b301e060b2a864886f70d01"
89 + "09100305300f060b2a864886f70d0109100306050030819230818f3063304f314d304b06035504031e44004d0061006e0061"
90 + "00670065006400200050004b00430053002300370020005400650073007400200052006f006f007400200041007500740068"
91 + "006f007200690074007902100ae59b0cb8119f8942eda74163413a0204285aadd33713104d128c5e1d70d9281f7c0df6fa42"
92 + "64fd9fa77fcde800aaf8ea33d533b8572a1b9c4a302b06092a864886f70d010701301406082a864886f70d03070408fc6d30"
93 + "2f218ea61f8008de1137262232ceae").HexToByteArray();
94
95 VerifyRecipients3(encodedMessage);
96 }
97
98 private static void VerifyRecipients3(byte[] encodedMessage)
99 {
100 string[] expectedIssuers = s_certs.Select(c => c.Issuer).OrderBy(s => s).ToArray();
101
102 EnvelopedCms ecms = new EnvelopedCms();
103 ecms.Decode(encodedMessage);
104
105 RecipientInfoCollection col = ecms.RecipientInfos;
106 int numRecipients = col.Count;
107 Assert.Equal(3, numRecipients);
108
109 RecipientInfo[] recipients = new RecipientInfo[numRecipients];
110 col.CopyTo(recipients, 0);
111
112 string[] actualIssuers = recipients.Select(r => r.RecipientIdentifier.Value).Cast<X509IssuerSerial>().Select(xis => xis.IssuerName).OrderBy(s => s).ToArray();
113 Assert.Equal<string>(expectedIssuers, actualIssuers);
114 }
115
116
117 [Fact]
118 public static void DecodeAllIndefinite()
119 {
120 byte[] encrypted = Convert.FromBase64String(
121 @"
122 MIAGCSqGSIb3DQEHA6CAMIACAQAxggFXMIIBUwIBADA7MDMxGTAXBgNVBAoMEERh
123 dGEgSW50ZXJjaGFuZ2UxFjAUBgNVBAMMDVVubyBUZXN0IFJvb3QCBFqG6RQwDQYJ
124 KoZIhvcNAQEBBQAEggEAUPilAHUe67HG5vDCO/JBmof44G/XnDLtiDrbxD4QekGq
125 mdPqazZiLDKEewlBy2uFJr/JijeYx6qNKTXs/EShw/lYnKisaK5ue6JZ7ssMunM9
126 HpkiDfM+iyN7PxnC1riZ/Kg2JExY8pf5R1Zuvu29JSLhM9ajWk9C1pBzQRJ4vkY2
127 OvFKR2th0Vgw7mTmc2X6HUK4tosB3LGKDVNd6BVoMQMvfkseCqeZOe1KIiBFmhyk
128 E+B2UZcD6Z6kLnCk4LNGyoyxW6Thv5s/lwP9p7trVVbPXbuep1l8uMCGj6vjTD66
129 AamEIRmTFvEVHzyO2MGG9V0bM+8UpqPAVFNCXOm6mjCABgkqhkiG9w0BBwEwFAYI
130 KoZIhvcNAwcECJ01qtX2EKx6oIAEEM7op+R2U3GQbYwlEj5X+h0AAAAAAAAAAAAA
131 ");
132 EnvelopedCms cms = new EnvelopedCms();
133 cms.Decode(encrypted);
134
135 RecipientInfoCollection recipientInfos = cms.RecipientInfos;
136
137 Assert.Equal(1, recipientInfos.Count);
138 Assert.Equal(
139 SubjectIdentifierType.IssuerAndSerialNumber,
140 recipientInfos[0].RecipientIdentifier.Type);
141
142 string expectedContentHex = "CEE8A7E4765371906D8C25123E57FA1D";
143
144 if (PlatformDetection.IsFullFramework)
145 {
146 // .NET Framework over-counts encrypted content.
147 expectedContentHex += "000000000000";
148 }
149
150 // Still encrypted.
151 Assert.Equal(
152 expectedContentHex,
153 cms.ContentInfo.Content.ByteArrayToHex());
154 }
155
156 [Fact]
157 public static void TestGetContentTypeEnveloped()
158 {
159 byte[] encodedMessage =
160 ("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253"
161 + "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004818013"
162 + "dc0eb2984a445d04a1f6246b8fe41f1d24507548d449d454d5bb5e0638d75ed101bf78c0155a5d208eb746755fbccbc86923"
163 + "8443760a9ae94770d6373e0197be23a6a891f0c522ca96b3e8008bf23547474b7e24e7f32e8134df3862d84f4dea2470548e"
164 + "c774dd74f149a56cdd966e141122900d00ad9d10ea1848541294a1302b06092a864886f70d010701301406082a864886f70d"
165 + "030704089c8119f6cf6b174c8008bcea3a10d0737eb9").HexToByteArray();
166
167 Oid contentType = ContentInfo.GetContentType(encodedMessage);
168 Assert.Equal(Oids.Pkcs7Enveloped, contentType.Value);
169 }
170
171 [Fact]
172 public static void TestContentTypeSigned()
173 {
174 byte[] encodedMessage =
175 ("3082032506092a864886f70d010702a082031630820312020101310b300906052b0e03021a0500301206092a864886f70d01"
176 + "0701a0050403010203a08202103082020c30820179a00302010202105d2ffff863babc9b4d3c80ab178a4cca300906052b0e"
177 + "03021d0500301e311c301a060355040313135253414b65795472616e736665724361706931301e170d313530343135303730"
178 + "3030305a170d3235303431353037303030305a301e311c301a060355040313135253414b65795472616e7366657243617069"
179 + "3130819f300d06092a864886f70d010101050003818d0030818902818100aa272700586c0cc41b05c65c7d846f5a2bc27b03"
180 + "e301c37d9bff6d75b6eb6671ba9596c5c63ba2b1af5c318d9ca39e7400d10c238ac72630579211b86570d1a1d44ec86aa8f6"
181 + "c9d2b4e283ea3535923f398a312a23eaeacd8d34faaca965cd910b37da4093ef76c13b337c1afab7d1d07e317b41a336baa4"
182 + "111299f99424408d0203010001a3533051304f0603551d0104483046801015432db116b35d07e4ba89edb2469d7aa120301e"
183 + "311c301a060355040313135253414b65795472616e73666572436170693182105d2ffff863babc9b4d3c80ab178a4cca3009"
184 + "06052b0e03021d05000381810081e5535d8eceef265acbc82f6c5f8bc9d84319265f3ccf23369fa533c8dc1938952c593166"
185 + "2d9ecd8b1e7b81749e48468167e2fce3d019fa70d54646975b6dc2a3ba72d5a5274c1866da6d7a5df47938e034a075d11957"
186 + "d653b5c78e5291e4401045576f6d4eda81bef3c369af56121e49a083c8d1adb09f291822e99a4296463181d73081d4020101"
187 + "3032301e311c301a060355040313135253414b65795472616e73666572436170693102105d2ffff863babc9b4d3c80ab178a"
188 + "4cca300906052b0e03021a0500300d06092a864886f70d010101050004818031a718ea1483c88494661e1d3dedfea0a3d97e"
189 + "eb64c3e093a628b257c0cfc183ecf11697ac84f2af882b8de0c793572af38dc15d1b6f3d8f2392ba1cc71210e177c146fd16"
190 + "b77a583b6411e801d7a2640d612f2fe99d87e9718e0e505a7ab9536d71dbde329da21816ce7da1416a74a3e0a112b86b33af"
191 + "336a2ba6ae2443d0ab").HexToByteArray();
192
193 Oid contentType = ContentInfo.GetContentType(encodedMessage);
194 Assert.Equal(Oids.Pkcs7Signed, contentType.Value);
195 }
196
197 [Fact]
198 public static void TestContent()
199 {
200 // Tests that the content is what it is expected to be, even if it's still encyrpted. This prevents from ambiguous definitions of content.
201
202 // The encoded message was built in ASN.1 editor and tested in framework. It contains an enveloped message version 0 with one recipient of
203 // key transport type. The symmetric algorythm is 3DES and the contained type is data.
204 byte[] encodedMessage =
205 ("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253"
206 + "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004818013"
207 + "dc0eb2984a445d04a1f6246b8fe41f1d24507548d449d454d5bb5e0638d75ed101bf78c0155a5d208eb746755fbccbc86923"
208 + "8443760a9ae94770d6373e0197be23a6a891f0c522ca96b3e8008bf23547474b7e24e7f32e8134df3862d84f4dea2470548e"
209 + "c774dd74f149a56cdd966e141122900d00ad9d10ea1848541294a1302b06092a864886f70d010701301406082a864886f70d"
210 + "030704089c8119f6cf6b174c8008bcea3a10d0737eb9").HexToByteArray();
211
212 EnvelopedCms cms = new EnvelopedCms();
213
214 cms.Decode(encodedMessage);
215
216 string expectedHex = "BCEA3A10D0737EB9";
217
218 if (PlatformDetection.IsFullFramework)
219 {
220 expectedHex = "BCEA3A10D0737EB9000000000000";
221 }
222
223 Assert.Equal(expectedHex, cms.ContentInfo.Content.ByteArrayToHex());
224 }
225
226 [Fact]
227 [OuterLoop(/* Leaks key on disk if interrupted */)]
228 public static void MultipleRecipientIdentifiers_RoundTrip()
229 {
230 ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
231 EnvelopedCms ecms = new EnvelopedCms(contentInfo);
232 CmsRecipientCollection recipients = new CmsRecipientCollection();
233 using (X509Certificate2 issuerSerialCert = Certificates.RSAKeyTransfer1.GetCertificate())
234 using (X509Certificate2 explicitSkiCert = Certificates.RSAKeyTransfer_ExplicitSki.GetCertificate())
235 {
236 // CmsRecipients have different identifiers to test multiple identifier encryption.
237 recipients.Add(new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, issuerSerialCert));
238 recipients.Add(new CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, explicitSkiCert));
239 ecms.Encrypt(recipients);
240 }
241
242 byte[] encodedMessage = ecms.Encode();
243
244 ecms = new EnvelopedCms();
245 ecms.Decode(encodedMessage);
246
247 // Try decoding it, doesn't really matter with which cert you want to do it as it's not what this
248 // test aims for.
249
250 using (X509Certificate2 privateCert = Certificates.RSAKeyTransfer_ExplicitSki.TryGetCertificateWithPrivateKey())
251 {
252 if (privateCert == null)
253 return; // CertLoader can't load the private certificate.
254
255 ecms.Decrypt(new X509Certificate2Collection(privateCert));
256 }
257 Assert.Equal(contentInfo.ContentType.Value, ecms.ContentInfo.ContentType.Value);
258 Assert.Equal<byte>(contentInfo.Content, ecms.ContentInfo.Content);
259 }
260
261 [Fact]
262 [OuterLoop(/* Leaks key on disk if interrupted */)]
263 public static void RoundTrip_ExplicitSki()
264 {
265 ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
266 EnvelopedCms ecms = new EnvelopedCms(contentInfo);
267 using (X509Certificate2 explicitSkiCert = Certificates.RSAKeyTransfer_ExplicitSki.GetCertificate())
268 {
269 CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, explicitSkiCert);
270 ecms.Encrypt(recipient);
271 }
272
273 byte[] encodedMessage = ecms.Encode();
274
275 ecms = new EnvelopedCms();
276 ecms.Decode(encodedMessage);
277
278 using (X509Certificate2 privateCert = Certificates.RSAKeyTransfer_ExplicitSki.TryGetCertificateWithPrivateKey())
279 {
280 if (privateCert == null)
281 return; // CertLoader can't load the private certificate.
282
283 ecms.Decrypt(new X509Certificate2Collection(privateCert));
284 }
285 Assert.Equal(contentInfo.ContentType.Value, ecms.ContentInfo.ContentType.Value);
286 Assert.Equal<byte>(contentInfo.Content, ecms.ContentInfo.Content);
287 }
288
289 private static X509Certificate2[] s_certs =
290 {
291 Certificates.RSAKeyTransfer1.GetCertificate(),
292 Certificates.RSAKeyTransfer2.GetCertificate(),
293 Certificates.DHKeyAgree1.GetCertificate(),
294 };
295 }
296 }
297
298