summaryrefslogtreecommitdiffstats
path: root/net/cert/internal/parse_certificate_unittest.cc
blob: 5292e5b284b89a00a321dbc79abb7e63ae9b6838 (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/cert/internal/parse_certificate.h"

#include "base/strings/stringprintf.h"
#include "net/cert/internal/test_helpers.h"
#include "net/der/input.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

// Pretty-prints a GeneralizedTime as a human-readable string for use in test
// expectations (it is more readable to specify the expected results as a
// string).
std::string ToString(const der::GeneralizedTime& time) {
  return base::StringPrintf(
      "year=%d, month=%d, day=%d, hours=%d, minutes=%d, seconds=%d", time.year,
      time.month, time.day, time.hours, time.minutes, time.seconds);
}

std::string GetFilePath(const std::string& file_name) {
  return std::string("net/data/parse_certificate_unittest/") + file_name;
}

// Loads certificate data and expectations from the PEM file |file_name|.
// Verifies that parsing the Certificate succeeds, and each parsed field matches
// the expectations.
void EnsureParsingCertificateSucceeds(const std::string& file_name) {
  std::string data;
  std::string expected_tbs_certificate;
  std::string expected_signature_algorithm;
  std::string expected_signature;

  // Read the certificate data and test expectations from a single PEM file.
  const PemBlockMapping mappings[] = {
      {"CERTIFICATE", &data},
      {"SIGNATURE", &expected_signature},
      {"SIGNATURE ALGORITHM", &expected_signature_algorithm},
      {"TBS CERTIFICATE", &expected_tbs_certificate},
  };
  ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));

  // Parsing the certificate should succeed.
  ParsedCertificate parsed;
  ASSERT_TRUE(ParseCertificate(InputFromString(&data), &parsed));

  // Ensure that the ParsedCertificate matches expectations.
  EXPECT_EQ(0, parsed.signature_value.unused_bits());
  EXPECT_EQ(InputFromString(&expected_signature),
            parsed.signature_value.bytes());
  EXPECT_EQ(InputFromString(&expected_signature_algorithm),
            parsed.signature_algorithm_tlv);
  EXPECT_EQ(InputFromString(&expected_tbs_certificate),
            parsed.tbs_certificate_tlv);
}

// Loads certificate data from the PEM file |file_name| and verifies that the
// Certificate parsing fails.
void EnsureParsingCertificateFails(const std::string& file_name) {
  std::string data;

  const PemBlockMapping mappings[] = {
      {"CERTIFICATE", &data},
  };

  ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));

  // Parsing the Certificate should fail.
  ParsedCertificate parsed;
  ASSERT_FALSE(ParseCertificate(InputFromString(&data), &parsed));
}

// Tests parsing a Certificate.
TEST(ParseCertificateTest, Version3) {
  EnsureParsingCertificateSucceeds("cert_version3.pem");
}

// Tests parsing a simplified Certificate-like structure (the sub-fields for
// algorithm and tbsCertificate are not actually valid, but ParseCertificate()
// doesn't check them)
TEST(ParseCertificateTest, Skeleton) {
  EnsureParsingCertificateSucceeds("cert_skeleton.pem");
}

// Tests parsing a Certificate that is not a sequence fails.
TEST(ParseCertificateTest, NotSequence) {
  EnsureParsingCertificateFails("cert_not_sequence.pem");
}

// Tests that uncomsumed data is not allowed after the main SEQUENCE.
TEST(ParseCertificateTest, DataAfterSignature) {
  EnsureParsingCertificateFails("cert_data_after_signature.pem");
}

// Tests that parsing fails if the signature BIT STRING is missing.
TEST(ParseCertificateTest, MissingSignature) {
  EnsureParsingCertificateFails("cert_missing_signature.pem");
}

// Tests that parsing fails if the signature is present but not a BIT STRING.
TEST(ParseCertificateTest, SignatureNotBitString) {
  EnsureParsingCertificateFails("cert_signature_not_bit_string.pem");
}

