Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Pkcs9AttributeTests.cs « tests « System.Security.Cryptography.Pkcs « libraries « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07c192c0f7a855df8fc0625b27c0e5ea22e3f782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Xunit;

using Test.Cryptography;

namespace System.Security.Cryptography.Pkcs.Tests
{
    public static class Pkcs9AttributeTests
    {
        [Fact]
        public static void Pkcs9AttributeObjectNullaryCtor()
        {
            Pkcs9AttributeObject p = new Pkcs9AttributeObject();
            Assert.Null(p.Oid);
            if (PlatformDetection.IsNetCore)
            {
                Assert.Empty(p.RawData);
            }
            else
            {
                Assert.Null(p.RawData);
            }
        }

        [Fact]
        public static void Pkcs9AttributeAsnEncodedDataCtorNullOid()
        {
            AsnEncodedData a = new AsnEncodedData(new byte[3]);
            object ign;
            AssertExtensions.Throws<ArgumentException, ArgumentNullException>("asnEncodedData", "asnEncodedData.Oid", () => ign = new Pkcs9AttributeObject(a));
        }

        [Fact]
        public static void InputDateTimeAsWindowsFileTimeBefore1601()
        {
            DateTime dt = new DateTime(1600, 12, 31, 11, 59, 59, DateTimeKind.Utc);
            AssertExtensions.Throws<CryptographicException, ArgumentOutOfRangeException>(() => new Pkcs9SigningTime(dt));
        }

        [Fact]
        public static void Pkcs9SigningTime_DateTimeMinValue()
        {
            AssertExtensions.Throws<CryptographicException, ArgumentOutOfRangeException>(() => new Pkcs9SigningTime(DateTime.MinValue));
        }

        [Fact]
        public static void InputDateTimeAsX509TimeBefore1950_Utc()
        {
            DateTime dt = new DateTime(1949, 12, 31, 23, 59, 59, DateTimeKind.Utc);
            Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
        }

        [Fact]
        public static void InputDateTimeAsX509TimeBefore1950_Unspecified()
        {
            DateTime dt = new DateTime(1949, 12, 30);
            Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
        }

        [Fact]
        public static void InputDateTimeAsX509TimeBefore1950_Local()
        {
            DateTime dt = new DateTime(1949, 12, 30, 00, 00, 00, DateTimeKind.Local);
            Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
        }

        [Fact]
        public static void InputDateTimeAsX509TimeAfter2049_Utc()
        {
            DateTime dt = new DateTime(2050, 01, 01, 00, 00, 00, DateTimeKind.Utc);
            Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
        }

        [Fact]
        public static void InputDateTimeAsX509TimeAfter2049_Unspecified()
        {
            DateTime dt = new DateTime(2050, 01, 02);
            Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
        }

        [Fact]
        public static void InputDateTimeAsX509TimeAfter2049_Local()
        {
            DateTime dt = new DateTime(2050, 01, 02, 00, 00, 00, DateTimeKind.Local);
            Assert.ThrowsAny<CryptographicException>(() => new Pkcs9SigningTime(dt));
        }

        [Fact]
        public static void InputDateTimeAsX509TimeBetween1950And2049_Utc()
        {
            var exception = Record.Exception(() => {
                DateTime dt = new DateTime(1950, 1, 1, 00, 00, 00, DateTimeKind.Utc);
                Pkcs9SigningTime st = new Pkcs9SigningTime(dt);
                dt = new DateTime(2049, 12, 31, 23, 59, 59, DateTimeKind.Utc);
                st = new Pkcs9SigningTime(dt);

                dt = new DateTime(1950, 1, 2);
                st = new Pkcs9SigningTime(dt);
                dt = new DateTime(2049, 12, 30);
                st = new Pkcs9SigningTime(dt);

                dt = new DateTime(1950, 1, 2, 00, 00, 00, DateTimeKind.Local);
                st = new Pkcs9SigningTime(dt);
                dt = new DateTime(2049, 12, 30, 23, 59, 59, DateTimeKind.Local);
                st = new Pkcs9SigningTime(dt);
            });
            Assert.Null(exception);
        }

        [Fact]
        public static void Pkcs9AttributeAsnEncodedDataCtorNullOidValue()
        {
            Oid oid = new Oid(null, null);

            AsnEncodedData a = new AsnEncodedData(oid, new byte[3]);
            object ign;
            AssertExtensions.Throws<ArgumentException, ArgumentNullException>("asnEncodedData", "oid.Value", () => ign = new Pkcs9AttributeObject(a));
        }

