1 // Copyright 2017 The Chromium Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "nist_pkits_unittest.h"
16 
17 #include "certificate_policies.h"
18 
19 #include <sstream>
20 
21 BSSL_NAMESPACE_BEGIN
22 
23 namespace {
24 
25 // 2.16.840.1.101.3.2.1.48.1
26 const uint8_t kTestPolicy1[] = {0x60, 0x86, 0x48, 0x01, 0x65,
27                                 0x03, 0x02, 0x01, 0x30, 0x01};
28 
29 // 2.16.840.1.101.3.2.1.48.2
30 const uint8_t kTestPolicy2[] = {0x60, 0x86, 0x48, 0x01, 0x65,
31                                 0x03, 0x02, 0x01, 0x30, 0x02};
32 
33 // 2.16.840.1.101.3.2.1.48.3
34 const uint8_t kTestPolicy3[] = {0x60, 0x86, 0x48, 0x01, 0x65,
35                                 0x03, 0x02, 0x01, 0x30, 0x03};
36 
37 // 2.16.840.1.101.3.2.1.48.6
38 const uint8_t kTestPolicy6[] = {0x60, 0x86, 0x48, 0x01, 0x65,
39                                 0x03, 0x02, 0x01, 0x30, 0x06};
40 
SetPolicySetFromString(const char * const policy_names,std::set<der::Input> * out)41 void SetPolicySetFromString(const char *const policy_names,
42                             std::set<der::Input> *out) {
43   out->clear();
44   std::istringstream stream(policy_names);
45   for (std::string line; std::getline(stream, line, ',');) {
46     size_t start = line.find_first_not_of(" \n\t\r\f\v");
47     if (start == std::string::npos) {
48       continue;
49     }
50     size_t end = line.find_last_not_of(" \n\t\r\f\v");
51     if (end == std::string::npos) {
52       continue;
53     }
54     std::string policy_name = line.substr(start, end + 1);
55     if (policy_name.empty()) {
56       continue;
57     }
58 
59     if (policy_name == "anyPolicy") {
60       out->insert(der::Input(kAnyPolicyOid));
61     } else if (policy_name == "NIST-test-policy-1") {
62       out->insert(der::Input(kTestPolicy1));
63     } else if (policy_name == "NIST-test-policy-2") {
64       out->insert(der::Input(kTestPolicy2));
65     } else if (policy_name == "NIST-test-policy-3") {
66       out->insert(der::Input(kTestPolicy3));
67     } else if (policy_name == "NIST-test-policy-6") {
68       out->insert(der::Input(kTestPolicy6));
69     } else {
70       ADD_FAILURE() << "Unknown policy name: " << policy_name;
71     }
72   }
73 }
74 
75 }  // namespace
76 
PkitsTestInfo()77 PkitsTestInfo::PkitsTestInfo() {
78   SetInitialPolicySet("anyPolicy");
79   SetUserConstrainedPolicySet("NIST-test-policy-1");
80 }
81 
82 PkitsTestInfo::PkitsTestInfo(const PkitsTestInfo &other) = default;
83 
84 PkitsTestInfo::~PkitsTestInfo() = default;
85 
SetInitialExplicitPolicy(bool b)86 void PkitsTestInfo::SetInitialExplicitPolicy(bool b) {
87   initial_explicit_policy =
88       b ? InitialExplicitPolicy::kTrue : InitialExplicitPolicy::kFalse;
89 }
90 
SetInitialPolicyMappingInhibit(bool b)91 void PkitsTestInfo::SetInitialPolicyMappingInhibit(bool b) {
92   initial_policy_mapping_inhibit = b ? InitialPolicyMappingInhibit::kTrue
93                                      : InitialPolicyMappingInhibit::kFalse;
94 }
95 
SetInitialInhibitAnyPolicy(bool b)96 void PkitsTestInfo::SetInitialInhibitAnyPolicy(bool b) {
97   initial_inhibit_any_policy =
98       b ? InitialAnyPolicyInhibit::kTrue : InitialAnyPolicyInhibit::kFalse;
99 }
100 
SetInitialPolicySet(const char * const policy_names)101 void PkitsTestInfo::SetInitialPolicySet(const char *const policy_names) {
102   SetPolicySetFromString(policy_names, &initial_policy_set);
103 }
104 
SetUserConstrainedPolicySet(const char * const policy_names)105 void PkitsTestInfo::SetUserConstrainedPolicySet(
106     const char *const policy_names) {
107   SetPolicySetFromString(policy_names, &user_constrained_policy_set);
108 }
109 
110 BSSL_NAMESPACE_END
111