// Tests that parsing fails if the main SEQUENCE is empty (missing all the
// fields).
TEST(ParseCertificateTest, EmptySequence) {
  EnsureParsingCertificateFails("cert_empty_sequence.pem");
}

// Tests what happens when the signature algorithm is present, but has the wrong
// tag.
TEST(ParseCertificateTest, AlgorithmNotSequence) {
  EnsureParsingCertificateFails("cert_algorithm_not_sequence.pem");
}

// Loads tbsCertificate data and expectations from the PEM file |file_name|.
// Verifies that parsing the TBSCertificate succeeds, and each parsed field
// matches the expectations.
void EnsureParsingTbsSucceeds(const std::string& file_name,
                              CertificateVersion expected_version) {
  std::string data;
  std::string expected_serial_number;
  std::string expected_signature_algorithm;
  std::string expected_issuer;
  std::string expected_validity_not_before;
  std::string expected_validity_not_after;
  std::string expected_subject;
  std::string expected_spki;
  std::string expected_issuer_unique_id;
  std::string expected_subject_unique_id;
  std::string expected_extensions;

  // Read the certificate data and test expectations from a single PEM file.
  const PemBlockMapping mappings[] = {
      {"TBS CERTIFICATE", &data},
      {"SIGNATURE ALGORITHM", &expected_signature_algorithm},
      {"SERIAL NUMBER", &expected_serial_number},
      {"ISSUER", &expected_issuer},
      {"VALIDITY NOTBEFORE", &expected_validity_not_before},
      {"VALIDITY NOTAFTER", &expected_validity_not_after},
      {"SUBJECT", &expected_subject},
      {"SPKI", &expected_spki},
      {"ISSUER UNIQUE ID", &expected_issuer_unique_id, true},
      {"SUBJECT UNIQUE ID", &expected_subject_unique_id, true},
      {"EXTENSIONS", &expected_extensions, true},
  };
  ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));

  // Parsing the TBSCertificate should succeed.
  ParsedTbsCertificate parsed;
  ASSERT_TRUE(ParseTbsCertificate(InputFromString(&data), &parsed));

  // Ensure that the ParsedTbsCertificate matches expectations.
  EXPECT_EQ(expected_version, parsed.version);

  EXPECT_EQ(InputFromString(&expected_serial_number), parsed.serial_number);
  EXPECT_EQ(InputFromString(&expected_signature_algorithm),
            parsed.signature_algorithm_tlv);

  EXPECT_EQ(InputFromString(&expected_issuer), parsed.issuer_tlv);

  // In the test expectations PEM file, validity is described as a
  // textual string of the parsed value (rather than as DER).
  EXPECT_EQ(expected_validity_not_before, ToString(parsed.validity_not_before));
  EXPECT_EQ(expected_validity_not_after, ToString(parsed.validity_not_after));

  EXPECT_EQ(InputFromString(&expected_subject), parsed.subject_tlv);
  EXPECT_EQ(InputFromString(&expected_spki), parsed.spki_tlv);

  EXPECT_EQ(InputFromString(&expected_issuer_unique_id),
            parsed.issuer_unique_id.bytes());
  EXPECT_EQ(!expected_issuer_unique_id.empty(), parsed.has_issuer_unique_id);
  EXPECT_EQ(InputFromString(&expected_subject_unique_id),
            parsed.subject_unique_id.bytes());
  EXPECT_EQ(!expected_subject_unique_id.empty(), parsed.has_subject_unique_id);

  EXPECT_EQ(InputFromString(&expected_extensions), parsed.extensions_tlv);
  EXPECT_EQ(!expected_extensions.empty(), parsed.has_extensions);
}

