OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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_helper.h" | |
6 | |
7 #include <certdb.h> | |
8 #include <pk11pub.h> | |
9 #include <secport.h> | |
10 | |
11 #include "base/strings/string16.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "components/url_formatter/url_formatter.h" | |
14 #include "net/cert/nss_cert_database_chromeos.h" | |
15 | |
16 namespace chromeos { | |
17 namespace certificate { | |
18 | |
19 namespace { | |
20 | |
21 // Convert a char* return value from NSS into a std::string and free the NSS | |
22 // memory. If |nss_text| is null, |alternative_text| will be returned instead. | |
23 std::string Stringize(char* nss_text, const std::string& alternative_text) { | |
24 if (!nss_text) | |
25 return alternative_text; | |
26 | |
27 std::string s = nss_text; | |
28 PORT_Free(nss_text); | |
29 return s; | |
30 } | |
31 | |
32 std::string GetNickname(net::X509Certificate::OSCertHandle cert_handle) { | |
33 if (!cert_handle->nickname) | |
34 return std::string(); | |
35 std::string name = cert_handle->nickname; | |
36 // Hack copied from mozilla: Cut off text before first :, which seems to | |
37 // just be the token name. | |
38 size_t colon_pos = name.find(':'); | |
39 if (colon_pos != std::string::npos) | |
40 name = name.substr(colon_pos + 1); | |
41 return name; | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 net::CertType GetCertType(net::X509Certificate::OSCertHandle cert) { | |
47 CERTCertTrust trust = {0}; | |
48 CERT_GetCertTrust(cert, &trust); | |
49 | |
50 unsigned all_flags = | |
51 trust.sslFlags | trust.emailFlags | trust.objectSigningFlags; | |
52 | |
53 if (cert->nickname && (all_flags & CERTDB_USER)) | |
54 return net::USER_CERT; | |
55 | |
56 if ((all_flags & CERTDB_VALID_CA) || CERT_IsCACert(cert, nullptr)) | |
57 return net::CA_CERT; | |
58 | |
59 // TODO(mattm): http://crbug.com/128633. | |
60 if (trust.sslFlags & CERTDB_TERMINAL_RECORD) | |
61 return net::SERVER_CERT; | |
62 | |
63 return net::OTHER_CERT; | |
64 } | |
65 | |
66 std::string GetCertTokenName(CERTCertificate* cert) { | |
67 std::string token; | |
68 if (cert->slot) | |
69 token = PK11_GetTokenName(cert->slot); | |
70 return token; | |
71 } | |
72 | |
73 std::string GetIssuerCommonName(net::X509Certificate::OSCertHandle cert_handle, | |
74 const std::string& alternative_text) { | |
75 return Stringize(CERT_GetCommonName(&cert_handle->issuer), alternative_text); | |
76 } | |
77 | |
78 std::string GetCertNameOrNickname( | |
79 net::X509Certificate::OSCertHandle cert_handle) { | |
80 std::string name = GetCertAsciiNameOrNickname(cert_handle); | |
81 if (!name.empty()) | |
82 name = base::UTF16ToUTF8(url_formatter::IDNToUnicode(name)); | |
83 return name; | |
84 } | |
85 | |
86 std::string GetCertAsciiNameOrNickname( | |
87 net::X509Certificate::OSCertHandle cert_handle) { | |
88 std::string alternative_text = GetNickname(cert_handle); | |
89 return Stringize(CERT_GetCommonName(&cert_handle->subject), alternative_text); | |
emaxx
2017/05/15 14:14:20
If CERT_GetCommonName() returns a non-null string
stevenjb
2017/05/15 15:40:58
Stringize() just constructs a string() from a char
| |
90 } | |
91 | |
92 } // namespace certificate | |
93 } // namespace chromeos | |
OLD | NEW |