Home | History | Annotate | Download | only in network
      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.
      4 
      5 #include "chromeos/network/certificate_pattern.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/values.h"
      9 #include "components/onc/onc_constants.h"
     10 
     11 namespace chromeos {
     12 
     13 namespace {
     14 
     15 bool GetAsListOfStrings(const base::Value& value,
     16                         std::vector<std::string>* result) {
     17   const base::ListValue* list = NULL;
     18   if (!value.GetAsList(&list))
     19     return false;
     20   result->clear();
     21   result->reserve(list->GetSize());
     22   for (size_t i = 0; i < list->GetSize(); i++) {
     23     std::string item;
     24     if (!list->GetString(i, &item))
     25       return false;
     26     result->push_back(item);
     27   }
     28   return true;
     29 }
     30 
     31 }  // namespace
     32 
     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),
     41       locality_(locality),
     42       organization_(organization),
     43       organizational_unit_(organizational_unit) {
     44 }
     45 
     46 IssuerSubjectPattern::IssuerSubjectPattern() {
     47 }
     48 
     49 IssuerSubjectPattern::~IssuerSubjectPattern() {
     50 }
     51 
     52 bool IssuerSubjectPattern::Empty() const {
     53   return common_name_.empty() && locality_.empty() && organization_.empty() &&
     54          organizational_unit_.empty();
     55 }
     56 
     57 void IssuerSubjectPattern::Clear() {
     58   common_name_.clear();
     59   locality_.clear();
     60   organization_.clear();
     61   organizational_unit_.clear();
     62 }
     63 
     64 void IssuerSubjectPattern::ReadFromONCDictionary(
     65     const base::DictionaryValue& dict) {
     66   Clear();
     67 
     68   dict.GetStringWithoutPathExpansion(onc::client_cert::kCommonName,
     69                                      &common_name_);
     70   dict.GetStringWithoutPathExpansion(onc::client_cert::kLocality, &locality_);
     71   dict.GetStringWithoutPathExpansion(onc::client_cert::kOrganization,
     72                                      &organization_);
     73   dict.GetStringWithoutPathExpansion(onc::client_cert::kOrganizationalUnit,
     74                                      &organizational_unit_);
     75 }
     76 
     77 ////////////////////////////////////////////////////////////////////////////////
     78 // CertificatePattern
     79 
     80 CertificatePattern::CertificatePattern() {
     81 }
     82 
     83 CertificatePattern::~CertificatePattern() {
     84 }
     85 
     86 bool CertificatePattern::Empty() const {
     87   return issuer_ca_pems_.empty() && issuer_.Empty() && subject_.Empty();
     88 }
     89 
     90 void CertificatePattern::Clear() {
     91   issuer_ca_pems_.clear();
     92   issuer_.Clear();
     93   subject_.Clear();
     94   enrollment_uri_list_.clear();
     95 }
     96 
     97 bool CertificatePattern::ReadFromONCDictionary(
     98     const base::DictionaryValue& dict) {
     99   Clear();
    100 
    101   const base::DictionaryValue* child_dict = NULL;
    102   const base::ListValue* child_list = NULL;
    103 
    104   // All of these are optional.
    105   if (dict.GetListWithoutPathExpansion(onc::client_cert::kIssuerCAPEMs,
    106                                        &child_list) &&
    107       child_list) {
    108     if (!GetAsListOfStrings(*child_list, &issuer_ca_pems_))
    109       return false;
    110   }
    111   if (dict.GetDictionaryWithoutPathExpansion(onc::client_cert::kIssuer,
    112                                              &child_dict) &&
    113       child_dict) {
    114     issuer_.ReadFromONCDictionary(*child_dict);
    115   }
    116   child_dict = NULL;
    117   if (dict.GetDictionaryWithoutPathExpansion(onc::client_cert::kSubject,
    118                                              &child_dict) &&
    119       child_dict) {
    120     subject_.ReadFromONCDictionary(*child_dict);
    121   }
    122   child_list = NULL;
    123   if (dict.GetListWithoutPathExpansion(onc::client_cert::kEnrollmentURI,
    124                                        &child_list) &&
    125       child_list) {
    126     if (!GetAsListOfStrings(*child_list, &enrollment_uri_list_))
    127       return false;
    128   }
    129 
    130   return true;
    131 }
    132 
    133 }  // namespace chromeos
    134