// Loads certificate data from the PEM file |file_name| and verifies that the
// Certificate parsing succeed, however the TBSCertificate parsing fails.
void EnsureParsingTbsFails(const std::string& file_name) {
  std::string data;

  const PemBlockMapping mappings[] = {
      {"TBS CERTIFICATE", &data},
  };

  ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));

  // Parsing the TBSCertificate should fail.
  ParsedTbsCertificate parsed;
  ASSERT_FALSE(ParseTbsCertificate(InputFromString(&data), &parsed));
}

// Tests parsing a TBSCertificate for v3 that contains no optional fields.
TEST(ParseTbsCertificateTest, Version3NoOptionals) {
  EnsureParsingTbsSucceeds("tbs_v3_no_optionals.pem", CertificateVersion::V3);
}

// Tests parsing a TBSCertificate for v3 that contains extensions.
TEST(ParseTbsCertificateTest, Version3WithExtensions) {
  EnsureParsingTbsSucceeds("tbs_v3_extensions.pem", CertificateVersion::V3);
}

// Tests parsing a TBSCertificate for v3 that contains no optional fields, and
// has a negative serial number.
//
// CAs are not supposed to include negative serial numbers, however RFC 5280
// expects consumers to deal with it anyway).
TEST(ParseTbsCertificateTest, NegativeSerialNumber) {
  EnsureParsingTbsSucceeds("tbs_negative_serial_number.pem",
                           CertificateVersion::V3);
}

// Tests parsing a TBSCertificate with a serial number that is 21 octets long
// (and the first byte is 0).
TEST(ParseTbCertificateTest, SerialNumber21OctetsLeading0) {
  EnsureParsingTbsFails("tbs_serial_number_21_octets_leading_0.pem");
}

// Tests parsing a TBSCertificate with a serial number that is 26 octets long
// (and does not contain a leading 0).
TEST(ParseTbsCertificateTest, SerialNumber26Octets) {
  EnsureParsingTbsFails("tbs_serial_number_26_octets.pem");
}

// Tests parsing a TBSCertificate which lacks a version number (causing it to
// default to v1).
TEST(ParseTbsCertificateTest, Version1) {
  EnsureParsingTbsSucceeds("tbs_v1.pem", CertificateVersion::V1);
}

// The version was set to v1 explicitly rather than omitting the version field.
TEST(ParseTbsCertificateTest, ExplicitVersion1) {
  EnsureParsingTbsFails("tbs_explicit_v1.pem");
}

// Extensions are not defined in version 1.
TEST(ParseTbsCertificateTest, Version1WithExtensions) {
  EnsureParsingTbsFails("tbs_v1_extensions.pem");
}

// Extensions are not defined in version 2.
TEST(ParseTbsCertificateTest, Version2WithExtensions) {
  EnsureParsingTbsFails("tbs_v2_extensions.pem");
}

// A boring version 2 certificate with none of the optional fields.
TEST(ParseTbsCertificateTest, Version2NoOptionals) {
  EnsureParsingTbsSucceeds("tbs_v2_no_optionals.pem", CertificateVersion::V2);
}

// A version 2 certificate with an issuer unique ID field.
TEST(ParseTbsCertificateTest, Version2IssuerUniqueId) {
  EnsureParsingTbsSucceeds("tbs_v2_issuer_unique_id.pem",
                           CertificateVersion::V2);
}

// A version 2 certificate with both a issuer and subject unique ID field.
TEST(ParseTbsCertificateTest, Version2IssuerAndSubjectUniqueId) {
  EnsureParsingTbsSucceeds("tbs_v2_issuer_and_subject_unique_id.pem",
                           CertificateVersion::V2);
}

// A version 3 certificate with all of the optional fields (issuer unique id,
// subject unique id, and extensions).
TEST(ParseTbsCertificateTest, Version3AllOptionals) {
  EnsureParsingTbsSucceeds("tbs_v3_all_optionals.pem", CertificateVersion::V3);
}

// The version was set to v4, which is unrecognized.
TEST(ParseTbsCertificateTest, Version4) {
  EnsureParsingTbsFails("tbs_v4.pem");
}