        [Fact]
        public static void Pkcs9AttributeAsnEncodedDataCtorEmptyOidValue()
        {
            Oid oid = new Oid(string.Empty, null);

            AsnEncodedData a = new AsnEncodedData(oid, new byte[3]);
            object ign;
            AssertExtensions.Throws<ArgumentException>("asnEncodedData", "oid.Value", () => ign = new Pkcs9AttributeObject(a));
        }

        [Fact]
        public static void Pkcs9AttributeCopyFromNullAsn()
        {
            Pkcs9AttributeObject p = new Pkcs9AttributeObject();
            Assert.Throws<ArgumentNullException>(() => p.CopyFrom(null));
        }

        [Fact]
        public static void Pkcs9AttributeCopyFromAsnNotAPkcs9Attribute()
        {
            // Pkcs9AttributeObject.CopyFrom(AsnEncodedData) refuses to accept any AsnEncodedData that isn't a Pkcs9AttributeObject-derived class.
            Pkcs9AttributeObject p = new Pkcs9AttributeObject();
            byte[] rawData = "041e4d00790020004400650073006300720069007000740069006f006e000000".HexToByteArray();
            AsnEncodedData a = new AsnEncodedData(Oids.DocumentName, rawData);
            AssertExtensions.Throws<ArgumentException>(null, () => p.CopyFrom(a));
        }

        [Fact]
        public static void DocumentDescriptionNullary()
        {
            Pkcs9DocumentDescription p = new Pkcs9DocumentDescription();
            if (PlatformDetection.IsNetCore)
            {
                Assert.Empty(p.RawData);
                Assert.Throws<CryptographicException>(() => p.DocumentDescription);
            }
            else
            {
                Assert.Null(p.RawData);
                Assert.Null(p.DocumentDescription);
            }
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentDescription, oid);
        }

        [Fact]
        public static void DocumentDescriptionFromRawData()
        {
            byte[] rawData = "041e4d00790020004400650073006300720069007000740069006f006e000000".HexToByteArray();
            Pkcs9DocumentDescription p = new Pkcs9DocumentDescription(rawData);
            Assert.Equal(rawData, p.RawData);
            string cookedData = p.DocumentDescription;
            Assert.Equal("My Description", cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentDescription, oid);
        }

        [Fact]
        public static void DocumentDescriptionMissingTerminator()
        {
            byte[] rawData = "041e4d00790020004400650073006300720069007000740069006f006e002100".HexToByteArray();
            Pkcs9DocumentDescription p = new Pkcs9DocumentDescription(rawData);
            Assert.Equal(rawData, p.RawData);
            string cookedData = p.DocumentDescription;
            Assert.Equal("My Description!", cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentDescription, oid);
        }

        [Fact]
        public static void DocumentDescriptionEmbeddedTerminator()
        {
            byte[] rawData = "041e4d00790020004400650073006300720000007000740069006f006e000000".HexToByteArray();
            Pkcs9DocumentDescription p = new Pkcs9DocumentDescription(rawData);
            Assert.Equal(rawData, p.RawData);
            string cookedData = p.DocumentDescription;
            Assert.Equal("My Descr", cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentDescription, oid);
        }

        [Fact]
        public static void DocumentDescriptionFromCookedData()
        {
            Pkcs9DocumentDescription p = new Pkcs9DocumentDescription("My Description");
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentDescription, oid);

