1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/network/certificate_pattern.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "components/onc/onc_constants.h"
15 // Keys for converting classes below to/from dictionaries.
16 const char kCommonNameKey
[] = "CommonName";
17 const char kLocalityKey
[] = "Locality";
18 const char kOrganizationKey
[] = "Organization";
19 const char kOrganizationalUnitKey
[] = "OrganizationalUnit";
20 const char kIssuerKey
[] = "Issuer";
21 const char kSubjectKey
[] = "Subject";
22 const char kEnrollmentUriKey
[] = "EnrollmentURI";
24 bool GetAsListOfStrings(const base::Value
& value
,
25 std::vector
<std::string
>* result
) {
26 const base::ListValue
* list
= NULL
;
27 if (!value
.GetAsList(&list
))
30 result
->reserve(list
->GetSize());
31 for (size_t i
= 0; i
< list
->GetSize(); i
++) {
33 if (!list
->GetString(i
, &item
))
35 result
->push_back(item
);
40 base::ListValue
* CreateListFromStrings(
41 const std::vector
<std::string
>& strings
) {
42 base::ListValue
* new_list
= new base::ListValue
;
43 for (std::vector
<std::string
>::const_iterator iter
= strings
.begin();
44 iter
!= strings
.end(); ++iter
) {
45 new_list
->Append(new base::StringValue(*iter
));
52 ////////////////////////////////////////////////////////////////////////////////
53 // IssuerSubjectPattern
54 IssuerSubjectPattern::IssuerSubjectPattern(const std::string
& common_name
,
55 const std::string
& locality
,
56 const std::string
& organization
,
57 const std::string
& organizational_unit
)
58 : common_name_(common_name
),
60 organization_(organization
),
61 organizational_unit_(organizational_unit
) { }
63 IssuerSubjectPattern::IssuerSubjectPattern() {}
65 IssuerSubjectPattern::~IssuerSubjectPattern() {}
67 bool IssuerSubjectPattern::Empty() const {
68 return common_name_
.empty() &&
70 organization_
.empty() &&
71 organizational_unit_
.empty();
74 void IssuerSubjectPattern::Clear() {
77 organization_
.clear();
78 organizational_unit_
.clear();
81 base::DictionaryValue
* IssuerSubjectPattern::CreateAsDictionary() const {
82 base::DictionaryValue
* dict
= new base::DictionaryValue
;
83 if (!common_name_
.empty())
84 dict
->SetString(kCommonNameKey
, common_name_
);
85 if (!locality_
.empty())
86 dict
->SetString(kLocalityKey
, locality_
);
87 if (!organization_
.empty())
88 dict
->SetString(kOrganizationKey
, organization_
);
89 if (!organizational_unit_
.empty())
90 dict
->SetString(kOrganizationalUnitKey
, organizational_unit_
);
94 bool IssuerSubjectPattern::CopyFromDictionary(
95 const base::DictionaryValue
& dict
) {
97 dict
.GetString(kCommonNameKey
, &common_name_
);
98 dict
.GetString(kLocalityKey
, &locality_
);
99 dict
.GetString(kOrganizationKey
, &organization_
);
100 dict
.GetString(kOrganizationalUnitKey
, &organizational_unit_
);
101 // If the dictionary wasn't empty, but we are, or vice versa, then something
103 DCHECK(dict
.empty() == Empty());
104 if (dict
.empty() != Empty())
109 ////////////////////////////////////////////////////////////////////////////////
110 // CertificatePattern
112 CertificatePattern::CertificatePattern() {}
114 CertificatePattern::~CertificatePattern() {}
116 bool CertificatePattern::Empty() const {
117 return issuer_ca_pems_
.empty() &&
122 void CertificatePattern::Clear() {
123 issuer_ca_pems_
.clear();
126 enrollment_uri_list_
.clear();
129 base::DictionaryValue
* CertificatePattern::CreateAsDictionary() const {
130 base::DictionaryValue
* dict
= new base::DictionaryValue
;
132 if (!issuer_ca_pems_
.empty()) {
133 dict
->Set(onc::certificate::kIssuerCAPEMs
,
134 CreateListFromStrings(issuer_ca_pems_
));
137 if (!issuer_
.Empty())
138 dict
->Set(kIssuerKey
, issuer_
.CreateAsDictionary());
140 if (!subject_
.Empty())
141 dict
->Set(kSubjectKey
, subject_
.CreateAsDictionary());
143 if (!enrollment_uri_list_
.empty())
144 dict
->Set(kEnrollmentUriKey
, CreateListFromStrings(enrollment_uri_list_
));
148 bool CertificatePattern::CopyFromDictionary(const base::DictionaryValue
&dict
) {
149 const base::DictionaryValue
* child_dict
= NULL
;
150 const base::ListValue
* child_list
= NULL
;
153 // All of these are optional.
154 if (dict
.GetList(onc::certificate::kIssuerCAPEMs
, &child_list
) &&
156 if (!GetAsListOfStrings(*child_list
, &issuer_ca_pems_
))
159 if (dict
.GetDictionary(kIssuerKey
, &child_dict
) && child_dict
) {
160 if (!issuer_
.CopyFromDictionary(*child_dict
))
164 if (dict
.GetDictionary(kSubjectKey
, &child_dict
) && child_dict
) {
165 if (!subject_
.CopyFromDictionary(*child_dict
))
169 if (dict
.GetList(kEnrollmentUriKey
, &child_list
) && child_list
) {
170 if (!GetAsListOfStrings(*child_list
, &enrollment_uri_list_
))
174 // If we didn't copy anything from the dictionary, then it had better be
176 DCHECK(dict
.empty() == Empty());
177 if (dict
.empty() != Empty())
183 } // namespace chromeos