// Tests that extraneous data after extensions in a v3 is rejected.
TEST(ParseTbsCertificateTest, Version3DataAfterExtensions) {
  EnsureParsingTbsFails("tbs_v3_data_after_extensions.pem");
}

// Tests using a real-world certificate (whereas the other tests are fabricated
// (and in fact invalid) data.
TEST(ParseTbsCertificateTest, Version3Real) {
  EnsureParsingTbsSucceeds("tbs_v3_real.pem", CertificateVersion::V3);
}

// Parses a TBSCertificate whose "validity" field expresses both notBefore
// and notAfter using UTCTime.
TEST(ParseTbsCertificateTest, ValidityBothUtcTime) {
  EnsureParsingTbsSucceeds("tbs_validity_both_utc_time.pem",
                           CertificateVersion::V3);
}

// Parses a TBSCertificate whose "validity" field expresses both notBefore
// and notAfter using GeneralizedTime.
TEST(ParseTbsCertificateTest, ValidityBothGeneralizedTime) {
  EnsureParsingTbsSucceeds("tbs_validity_both_generalized_time.pem",
                           CertificateVersion::V3);
}

// Parses a TBSCertificate whose "validity" field expresses notBefore using
// UTCTime and notAfter using GeneralizedTime.
TEST(ParseTbsCertificateTest, ValidityUTCTimeAndGeneralizedTime) {
  EnsureParsingTbsSucceeds("tbs_validity_utc_time_and_generalized_time.pem",
                           CertificateVersion::V3);
}

// Parses a TBSCertificate whose validity" field expresses notBefore using
// GeneralizedTime and notAfter using UTCTime. Also of interest, notBefore >
// notAfter. Parsing will succeed, however no time can satisfy this constraint.
TEST(ParseTbsCertificateTest, ValidityGeneralizedTimeAndUTCTime) {
  EnsureParsingTbsSucceeds("tbs_validity_generalized_time_and_utc_time.pem",
                           CertificateVersion::V3);
}

// Parses a TBSCertificate whose "validity" field does not strictly follow
// the DER rules (and fails to be parsed).
TEST(ParseTbsCertificateTest, ValidityRelaxed) {
  EnsureParsingTbsFails("tbs_validity_relaxed.pem");
}

// Reads a PEM file containing a block "EXTENSION". This input will be
// passed to ParseExtension, and the results filled in |out|.
bool ParseExtensionFromFile(const std::string& file_name,
                            ParsedExtension* out,
                            std::string* data) {
  const PemBlockMapping mappings[] = {
      {"EXTENSION", data},
  };

  EXPECT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));
  return ParseExtension(InputFromString(data), out);
}

// Parses an Extension whose critical field is true (255).
TEST(ParseExtensionTest, Critical) {
  std::string data;
  ParsedExtension extension;
  ASSERT_TRUE(
      ParseExtensionFromFile("extension_critical.pem", &extension, &data));

  EXPECT_TRUE(extension.critical);

  const uint8_t kExpectedOid[] = {0x55, 0x1d, 0x13};
  EXPECT_EQ(der::Input(kExpectedOid), extension.oid);

  const uint8_t kExpectedValue[] = {0x30, 0x00};
  EXPECT_EQ(der::Input(kExpectedValue), extension.value);
}

// Parses an Extension whose critical field is false (omitted).
TEST(ParseExtensionTest, NotCritical) {
  std::string data;
  ParsedExtension extension;
  ASSERT_TRUE(
      ParseExtensionFromFile("extension_not_critical.pem", &extension, &data));

  EXPECT_FALSE(extension.critical);

  const uint8_t kExpectedOid[] = {0x55, 0x1d, 0x13};
  EXPECT_EQ(der::Input(kExpectedOid), extension.oid);

  const uint8_t kExpectedValue[] = {0x30, 0x00};
  EXPECT_EQ(der::Input(kExpectedValue), extension.value);
}

