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