| 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 EdgeCasesTests |
| 15 |
{ |
| 16 |
public static bool SupportsRc4 { get; } = ContentEncryptionAlgorithmTests.SupportsRc4; |
| 17 |
|
| 18 |
public static bool SupportsCngCertificates { get; } = (!PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer); |
| 19 |
|
| 20 |
[ConditionalFact(nameof(SupportsCngCertificates))] |
| 21 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 22 |
public static void ImportEdgeCase() |
| 23 |
{ |
| 24 |
// |
| 25 |
// Pfx's imported into a certificate collection propagate their "delete on Dispose" behavior to its cloned instances: |
| 26 |
// a subtle difference from Pfx's created using the X509Certificate2 constructor that can lead to premature or |
| 27 |
// double key deletion. Since EnvelopeCms.Decrypt() has no legitimate reason to clone the extraStore certs, this shouldn't |
| 28 |
// be a problem, but this test will verify that it isn't. |
| 29 |
// |
| 30 |
|
| 31 |
byte[] encodedMessage = |
| 32 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 33 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e" |
| 34 |
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c" |
| 35 |
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4" |
| 36 |
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d" |
| 37 |
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray(); |
| 38 |
|
| 39 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 40 |
ecms.Decode(encodedMessage); |
| 41 |
|
| 42 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.LoadPfxUsingCollectionImport()) |
| 43 |
{ |
| 44 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(cert); |
| 45 |
ecms.Decrypt(extraStore); |
| 46 |
|
| 47 |
byte[] expectedContent = { 1, 2, 3 }; |
| 48 |
ContentInfo contentInfo = ecms.ContentInfo; |
| 49 |
Assert.Equal<byte>(expectedContent, contentInfo.Content); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
[ConditionalFact(nameof(SupportsCngCertificates))] |
| 54 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 55 |
public static void ImportEdgeCaseSki() |
| 56 |
{ |
| 57 |
byte[] encodedMessage = |
| 58 |
("3081f206092a864886f70d010703a081e43081e10201023181ae3081ab0201028014f2008aa9fa3742e8370cb1674ce1d158" |
| 59 |
+ "2921dcc3300d06092a864886f70d01010105000481804336e978bc72ba2f5264cd854867fac438f36f2b3df6004528f2df83" |
| 60 |
+ "4fb2113d6f7c07667e7296b029756222d6ced396a8fffed32be838eec7f2e54b9467fa80f85d097f7d1f0fbde57e07ab3d46" |
| 61 |
+ "a60b31f37ef9844dcab2a8eef4fec5579fac5ec1e7ee82409898e17d30c3ac1a407fca15d23c9df2904a707294d78d4300ba" |
| 62 |
+ "302b06092a864886f70d010701301406082a864886f70d03070408355c596e3e8540608008f1f811e862e51bbd").HexToByteArray(); |
| 63 |
|
| 64 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 65 |
ecms.Decode(encodedMessage); |
| 66 |
|
| 67 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.LoadPfxUsingCollectionImport()) |
| 68 |
{ |
| 69 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(cert); |
| 70 |
ecms.Decrypt(extraStore); |
| 71 |
|
| 72 |
byte[] expectedContent = { 1, 2, 3 }; |
| 73 |
ContentInfo contentInfo = ecms.ContentInfo; |
| 74 |
Assert.Equal<byte>(new byte[] { 1, 2, 3 }, contentInfo.Content); |
| 75 |
Assert.Equal<byte>(expectedContent, contentInfo.Content); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private static X509Certificate2 LoadPfxUsingCollectionImport(this CertLoader certLoader) |
| 80 |
{ |
| 81 |
byte[] pfxData = certLoader.PfxData; |
| 82 |
string password = certLoader.Password; |
| 83 |
X509Certificate2Collection collection = new X509Certificate2Collection(); |
| 84 |
collection.Import(pfxData, password, X509KeyStorageFlags.DefaultKeySet); |
| 85 |
Assert.Equal(1, collection.Count); |
| 86 |
return collection[0]; |
| 87 |
} |
| 88 |
|
| 89 |
[Fact] |
| 90 |
public static void ZeroLengthContextUntagged_FixedValue() |
| 91 |
{ |
| 92 |
// This test ensures that we can handle when the enveloped message has no content inside. This test differs |
| 93 |
// from "ZeroLengthContent_FixedValue" in that it doesn't have a context specific [0] in the EncryptedContentInfo |
| 94 |
// section of the DER encoding of the message. |
| 95 |
// EncryptedContentInfo ::= SEQUENCE { |
| 96 |
// contentType ContentType, |
| 97 |
// contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier, |
| 98 |
// encryptedContent[0] IMPLICIT EncryptedContent OPTIONAL } |
| 99 |
// The input was created with ASN1 editor and verified with .NET framework. It's an enveloped message, version 0, |
| 100 |
// with one Key Transport recipient and holds data encrypted with 3DES. |
| 101 |
byte[] content = |
| 102 |
("3082010206092A864886F70D010703A081F43081F10201003181C83081C5020100302E301A311830160603550403130F" |
| 103 |
+ "5253414B65795472616E7366657231021031D935FB63E8CFAB48A0BF7B397B67C0300D06092A864886F70D0101010500" |
| 104 |
+ "04818009C16B674495C2C3D4763189C3274CF7A9142FBEEC8902ABDC9CE29910D541DF910E029A31443DC9A9F3B05F02" |
| 105 |
+ "DA1C38478C400261C734D6789C4197C20143C4312CEAA99ECB1849718326D4FC3B7FBB2D1D23281E31584A63E99F2C17" |
| 106 |
+ "132BCD8EDDB632967125CD0A4BAA1EFA8CE4C855F7C093339211BDF990CEF5CCE6CD74302106092A864886F70D010701" |
| 107 |
+ "301406082A864886F70D03070408779B3DE045826B18").HexToByteArray(); |
| 108 |
|
| 109 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 110 |
ecms.Decode(content); |
| 111 |
|
| 112 |
int expected = PlatformDetection.IsFullFramework ? 6 : 0; // Desktop bug gives 6 |
| 113 |
Assert.Equal(expected, ecms.ContentInfo.Content.Length); |
| 114 |
Assert.Equal(Oids.Pkcs7Data, ecms.ContentInfo.ContentType.Value); |
| 115 |
} |
| 116 |
|
| 117 |
[Fact] |
| 118 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 119 |
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Desktop rejects zero length content: corefx#18724")] |
| 120 |
public static void ZeroLengthContent_RoundTrip() |
| 121 |
{ |
| 122 |
ContentInfo contentInfo = new ContentInfo(Array.Empty<byte>()); |
| 123 |
EnvelopedCms ecms = new EnvelopedCms(contentInfo); |
| 124 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 125 |
{ |
| 126 |
CmsRecipient cmsRecipient = new CmsRecipient(cert); |
| 127 |
ecms.Encrypt(cmsRecipient); |
| 128 |
} |
| 129 |
byte[] encodedMessage = ecms.Encode(); |
| 130 |
ValidateZeroLengthContent(encodedMessage); |
| 131 |
} |
| 132 |
|
| 133 |
[ConditionalFact(nameof(SupportsCngCertificates))] |
| 134 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 135 |
public static void ZeroLengthContent_FixedValue() |
| 136 |
{ |
| 137 |
byte[] encodedMessage = |
| 138 |
("3082010406092a864886f70d010703a081f63081f30201003181c83081c5020100302e301a311830160603550403130f5253" |
| 139 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004818009" |
| 140 |
+ "c16b674495c2c3d4763189c3274cf7a9142fbeec8902abdc9ce29910d541df910e029a31443dc9a9f3b05f02da1c38478c40" |
| 141 |
+ "0261c734d6789c4197c20143c4312ceaa99ecb1849718326d4fc3b7fbb2d1d23281e31584a63e99f2c17132bcd8eddb63296" |
| 142 |
+ "7125cd0a4baa1efa8ce4c855f7c093339211bdf990cef5cce6cd74302306092a864886f70d010701301406082a864886f70d" |
| 143 |
+ "03070408779b3de045826b188000").HexToByteArray(); |
| 144 |
ValidateZeroLengthContent(encodedMessage); |
| 145 |
} |
| 146 |
|
| 147 |
[ConditionalFact(nameof(SupportsRc4))] |
| 148 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 149 |
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap, "RC4 isn't available via CNG, and CNG is the only library available to UWP")] |
| 150 |
public static void Rc4AndCngWrappersDontMixTest() |
| 151 |
{ |
| 152 |
// |
| 153 |
// Combination of RC4 over a CAPI certificate. |
| 154 |
// |
| 155 |
// This works as long as the PKCS implementation opens the cert using CAPI. If he creates a CNG wrapper handle (by passing CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG), |
| 156 |
// the test fails with a NOTSUPPORTED crypto exception inside Decrypt(). The same happens if the key is genuinely CNG. |
| 157 |
// |
| 158 |
|
| 159 |
byte[] content = { 6, 3, 128, 33, 44 }; |
| 160 |
AlgorithmIdentifier rc4 = new AlgorithmIdentifier(new Oid(Oids.Rc4)); |
| 161 |
|
| 162 |
EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(content), rc4); |
| 163 |
CmsRecipientCollection recipients = new CmsRecipientCollection(new CmsRecipient(Certificates.RSAKeyTransferCapi1.GetCertificate())); |
| 164 |
ecms.Encrypt(recipients); |
| 165 |
byte[] encodedMessage = ecms.Encode(); |
| 166 |
|
| 167 |
ecms = new EnvelopedCms(); |
| 168 |
ecms.Decode(encodedMessage); |
| 169 |
|
| 170 |
// In order to actually use the CAPI version of the key, perphemeral loading must be specified. |
| 171 |
using (X509Certificate2 cert = Certificates.RSAKeyTransferCapi1.CloneAsPerphemeralLoader().TryGetCertificateWithPrivateKey()) |
| 172 |
{ |
| 173 |
if (cert == null) |
| 174 |
return; // Sorry - CertLoader is not configured to load certs with private keys - we've tested as much as we can. |
| 175 |
|
| 176 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(); |
| 177 |
extraStore.Add(cert); |
| 178 |
ecms.Decrypt(extraStore); |
| 179 |
} |
| 180 |
|
| 181 |
ContentInfo contentInfo = ecms.ContentInfo; |
| 182 |
Assert.Equal<byte>(content, contentInfo.Content); |
| 183 |
} |
| 184 |
|
| 185 |
private static void ValidateZeroLengthContent(byte[] encodedMessage) |
| 186 |
{ |
| 187 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 188 |
ecms.Decode(encodedMessage); |
| 189 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.TryGetCertificateWithPrivateKey()) |
| 190 |
{ |
| 191 |
if (cert == null) |
| 192 |
return; |
| 193 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(cert); |
| 194 |
ecms.Decrypt(extraStore); |
| 195 |
ContentInfo contentInfo = ecms.ContentInfo; |
| 196 |
byte[] content = contentInfo.Content; |
| 197 |
|
| 198 |
int expected = PlatformDetection.IsFullFramework ? 6 : 0; // Desktop bug gives 6 |
| 199 |
Assert.Equal(expected, content.Length); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
[Fact] |
| 204 |
public static void ReuseEnvelopeCmsEncodeThenDecode() |
| 205 |
{ |
| 206 |
// Test ability to encrypt, encode and decode all in one EnvelopedCms instance. |
| 207 |
|
| 208 |
ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 209 |
EnvelopedCms ecms = new EnvelopedCms(contentInfo); |
| 210 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 211 |
{ |
| 212 |
CmsRecipient cmsRecipient = new CmsRecipient(cert); |
| 213 |
ecms.Encrypt(cmsRecipient); |
| 214 |
} |
| 215 |
|
| 216 |
byte[] encodedMessage = ecms.Encode(); |
| 217 |
ecms.Decode(encodedMessage); |
| 218 |
|
| 219 |
RecipientInfoCollection recipients = ecms.RecipientInfos; |
| 220 |
Assert.Equal(1, recipients.Count); |
| 221 |
RecipientInfo recipientInfo = recipients[0]; |
| 222 |
KeyTransRecipientInfo recipient = recipientInfo as KeyTransRecipientInfo; |
| 223 |
Assert.NotNull(recipientInfo); |
| 224 |
|
| 225 |
SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier; |
| 226 |
object value = subjectIdentifier.Value; |
| 227 |
Assert.True(value is X509IssuerSerial); |
| 228 |
X509IssuerSerial xis = (X509IssuerSerial)value; |
| 229 |
Assert.Equal("CN=RSAKeyTransfer1", xis.IssuerName); |
| 230 |
Assert.Equal("31D935FB63E8CFAB48A0BF7B397B67C0", xis.SerialNumber); |
| 231 |
} |
| 232 |
|
| 233 |
[Fact] |
| 234 |
public static void ReuseEnvelopeCmsDecodeThenEncode() |
| 235 |
{ |
| 236 |
byte[] encodedMessage = |
| 237 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 238 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e" |
| 239 |
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c" |
| 240 |
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4" |
| 241 |
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d" |
| 242 |
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray(); |
| 243 |
|
| 244 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 245 |
ecms.Decode(encodedMessage); |
| 246 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 247 |
{ |
| 248 |
CmsRecipient cmsRecipient = new CmsRecipient(cert); |
| 249 |
ecms.Encrypt(cmsRecipient); |
| 250 |
} |
| 251 |
|
| 252 |
encodedMessage = ecms.Encode(); |
| 253 |
ecms.Decode(encodedMessage); |
| 254 |
|
| 255 |
RecipientInfoCollection recipients = ecms.RecipientInfos; |
| 256 |
Assert.Equal(1, recipients.Count); |
| 257 |
RecipientInfo recipientInfo = recipients[0]; |
| 258 |
KeyTransRecipientInfo recipient = recipientInfo as KeyTransRecipientInfo; |
| 259 |
Assert.NotNull(recipientInfo); |
| 260 |
|
| 261 |
SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier; |
| 262 |
object value = subjectIdentifier.Value; |
| 263 |
Assert.True(value is X509IssuerSerial); |
| 264 |
X509IssuerSerial xis = (X509IssuerSerial)value; |
| 265 |
Assert.Equal("CN=RSAKeyTransfer1", xis.IssuerName); |
| 266 |
Assert.Equal("31D935FB63E8CFAB48A0BF7B397B67C0", xis.SerialNumber); |
| 267 |
} |
| 268 |
|
| 269 |
[Fact] |
| 270 |
public static void EnvelopedCmsNullContent() |
| 271 |
{ |
| 272 |
object ignore; |
| 273 |
Assert.Throws<ArgumentNullException>(() => ignore = new EnvelopedCms(null)); |
| 274 |
Assert.Throws<ArgumentNullException>(() => ignore = new EnvelopedCms(null, new AlgorithmIdentifier(new Oid(Oids.TripleDesCbc)))); |
| 275 |
} |
| 276 |
|
| 277 |
[Fact] |
| 278 |
public static void EnvelopedCmsNullAlgorithm() |
| 279 |
{ |
| 280 |
object ignore; |
| 281 |
ContentInfo contentInfo = new ContentInfo(new byte[3]); |
| 282 |
Assert.Throws<ArgumentNullException>(() => ignore = new EnvelopedCms(contentInfo, null)); |
| 283 |
} |
| 284 |
|
| 285 |
[Fact] |
| 286 |
public static void EnvelopedCmsEncryptWithNullRecipient() |
| 287 |
{ |
| 288 |
EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(new byte[3])); |
| 289 |
Assert.Throws<ArgumentNullException>(() => ecms.Encrypt((CmsRecipient)null)); |
| 290 |
} |
| 291 |
|
| 292 |
[Fact] |
| 293 |
public static void EnvelopedCmsEncryptWithNullRecipients() |
| 294 |
{ |
| 295 |
EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(new byte[3])); |
| 296 |
Assert.Throws<ArgumentNullException>(() => ecms.Encrypt((CmsRecipientCollection)null)); |
| 297 |
} |
| 298 |
|
| 299 |
[Fact] |
| 300 |
// On the desktop, this throws up a UI for the user to select a recipient. We don't support that. |
| 301 |
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] |
| 302 |
public static void EnvelopedCmsEncryptWithZeroRecipients() |
| 303 |
{ |
| 304 |
EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(new byte[3])); |
| 305 |
Assert.Throws<PlatformNotSupportedException>(() => ecms.Encrypt(new CmsRecipientCollection())); |
| 306 |
} |
| 307 |
|
| 308 |
[Fact] |
| 309 |
public static void EnvelopedCmsNullDecode() |
| 310 |
{ |
| 311 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 312 |
Assert.Throws<ArgumentNullException>(() => ecms.Decode(null)); |
| 313 |
} |
| 314 |
|
| 315 |
[Fact] |
| 316 |
public static void EnvelopedCmsDecryptNullary() |
| 317 |
{ |
| 318 |
byte[] encodedMessage = |
| 319 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 320 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e" |
| 321 |
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c" |
| 322 |
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4" |
| 323 |
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d" |
| 324 |
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray(); |
| 325 |
|
| 326 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 327 |
ecms.Decode(encodedMessage); |
| 328 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt()); |
| 329 |
} |
| 330 |
|
| 331 |
[Fact] |
| 332 |
public static void EnvelopedCmsDecryptNullRecipient() |
| 333 |
{ |
| 334 |
byte[] encodedMessage = |
| 335 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 336 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e" |
| 337 |
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c" |
| 338 |
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4" |
| 339 |
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d" |
| 340 |
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray(); |
| 341 |
|
| 342 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 343 |
ecms.Decode(encodedMessage); |
| 344 |
RecipientInfo recipientInfo = null; |
| 345 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(); |
| 346 |
Assert.Throws<ArgumentNullException>(() => ecms.Decrypt(recipientInfo)); |
| 347 |
Assert.Throws<ArgumentNullException>(() => ecms.Decrypt(recipientInfo, extraStore)); |
| 348 |
} |
| 349 |
|
| 350 |
[Fact] |
| 351 |
public static void EnvelopedCmsDecryptNullExtraStore() |
| 352 |
{ |
| 353 |
byte[] encodedMessage = |
| 354 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 355 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e" |
| 356 |
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c" |
| 357 |
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4" |
| 358 |
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d" |
| 359 |
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray(); |
| 360 |
|
| 361 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 362 |
ecms.Decode(encodedMessage); |
| 363 |
RecipientInfo recipientInfo = ecms.RecipientInfos[0]; |
| 364 |
X509Certificate2Collection extraStore = null; |
| 365 |
Assert.Throws<ArgumentNullException>(() => ecms.Decrypt(extraStore)); |
| 366 |
Assert.Throws<ArgumentNullException>(() => ecms.Decrypt(recipientInfo, extraStore)); |
| 367 |
} |
| 368 |
|
| 369 |
[Fact] |
| 370 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 371 |
public static void EnvelopedCmsDecryptWithoutMatchingCert() |
| 372 |
{ |
| 373 |
// You don't have the private key? No message for you. |
| 374 |
|
| 375 |
// This is the private key that "we don't have." We want to force it to load anyway, though, to trigger |
| 376 |
// the "fail the test due to bad machine config" exception if someone left this cert in the MY store check. |
| 377 |
using (X509Certificate2 ignore = Certificates.RSAKeyTransfer1.TryGetCertificateWithPrivateKey()) |
| 378 |
{ } |
| 379 |
|
| 380 |
byte[] encodedMessage = |
| 381 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 382 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e" |
| 383 |
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c" |
| 384 |
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4" |
| 385 |
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d" |
| 386 |
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray(); |
| 387 |
|
| 388 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 389 |
ecms.Decode(encodedMessage); |
| 390 |
RecipientInfo recipientInfo = ecms.RecipientInfos[0]; |
| 391 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(); |
| 392 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt(recipientInfo)); |
| 393 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt(extraStore)); |
| 394 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt(recipientInfo, extraStore)); |
| 395 |
} |
| 396 |
|
| 397 |
[Fact] |
| 398 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 399 |
public static void EnvelopedCmsDecryptWithoutMatchingCertSki() |
| 400 |
{ |
| 401 |
// You don't have the private key? No message for you. |
| 402 |
|
| 403 |
// This is the private key that "we don't have." We want to force it to load anyway, though, to trigger |
| 404 |
// the "fail the test due to bad machine config" exception if someone left this cert in the MY store check. |
| 405 |
using (X509Certificate2 ignore = Certificates.RSAKeyTransfer1.TryGetCertificateWithPrivateKey()) |
| 406 |
{ } |
| 407 |
|
| 408 |
byte[] encodedMessage = |
| 409 |
("3081f206092a864886f70d010703a081e43081e10201023181ae3081ab0201028014f2008aa9fa3742e8370cb1674ce1d158" |
| 410 |
+ "2921dcc3300d06092a864886f70d01010105000481804336e978bc72ba2f5264cd854867fac438f36f2b3df6004528f2df83" |
| 411 |
+ "4fb2113d6f7c07667e7296b029756222d6ced396a8fffed32be838eec7f2e54b9467fa80f85d097f7d1f0fbde57e07ab3d46" |
| 412 |
+ "a60b31f37ef9844dcab2a8eef4fec5579fac5ec1e7ee82409898e17d30c3ac1a407fca15d23c9df2904a707294d78d4300ba" |
| 413 |
+ "302b06092a864886f70d010701301406082a864886f70d03070408355c596e3e8540608008f1f811e862e51bbd").HexToByteArray(); |
| 414 |
|
| 415 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 416 |
ecms.Decode(encodedMessage); |
| 417 |
RecipientInfo recipientInfo = ecms.RecipientInfos[0]; |
| 418 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(); |
| 419 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt(recipientInfo)); |
| 420 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt(extraStore)); |
| 421 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt(recipientInfo, extraStore)); |
| 422 |
} |
| 423 |
|
| 424 |
[Fact] |
| 425 |
public static void AlgorithmIdentifierNullaryCtor() |
| 426 |
{ |
| 427 |
AlgorithmIdentifier a = new AlgorithmIdentifier(); |
| 428 |
Assert.Equal(Oids.TripleDesCbc, a.Oid.Value); |
| 429 |
Assert.Equal(0, a.KeyLength); |
| 430 |
} |
| 431 |
|
| 432 |
[Fact] |
| 433 |
public static void CmsRecipient1AryCtor() |
| 434 |
{ |
| 435 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 436 |
{ |
| 437 |
CmsRecipient r = new CmsRecipient(cert); |
| 438 |
Assert.Equal(SubjectIdentifierType.IssuerAndSerialNumber, r.RecipientIdentifierType); |
| 439 |
Assert.Same(cert, r.Certificate); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
[Fact] |
| 444 |
public static void CmsRecipientPassUnknown() |
| 445 |
{ |
| 446 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 447 |
{ |
| 448 |
CmsRecipient r = new CmsRecipient(SubjectIdentifierType.Unknown, cert); |
| 449 |
Assert.Equal(SubjectIdentifierType.IssuerAndSerialNumber, r.RecipientIdentifierType); |
| 450 |
Assert.Same(cert, r.Certificate); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
[Fact] |
| 455 |
public static void CmsRecipientPassNullCertificate() |
| 456 |
{ |
| 457 |
object ignore; |
| 458 |
Assert.Throws<ArgumentNullException>(() => ignore = new CmsRecipient(null)); |
| 459 |
Assert.Throws<ArgumentNullException>(() => ignore = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, null)); |
| 460 |
} |
| 461 |
|
| 462 |
[Fact] |
| 463 |
public static void ContentInfoNullOid() |
| 464 |
{ |
| 465 |
object ignore; |
| 466 |
Assert.Throws<ArgumentNullException>(() => ignore = new ContentInfo(null, new byte[3])); |
| 467 |
} |
| 468 |
|
| 469 |
[Fact] |
| 470 |
public static void ContentInfoNullContent() |
| 471 |
{ |
| 472 |
object ignore; |
| 473 |
Assert.Throws<ArgumentNullException>(() => ignore = new ContentInfo(null)); |
| 474 |
Assert.Throws<ArgumentNullException>(() => ignore = new ContentInfo(null, null)); |
| 475 |
} |
| 476 |
|
| 477 |
[Fact] |
| 478 |
public static void ContentInfoGetContentTypeNull() |
| 479 |
{ |
| 480 |
Assert.Throws<ArgumentNullException>(() => ContentInfo.GetContentType(null)); |
| 481 |
} |
| 482 |
|
| 483 |
[Fact] |
| 484 |
public static void ContentInfoGetContentTypeUnknown() |
| 485 |
{ |
| 486 |
byte[] encodedMessage = |
| 487 |
("301A06092A864886F70D010700A00D040B48656C6C6F202E4E455421").HexToByteArray(); |
| 488 |
Assert.ThrowsAny<CryptographicException>(() => ContentInfo.GetContentType(encodedMessage)); |
| 489 |
} |
| 490 |
|
| 491 |
[Fact] |
| 492 |
public static void CryptographicAttributeObjectOidCtor() |
| 493 |
{ |
| 494 |
Oid oid = new Oid(Oids.DocumentDescription); |
| 495 |
CryptographicAttributeObject cao = new CryptographicAttributeObject(oid); |
| 496 |
Assert.Equal(oid.Value, cao.Oid.Value); |
| 497 |
Assert.Equal(0, cao.Values.Count); |
| 498 |
} |
| 499 |
|
| 500 |
[Fact] |
| 501 |
public static void CryptographicAttributeObjectPassNullValuesToCtor() |
| 502 |
{ |
| 503 |
Oid oid = new Oid(Oids.DocumentDescription); |
| 504 |
// This is legal and equivalent to passing a zero-length AsnEncodedDataCollection. |
| 505 |
CryptographicAttributeObject cao = new CryptographicAttributeObject(oid, null); |
| 506 |
Assert.Equal(oid.Value, cao.Oid.Value); |
| 507 |
Assert.Equal(0, cao.Values.Count); |
| 508 |
} |
| 509 |
|
| 510 |
[Fact] |
| 511 |
public static void CryptographicAttributeObjectMismatch() |
| 512 |
{ |
| 513 |
Oid oid = new Oid(Oids.DocumentDescription); |
| 514 |
Oid wrongOid = new Oid(Oids.DocumentName); |
| 515 |
|
| 516 |
AsnEncodedDataCollection col = new AsnEncodedDataCollection(); |
| 517 |
col.Add(new AsnEncodedData(oid, new byte[3])); |
| 518 |
|
| 519 |
object ignore; |
| 520 |
Assert.Throws<InvalidOperationException>(() => ignore = new CryptographicAttributeObject(wrongOid, col)); |
| 521 |
} |
| 522 |
|
| 523 |
[Fact] |
| 524 |
public static void EncryptEnvelopedOctetStringWithIncompleteContent() |
| 525 |
{ |
| 526 |
byte[] content = "04040203".HexToByteArray(); |
| 527 |
ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content); |
| 528 |
|
| 529 |
using (X509Certificate2 certificate = Certificates.RSAKeyTransferCapi1.GetCertificate()) |
| 530 |
{ |
| 531 |
string certSubjectName = certificate.Subject; |
| 532 |
AlgorithmIdentifier alg = new AlgorithmIdentifier(new Oid(Oids.Aes256)); |
| 533 |
EnvelopedCms ecms = new EnvelopedCms(contentInfo, alg); |
| 534 |
CmsRecipient cmsRecipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, certificate); |
| 535 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Encrypt(cmsRecipient)); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
[Fact] |
| 540 |
public static void EncryptEnvelopedOneByteArray() |
| 541 |
{ |
| 542 |
byte[] content = "04".HexToByteArray(); |
| 543 |
ContentInfo contentInfo = new ContentInfo(new Oid(Oids.Pkcs7Enveloped), content); |
| 544 |
|
| 545 |
using (X509Certificate2 certificate = Certificates.RSAKeyTransferCapi1.GetCertificate()) |
| 546 |
{ |
| 547 |
string certSubjectName = certificate.Subject; |
| 548 |
AlgorithmIdentifier alg = new AlgorithmIdentifier(new Oid(Oids.Aes256)); |
| 549 |
EnvelopedCms ecms = new EnvelopedCms(contentInfo, alg); |
| 550 |
CmsRecipient cmsRecipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, certificate); |
| 551 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Encrypt(cmsRecipient)); |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
|