// Parses an Extension whose critical field is 0. This is in one sense FALSE,
// however because critical has DEFAULT of false this is in fact invalid
// DER-encoding.
TEST(ParseExtensionTest, Critical0) {
  std::string data;
  ParsedExtension extension;
  ASSERT_FALSE(
      ParseExtensionFromFile("extension_critical_0.pem", &extension, &data));
}

// Parses an Extension whose critical field is 3. Under DER-encoding BOOLEAN
// values must an octet of either all zero bits, or all 1 bits, so this is not
// valid.
TEST(ParseExtensionTest, Critical3) {
  std::string data;
  ParsedExtension extension;
  ASSERT_FALSE(
      ParseExtensionFromFile("extension_critical_3.pem", &extension, &data));
}

// Runs a test for extensions parsing. The input file is a PEM file which
// contains a DER-encoded Extensions sequence, as well as the expected value
// for each contained extension.
void EnsureParsingExtensionsSucceeds(
    const std::string& file_name,
    std::map<der::Input, ParsedExtension>* extensions,
    std::string* data) {
  const PemBlockMapping mappings[] = {
      // Test Input.
      {"EXTENSIONS", data},
  };

  ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));
  ASSERT_TRUE(ParseExtensions(InputFromString(data), extensions));
}

// Runs a test that verifies extensions parsing fails. The input file is a PEM
// file which contains a DER-encoded Extensions sequence.
void EnsureParsingExtensionsFails(const std::string& file_name) {
  std::string data;

  const PemBlockMapping mappings[] = {
      {"EXTENSIONS", &data},
  };

  std::map<der::Input, ParsedExtension> extensions;
  ASSERT_TRUE(ReadTestDataFromPemFile(GetFilePath(file_name), mappings));
  ASSERT_FALSE(ParseExtensions(InputFromString(&data), &extensions));
}

// Parses an Extensions that is an empty sequence.
TEST(ParseExtensionsTest, EmptySequence) {
  EnsureParsingExtensionsFails("extensions_empty_sequence.pem");
}

// Parses an Extensions that is not a sequence.
TEST(ParseExtensionsTest, NotSequence) {
  EnsureParsingExtensionsFails("extensions_not_sequence.pem");
}

// Parses an Extensions that has data after the sequence.
TEST(ParseExtensionsTest, DataAfterSequence) {
  EnsureParsingExtensionsFails("extensions_data_after_sequence.pem");
}

// Parses an Extensions that contains duplicated key usages.
TEST(ParseExtensionsTest, DuplicateKeyUsage) {
  EnsureParsingExtensionsFails("extensions_duplicate_key_usage.pem");
}

// Parses an Extensions that contains an unknown critical extension.
TEST(ParseExtensionsTest, UnknownCritical) {
  std::string data;
  std::map<der::Input, ParsedExtension> extensions;
  EnsureParsingExtensionsSucceeds("extensions_unknown_critical.pem",
                                  &extensions, &data);

  ASSERT_EQ(1u, extensions.size());
  // This OID corresponds with
  // 1.2.840.113554.4.1.72585.0 (https://davidben.net/oid)
  const uint8_t oid[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12,
                         0x04, 0x01, 0x84, 0xb7, 0x09, 0x00};

  auto iter = extensions.find(der::Input(oid));
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_TRUE(iter->second.critical);
  EXPECT_EQ(4u, iter->second.value.Length());
}

// Parses an Extensions that contains an unknown non-critical extension.
TEST(ParseExtensionsTest, UnknownNonCritical) {
  std::string data;
  std::map<der::Input, ParsedExtension> extensions;
  EnsureParsingExtensionsSucceeds("extensions_unknown_non_critical.pem",
                                  &extensions, &data);

  ASSERT_EQ(1u, extensions.size());
  // This OID corresponds with
  // 1.2.840.113554.4.1.72585.0 (https://davidben.net/oid)
  const uint8_t oid[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12,
                         0x04, 0x01, 0x84, 0xb7, 0x09, 0x00};

  auto iter = extensions.find(der::Input(oid));
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_FALSE(iter->second.critical);
  EXPECT_EQ(4u, iter->second.value.Length());
}

