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 bool GetAsListOfStrings(const base::Value& value,
16 std::vector<std::string>* result) {
17 const base::ListValue* list = NULL;
18 if (!value.GetAsList(&list))
21 result->reserve(list->GetSize());
22 for (size_t i = 0; i < list->GetSize(); i++) {
24 if (!list->GetString(i, &item))
26 result->push_back(item);
33 ////////////////////////////////////////////////////////////////////////////////
34 // IssuerSubjectPattern
35 IssuerSubjectPattern::IssuerSubjectPattern(
36 const std::string& common_name,
37 const std::string& locality,
38 const std::string& organization,
39 const std::string& organizational_unit)
40 : common_name_(common_name),
42 organization_(organization),
43 organizational_unit_(organizational_unit) {
46 IssuerSubjectPattern::IssuerSubjectPattern() {
49 IssuerSubjectPattern::~IssuerSubjectPattern() {
52 bool IssuerSubjectPattern::Empty() const {
53 return common_name_.empty() && locality_.empty() && organization_.empty() &&
54 organizational_unit_.empty();
57 void IssuerSubjectPattern::Clear() {
60 organization_.clear();
61 organizational_unit_.clear();
64 void IssuerSubjectPattern::ReadFromONCDictionary(
65 const base::DictionaryValue& dict) {
68 dict.GetStringWithoutPathExpansion(onc::client_cert::kCommonName,
70 dict.GetStringWithoutPathExpansion(onc::client_cert::kLocality, &locality_);
71 dict.GetStringWithoutPathExpansion(onc::client_cert::kOrganization,
73 dict.GetStringWithoutPathExpansion(onc::client_cert::kOrganizationalUnit,
74 &organizational_unit_);
77 ////////////////////////////////////////////////////////////////////////////////
80 CertificatePattern::CertificatePattern() {
83 CertificatePattern::~CertificatePattern() {
86 bool CertificatePattern::Empty() const {
87 return issuer_ca_pems_.empty() && issuer_.Empty() && subject_.Empty();
90 void CertificatePattern::Clear() {
91 issuer_ca_pems_.clear();
94 enrollment_uri_list_.clear();
97 bool CertificatePattern::ReadFromONCDictionary(
98 const base::DictionaryValue& dict) {
101 const base::DictionaryValue* child_dict = NULL;
102 const base::ListValue* child_list = NULL;
104 // All of these are optional.
105 if (dict.GetListWithoutPathExpansion(onc::client_cert::kIssuerCAPEMs,
108 if (!GetAsListOfStrings(*child_list, &issuer_ca_pems_))
111 if (dict.GetDictionaryWithoutPathExpansion(onc::client_cert::kIssuer,
114 issuer_.ReadFromONCDictionary(*child_dict);
117 if (dict.GetDictionaryWithoutPathExpansion(onc::client_cert::kSubject,
120 subject_.ReadFromONCDictionary(*child_dict);
123 if (dict.GetListWithoutPathExpansion(onc::client_cert::kEnrollmentURI,
126 if (!GetAsListOfStrings(*child_list, &enrollment_uri_list_))
133 } // namespace chromeos