Chromium Code Reviews| 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 the arg is null, an empty string will be returned instead. | |
|
pmarko
2017/05/11 12:32:46
- Please remove extra space
- I'd suggest |nss_tex
stevenjb
2017/05/12 19:27:50
Done.
| |
| 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; | |
| 81 std::string common_name = | |
| 82 Stringize(CERT_GetCommonName(&cert_handle->subject), std::string()); | |
| 83 if (!common_name.empty()) | |
| 84 name = base::UTF16ToUTF8(url_formatter::IDNToUnicode(common_name)); | |
| 85 if (!name.empty()) | |
| 86 return name; | |
| 87 return GetNickname(cert_handle); | |
| 88 } | |
| 89 | |
| 90 std::string GetCertAsciiNameOrNickname( | |
| 91 net::X509Certificate::OSCertHandle cert_handle) { | |
| 92 std::string name = | |
| 93 Stringize(CERT_GetCommonName(&cert_handle->subject), std::string()); | |
|
pmarko
2017/05/11 12:32:46
Couldn't we pass GetNickname(cert_handle) here ins
stevenjb
2017/05/12 19:27:50
This was copied, but I think the extra simplicity
| |
| 94 if (!name.empty()) | |
| 95 return name; | |
| 96 return GetNickname(cert_handle); | |
| 97 } | |
| 98 | |
| 99 } // namespace certificate | |
| 100 } // namespace chromeos | |
| OLD | NEW |