Debian Codegraph @

external/corefx/src/System.Security.Cryptography.Pkcs/tests/EnvelopedCms/KeyAgreeRecipientInfoTests.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.Pkcs.Tests;
6 using System.Security.Cryptography.Xml;
7 using System.Security.Cryptography.X509Certificates;
8 using Xunit;
9
10 using Test.Cryptography;
11
12 namespace System.Security.Cryptography.Pkcs.EnvelopedCmsTests.Tests
13 {
14 public static partial class KeyAgreeRecipientInfoTests
15 {
16 public static bool SupportsDiffieHellman => PlatformDetection.IsWindows;
17 public static bool DoesNotSupportDiffieHellman => !SupportsDiffieHellman;
18
19 [ConditionalFact(nameof(DoesNotSupportDiffieHellman))]
20 public static void TestKeyAgreement_PlatformNotSupported()
21 {
22 ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
23 EnvelopedCms ecms = new EnvelopedCms(contentInfo);
24 using (X509Certificate2 cert = Certificates.DHKeyAgree1.GetCertificate())
25 {
26 CmsRecipient cmsRecipient = new CmsRecipient(cert);
27
28 CryptographicException e =
29 Assert.Throws<CryptographicException>(() => ecms.Encrypt(cmsRecipient));
30
31 Assert.Contains(cert.GetKeyAlgorithm(), e.Message);
32 }
33 }
34
35 [ConditionalFact(nameof(SupportsDiffieHellman))]
36 public static void TestKeyAgreeVersion_RoundTrip()
37 {
38 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
39 Assert.Equal(3, recipient.Version);
40 }
41
42 [Fact]
43 public static void TestKeyAgreeVersion_FixedValue()
44 {
45 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
46 Assert.Equal(3, recipient.Version);
47 }
48
49 [ConditionalFact(nameof(SupportsDiffieHellman))]
50 public static void TestKeyAgreeType_RoundTrip()
51 {
52 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
53 Assert.Equal(RecipientInfoType.KeyAgreement, recipient.Type);
54 }
55
56 [Fact]
57 public static void TestKeyAgreeType_FixedValue()
58 {
59 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
60 Assert.Equal(RecipientInfoType.KeyAgreement, recipient.Type);
61 }
62
63 [ConditionalFact(nameof(SupportsDiffieHellman))]
64 public static void TestKeyAgreesRecipientIdType_RoundTrip()
65 {
66 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
67 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
68 Assert.Equal(SubjectIdentifierType.IssuerAndSerialNumber, subjectIdentifier.Type);
69 }
70
71 [Fact]
72 public static void TestKeyAgreeRecipientIdType_FixedValue()
73 {
74 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
75 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
76 Assert.Equal(SubjectIdentifierType.IssuerAndSerialNumber, subjectIdentifier.Type);
77 }
78
79 [ConditionalFact(nameof(SupportsDiffieHellman))]
80 public static void TestKeyAgreeRecipientIdValue_RoundTrip()
81 {
82 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
83 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
84 object value = subjectIdentifier.Value;
85 Assert.True(value is X509IssuerSerial);
86 X509IssuerSerial xis = (X509IssuerSerial)value;
87 Assert.Equal("CN=\"Managed PKCS#7 Test Root Authority\"", xis.IssuerName);
88 Assert.Equal("0AE59B0CB8119F8942EDA74163413A02", xis.SerialNumber);
89 }
90
91 [Fact]
92 public static void TestKeyAgreeRecipientIdValue_FixedValue()
93 {
94 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
95 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
96 object value = subjectIdentifier.Value;
97 Assert.True(value is X509IssuerSerial);
98 X509IssuerSerial xis = (X509IssuerSerial)value;
99 Assert.Equal("CN=\"Managed PKCS#7 Test Root Authority\"", xis.IssuerName);
100 Assert.Equal("0AE59B0CB8119F8942EDA74163413A02", xis.SerialNumber);
101 }
102
103 [ConditionalFact(nameof(SupportsDiffieHellman))]
104 public static void TestKeyAgreeRecipientIdType_Ski_RoundTrip()
105 {
106 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1(SubjectIdentifierType.SubjectKeyIdentifier);
107 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
108 Assert.Equal(SubjectIdentifierType.SubjectKeyIdentifier, subjectIdentifier.Type);
109 }
110
111 [Fact]
112 public static void TestKeyAgreeRecipientIdType_Ski_FixedValue()
113 {
114 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1(SubjectIdentifierType.SubjectKeyIdentifier);
115 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
116 Assert.Equal(SubjectIdentifierType.SubjectKeyIdentifier, subjectIdentifier.Type);
117 }
118
119 [ConditionalFact(nameof(SupportsDiffieHellman))]
120 public static void TestKeyAgreeRecipientIdValue_Ski_RoundTrip()
121 {
122 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1(SubjectIdentifierType.SubjectKeyIdentifier);
123 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
124 object value = subjectIdentifier.Value;
125 Assert.True(value is string);
126 string ski = (string)value;
127 Assert.Equal("10DA1370316788112EB8594C864C2420AE7FBA42", ski);
128 }
129
130 [Fact]
131 public static void TestKeyAgreeRecipientIdValue_Ski_FixedValue()
132 {
133 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1(SubjectIdentifierType.SubjectKeyIdentifier);
134 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
135 object value = subjectIdentifier.Value;
136 Assert.True(value is string);
137 string ski = (string)value;
138 Assert.Equal("10DA1370316788112EB8594C864C2420AE7FBA42", ski);
139 }
140
141 [ConditionalFact(nameof(SupportsDiffieHellman))]
142 public static void TestKeyAgreeKeyEncryptionAlgorithm_RoundTrip()
143 {
144 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
145 AlgorithmIdentifier a = recipient.KeyEncryptionAlgorithm;
146 Assert.Equal(Oids.Esdh, a.Oid.Value);
147 Assert.Equal(0, a.KeyLength);
148 }
149
150 [Fact]
151 public static void TestKeyAgreeKeyEncryptionAlgorithm_FixedValue()
152 {
153 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
154 AlgorithmIdentifier a = recipient.KeyEncryptionAlgorithm;
155 Assert.Equal(Oids.Esdh, a.Oid.Value);
156 Assert.Equal(0, a.KeyLength);
157 }
158
159 [ConditionalFact(nameof(SupportsDiffieHellman))]
160 public static void TestKeyAgreeEncryptedKey_RoundTrip()
161 {
162 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
163 byte[] encryptedKey = recipient.EncryptedKey;
164 Assert.Equal(0x28, encryptedKey.Length); // Since the content encryption key is randomly generated each time, we can only test the length.
165 }
166
167 [Fact]
168 public static void TestKeyAgreeEncryptedKey_FixedValue()
169 {
170 byte[] expectedEncryptedKey = "c39323a9f5113c1465bf27b558ffeda656d606e08f8dc37e67cb8cbf7fb04d71dbe20071eaaa20db".HexToByteArray();
171
172 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
173 byte[] encryptedKey = recipient.EncryptedKey;
174 Assert.Equal<byte>(expectedEncryptedKey, encryptedKey);
175 }
176
177 [ConditionalFact(nameof(SupportsDiffieHellman))]
178 public static void TestKeyAgreeOriginatorIdentifierOrKey_RoundTrip()
179 {
180 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
181 SubjectIdentifierOrKey originator = recipient.OriginatorIdentifierOrKey;
182 Assert.Equal(SubjectIdentifierOrKeyType.PublicKeyInfo, originator.Type);
183 object value = originator.Value;
184 Assert.True(value is PublicKeyInfo);
185 PublicKeyInfo pki = (PublicKeyInfo)value;
186 AlgorithmIdentifier a = pki.Algorithm;
187 Assert.Equal(Oids.Dh, a.Oid.Value);
188 byte[] key = pki.KeyValue;
189 Assert.NotNull(key); // Key is randomly generated (and encoding makes the length unpredictable) so not much we can do here.
190 Assert.NotEmpty(key);
191 }
192
193 [Fact]
194 public static void TestKeyAgreeOriginatorIdentifierOrKey_FixedValue()
195 {
196 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
197 SubjectIdentifierOrKey originator = recipient.OriginatorIdentifierOrKey;
198 Assert.Equal(SubjectIdentifierOrKeyType.PublicKeyInfo, originator.Type);
199 object value = originator.Value;
200 Assert.True(value is PublicKeyInfo);
201 PublicKeyInfo pki = (PublicKeyInfo)value;
202 AlgorithmIdentifier a = pki.Algorithm;
203 Assert.Equal(Oids.Dh, a.Oid.Value);
204 byte[] key = pki.KeyValue;
205 byte[] expectedKey =
206 ("0281806F96EF8C53A6919CC976E88B8F426696E7B7970ABC6BD4ABBDCF4CF34F89CEB6E8EF675000FAD2ECA3CAF9D0E51B00"
207 + "4FD19A943F1779748F343FE2059E6E8208D64CB2A5BF33B2C41C20F4AE950D8F8BD720F5747D7930AF86C612088747B5315A"
208 + "E68159A5AE8A80E928AA71F4E889CB2D581845EDC8F79DA5894CB7A40F9FBE").HexToByteArray();
209
210 Assert.Equal(expectedKey, key);
211 }
212
213 [ConditionalFact(nameof(SupportsDiffieHellman))]
214 public static void TestKeyAgreeDate_RoundTrip()
215 {
216 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
217 DateTime ignore;
218 Assert.Throws<InvalidOperationException>(() => ignore = recipient.Date);
219 }
220
221 [Fact]
222 public static void TestKeyAgreeDate_FixedValue()
223 {
224 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
225 DateTime ignore;
226 Assert.Throws<InvalidOperationException>(() => ignore = recipient.Date);
227 }
228
229 [ConditionalFact(nameof(SupportsDiffieHellman))]
230 public static void TestKeyAgreeDate_RoundTrip_Ski()
231 {
232 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel(SubjectIdentifierType.SubjectKeyIdentifier);
233 DateTime date = recipient.Date;
234 long ticks = date.Ticks;
235 long expectedTicks = 0x0701ce1722770000;
236 Assert.Equal(expectedTicks, ticks);
237 }
238
239 [Fact]
240 public static void TestKeyAgreeDate_FixedValue_Ski()
241 {
242 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1(SubjectIdentifierType.SubjectKeyIdentifier);
243 DateTime date = recipient.Date;
244 long ticks = date.Ticks;
245 long expectedTicks = 0x0701ce1722770000;
246 Assert.Equal(expectedTicks, ticks);
247 }
248
249
250 [ConditionalFact(nameof(SupportsDiffieHellman))]
251 public static void TestKeyAgreeOtherKeyAttribute_RoundTrip()
252 {
253 KeyAgreeRecipientInfo recipient = EncodeKeyAgreel();
254 CryptographicAttributeObject ignore;
255 Assert.Throws<InvalidOperationException>(() => ignore = recipient.OtherKeyAttribute);
256 }
257
258 [Fact]
259 public static void TestKeyAgreeOtherKeyAttribute_FixedValue()
260 {
261 KeyAgreeRecipientInfo recipient = FixedValueKeyAgree1();
262 CryptographicAttributeObject ignore;
263 Assert.Throws<InvalidOperationException>(() => ignore = recipient.OtherKeyAttribute);
264 }
265
266 [Fact]
267 public static void TestKeyAgreeOtherKeyAttribute_FixedValue_Ski()
268 {
269 //
270 // Test a KeyAgree CMS that embeds an OtherKeyAttribute in the recipient. We don't surface a way to generate this
271 // in the EnvelopedCms class so we can only do a fixed-value test here.
272 //
273 byte[] encryptedCms =
274 ("3082015e06092a864886f70d010703a082014f3082014b02010231820117a1820113020103a08195a18192300906072a8648"
275 + "ce3e020103818400028180313d5659f9a8633243f97d11462a3b07702802fed45abbe68e3f2670bfa500d6b4f70c1c0dceac"
276 + "adffe9736764204806710d4e834b48773b7a4696a690a03abbc38c936483d2f3ccb8d764f66dc269d78a77821edd0decdef2"
277 + "b1d4356c1d1f1b34cc81c76214b8a04f3d70e1e9dc9a589cc8410599dceafad903b7b01b6b2489301e060b2a864886f70d01"
278 + "09100305300f060b2a864886f70d0109100306050030563054a028041413213309b39348347fb6a2155a53172be79c19c530"
279 + "1006092a864886f70d0107010403424d580428da28837b15e9bdf5528180a2e9beb91bf0d4519a5d76b655ebac9c43012d50"
280 + "d6b55f0618380210e4302b06092a864886f70d010701301406082a864886f70d030704087bb5f0d33e6e0354800814219a66"
281 + "c2ea1449").HexToByteArray();
282
283 EnvelopedCms envelopedCms = new EnvelopedCms();
284 envelopedCms.Decode(encryptedCms);
285 KeyAgreeRecipientInfo r = (KeyAgreeRecipientInfo)(envelopedCms.RecipientInfos[0]);
286 CryptographicAttributeObject attribute = r.OtherKeyAttribute;
287 Assert.Equal(Oids.Pkcs7Data, attribute.Oid.Value);
288 Assert.Equal(1, attribute.Values.Count);
289 AsnEncodedData asnData = attribute.Values[0];
290 byte[] expectedAsnData = "0403424d58".HexToByteArray();
291 Assert.Equal<byte>(expectedAsnData, asnData.RawData);
292 }
293
294 [Fact]
295 public static void TestMultipleKeyAgree_ShortNotation()
296 {
297 // KeyAgreement recipients are defined in RFC 2630 as
298 //
299 // KeyAgreeRecipientInfo::= SEQUENCE {
300 // version CMSVersion, --always set to 3
301 // originator[0] EXPLICIT OriginatorIdentifierOrKey,
302 // ukm[1] EXPLICIT UserKeyingMaterial OPTIONAL,
303 // keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
304 // recipientEncryptedKeys RecipientEncryptedKeys }
305 //
306 // RecipientEncryptedKeys::= SEQUENCE OF RecipientEncryptedKey
307 //
308 // RecipientEncryptedKey::= SEQUENCE {
309 // rid KeyAgreeRecipientIdentifier,
310 // encryptedKey EncryptedKey }
311 //
312 // When RecipientEncryptedKeys has more than one sequence in it, then different KeyAgreement recipients are created, where each
313 // recipient hold the information from one RecipientEncryptedKey. This message has one KeyAgreeRecipientInfo object that has two
314 // RecipientEncryptedKeys so it needs to have two recipients in the end.
315
316 byte[] encodedMessage =
317 ("3082019206092A864886F70D010703A08201833082017F0201023182014BA1820147020103A08196A18193300906072A"
318 + "8648CE3E02010381850002818100AC89002E19D3A7DC35DAFBF083413483EF14691FC00A465B957496CA860BA4918182"
319 + "1CAFB50EB25330952BB11A71A44B44691CF9779999F1115497CD1CE238B452CA95622AF968E39F06E165D2EBE1991493"
320 + "70334D925AA47273751AC63A0EF80CDCF6331ED3324CD689BFFC90E61E9CC921C88EF5FB92B863053C4C1FABFE15301E"
321 + "060B2A864886F70D0109100305300F060B2A864886F70D010910030605003081883042A016041410DA1370316788112E"
322 + "B8594C864C2420AE7FBA420428DFBDC19AD44063478A0C125641BE274113441AD5891C78F925097F06A3DF57F3F1E6D1"
323 + "160F8D3C223042A016041411DA1370316788112EB8594C864C2420AE7FBA420428DFBDC19AD44063478A0C125641BE27"
324 + "4113441AD5891C78F925097F06A3DF57F3F1E6D1160F8D3C22302B06092A864886F70D010701301406082A864886F70D"
325 + "030704088AADC286F258F6D78008FC304F518A653F83").HexToByteArray();
326
327 EnvelopedCms ecms = new EnvelopedCms();
328 ecms.Decode(encodedMessage);
329
330 RecipientInfoCollection recipients = ecms.RecipientInfos;
331 Assert.Equal(2, recipients.Count);
332 RecipientInfo recipient0 = recipients[0];
333 RecipientInfo recipient1 = recipients[1];
334
335 Assert.IsType<KeyAgreeRecipientInfo>(recipient0);
336 Assert.IsType<KeyAgreeRecipientInfo>(recipient1);
337
338 KeyAgreeRecipientInfo recipient0Cast = recipient0 as KeyAgreeRecipientInfo;
339 KeyAgreeRecipientInfo recipient1Cast = recipient1 as KeyAgreeRecipientInfo;
340
341 Assert.Equal(3, recipient0.Version);
342 Assert.Equal(3, recipient1.Version);
343
344 Assert.Equal(SubjectIdentifierOrKeyType.PublicKeyInfo, recipient0Cast.OriginatorIdentifierOrKey.Type);
345 Assert.Equal(SubjectIdentifierOrKeyType.PublicKeyInfo, recipient1Cast.OriginatorIdentifierOrKey.Type);
346
347 Assert.Equal(SubjectIdentifierType.SubjectKeyIdentifier, recipient0Cast.RecipientIdentifier.Type);
348 Assert.Equal(SubjectIdentifierType.SubjectKeyIdentifier, recipient1Cast.RecipientIdentifier.Type);
349
350 Assert.Equal("10DA1370316788112EB8594C864C2420AE7FBA42", recipient0Cast.RecipientIdentifier.Value);
351 Assert.Equal("11DA1370316788112EB8594C864C2420AE7FBA42", recipient1Cast.RecipientIdentifier.Value);
352 }
353
354 private static KeyAgreeRecipientInfo EncodeKeyAgreel(SubjectIdentifierType type = SubjectIdentifierType.IssuerAndSerialNumber)
355 {
356 ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
357 EnvelopedCms ecms = new EnvelopedCms(contentInfo);
358 using (X509Certificate2 cert = Certificates.DHKeyAgree1.GetCertificate())
359 {
360 CmsRecipient cmsRecipient = new CmsRecipient(type, cert);
361 ecms.Encrypt(cmsRecipient);
362 }
363 byte[] encodedMessage = ecms.Encode();
364
365 EnvelopedCms ecms2 = new EnvelopedCms();
366 ecms2.Decode(encodedMessage);
367
368 RecipientInfoCollection recipients = ecms2.RecipientInfos;
369 Assert.Equal(1, recipients.Count);
370 RecipientInfo recipientInfo = recipients[0];
371 Assert.True(recipientInfo is KeyAgreeRecipientInfo);
372 return (KeyAgreeRecipientInfo)recipientInfo;
373 }
374
375 private static KeyAgreeRecipientInfo FixedValueKeyAgree1(SubjectIdentifierType type = SubjectIdentifierType.IssuerAndSerialNumber)
376 {
377 byte[] encodedMessage;
378 switch (type)
379 {
380 case SubjectIdentifierType.IssuerAndSerialNumber:
381 encodedMessage = s_KeyAgreeEncodedMessage;
382 break;
383
384 case SubjectIdentifierType.SubjectKeyIdentifier:
385 encodedMessage = s_KeyAgreeEncodedMessage_Ski;
386 break;
387
388 default:
389 throw new Exception("Bad SubjectIdentifierType.");
390 }
391
392 EnvelopedCms ecms = new EnvelopedCms();
393 ecms.Decode(encodedMessage);
394
395 RecipientInfoCollection recipients = ecms.RecipientInfos;
396 Assert.Equal(1, recipients.Count);
397 RecipientInfo recipientInfo = recipients[0];
398 Assert.True(recipientInfo is KeyAgreeRecipientInfo);
399 return (KeyAgreeRecipientInfo)recipientInfo;
400 }
401
402 private static byte[] s_KeyAgreeEncodedMessage =
403 ("3082019b06092a864886f70d010703a082018c3082018802010231820154a1820150020103a08195a18192300906072a8648"
404 + "ce3e0201038184000281806f96ef8c53a6919cc976e88b8f426696e7b7970abc6bd4abbdcf4cf34f89ceb6e8ef675000fad2"
405 + "eca3caf9d0e51b004fd19a943f1779748f343fe2059e6e8208d64cb2a5bf33b2c41c20f4ae950d8f8bd720f5747d7930af86"
406 + "c612088747b5315ae68159a5ae8a80e928aa71f4e889cb2d581845edc8f79da5894cb7a40f9fbe301e060b2a864886f70d01"
407 + "09100305300f060b2a864886f70d0109100306050030819230818f3063304f314d304b06035504031e44004d0061006e0061"
408 + "00670065006400200050004b00430053002300370020005400650073007400200052006f006f007400200041007500740068"
409 + "006f007200690074007902100ae59b0cb8119f8942eda74163413a020428c39323a9f5113c1465bf27b558ffeda656d606e0"
410 + "8f8dc37e67cb8cbf7fb04d71dbe20071eaaa20db302b06092a864886f70d010701301406082a864886f70d0307040879d81f"
411 + "ee4a736dde800892bc977ea496752d").HexToByteArray();
412
413 private static byte[] s_KeyAgreeEncodedMessage_Ski =
414 ("3082014d06092a864886f70d010703a082013e3082013a02010231820106a1820102020103a08196a18193300906072a8648"
415 + "ce3e02010381850002818100ac89002e19d3a7dc35dafbf083413483ef14691fc00a465b957496ca860ba49181821cafb50e"
416 + "b25330952bb11a71a44b44691cf9779999f1115497cd1ce238b452ca95622af968e39f06e165d2ebe199149370334d925aa4"
417 + "7273751ac63a0ef80cdcf6331ed3324cd689bffc90e61e9cc921c88ef5fb92b863053c4c1fabfe15301e060b2a864886f70d"
418 + "0109100305300f060b2a864886f70d0109100306050030443042a016041410da1370316788112eb8594c864c2420ae7fba42"
419 + "0428dfbdc19ad44063478a0c125641be274113441ad5891c78f925097f06a3df57f3f1e6d1160f8d3c22302b06092a864886"
420 + "f70d010701301406082a864886f70d030704088aadc286f258f6d78008fc304f518a653f83").HexToByteArray();
421 }
422 }
423
424