| 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 StateTests |
| 23 |
{ |
| 24 |
public static bool SupportsCngCertificates { get; } = (!PlatformDetection.IsFullFramework || PlatformDetection.IsNetfx462OrNewer); |
| 25 |
|
| 26 |
// |
| 27 |
// Exercises various edge cases when EnvelopedCms methods and properties are called out of the "expected" order. |
| 28 |
// |
| 29 |
// The "expected" sequences are one of: |
| 30 |
// |
| 31 |
// ctor(ContentInfo)|ctor(ContentInfo,AlgorithmIdentifier) => Encrypt() => Encode() |
| 32 |
// |
| 33 |
// or |
| 34 |
// |
| 35 |
// ctor() => Decode() => RecipientInfos => Decrypt() => ContentInfo |
| 36 |
// |
| 37 |
// Most of these semantics are driven by backward compatibility. A tighter api design wouldn't |
| 38 |
// have exposed all these state transitions in the first place. |
| 39 |
// |
| 40 |
// The four states an EnvelopedCms can be in are as follows: |
| 41 |
// |
| 42 |
// State 1: Post constructor |
| 43 |
// |
| 44 |
// There are three constructor overloads, but all but one just supply default arguments |
| 45 |
// so we can consider there to be just one constructor. |
| 46 |
// |
| 47 |
// At this stage, there is no CMS underneath, just some properties (ContentInfo, AlgorithmIdentifier, etc.) |
| 48 |
// that serve as implicit inputs to a future Encrypt() call. |
| 49 |
// |
| 50 |
// State 2: Post Encrypt() |
| 51 |
// |
| 52 |
// Encrypt() can be called at any time and it effectively resets the state of the EnvelopedCms |
| 53 |
// (although the prior contents of ContentInfo, ContentEncryptionAlgorithm, UnprotectedAttributes and Certificates |
| 54 |
// will naturally influence the contents of CMS is constructed.) |
| 55 |
// |
| 56 |
// Encrypt() actually both encrypts and encodes, but you have to call Encode() afterward to pick up the encoded bytes. |
| 57 |
// |
| 58 |
// State 3: Post Decode() |
| 59 |
// |
| 60 |
// Decode() can also be called at any time - it's effectively a constructor that resets the internal |
| 61 |
// state and all the member properties. |
| 62 |
// |
| 63 |
// In this state, you can invoke the RecipientInfos properties to decide which recipient to pass to Decrypt(). |
| 64 |
// |
| 65 |
// State 4: Post Decrypt() |
| 66 |
// |
| 67 |
// A Decrypt() can only happen after a Decode(). |
| 68 |
// |
| 69 |
// Once in this state, you can fetch ContentInfo to get the decrypted content |
| 70 |
// but otherwise, the CMS is in a pretty useless state. |
| 71 |
// |
| 72 |
|
| 73 |
// |
| 74 |
// State 1 |
| 75 |
// |
| 76 |
// Constructed using any of the constructor overloads. |
| 77 |
// |
| 78 |
|
| 79 |
[Fact] |
| 80 |
public static void PostCtor_Version() |
| 81 |
{ |
| 82 |
// Version returns 0 by fiat. |
| 83 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 84 |
Assert.Equal(0, ecms.Version); |
| 85 |
} |
| 86 |
|
| 87 |
[Fact] |
| 88 |
public static void PostCtor_RecipientInfos() |
| 89 |
{ |
| 90 |
// RecipientInfo returns empty collection by fiat. |
| 91 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 92 |
RecipientInfoCollection recipients = ecms.RecipientInfos; |
| 93 |
Assert.Equal(0, recipients.Count); |
| 94 |
} |
| 95 |
|
| 96 |
[Fact] |
| 97 |
public static void PostCtor_Encode() |
| 98 |
{ |
| 99 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 100 |
Assert.Throws<InvalidOperationException>(() => ecms.Encode()); |
| 101 |
} |
| 102 |
|
| 103 |
[Fact] |
| 104 |
public static void PostCtor_Decrypt() |
| 105 |
{ |
| 106 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 107 |
Assert.Throws<InvalidOperationException>(() => ecms.Decrypt()); |
| 108 |
} |
| 109 |
|
| 110 |
[Fact] |
| 111 |
public static void PostCtor_ContentInfo() |
| 112 |
{ |
| 113 |
ContentInfo expectedContentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 114 |
EnvelopedCms ecms = new EnvelopedCms(expectedContentInfo); |
| 115 |
ContentInfo actualContentInfo = ecms.ContentInfo; |
| 116 |
Assert.Equal(expectedContentInfo.ContentType, actualContentInfo.ContentType); |
| 117 |
Assert.Equal<byte>(expectedContentInfo.Content, actualContentInfo.Content); |
| 118 |
} |
| 119 |
|
| 120 |
// |
| 121 |
// State 2 |
| 122 |
// |
| 123 |
// Called constructor + Encrypt() |
| 124 |
// |
| 125 |
|
| 126 |
[Fact] |
| 127 |
public static void PostEncrypt_Version() |
| 128 |
{ |
| 129 |
ContentInfo expectedContentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 130 |
EnvelopedCms ecms = new EnvelopedCms(expectedContentInfo); |
| 131 |
int versionBeforeEncrypt = ecms.Version; |
| 132 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 133 |
{ |
| 134 |
ecms.Encrypt(new CmsRecipient(cert)); |
| 135 |
} |
| 136 |
|
| 137 |
// Encrypt does not update Version member. |
| 138 |
Assert.Equal(versionBeforeEncrypt, ecms.Version); |
| 139 |
} |
| 140 |
|
| 141 |
[Fact] |
| 142 |
public static void PostEncrypt_RecipientInfos() |
| 143 |
{ |
| 144 |
ContentInfo expectedContentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 145 |
EnvelopedCms ecms = new EnvelopedCms(expectedContentInfo); |
| 146 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 147 |
{ |
| 148 |
ecms.Encrypt(new CmsRecipient(cert)); |
| 149 |
} |
| 150 |
|
| 151 |
object ignore; |
| 152 |
Assert.ThrowsAny<CryptographicException>(() => ignore = ecms.RecipientInfos); |
| 153 |
} |
| 154 |
|
| 155 |
[Fact] |
| 156 |
public static void PostEncrypt_Decrypt() |
| 157 |
{ |
| 158 |
ContentInfo expectedContentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 159 |
EnvelopedCms ecms = new EnvelopedCms(expectedContentInfo); |
| 160 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 161 |
{ |
| 162 |
ecms.Encrypt(new CmsRecipient(cert)); |
| 163 |
} |
| 164 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt()); |
| 165 |
} |
| 166 |
|
| 167 |
[Fact] |
| 168 |
public static void PostEncrypt_ContentInfo() |
| 169 |
{ |
| 170 |
ContentInfo expectedContentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 171 |
EnvelopedCms ecms = new EnvelopedCms(expectedContentInfo); |
| 172 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 173 |
{ |
| 174 |
ecms.Encrypt(new CmsRecipient(cert)); |
| 175 |
} |
| 176 |
|
| 177 |
// Encrypting does not update ContentInfo. |
| 178 |
ContentInfo actualContentInfo = ecms.ContentInfo; |
| 179 |
Assert.Equal(expectedContentInfo.ContentType, actualContentInfo.ContentType); |
| 180 |
Assert.Equal<byte>(expectedContentInfo.Content, actualContentInfo.Content); |
| 181 |
} |
| 182 |
|
| 183 |
[Fact] |
| 184 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 185 |
public static void PostEncrypt_Certs() |
| 186 |
{ |
| 187 |
ContentInfo expectedContentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 188 |
EnvelopedCms ecms = new EnvelopedCms(expectedContentInfo); |
| 189 |
ecms.Certificates.Add(Certificates.RSAKeyTransfer2.GetCertificate()); |
| 190 |
ecms.Certificates.Add(Certificates.RSAKeyTransfer3.GetCertificate()); |
| 191 |
|
| 192 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 193 |
{ |
| 194 |
ecms.Encrypt(new CmsRecipient(cert)); |
| 195 |
} |
| 196 |
|
| 197 |
Assert.Equal(Certificates.RSAKeyTransfer2.GetCertificate(), ecms.Certificates[0]); |
| 198 |
Assert.Equal(Certificates.RSAKeyTransfer3.GetCertificate(), ecms.Certificates[1]); |
| 199 |
} |
| 200 |
|
| 201 |
// |
| 202 |
// State 3: Called Decode() |
| 203 |
// |
| 204 |
|
| 205 |
public static void PostDecode_Encode(bool isRunningOnDesktop) |
| 206 |
{ |
| 207 |
byte[] encodedMessage = |
| 208 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 209 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e" |
| 210 |
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c" |
| 211 |
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4" |
| 212 |
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d" |
| 213 |
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray(); |
| 214 |
|
| 215 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 216 |
ecms.Decode(encodedMessage); |
| 217 |
|
| 218 |
// This should really have thrown an InvalidOperationException. Instead, you get... something back. |
| 219 |
string expectedString = isRunningOnDesktop ? "35edc437e31d0b70000000000000" : "35edc437e31d0b70"; |
| 220 |
byte[] expectedGarbage = expectedString.HexToByteArray(); |
| 221 |
byte[] garbage = ecms.Encode(); |
| 222 |
Assert.Equal(expectedGarbage, garbage); |
| 223 |
} |
| 224 |
|
| 225 |
[Fact] |
| 226 |
[SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] |
| 227 |
public static void PostDecode_Encode_net46() |
| 228 |
{ |
| 229 |
PostDecode_Encode(isRunningOnDesktop: true); |
| 230 |
} |
| 231 |
|
| 232 |
[Fact] |
| 233 |
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] |
| 234 |
public static void PostDecode_Encode_netcore() |
| 235 |
{ |
| 236 |
PostDecode_Encode(isRunningOnDesktop: false); |
| 237 |
} |
| 238 |
|
| 239 |
public static void PostDecode_ContentInfo(bool isRunningOnDesktop) |
| 240 |
{ |
| 241 |
byte[] encodedMessage = |
| 242 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 243 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e" |
| 244 |
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c" |
| 245 |
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4" |
| 246 |
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d" |
| 247 |
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray(); |
| 248 |
|
| 249 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 250 |
ecms.Decode(encodedMessage); |
| 251 |
|
| 252 |
// This gets you the encrypted inner content. |
| 253 |
ContentInfo contentInfo = ecms.ContentInfo; |
| 254 |
Assert.Equal(Oids.Pkcs7Data, contentInfo.ContentType.Value); |
| 255 |
string expectedString = isRunningOnDesktop ? "35edc437e31d0b70000000000000" : "35edc437e31d0b70"; |
| 256 |
byte[] expectedGarbage = expectedString.HexToByteArray(); |
| 257 |
Assert.Equal(expectedGarbage, contentInfo.Content); |
| 258 |
} |
| 259 |
|
| 260 |
[Fact] |
| 261 |
[SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] |
| 262 |
public static void PostDecode_ContentInfo_net46() |
| 263 |
{ |
| 264 |
PostDecode_ContentInfo(isRunningOnDesktop: true); |
| 265 |
} |
| 266 |
|
| 267 |
[Fact] |
| 268 |
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] |
| 269 |
public static void PostDecode_ContentInfo_netcore() |
| 270 |
{ |
| 271 |
PostDecode_ContentInfo(isRunningOnDesktop: false); |
| 272 |
} |
| 273 |
|
| 274 |
// |
| 275 |
// State 4: Called Decode() + Decrypt() |
| 276 |
// |
| 277 |
[ConditionalFact(nameof(SupportsCngCertificates))] |
| 278 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 279 |
public static void PostDecrypt_Encode() |
| 280 |
{ |
| 281 |
byte[] expectedContent = { 6, 3, 128, 33, 44 }; |
| 282 |
EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(expectedContent)); |
| 283 |
ecms.Encrypt(new CmsRecipient(Certificates.RSAKeyTransfer1.GetCertificate())); |
| 284 |
byte[] encodedMessage = |
| 285 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 286 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004818067" |
| 287 |
+ "6bada56dcaf2e65226941242db73b5a5420a6212cd6af662db52fdc0ca63875cb69066f7074da0fc009ce724e2d73fb19380" |
| 288 |
+ "2deea8d92b069486a41c7c4fc3cd0174a918a559f79319039b40ae797bcacc909c361275ee2a5b1f0ff09fb5c19508e3f5ac" |
| 289 |
+ "051ac0f03603c27fb8993d49ac428f8bcfc23a90ef9b0fac0f423a302b06092a864886f70d010701301406082a864886f70d" |
| 290 |
+ "0307040828dc4d72ca3132e48008546cc90f2c5d4b79").HexToByteArray(); |
| 291 |
ecms.Decode(encodedMessage); |
| 292 |
|
| 293 |
using (X509Certificate2 cer = Certificates.RSAKeyTransfer1.TryGetCertificateWithPrivateKey()) |
| 294 |
{ |
| 295 |
if (cer == null) |
| 296 |
return; // Sorry - CertLoader is not configured to load certs with private keys - we've tested as much as we can. |
| 297 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(cer); |
| 298 |
RecipientInfoCollection r = ecms.RecipientInfos; |
| 299 |
ecms.Decrypt(r[0], extraStore); |
| 300 |
|
| 301 |
// Desktop compat: Calling Encode() at this point should have thrown an InvalidOperationException. Instead, it returns |
| 302 |
// the decrypted inner content (same as ecms.ContentInfo.Content). This is easy for someone to take a reliance on |
| 303 |
// so for compat sake, we'd better keep it. |
| 304 |
byte[] encoded = ecms.Encode(); |
| 305 |
Assert.Equal<byte>(expectedContent, encoded); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
[ConditionalFact(nameof(SupportsCngCertificates))] |
| 310 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 311 |
public static void PostDecrypt_RecipientInfos() |
| 312 |
{ |
| 313 |
byte[] expectedContent = { 6, 3, 128, 33, 44 }; |
| 314 |
|
| 315 |
EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(expectedContent)); |
| 316 |
ecms.Encrypt(new CmsRecipient(Certificates.RSAKeyTransfer1.GetCertificate())); |
| 317 |
byte[] encodedMessage = |
| 318 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 319 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004818067" |
| 320 |
+ "6bada56dcaf2e65226941242db73b5a5420a6212cd6af662db52fdc0ca63875cb69066f7074da0fc009ce724e2d73fb19380" |
| 321 |
+ "2deea8d92b069486a41c7c4fc3cd0174a918a559f79319039b40ae797bcacc909c361275ee2a5b1f0ff09fb5c19508e3f5ac" |
| 322 |
+ "051ac0f03603c27fb8993d49ac428f8bcfc23a90ef9b0fac0f423a302b06092a864886f70d010701301406082a864886f70d" |
| 323 |
+ "0307040828dc4d72ca3132e48008546cc90f2c5d4b79").HexToByteArray(); |
| 324 |
ecms.Decode(encodedMessage); |
| 325 |
|
| 326 |
using (X509Certificate2 cer = Certificates.RSAKeyTransfer1.TryGetCertificateWithPrivateKey()) |
| 327 |
{ |
| 328 |
if (cer == null) |
| 329 |
return; // Sorry - CertLoader is not configured to load certs with private keys - we've tested as much as we can. |
| 330 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(cer); |
| 331 |
RecipientInfoCollection col1 = ecms.RecipientInfos; |
| 332 |
ecms.Decrypt(col1[0], extraStore); |
| 333 |
|
| 334 |
// Make sure we can still RecipientInfos after a Decrypt() |
| 335 |
RecipientInfoCollection col2 = ecms.RecipientInfos; |
| 336 |
Assert.Equal(col1.Count, col2.Count); |
| 337 |
|
| 338 |
RecipientInfo r1 = col1[0]; |
| 339 |
RecipientInfo r2 = col2[0]; |
| 340 |
|
| 341 |
X509IssuerSerial is1 = (X509IssuerSerial)(r1.RecipientIdentifier.Value); |
| 342 |
X509IssuerSerial is2 = (X509IssuerSerial)(r2.RecipientIdentifier.Value); |
| 343 |
Assert.Equal(is1.IssuerName, is2.IssuerName); |
| 344 |
Assert.Equal(is1.SerialNumber, is2.SerialNumber); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
[ConditionalFact(nameof(SupportsCngCertificates))] |
| 349 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 350 |
public static void PostDecrypt_Decrypt() |
| 351 |
{ |
| 352 |
byte[] expectedContent = { 6, 3, 128, 33, 44 }; |
| 353 |
|
| 354 |
byte[] encodedMessage = |
| 355 |
("308202b006092a864886f70d010703a08202a13082029d020100318202583081c5020100302e301a31183016060355040313" |
| 356 |
+ "0f5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004" |
| 357 |
+ "81801026d9fb60d1a55686b73cf859c8bd66b58defda5e23e3da5f535f1427e3c5f7a4a2a94373e8e3ba5488a7c6a1059bfb" |
| 358 |
+ "57301156698e7fca62671426d388fb3fb4373c9cb53132fda067598256bbfe8491b14dadaaf04d5fdfb2463f358ad0d6a594" |
| 359 |
+ "bf6a4fbab6b3d725f08032e601492265e6336d5a638096f9975025ccd6393081c5020100302e301a31183016060355040313" |
| 360 |
+ "0f5253414b65795472616e736665723202102bce9f9ece39f98044f0cd2faa9a14e7300d06092a864886f70d010101050004" |
| 361 |
+ "8180b6497a2b789728f200ca1f974a676c531a4769f03f3929bd7526e7333ea483b4abb530a49c8532db5d4a4df66f173e3e" |
| 362 |
+ "a4ba9e4814b584dc987ac87c46bb131daab535140968aafad8808100a2515e9c6d0c1f382b024992ce36b70b841628e0eb43" |
| 363 |
+ "4db89545d702a8fbd3403188e7de7cb4bc1dcc3bc325467570654aaf2ee83081c5020100302e301a31183016060355040313" |
| 364 |
+ "0f5253414b65795472616e736665723302104497d870785a23aa4432ed0106ef72a6300d06092a864886f70d010101050004" |
| 365 |
+ "81807517e594c353d41abff334c6162988b78e05df7d79457c146fbc886d2d8057f594fa3a96cd8df5842c9758baac1fcdd5" |
| 366 |
+ "d9672a9f8ef9426326cccaaf5954f2ae657f8c7b13aef2f811adb4954323aa8319a1e8f2ad4e5c96c1d3fbe413ae479e471b" |
| 367 |
+ "b701cbdfa145c9b64f5e1f69f472804995d56c31351553f779cf8efec237303c06092a864886f70d010701301d0609608648" |
| 368 |
+ "01650304012a041023a114c149d7d4017ce2f5ec7c5d53f980104e50ab3c15533743dd054ef3ff8b9d83").HexToByteArray(); |
| 369 |
|
| 370 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 371 |
ecms.Decode(encodedMessage); |
| 372 |
|
| 373 |
using (X509Certificate2 cert1 = Certificates.RSAKeyTransfer1.TryGetCertificateWithPrivateKey()) |
| 374 |
using (X509Certificate2 cert2 = Certificates.RSAKeyTransfer2.TryGetCertificateWithPrivateKey()) |
| 375 |
using (X509Certificate2 cert3 = Certificates.RSAKeyTransfer3.TryGetCertificateWithPrivateKey()) |
| 376 |
{ |
| 377 |
if (cert1 == null || cert2 == null || cert3 == null) |
| 378 |
return; // Sorry - CertLoader is not configured to load certs with private keys - we've tested as much as we can. |
| 379 |
X509Certificate2Collection extraStore = new X509Certificate2Collection(); |
| 380 |
extraStore.Add(cert1); |
| 381 |
extraStore.Add(cert2); |
| 382 |
extraStore.Add(cert3); |
| 383 |
RecipientInfoCollection r = ecms.RecipientInfos; |
| 384 |
ecms.Decrypt(r[0], extraStore); |
| 385 |
ContentInfo contentInfo = ecms.ContentInfo; |
| 386 |
Assert.Equal<byte>(expectedContent, contentInfo.Content); |
| 387 |
|
| 388 |
// Though this doesn't seem like a terribly unreasonable thing to attempt, attempting to call Decrypt() again |
| 389 |
// after a successful Decrypt() throws a CryptographicException saying "Already decrypted." |
| 390 |
Assert.ThrowsAny<CryptographicException>(() => ecms.Decrypt(r[1], extraStore)); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
[Fact] |
| 395 |
public static void PostEncode_DifferentData() |
| 396 |
{ |
| 397 |
// This ensures that the decoding and encoding output different values to make sure Encrypt changes the state of the data. |
| 398 |
byte[] encoded = |
| 399 |
("3082010206092A864886F70D010703A081F43081F10201003181C83081C5020100302E301A311830160603550403130F" |
| 400 |
+ "5253414B65795472616E7366657231021031D935FB63E8CFAB48A0BF7B397B67C0300D06092A864886F70D0101010500" |
| 401 |
+ "04818009C16B674495C2C3D4763189C3274CF7A9142FBEEC8902ABDC9CE29910D541DF910E029A31443DC9A9F3B05F02" |
| 402 |
+ "DA1C38478C400261C734D6789C4197C20143C4312CEAA99ECB1849718326D4FC3B7FBB2D1D23281E31584A63E99F2C17" |
| 403 |
+ "132BCD8EDDB632967125CD0A4BAA1EFA8CE4C855F7C093339211BDF990CEF5CCE6CD74302106092A864886F70D010701" |
| 404 |
+ "301406082A864886F70D03070408779B3DE045826B18").HexToByteArray(); |
| 405 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 406 |
ecms.Decode(encoded); |
| 407 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 408 |
{ |
| 409 |
ecms.Encrypt(new CmsRecipient(cert)); |
| 410 |
} |
| 411 |
|
| 412 |
byte[] encrypted = ecms.Encode(); |
| 413 |
|
| 414 |
Assert.NotEqual<byte>(encoded, encrypted); |
| 415 |
} |
| 416 |
|
| 417 |
private static void AssertEncryptedContentEqual(byte[] expected, byte[] actual) |
| 418 |
{ |
| 419 |
if (expected.SequenceEqual(actual)) |
| 420 |
return; |
| 421 |
|
| 422 |
if (actual.Length > expected.Length && actual.Take(expected.Length).SequenceEqual(expected)) |
| 423 |
throw new Exception("Returned content had extra bytes padded. If you're running this test on the desktop framework, this is a known bug."); |
| 424 |
|
| 425 |
Assert.Equal<byte>(expected, actual); |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|