            Pkcs9DocumentDescription p2 = new Pkcs9DocumentDescription(p.RawData);
            string cookedData = p2.DocumentDescription;
            Assert.Equal("My Description", cookedData);
        }

        [Fact]
        public static void DocumentDescriptionNullValue()
        {
            object ignore;
            Assert.Throws<ArgumentNullException>(() => ignore = new Pkcs9DocumentDescription((string)null));
        }

        [Fact]
        public static void DocumentNameNullary()
        {
            Pkcs9DocumentName p = new Pkcs9DocumentName();
            if (PlatformDetection.IsNetCore)
            {
                Assert.Empty(p.RawData);
                Assert.Throws<CryptographicException>(() => p.DocumentName);
            }
            else
            {
                Assert.Null(p.RawData);
                Assert.Null(p.DocumentName);
            }
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentName, oid);
        }

        [Fact]
        public static void DocumentNameFromRawData()
        {
            byte[] rawData = "04104d00790020004e0061006d0065000000".HexToByteArray();
            Pkcs9DocumentName p = new Pkcs9DocumentName(rawData);
            Assert.Equal(rawData, p.RawData);
            string cookedData = p.DocumentName;
            Assert.Equal("My Name", cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentName, oid);
        }

        [Fact]
        public static void DocumentNameMissingTerminator()
        {
            byte[] rawData = "04104d00790020004e0061006d0065002100".HexToByteArray();
            Pkcs9DocumentName p = new Pkcs9DocumentName(rawData);
            Assert.Equal(rawData, p.RawData);
            string cookedData = p.DocumentName;
            Assert.Equal("My Name!", cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentName, oid);
        }

        [Fact]
        public static void DocumentNameEmbeddedTerminator()
        {
            byte[] rawData = "04104d00790020004e006100000065000000".HexToByteArray();
            Pkcs9DocumentName p = new Pkcs9DocumentName(rawData);
            Assert.Equal(rawData, p.RawData);
            string cookedData = p.DocumentName;
            Assert.Equal("My Na", cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentName, oid);
        }

        [Fact]
        public static void DocumentNameFromCookedData()
        {
            Pkcs9DocumentName p = new Pkcs9DocumentName("My Name");
            string oid = p.Oid.Value;
            Assert.Equal(s_OidDocumentName, oid);

            Pkcs9DocumentName p2 = new Pkcs9DocumentName(p.RawData);
            string cookedData = p2.DocumentName;
            Assert.Equal("My Name", cookedData);
        }

        [Fact]
        public static void DocumentNamenNullValue()
        {
            object ignore;
            Assert.Throws<ArgumentNullException>(() => ignore = new Pkcs9DocumentName((string)null));
        }

        [Fact]
        public static void SigningTimeNullary()
        {
            Pkcs9SigningTime p = new Pkcs9SigningTime();

            // the default constructor initializes with DateTime.Now.
            Assert.NotNull(p.RawData);
            Assert.Equal(DateTimeKind.Local, p.SigningTime.Kind);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidSigningTime, oid);
        }

        [Fact]
        public static void SigningTimeFromRawData()
        {
            DateTime dateTime = new DateTime(2015, 4, 1);
            byte[] rawData = "170d3135303430313030303030305a".HexToByteArray();
            Pkcs9SigningTime p = new Pkcs9SigningTime(rawData);
            Assert.Equal(rawData, p.RawData);
            DateTime cookedData = p.SigningTime;
            Assert.Equal(DateTimeKind.Utc, cookedData.Kind);
            Assert.Equal(dateTime, cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidSigningTime, oid);
        }

        [Fact]
        public static void SigningTimeFromCookedData_Unspecified()
        {
            DateTime dateTime = new DateTime(2015, 4, 1);
            Pkcs9SigningTime p = new Pkcs9SigningTime(dateTime);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidSigningTime, oid);

            Pkcs9SigningTime p2 = new Pkcs9SigningTime(p.RawData);
            DateTime cookedData = p2.SigningTime;
            Assert.Equal(dateTime, cookedData);
            Assert.Equal(DateTimeKind.Utc, cookedData.Kind);
        }

        [Fact]
        public static void SigningTimeFromCookedData_Local()
        {
            DateTime dateTime = new DateTime(2015, 4, 1, 0, 0, 0, DateTimeKind.Local);
            Pkcs9SigningTime p = new Pkcs9SigningTime(dateTime);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidSigningTime, oid);

            Pkcs9SigningTime p2 = new Pkcs9SigningTime(p.RawData);
            DateTime cookedData = p2.SigningTime;
            Assert.Equal(dateTime, cookedData.ToLocalTime());
            Assert.Equal(DateTimeKind.Utc, cookedData.Kind);
        }

        [Fact]
        public static void SigningTimeFromCookedData_Utc()
        {
            DateTime dateTime = new DateTime(2015, 4, 1, 0, 0, 0, DateTimeKind.Utc);
            Pkcs9SigningTime p = new Pkcs9SigningTime(dateTime);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidSigningTime, oid);

            Pkcs9SigningTime p2 = new Pkcs9SigningTime(p.RawData);
            DateTime cookedData = p2.SigningTime;
            Assert.Equal(dateTime, cookedData);
            Assert.Equal(DateTimeKind.Utc, cookedData.Kind);
        }

        [Fact]
        public static void ContentTypeNullary()
        {
            Pkcs9ContentType p = new Pkcs9ContentType();
            if (PlatformDetection.IsNetCore)
            {
                Assert.Empty(p.RawData);
                Assert.Throws<CryptographicException>(() => p.ContentType);
            }
            else
            {
                Assert.Null(p.RawData);
                Assert.Null(p.ContentType);
            }
            string oid = p.Oid.Value;
            Assert.Equal(s_OidContentType, oid);
        }

        [Fact]
        public static void ContentTypeFromRawData()
        {
            byte[] rawData = { ASN_TAG_OBJID, 0, 42, 0x9f, 0xa2, 0, 0x82, 0xf3, 0 };
            rawData[1] = (byte)(rawData.Length - 2);
            Pkcs9ContentType p = CreatePkcs9ContentType(rawData);
            Assert.Equal(rawData, p.RawData);
            string cookedData = p.ContentType.Value;
            Assert.Equal("1.2.512256.47488", cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidContentType, oid);
        }

        [Fact]
        public static void ContentTypeFromCookedData()
        {
            string contentType = "1.3.8473.23.4773.23";
            byte[] encodedContentType = "06072bc21917a52517".HexToByteArray();
            Pkcs9ContentType p = new Pkcs9ContentType();
            Pkcs9AttributeObject pkcs9AttributeObject = new Pkcs9AttributeObject(p.Oid, encodedContentType);
            p.CopyFrom(pkcs9AttributeObject);

            string cookedData = p.ContentType.Value;
            Assert.Equal(contentType, cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidContentType, oid);
        }

        [Fact]
        public static void ContentTypeFromRawDataMinimal()
        {
            byte[] rawData = { ASN_TAG_OBJID, 0 };
            rawData[1] = (byte)(rawData.Length - 2);
            Pkcs9ContentType p = CreatePkcs9ContentType(rawData);
            Assert.Equal(rawData, p.RawData);
            string cookedData = p.ContentType.Value;
            Assert.Equal("", cookedData);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidContentType, oid);
        }

        [Fact]
        public static void ContentTypeBadData()
        {
            Assert.ThrowsAny<CryptographicException>(() => CreatePkcs9ContentTypeAndExtractContentType(new byte[0]));  // Too short
            Assert.ThrowsAny<CryptographicException>(() => CreatePkcs9ContentTypeAndExtractContentType(new byte[1]));  // Too short
            Assert.ThrowsAny<CryptographicException>(() => CreatePkcs9ContentTypeAndExtractContentType(new byte[2]));  // Does not start with ASN_TAG_OBJID.
            Assert.ThrowsAny<CryptographicException>(() => CreatePkcs9ContentTypeAndExtractContentType(new byte[] { ASN_TAG_OBJID, 1 }));  // Bad length byte.
        }

        [Fact]
        public static void MessageDigestNullary()
        {
            Pkcs9MessageDigest p = new Pkcs9MessageDigest();
            if (PlatformDetection.IsNetCore)
            {
                Assert.Empty(p.RawData);
                Assert.Throws<CryptographicException>(() => p.MessageDigest);
            }
            else
            {
                Assert.Null(p.RawData);
                Assert.Null(p.MessageDigest);
            }
            string oid = p.Oid.Value;
            Assert.Equal(s_OidMessageDigest, oid);
        }

        [Fact]
        public static void MessageDigestFromRawData()
        {
            byte[] messageDigest = { 3, 45, 88, 128, 93 };
            List<byte> encodedMessageDigestList = new List<byte>(messageDigest.Length + 2);
            encodedMessageDigestList.Add(4);
            encodedMessageDigestList.Add(checked((byte)(messageDigest.Length)));
            encodedMessageDigestList.AddRange(messageDigest);
            byte[] encodedMessageDigest = encodedMessageDigestList.ToArray();

            Pkcs9MessageDigest p = new Pkcs9MessageDigest();
            Pkcs9AttributeObject pAttribute = new Pkcs9AttributeObject(s_OidMessageDigest, encodedMessageDigest);
            p.CopyFrom(pAttribute);
            Assert.Equal<byte>(encodedMessageDigest, p.RawData);
            Assert.Equal<byte>(messageDigest, p.MessageDigest);
            string oid = p.Oid.Value;
            Assert.Equal(s_OidMessageDigest, oid);
        }

        private static void CreatePkcs9ContentTypeAndExtractContentType(byte[] rawData)
        {
            Oid contentType = CreatePkcs9ContentType(rawData).ContentType;
        }

        private static Pkcs9ContentType CreatePkcs9ContentType(byte[] rawData)
        {
            Pkcs9ContentType pkcs9ContentType = new Pkcs9ContentType();
            Pkcs9AttributeObject pkcs9AttributeObject = new Pkcs9AttributeObject(pkcs9ContentType.Oid, rawData);
            pkcs9ContentType.CopyFrom(pkcs9AttributeObject);
            return pkcs9ContentType;
        }

        private const byte ASN_TAG_OBJID = 0x06;

        private const string s_OidDocumentDescription = "1.3.6.1.4.1.311.88.2.2";
        private const string s_OidDocumentName = "1.3.6.1.4.1.311.88.2.1";
        private const string s_OidSigningTime = "1.2.840.113549.1.9.5";
        private const string s_OidContentType = "1.2.840.113549.1.9.3";
        private const string s_OidMessageDigest = "1.2.840.113549.1.9.4";
    }
}