| 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.Collections.Generic; |
| 7 |
using System.Security.Cryptography.X509Certificates; |
| 8 |
using Xunit; |
| 9 |
|
| 10 |
using Test.Cryptography; |
| 11 |
using System.Security.Cryptography.Pkcs.Tests; |
| 12 |
|
| 13 |
namespace System.Security.Cryptography.Pkcs.EnvelopedCmsTests.Tests |
| 14 |
{ |
| 15 |
public static partial class UnprotectedAttributeTests |
| 16 |
{ |
| 17 |
[Fact] |
| 18 |
public static void TestUnprotectedAttributes0_RoundTrip() |
| 19 |
{ |
| 20 |
byte[] encodedMessage = CreateEcmsWithAttributes(); |
| 21 |
VerifyUnprotectedAttributes0(encodedMessage); |
| 22 |
} |
| 23 |
|
| 24 |
[Fact] |
| 25 |
public static void TestUnprotectedAttributes0_FixedValue() |
| 26 |
{ |
| 27 |
byte[] encodedMessage = |
| 28 |
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253" |
| 29 |
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d010101050004818048" |
| 30 |
+ "fea7a626159f3792b8daa8e064de2f986cf0e432428c6cb80ed4bf6e593db74a7f907e0918b4bee6d55e1e0f9da6b6519e42" |
| 31 |
+ "23d58b0f717165a727d7dac87556916c800e2346beac5a825c973e9bba4fe6c549baafd151d85fd7c266769dbb57f28e45f8" |
| 32 |
+ "6bb5478d018e132cb576079d8c2a7f4217973c3ff1f0617364809c302b06092a864886f70d010701301406082a864886f70d" |
| 33 |
+ "030704083979569d26db4c278008d2fa4271358f9a2f").HexToByteArray(); |
| 34 |
VerifyUnprotectedAttributes0(encodedMessage); |
| 35 |
} |
| 36 |
|
| 37 |
private static void VerifyUnprotectedAttributes0(byte[] encodedMessage) |
| 38 |
{ |
| 39 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 40 |
ecms.Decode(encodedMessage); |
| 41 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 42 |
Assert.Equal(0, attributes.Length); |
| 43 |
} |
| 44 |
|
| 45 |
[Fact] |
| 46 |
public static void TestUnprotectedAttributes1_DocumentDescription_RoundTrip() |
| 47 |
{ |
| 48 |
byte[] encodedMessage = CreateEcmsWithAttributes(new Pkcs9DocumentDescription("My Description")); |
| 49 |
VerifyUnprotectedAttributes1_DocumentDescription(encodedMessage); |
| 50 |
} |
| 51 |
|
| 52 |
[Fact] |
| 53 |
public static void TestUnprotectedAttributes1_DocumentDescription_FixedValue() |
| 54 |
{ |
| 55 |
byte[] encodedMessage = |
| 56 |
("3082014006092a864886f70d010703a08201313082012d0201023181c83081c5020100302e301a311830160603550403130f" |
| 57 |
+ "5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481" |
| 58 |
+ "801dba607648ab56b88dffb4bfe36170d698910562cef64dc2f3098ca2d7fa97334e721b6d00250470f8a16b5d2f5f38cc84" |
| 59 |
+ "c05d49e5aa7390ed4e0d6a7369f72f8bb0209545b6b8d3302c2c3fc8c8e7f5a54dd30b20855a77b5cd40c6f2b59376252aef" |
| 60 |
+ "e1d90965916e25c63f509e32f86a9213b740796927bbb0573b024b7ba3302b06092a864886f70d010701301406082a864886" |
| 61 |
+ "f70d03070408df8ced363e2a76288008262ce8fe027530a3a130302e060a2b0601040182375802023120041e4d0079002000" |
| 62 |
+ "4400650073006300720069007000740069006f006e000000").HexToByteArray(); |
| 63 |
VerifyUnprotectedAttributes1_DocumentDescription(encodedMessage); |
| 64 |
} |
| 65 |
|
| 66 |
private static void VerifyUnprotectedAttributes1_DocumentDescription(byte[] encodedMessage) |
| 67 |
{ |
| 68 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 69 |
ecms.Decode(encodedMessage); |
| 70 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 71 |
Assert.Equal(1, attributes.Length); |
| 72 |
attributes[0].AssertIsDocumentationDescription("My Description"); |
| 73 |
} |
| 74 |
|
| 75 |
[Fact] |
| 76 |
public static void TestUnprotectedAttributes1_DocumenName_RoundTrip() |
| 77 |
{ |
| 78 |
byte[] encodedMessage = CreateEcmsWithAttributes(new Pkcs9DocumentName("My Name")); |
| 79 |
VerifyUnprotectedAttributes1_DocumentName(encodedMessage); |
| 80 |
} |
| 81 |
|
| 82 |
[Fact] |
| 83 |
public static void TestUnprotectedAttributes1_DocumentName_FixedValue() |
| 84 |
{ |
| 85 |
byte[] encodedMessage = |
| 86 |
("3082013206092a864886f70d010703a08201233082011f0201023181c83081c5020100302e301a311830160603550403130f" |
| 87 |
+ "5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481" |
| 88 |
+ "8000d17895fe3d35a30f0666e4064cc40757251404f785c4878ab98d1440d7f1706991cf854b2498bca8781c7728805b03e8" |
| 89 |
+ "d36329f34d7508bf38e4e4d5447e8c7d6d1c652f0a40bb3fc396e1139094d6349e08a7d233b9323c4760a96199660c87c7f0" |
| 90 |
+ "2c891711efcee6085f07fa0060da6f9b22d895b312caed824916b14314302b06092a864886f70d010701301406082a864886" |
| 91 |
+ "f70d03070408817ee1c4bb617f828008de69d9d27afef823a1223020060a2b060104018237580201311204104d0079002000" |
| 92 |
+ "4e0061006d0065000000").HexToByteArray(); |
| 93 |
VerifyUnprotectedAttributes1_DocumentName(encodedMessage); |
| 94 |
} |
| 95 |
|
| 96 |
private static void VerifyUnprotectedAttributes1_DocumentName(byte[] encodedMessage) |
| 97 |
{ |
| 98 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 99 |
ecms.Decode(encodedMessage); |
| 100 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 101 |
Assert.Equal(1, attributes.Length); |
| 102 |
attributes[0].AssertIsDocumentationName("My Name"); |
| 103 |
} |
| 104 |
|
| 105 |
[Fact] |
| 106 |
public static void TestUnprotectedAttributes1_SigningTime_RoundTrip() |
| 107 |
{ |
| 108 |
byte[] encodedMessage = CreateEcmsWithAttributes(new Pkcs9SigningTime(new DateTime(2018, 4, 1, 8, 30, 05))); |
| 109 |
VerifyUnprotectedAttributes1_SigningTime(encodedMessage); |
| 110 |
} |
| 111 |
|
| 112 |
[Fact] |
| 113 |
public static void TestUnprotectedAttributes1_SigningTime_FixedValue() |
| 114 |
{ |
| 115 |
byte[] encodedMessage = |
| 116 |
("3082012e06092a864886f70d010703a082011f3082011b0201023181c83081c5020100302e301a311830160603550403130f" |
| 117 |
+ "5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481" |
| 118 |
+ "801cf4efe18f457c32753fd3c908372ac8075cd4c4bdec98a912cc241b604b7ba46f44c657c769e84998384c28ee9a670d7d" |
| 119 |
+ "8d7427f5a3bffb2a6e09269f1c89a7b1906eb0a828487d6e4fcb2c6e9023f3d9f8114701cf01c2fcb4db0c9ab7144c8e2b73" |
| 120 |
+ "6551ca1a348ac3c25cca9de1bdef79c0bdd2ba8b79e6e668f947cf1bc7302b06092a864886f70d010701301406082a864886" |
| 121 |
+ "f70d03070408e7575fbec5da862080084306defef088dd0ea11e301c06092a864886f70d010905310f170d31383034303130" |
| 122 |
+ "38333030355a").HexToByteArray(); |
| 123 |
|
| 124 |
VerifyUnprotectedAttributes1_SigningTime(encodedMessage); |
| 125 |
} |
| 126 |
|
| 127 |
private static void VerifyUnprotectedAttributes1_SigningTime(byte[] encodedMessage) |
| 128 |
{ |
| 129 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 130 |
ecms.Decode(encodedMessage); |
| 131 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 132 |
Assert.Equal(1, attributes.Length); |
| 133 |
DateTime expectedSigningTIme = new DateTime(2018, 4, 1, 8, 30, 05); |
| 134 |
attributes[0].AssertIsSigningTime(expectedSigningTIme); |
| 135 |
} |
| 136 |
|
| 137 |
[Fact] |
| 138 |
public static void TestUnprotectedAttributes1_ContentType_RoundTrip() |
| 139 |
{ |
| 140 |
byte[] rawData = "06072a9fa20082f300".HexToByteArray(); |
| 141 |
Pkcs9AttributeObject pkcs9ContentType = new Pkcs9AttributeObject(Oids.ContentType, rawData); |
| 142 |
|
| 143 |
byte[] encodedMessage = CreateEcmsWithAttributes(pkcs9ContentType); |
| 144 |
VerifyUnprotectedAttributes1_ContentType(encodedMessage); |
| 145 |
} |
| 146 |
|
| 147 |
[Fact] |
| 148 |
public static void TestUnprotectedAttributes1_ContentType_FixedValue() |
| 149 |
{ |
| 150 |
byte[] encodedMessage = |
| 151 |
("3082012806092a864886f70d010703a0820119308201150201023181c83081c5020100302e301a311830160603550403130f" |
| 152 |
+ "5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481" |
| 153 |
+ "804abd2e5b20b7d255234ffa6b025a9c6a35362ac4affc4d78ed02a647ac9d9cb8d02bf64f924f19c731fc6e7f333180c6f1" |
| 154 |
+ "0be40c382b5da5db96b0303819573dc3598aa978704ee96a98113ec110d48aef57c745ee0188feceac27a3739663bb52fccc" |
| 155 |
+ "37e106ed3d8ecf3806bcc4df83ce989080405e3e856e725a85aa205bda302b06092a864886f70d010701301406082a864886" |
| 156 |
+ "f70d030704084a0375f470805f908008522c8448feaf357da118301606092a864886f70d010903310906072a9fa20082f300").HexToByteArray(); |
| 157 |
|
| 158 |
VerifyUnprotectedAttributes1_ContentType(encodedMessage); |
| 159 |
} |
| 160 |
|
| 161 |
private static void VerifyUnprotectedAttributes1_ContentType(byte[] encodedMessage) |
| 162 |
{ |
| 163 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 164 |
ecms.Decode(encodedMessage); |
| 165 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 166 |
Assert.Equal(1, attributes.Length); |
| 167 |
attributes[0].AssertIsContentType("1.2.512256.47488"); |
| 168 |
} |
| 169 |
|
| 170 |
[Fact] |
| 171 |
public static void TestUnprotectedAttributes1_MessageDigest_RoundTrip() |
| 172 |
{ |
| 173 |
byte[] rawData = "0405032d58805d".HexToByteArray(); |
| 174 |
Pkcs9AttributeObject pkcs9MessageDigest = new Pkcs9AttributeObject(Oids.MessageDigest, rawData); |
| 175 |
|
| 176 |
byte[] encodedMessage = CreateEcmsWithAttributes(pkcs9MessageDigest); |
| 177 |
VerifyUnprotectedAttributes1_MessageDigest(encodedMessage); |
| 178 |
} |
| 179 |
|
| 180 |
[Fact] |
| 181 |
public static void TestUnprotectedAttributes1_MessageDigest_FixedValue() |
| 182 |
{ |
| 183 |
byte[] encodedMessage = |
| 184 |
("3082012606092a864886f70d010703a0820117308201130201023181c83081c5020100302e301a311830160603550403130f" |
| 185 |
+ "5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481" |
| 186 |
+ "802ed2085366bf01caca75c260d900c12ef3377f868d879c7b33734dd912aeffcb55f8f8fefea550bd75472ad20f56dd6edf" |
| 187 |
+ "d1c9669f3653e41d48f03c0796513da5b922587415853fc46ef5f452cea25a58c6da296527c51111a2fa6e472651391e2c3a" |
| 188 |
+ "ffb081ce6f7e4aee275d0f3d3e351b5e76c84afb5f80bb1ef594eb9b92302b06092a864886f70d010701301406082a864886" |
| 189 |
+ "f70d030704088b5d38dad37f599280081d626f58eabeeb0aa116301406092a864886f70d01090431070405032d58805d").HexToByteArray(); |
| 190 |
|
| 191 |
VerifyUnprotectedAttributes1_MessageDigest(encodedMessage); |
| 192 |
} |
| 193 |
|
| 194 |
private static void VerifyUnprotectedAttributes1_MessageDigest(byte[] encodedMessage) |
| 195 |
{ |
| 196 |
byte[] expectedMessageDigest = "032d58805d".HexToByteArray(); |
| 197 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 198 |
ecms.Decode(encodedMessage); |
| 199 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 200 |
Assert.Equal(1, attributes.Length); |
| 201 |
attributes[0].AssertIsMessageDigest(expectedMessageDigest); |
| 202 |
} |
| 203 |
|
| 204 |
[Fact] |
| 205 |
public static void TestUnprotectedAttributes1_Merge3_RoundTrip() |
| 206 |
{ |
| 207 |
byte[] encodedMessage = CreateEcmsWithAttributes( |
| 208 |
new Pkcs9DocumentDescription("My Description 1"), |
| 209 |
new Pkcs9DocumentDescription("My Description 2"), |
| 210 |
new Pkcs9DocumentDescription("My Description 3") |
| 211 |
); |
| 212 |
VerifyUnprotectedAttributes_Merge3(encodedMessage); |
| 213 |
} |
| 214 |
|
| 215 |
[Fact] |
| 216 |
public static void TestUnprotectedAttributes1_Merge3_FixedValue() |
| 217 |
{ |
| 218 |
byte[] encodedMessage = |
| 219 |
("3082018c06092a864886f70d010703a082017d308201790201023181c83081c5020100302e301a311830160603550403130f" |
| 220 |
+ "5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481" |
| 221 |
+ "809203ec6ad0877ae56dbc8a480e59e94ca953839f8f769ec1b262b4f4916c9863ee9c41007c356fbf2fc3f3d03e4aa1ea46" |
| 222 |
+ "575d0fbc0c5f47e41778f34535eea84e648009955f271a9e3c24f8c192d31406d46a40396b78d4013cfe3dcc443ac9ca9213" |
| 223 |
+ "7ffa503297ca1d241f68e905c60134c02e7ce8e1e67481abebe3d7faa1302b06092a864886f70d010701301406082a864886" |
| 224 |
+ "f70d030704085e2e9ca7859583a08008fb219dbb2a84a2eda17c307a060a2b060104018237580202316c04224d0079002000" |
| 225 |
+ "4400650073006300720069007000740069006f006e0020003100000004224d00790020004400650073006300720069007000" |
| 226 |
+ "740069006f006e0020003200000004224d00790020004400650073006300720069007000740069006f006e00200033000000").HexToByteArray(); |
| 227 |
VerifyUnprotectedAttributes_Merge3(encodedMessage); |
| 228 |
} |
| 229 |
|
| 230 |
private static void VerifyUnprotectedAttributes_Merge3(byte[] encodedMessage) |
| 231 |
{ |
| 232 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 233 |
ecms.Decode(encodedMessage); |
| 234 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 235 |
Assert.Equal(3, attributes.Length); |
| 236 |
attributes[0].AssertIsDocumentationDescription("My Description 1"); |
| 237 |
attributes[1].AssertIsDocumentationDescription("My Description 2"); |
| 238 |
attributes[2].AssertIsDocumentationDescription("My Description 3"); |
| 239 |
} |
| 240 |
|
| 241 |
[Fact] |
| 242 |
public static void TestUnprotectedAttributes1_Heterogenous3_RoundTrip() |
| 243 |
{ |
| 244 |
byte[] encodedMessage = CreateEcmsWithAttributes( |
| 245 |
new Pkcs9DocumentDescription("My Description 1"), |
| 246 |
new Pkcs9DocumentDescription("My Description 2"), |
| 247 |
new Pkcs9DocumentName("My Name 1") |
| 248 |
); |
| 249 |
VerifyUnprotectedAttributes_Heterogenous3(encodedMessage); |
| 250 |
} |
| 251 |
|
| 252 |
private static void VerifyUnprotectedAttributes_Heterogenous3(byte[] encodedMessage) |
| 253 |
{ |
| 254 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 255 |
ecms.Decode(encodedMessage); |
| 256 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 257 |
Assert.Equal(3, attributes.Length); |
| 258 |
attributes[0].AssertIsDocumentationName("My Name 1"); |
| 259 |
attributes[1].AssertIsDocumentationDescription("My Description 1"); |
| 260 |
attributes[2].AssertIsDocumentationDescription("My Description 2"); |
| 261 |
} |
| 262 |
|
| 263 |
[Fact] |
| 264 |
public static void TestUnprotectedAttributes1_EmptySet() |
| 265 |
{ |
| 266 |
// This tests the behavior of unprotected attribute extraction when one of the attribute sequences declares an |
| 267 |
// attribute type, but the contained SET OF AttributeValue is empty. |
| 268 |
// |
| 269 |
// Attribute ::= SEQUENCE { |
| 270 |
// attrType OBJECT IDENTIFIER, |
| 271 |
// attrValues SET OF AttributeValue } |
| 272 |
// |
| 273 |
// The encoded message was built in ASN.1 editor and tested in framework.It contains an enveloped message |
| 274 |
// version 2 with a key transport recipient, the enveloped message contains data encrypted with 3DES. |
| 275 |
// |
| 276 |
// The attributes set is built as |
| 277 |
// { |
| 278 |
// { attrType: document description, attrValues: { value1, value2 } }, |
| 279 |
// { attrType: document name, attrValues: { } }, |
| 280 |
// } |
| 281 |
// |
| 282 |
// The important part of this test is that there are 0 attributes of a type that is declared within the encoded message. |
| 283 |
// This should return 2 as it should create a CryptographicAttributeObjectCollection with two CryptographicAttributeObjects, |
| 284 |
// the first one holding a list of document description with the two values, the second one holding an empty list of |
| 285 |
// document name. |
| 286 |
|
| 287 |
byte[] encodedMessage = |
| 288 |
("3082017806092A864886F70D010703A0820169308201650201023181C83081C5020100302E301A311830160603550403" |
| 289 |
+ "130F5253414B65795472616E7366657231021031D935FB63E8CFAB48A0BF7B397B67C0300D06092A864886F70D010101" |
| 290 |
+ "05000481802EE6A4AAA9F907E8EF472D8CD8603098488EC1C462815E6FC5A53A3DF6EB730F3D191746FDBBCA89114C6D" |
| 291 |
+ "45FB6C4F26088043894D5A706889A29D52E03ABEDFAC98336BD01B0A9CFA57CC6C80908F4B42EFCE5E60E7A761451A4D" |
| 292 |
+ "1A39783072000E551062027795A1CEB079791BA48C5F77D360EE48E185DE6C8CCB1C093D4B302B06092A864886F70D01" |
| 293 |
+ "0701301406082A864886F70D03070408F55F613664678EE9800800BC3504D1F59470A168300E060A2B06010401823758" |
| 294 |
+ "020131003056060A2B060104018237580202314804224D00790020004400650073006300720069007000740069006F00" |
| 295 |
+ "6E0020003100000004224D00790020004400650073006300720069007000740069006F006E00200032000000").HexToByteArray(); |
| 296 |
|
| 297 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 298 |
ecms.Decode(encodedMessage); |
| 299 |
|
| 300 |
Assert.Equal(2, ecms.UnprotectedAttributes.Count); |
| 301 |
|
| 302 |
CryptographicAttributeObjectCollection collection = ecms.UnprotectedAttributes; |
| 303 |
string attrObj0Oid = collection[0].Oid.Value; |
| 304 |
|
| 305 |
CryptographicAttributeObject documentDescObj = (attrObj0Oid == Oids.DocumentDescription) ? |
| 306 |
collection[0] : |
| 307 |
collection[1]; |
| 308 |
|
| 309 |
CryptographicAttributeObject documentNameObj = (attrObj0Oid == Oids.DocumentName) ? |
| 310 |
collection[0] : |
| 311 |
collection[1]; |
| 312 |
|
| 313 |
Assert.Equal(0, documentNameObj.Values.Count); |
| 314 |
Assert.Equal(2, documentDescObj.Values.Count); |
| 315 |
} |
| 316 |
|
| 317 |
[Fact] |
| 318 |
public static void TestUnprotectedAttributes1_Arbitrary_RoundTrip() |
| 319 |
{ |
| 320 |
byte[] encodedMessage = CreateEcmsWithAttributes( |
| 321 |
new AsnEncodedData(new Oid(Oids.Pkcs7Data), new byte[] { 4, 3, 6, 7, 8 }) |
| 322 |
); |
| 323 |
VerifyUnprotectedAttributes_Arbitrary1(encodedMessage); |
| 324 |
} |
| 325 |
|
| 326 |
[Fact] |
| 327 |
public static void TestUnprotectedAttributes1_Arbitrary_FixedValue() |
| 328 |
{ |
| 329 |
byte[] encodedMessage = |
| 330 |
("3082012406092a864886f70d010703a0820115308201110201023181c83081c5020100302e301a311830160603550403130f" |
| 331 |
+ "5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481" |
| 332 |
+ "800f310e1c63177fd87bbacd13de65aae943fff87a1da112b9d1a2578c444ebfeb5d72db13104e1f72231651f1a46dec72c1" |
| 333 |
+ "91ae859e2cd96df3f94599fff2fdc9074ea9722739c9b0ac870acd073c11375d79ab7679b0d2ebab839f0c2ee975d7ef4a59" |
| 334 |
+ "5933aebfcae745f98109c0e5cfd298960cebd244d6a029d9f21bfe60fb302b06092a864886f70d010701301406082a864886" |
| 335 |
+ "f70d03070408aa76edcbc0a03d7d8008aefe2086db6f8f7ca114301206092a864886f70d01070131050403060708").HexToByteArray(); |
| 336 |
VerifyUnprotectedAttributes_Arbitrary1(encodedMessage); |
| 337 |
} |
| 338 |
|
| 339 |
private static void VerifyUnprotectedAttributes_Arbitrary1(byte[] encodedMessage) |
| 340 |
{ |
| 341 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 342 |
ecms.Decode(encodedMessage); |
| 343 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 344 |
Assert.Equal(1, attributes.Length); |
| 345 |
AsnEncodedData a = attributes[0]; |
| 346 |
Assert.Equal(Oids.Pkcs7Data, a.Oid.Value); |
| 347 |
byte[] expectedRawData = { 4, 3, 6, 7, 8 }; |
| 348 |
Assert.Equal<byte>(expectedRawData, a.RawData); |
| 349 |
} |
| 350 |
|
| 351 |
[Fact] |
| 352 |
public static void TestUnprotectedAttributes1_OutOfNamespace_RoundTrip() |
| 353 |
{ |
| 354 |
byte[] constraintsRawData = "30070101ff02020100".HexToByteArray(); |
| 355 |
|
| 356 |
byte[] encodedMessage = CreateEcmsWithAttributes( |
| 357 |
new AsnEncodedData(Oids.BasicConstraints2, constraintsRawData) |
| 358 |
); |
| 359 |
VerifyUnprotectedAttributes1_OutOfNamespace(encodedMessage); |
| 360 |
} |
| 361 |
|
| 362 |
[Fact] |
| 363 |
public static void TestUnprotectedAttributes1_OutOfNamespace_FixedValue() |
| 364 |
{ |
| 365 |
byte[] encodedMessage = |
| 366 |
("3082012206092a864886f70d010703a08201133082010f0201023181c83081c5020100302e301a311830160603550403130f" |
| 367 |
+ "5253414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481" |
| 368 |
+ "801dd92ffa848bc03011c8aae71d63c9a81fdbb2e2c82c284bcbe40d77c3f67beb8cdd2c017470a48189e8ccd3d310bde567" |
| 369 |
+ "202e3a03cb9866d19262bda6a489c957e14b1068ecfb2ae8ea1cbf47c6a934a5eed8ce05965356e033f2a1c68001cd308604" |
| 370 |
+ "50f28b7949af886727fb506d64ae7889f613c03729a7b834591881666c302b06092a864886f70d010701301406082a864886" |
| 371 |
+ "f70d0307040814a020bca56417de8008a260d0e4e138743ea11230100603551d13310930070101ff02020100").HexToByteArray(); |
| 372 |
|
| 373 |
VerifyUnprotectedAttributes1_OutOfNamespace(encodedMessage); |
| 374 |
} |
| 375 |
|
| 376 |
private static void VerifyUnprotectedAttributes1_OutOfNamespace(byte[] encodedMessage) |
| 377 |
{ |
| 378 |
byte[] constraintsRawData = "30070101ff02020100".HexToByteArray(); |
| 379 |
|
| 380 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 381 |
ecms.Decode(encodedMessage); |
| 382 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 383 |
Assert.Equal(1, attributes.Length); |
| 384 |
AsnEncodedData a = attributes[0]; |
| 385 |
Assert.Equal(Oids.BasicConstraints2, a.Oid.Value); |
| 386 |
Assert.Equal<byte>(constraintsRawData, a.RawData); |
| 387 |
} |
| 388 |
|
| 389 |
[Fact] |
| 390 |
public static void TestUnprotectedAttributes_AlwaysReturnsPkcs9AttributeObject() |
| 391 |
{ |
| 392 |
byte[] encodedMessage = CreateEcmsWithAttributes( |
| 393 |
new AsnEncodedData(new Oid(Oids.Pkcs7Data), new byte[] { 4, 3, 6, 7, 8 }) |
| 394 |
); |
| 395 |
|
| 396 |
// ecms.Decode() always populates UnprotectedAttribute objects with Pkcs9AttributeObjects (or one of its derived classes) rather than |
| 397 |
// AsnEncodedData instances. Verify that any new implementation does this as someone out there is probably |
| 398 |
// casting to Pkcs9AttributeObject without checking. |
| 399 |
|
| 400 |
EnvelopedCms ecms = new EnvelopedCms(); |
| 401 |
ecms.Decode(encodedMessage); |
| 402 |
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 403 |
Assert.Equal(1, attributes.Length); |
| 404 |
AsnEncodedData a = attributes[0]; |
| 405 |
Assert.True(a is Pkcs9AttributeObject); |
| 406 |
} |
| 407 |
|
| 408 |
[Fact] |
| 409 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 410 |
public static void PostEncrypt_UnprotectedAttributes() |
| 411 |
{ |
| 412 |
byte[] docDescription = ("041E4D00790020004400650073006300720069007000740069006F006E000000").HexToByteArray(); |
| 413 |
byte[] docName1 = ("0410AA00790020004E0061006D0065000000").HexToByteArray(); |
| 414 |
byte[] docName2 = ("04104D00790020004E0061006D0065000000").HexToByteArray(); |
| 415 |
ContentInfo expectedContentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 416 |
EnvelopedCms ecms = new EnvelopedCms(expectedContentInfo); |
| 417 |
AsnEncodedData[] attributes = { |
| 418 |
new AsnEncodedData(Oids.DocumentName, docName1), |
| 419 |
new AsnEncodedData(Oids.DocumentName, docName2), |
| 420 |
new AsnEncodedData(Oids.DocumentDescription, docDescription)}; |
| 421 |
|
| 422 |
foreach (AsnEncodedData attribute in attributes) |
| 423 |
{ |
| 424 |
ecms.UnprotectedAttributes.Add(attribute); |
| 425 |
} |
| 426 |
|
| 427 |
AsnEncodedData[] before = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 428 |
|
| 429 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 430 |
{ |
| 431 |
ecms.Encrypt(new CmsRecipient(cert)); |
| 432 |
} |
| 433 |
|
| 434 |
AsnEncodedData[] after = ecms.UnprotectedAttributes.FlattenAndSort(); |
| 435 |
|
| 436 |
// There are three objects, but ecms.UnprotectedAttributes.Count returns the count of different Oids, |
| 437 |
// not the amount of objects inside. |
| 438 |
Assert.Equal(2, ecms.UnprotectedAttributes.Count); |
| 439 |
|
| 440 |
Assert.Equal(before.Length, after.Length); |
| 441 |
for (int i = 0; i<before.Length; i++) |
| 442 |
{ |
| 443 |
Assert.Equal(before[i].GetType(), after[i].GetType()); |
| 444 |
Assert.Equal(before[i].Oid.Value, after[i].Oid.Value); |
| 445 |
Assert.Equal(before[i].RawData, after[i].RawData); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
[Theory] |
| 450 |
[InlineData(false, 0)] |
| 451 |
[InlineData(true, 2)] |
| 452 |
[OuterLoop(/* Leaks key on disk if interrupted */)] |
| 453 |
public static void TestVersionNumber_RoundTrip(bool addUnprotectedAttrs, int expectedVersion) |
| 454 |
{ |
| 455 |
ContentInfo expectedContentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 456 |
byte[] docName = ("0410AA00790020004E0061006D0065000000").HexToByteArray(); |
| 457 |
EnvelopedCms ecms = new EnvelopedCms(expectedContentInfo); |
| 458 |
|
| 459 |
if (addUnprotectedAttrs) |
| 460 |
ecms.UnprotectedAttributes.Add(new AsnEncodedData(new Oid(Oids.DocumentName), docName)); |
| 461 |
|
| 462 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 463 |
{ |
| 464 |
ecms.Encrypt(new CmsRecipient(cert)); |
| 465 |
} |
| 466 |
|
| 467 |
byte[] encodedMessage = ecms.Encode(); |
| 468 |
|
| 469 |
ecms = new EnvelopedCms(); |
| 470 |
ecms.Decode(encodedMessage); |
| 471 |
|
| 472 |
Assert.Equal(expectedVersion, ecms.Version); |
| 473 |
} |
| 474 |
|
| 475 |
private static void AssertIsDocumentationDescription(this AsnEncodedData attribute, string expectedDocumentDescription) |
| 476 |
{ |
| 477 |
Assert.Equal(Oids.DocumentDescription, attribute.Oid.Value); |
| 478 |
Pkcs9DocumentDescription enhancedAttribute = attribute as Pkcs9DocumentDescription; |
| 479 |
Assert.NotNull(enhancedAttribute); |
| 480 |
Assert.Equal(expectedDocumentDescription, enhancedAttribute.DocumentDescription); |
| 481 |
} |
| 482 |
|
| 483 |
private static void AssertIsDocumentationName(this AsnEncodedData attribute, string expectedDocumentName) |
| 484 |
{ |
| 485 |
Assert.Equal(Oids.DocumentName, attribute.Oid.Value); |
| 486 |
Pkcs9DocumentName enhancedAttribute = attribute as Pkcs9DocumentName; |
| 487 |
Assert.NotNull(enhancedAttribute); |
| 488 |
Assert.Equal(expectedDocumentName, enhancedAttribute.DocumentName); |
| 489 |
} |
| 490 |
|
| 491 |
private static void AssertIsSigningTime(this AsnEncodedData attribute, DateTime expectedTime) |
| 492 |
{ |
| 493 |
Assert.Equal(Oids.SigningTime, attribute.Oid.Value); |
| 494 |
Pkcs9SigningTime enhancedAttribute = attribute as Pkcs9SigningTime; |
| 495 |
Assert.NotNull(enhancedAttribute); |
| 496 |
Assert.Equal(expectedTime, enhancedAttribute.SigningTime); |
| 497 |
} |
| 498 |
|
| 499 |
private static void AssertIsContentType(this AsnEncodedData attribute, string expectedContentType) |
| 500 |
{ |
| 501 |
Assert.Equal(Oids.ContentType, attribute.Oid.Value); |
| 502 |
Pkcs9ContentType enhancedAttribute = attribute as Pkcs9ContentType; |
| 503 |
Assert.NotNull(enhancedAttribute); |
| 504 |
Assert.Equal(expectedContentType, enhancedAttribute.ContentType.Value); |
| 505 |
} |
| 506 |
|
| 507 |
private static void AssertIsMessageDigest(this AsnEncodedData attribute, byte[] expectedDigest) |
| 508 |
{ |
| 509 |
Assert.Equal(Oids.MessageDigest, attribute.Oid.Value); |
| 510 |
Pkcs9MessageDigest enhancedAttribute = attribute as Pkcs9MessageDigest; |
| 511 |
Assert.NotNull(enhancedAttribute); |
| 512 |
Assert.Equal<byte>(expectedDigest, enhancedAttribute.MessageDigest); |
| 513 |
} |
| 514 |
|
| 515 |
private static byte[] CreateEcmsWithAttributes(params AsnEncodedData[] attributes) |
| 516 |
{ |
| 517 |
ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 }); |
| 518 |
EnvelopedCms ecms = new EnvelopedCms(contentInfo); |
| 519 |
|
| 520 |
foreach (AsnEncodedData attribute in attributes) |
| 521 |
{ |
| 522 |
ecms.UnprotectedAttributes.Add(attribute); |
| 523 |
} |
| 524 |
|
| 525 |
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate()) |
| 526 |
{ |
| 527 |
CmsRecipient cmsRecipient = new CmsRecipient(cert); |
| 528 |
ecms.Encrypt(cmsRecipient); |
| 529 |
} |
| 530 |
byte[] encodedMessage = ecms.Encode(); |
| 531 |
return encodedMessage; |
| 532 |
} |
| 533 |
|
| 534 |
private static AsnEncodedData[] FlattenAndSort(this CryptographicAttributeObjectCollection col) |
| 535 |
{ |
| 536 |
List<AsnEncodedData> attributes = new List<AsnEncodedData>(); |
| 537 |
foreach (CryptographicAttributeObject cao in col) |
| 538 |
{ |
| 539 |
AsnEncodedDataCollection acol = cao.Values; |
| 540 |
foreach (AsnEncodedData a in acol) |
| 541 |
{ |
| 542 |
attributes.Add(a); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
return attributes.OrderBy(a => a.RawData.ByteArrayToHex()).ToArray(); |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
|