// Parses an Extensions that contains a basic constraints.
TEST(ParseExtensionsTest, BasicConstraints) {
  std::string data;
  std::map<der::Input, ParsedExtension> extensions;
  EnsureParsingExtensionsSucceeds("extensions_basic_constraints.pem",
                                  &extensions, &data);

  ASSERT_EQ(1u, extensions.size());

  auto iter = extensions.find(BasicConstraintsOid());
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_TRUE(iter->second.critical);
  EXPECT_EQ(2u, iter->second.value.Length());
}

// Parses an Extensions that contains an extended key usages.
TEST(ParseExtensionsTest, ExtendedKeyUsage) {
  std::string data;
  std::map<der::Input, ParsedExtension> extensions;
  EnsureParsingExtensionsSucceeds("extensions_extended_key_usage.pem",
                                  &extensions, &data);

  ASSERT_EQ(1u, extensions.size());

  auto iter = extensions.find(ExtKeyUsageOid());
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_FALSE(iter->second.critical);
  EXPECT_EQ(45u, iter->second.value.Length());
}

// Parses an Extensions that contains a key usage.
TEST(ParseExtensionsTest, KeyUsage) {
  std::string data;
  std::map<der::Input, ParsedExtension> extensions;
  EnsureParsingExtensionsSucceeds("extensions_key_usage.pem", &extensions,
                                  &data);

  ASSERT_EQ(1u, extensions.size());

  auto iter = extensions.find(KeyUsageOid());
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_TRUE(iter->second.critical);
  EXPECT_EQ(4u, iter->second.value.Length());
}

// Parses an Extensions that contains a policies extension.
TEST(ParseExtensionsTest, Policies) {
  std::string data;
  std::map<der::Input, ParsedExtension> extensions;
  EnsureParsingExtensionsSucceeds("extensions_policies.pem", &extensions,
                                  &data);

  ASSERT_EQ(1u, extensions.size());

  auto iter = extensions.find(CertificatePoliciesOid());
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_FALSE(iter->second.critical);
  EXPECT_EQ(95u, iter->second.value.Length());
}

// Parses an Extensions that contains a subjectaltname extension.
TEST(ParseExtensionsTest, SubjectAltName) {
  std::string data;
  std::map<der::Input, ParsedExtension> extensions;
  EnsureParsingExtensionsSucceeds("extensions_subject_alt_name.pem",
                                  &extensions, &data);

  ASSERT_EQ(1u, extensions.size());

  auto iter = extensions.find(SubjectAltNameOid());
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_FALSE(iter->second.critical);
  EXPECT_EQ(23u, iter->second.value.Length());
}

// Parses an Extensions that contains multiple extensions, sourced from a
// real-world certificate.
TEST(ParseExtensionsTest, Real) {
  std::string data;
  std::map<der::Input, ParsedExtension> extensions;
  EnsureParsingExtensionsSucceeds("extensions_real.pem", &extensions, &data);

  ASSERT_EQ(7u, extensions.size());

  auto iter = extensions.find(KeyUsageOid());
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_TRUE(iter->second.critical);
  EXPECT_EQ(4u, iter->second.value.Length());

  iter = extensions.find(BasicConstraintsOid());
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_TRUE(iter->second.critical);
  EXPECT_EQ(8u, iter->second.value.Length());

  iter = extensions.find(CertificatePoliciesOid());
  ASSERT_TRUE(iter != extensions.end());
  EXPECT_FALSE(iter->second.critical);
  EXPECT_EQ(16u, iter->second.value.Length());

  // TODO(eroman): Verify the other 4 extensions' values.
}

}  // namespace

}  // namespace net