xref: /aosp_15_r20/external/cronet/net/cert/x509_cert_types.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 "net/cert/x509_cert_types.h"
6 
7 #include "third_party/boringssl/src/pki/input.h"
8 #include "third_party/boringssl/src/pki/parse_name.h"
9 
10 namespace net {
11 
12 CertPrincipal::CertPrincipal() = default;
13 
14 CertPrincipal::CertPrincipal(const CertPrincipal&) = default;
15 
16 CertPrincipal::CertPrincipal(CertPrincipal&&) = default;
17 
18 CertPrincipal::~CertPrincipal() = default;
19 
20 bool CertPrincipal::operator==(const CertPrincipal& other) const = default;
21 
EqualsForTesting(const CertPrincipal & other) const22 bool CertPrincipal::EqualsForTesting(const CertPrincipal& other) const {
23   return *this == other;
24 }
25 
ParseDistinguishedName(bssl::der::Input ber_name_data,PrintableStringHandling printable_string_handling)26 bool CertPrincipal::ParseDistinguishedName(
27     bssl::der::Input ber_name_data,
28     PrintableStringHandling printable_string_handling) {
29   bssl::RDNSequence rdns;
30   if (!ParseName(ber_name_data, &rdns)) {
31     return false;
32   }
33 
34   auto string_handling =
35       printable_string_handling == PrintableStringHandling::kAsUTF8Hack
36           ? bssl::X509NameAttribute::PrintableStringHandling::kAsUTF8Hack
37           : bssl::X509NameAttribute::PrintableStringHandling::kDefault;
38   for (const bssl::RelativeDistinguishedName& rdn : rdns) {
39     for (const bssl::X509NameAttribute& name_attribute : rdn) {
40       if (name_attribute.type == bssl::der::Input(bssl::kTypeCommonNameOid)) {
41         if (common_name.empty() &&
42             !name_attribute.ValueAsStringWithUnsafeOptions(string_handling,
43                                                            &common_name)) {
44           return false;
45         }
46       } else if (name_attribute.type ==
47                  bssl::der::Input(bssl::kTypeLocalityNameOid)) {
48         if (locality_name.empty() &&
49             !name_attribute.ValueAsStringWithUnsafeOptions(string_handling,
50                                                            &locality_name)) {
51           return false;
52         }
53       } else if (name_attribute.type ==
54                  bssl::der::Input(bssl::kTypeStateOrProvinceNameOid)) {
55         if (state_or_province_name.empty() &&
56             !name_attribute.ValueAsStringWithUnsafeOptions(
57                 string_handling, &state_or_province_name)) {
58           return false;
59         }
60       } else if (name_attribute.type ==
61                  bssl::der::Input(bssl::kTypeCountryNameOid)) {
62         if (country_name.empty() &&
63             !name_attribute.ValueAsStringWithUnsafeOptions(string_handling,
64                                                            &country_name)) {
65           return false;
66         }
67       } else if (name_attribute.type ==
68                  bssl::der::Input(bssl::kTypeOrganizationNameOid)) {
69         std::string s;
70         if (!name_attribute.ValueAsStringWithUnsafeOptions(string_handling, &s))
71           return false;
72         organization_names.push_back(s);
73       } else if (name_attribute.type ==
74                  bssl::der::Input(bssl::kTypeOrganizationUnitNameOid)) {
75         std::string s;
76         if (!name_attribute.ValueAsStringWithUnsafeOptions(string_handling, &s))
77           return false;
78         organization_unit_names.push_back(s);
79       }
80     }
81   }
82   return true;
83 }
84 
GetDisplayName() const85 std::string CertPrincipal::GetDisplayName() const {
86   if (!common_name.empty())
87     return common_name;
88   if (!organization_names.empty())
89     return organization_names[0];
90   if (!organization_unit_names.empty())
91     return organization_unit_names[0];
92 
93   return std::string();
94 }
95 
96 }  // namespace net
97