Debian Codegraph @

external/corefx/src/System.Security.Cryptography.Pkcs/tests/EnvelopedCms/KeyTransRecipientInfoTests.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.IO;
6 using System.Linq;
7 using System.Globalization;
8 using System.Collections.Generic;
9 using System.Security.Cryptography;
10 using System.Runtime.InteropServices;
11 using System.Text;
12 using System.Security.Cryptography.Pkcs;
13 using System.Security.Cryptography.Xml;
14 using System.Security.Cryptography.X509Certificates;
15 using Xunit;
16
17 using Test.Cryptography;
18 using System.Security.Cryptography.Pkcs.Tests;
19
20 namespace System.Security.Cryptography.Pkcs.EnvelopedCmsTests.Tests
21 {
22 public static partial class KeyTransRecipientInfoTests
23 {
24 [Fact]
25 public static void TestKeyTransVersion_RoundTrip()
26 {
27 KeyTransRecipientInfo recipient = EncodeKeyTransl();
28 Assert.Equal(0, recipient.Version);
29 }
30
31 [Fact]
32 public static void TestKeyTransVersion_FixedValue()
33 {
34 KeyTransRecipientInfo recipient = FixedValueKeyTrans1();
35 Assert.Equal(0, recipient.Version);
36 }
37
38 [Fact]
39 public static void TestKeyTransType_RoundTrip()
40 {
41 KeyTransRecipientInfo recipient = EncodeKeyTransl();
42 Assert.Equal(RecipientInfoType.KeyTransport, recipient.Type);
43 }
44
45 [Fact]
46 public static void TestKeyTransType_FixedValue()
47 {
48 KeyTransRecipientInfo recipient = FixedValueKeyTrans1();
49 Assert.Equal(RecipientInfoType.KeyTransport, recipient.Type);
50 }
51
52 [Fact]
53 public static void TestKeyTransRecipientIdType_RoundTrip()
54 {
55 KeyTransRecipientInfo recipient = EncodeKeyTransl();
56 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
57 Assert.Equal(SubjectIdentifierType.IssuerAndSerialNumber, subjectIdentifier.Type);
58 }
59
60 [Fact]
61 public static void TestKeyTransRecipientIdType_FixedValue()
62 {
63 KeyTransRecipientInfo recipient = FixedValueKeyTrans1();
64 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
65 Assert.Equal(SubjectIdentifierType.IssuerAndSerialNumber, subjectIdentifier.Type);
66 }
67
68 [Fact]
69 public static void TestKeyTransRecipientIdValue_RoundTrip()
70 {
71 KeyTransRecipientInfo recipient = EncodeKeyTransl();
72 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
73 object value = subjectIdentifier.Value;
74 Assert.True(value is X509IssuerSerial);
75 X509IssuerSerial xis = (X509IssuerSerial)value;
76 Assert.Equal("CN=RSAKeyTransfer1", xis.IssuerName);
77 Assert.Equal("31D935FB63E8CFAB48A0BF7B397B67C0", xis.SerialNumber);
78 }
79
80 [Fact]
81 public static void TestKeyTransRecipientIdValue_FixedValue()
82 {
83 KeyTransRecipientInfo recipient = FixedValueKeyTrans1();
84 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
85 object value = subjectIdentifier.Value;
86 Assert.True(value is X509IssuerSerial);
87 X509IssuerSerial xis = (X509IssuerSerial)value;
88 Assert.Equal("CN=RSAKeyTransfer1", xis.IssuerName);
89 Assert.Equal("31D935FB63E8CFAB48A0BF7B397B67C0", xis.SerialNumber);
90 }
91
92 [Fact]
93 public static void TestKeyTransRecipientIdType_Ski_RoundTrip()
94 {
95 KeyTransRecipientInfo recipient = EncodeKeyTransl(SubjectIdentifierType.SubjectKeyIdentifier);
96 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
97 Assert.Equal(SubjectIdentifierType.SubjectKeyIdentifier, subjectIdentifier.Type);
98 }
99
100 [Fact]
101 public static void TestKeyTransRecipientIdType_Ski_FixedValue()
102 {
103 KeyTransRecipientInfo recipient = FixedValueKeyTrans1(SubjectIdentifierType.SubjectKeyIdentifier);
104 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
105 Assert.Equal(SubjectIdentifierType.SubjectKeyIdentifier, subjectIdentifier.Type);
106 }
107
108 [Fact]
109 public static void TestKeyTransRecipientIdValue_Ski_RoundTrip()
110 {
111 KeyTransRecipientInfo recipient = EncodeKeyTransl(SubjectIdentifierType.SubjectKeyIdentifier);
112 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
113 object value = subjectIdentifier.Value;
114 Assert.True(value is string);
115 string ski = (string)value;
116 Assert.Equal("F2008AA9FA3742E8370CB1674CE1D1582921DCC3", ski);
117 }
118
119 [Fact]
120 [OuterLoop(/* Leaks key on disk if interrupted */)]
121 public static void TestKeyTransRecipientIdValue_ExplicitSki_RoundTrip()
122 {
123 ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
124 EnvelopedCms ecms = new EnvelopedCms(contentInfo);
125 using (X509Certificate2 cert = Certificates.RSAKeyTransfer_ExplicitSki.GetCertificate())
126 {
127 CmsRecipient cmsRecipient = new CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, cert);
128 ecms.Encrypt(cmsRecipient);
129 }
130 byte[] encodedMessage = ecms.Encode();
131
132 EnvelopedCms ecms2 = new EnvelopedCms();
133 ecms2.Decode(encodedMessage);
134
135 RecipientInfoCollection recipients = ecms2.RecipientInfos;
136 Assert.Equal(1, recipients.Count);
137 RecipientInfo recipientInfo = recipients[0];
138 Assert.IsType<KeyTransRecipientInfo>(recipientInfo);
139 KeyTransRecipientInfo recipient = (KeyTransRecipientInfo)recipientInfo;
140
141 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
142 object value = subjectIdentifier.Value;
143 Assert.IsType<string>(value);
144 string ski = (string)value;
145 Assert.Equal("01952851C55DB594B0C6167F5863C5B6B67AEFE6", ski);
146 }
147
148 [Fact]
149 public static void TestKeyTransRecipientIdValue_Ski_FixedValue()
150 {
151 KeyTransRecipientInfo recipient = FixedValueKeyTrans1(SubjectIdentifierType.SubjectKeyIdentifier);
152 SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
153 object value = subjectIdentifier.Value;
154 Assert.True(value is string);
155 string ski = (string)value;
156 Assert.Equal("F2008AA9FA3742E8370CB1674CE1D1582921DCC3", ski);
157 }
158
159 [Fact]
160 public static void TestKeyTransKeyEncryptionAlgorithm_RoundTrip()
161 {
162 KeyTransRecipientInfo recipient = EncodeKeyTransl();
163 AlgorithmIdentifier a = recipient.KeyEncryptionAlgorithm;
164 Assert.Equal(Oids.Rsa, a.Oid.Value);
165 Assert.Equal(0, a.KeyLength);
166 }
167
168 [Fact]
169 public static void TestKeyTransKeyEncryptionAlgorithm_FixedValue()
170 {
171 KeyTransRecipientInfo recipient = FixedValueKeyTrans1();
172 AlgorithmIdentifier a = recipient.KeyEncryptionAlgorithm;
173 Assert.Equal(Oids.Rsa, a.Oid.Value);
174 Assert.Equal(0, a.KeyLength);
175 }
176
177 [Fact]
178 public static void TestKeyTransEncryptedKey_RoundTrip()
179 {
180 KeyTransRecipientInfo recipient = EncodeKeyTransl();
181 byte[] encryptedKey = recipient.EncryptedKey;
182 Assert.Equal(128, encryptedKey.Length); // Since the content encryption key is randomly generated each time, we can only test the length.
183 }
184
185 [Fact]
186 public static void TestKeyTransEncryptedKey_FixedValue()
187 {
188 byte[] expectedEncryptedKey =
189 ("5ebb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee5"
190 + "0c25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160"
191 + "c496726216e986869eed578bda652855c85604a056201538ee56b6c4").HexToByteArray();
192
193 KeyTransRecipientInfo recipient = FixedValueKeyTrans1();
194 byte[] encryptedKey = recipient.EncryptedKey;
195 Assert.Equal<byte>(expectedEncryptedKey, encryptedKey);
196 }
197
198 private static KeyTransRecipientInfo EncodeKeyTransl(SubjectIdentifierType type = SubjectIdentifierType.IssuerAndSerialNumber)
199 {
200 ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
201 EnvelopedCms ecms = new EnvelopedCms(contentInfo);
202 using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
203 {
204 CmsRecipient cmsRecipient = new CmsRecipient(type, cert);
205 ecms.Encrypt(cmsRecipient);
206 }
207 byte[] encodedMessage = ecms.Encode();
208
209 EnvelopedCms ecms2 = new EnvelopedCms();
210 ecms2.Decode(encodedMessage);
211
212 RecipientInfoCollection recipients = ecms2.RecipientInfos;
213 Assert.Equal(1, recipients.Count);
214 RecipientInfo recipientInfo = recipients[0];
215 Assert.IsType<KeyTransRecipientInfo>(recipientInfo);
216 return (KeyTransRecipientInfo)recipientInfo;
217 }
218
219 private static KeyTransRecipientInfo FixedValueKeyTrans1(SubjectIdentifierType type = SubjectIdentifierType.IssuerAndSerialNumber)
220 {
221 byte[] encodedMessage;
222 switch (type)
223 {
224 case SubjectIdentifierType.IssuerAndSerialNumber:
225 encodedMessage = s_KeyTransEncodedMessage;
226 break;
227
228 case SubjectIdentifierType.SubjectKeyIdentifier:
229 encodedMessage = s_KeyTransEncodedMessage_Ski;
230 break;
231
232 default:
233 throw new Exception("Bad SubjectIdentifierType.");
234 }
235
236 EnvelopedCms ecms = new EnvelopedCms();
237 ecms.Decode(encodedMessage);
238
239 RecipientInfoCollection recipients = ecms.RecipientInfos;
240 Assert.Equal(1, recipients.Count);
241 RecipientInfo recipientInfo = recipients[0];
242 Assert.IsType<KeyTransRecipientInfo>(recipientInfo);
243 return (KeyTransRecipientInfo)recipientInfo;
244 }
245
246 private static byte[] s_KeyTransEncodedMessage =
247 ("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253"
248 + "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e"
249 + "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c"
250 + "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4"
251 + "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d"
252 + "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray();
253
254 private static byte[] s_KeyTransEncodedMessage_Ski =
255 ("3081f206092a864886f70d010703a081e43081e10201023181ae3081ab0201028014f2008aa9fa3742e8370cb1674ce1d158"
256 + "2921dcc3300d06092a864886f70d01010105000481804336e978bc72ba2f5264cd854867fac438f36f2b3df6004528f2df83"
257 + "4fb2113d6f7c07667e7296b029756222d6ced396a8fffed32be838eec7f2e54b9467fa80f85d097f7d1f0fbde57e07ab3d46"
258 + "a60b31f37ef9844dcab2a8eef4fec5579fac5ec1e7ee82409898e17d30c3ac1a407fca15d23c9df2904a707294d78d4300ba"
259 + "302b06092a864886f70d010701301406082a864886f70d03070408355c596e3e8540608008f1f811e862e51bbd").HexToByteArray();
260 }
261 }
262
263