| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/x509_certificate.h" | 5 #include "net/cert/x509_certificate.h" |
| 6 | 6 |
| 7 #include <cert.h> | 7 #include <cert.h> |
| 8 #include <cryptohi.h> | 8 #include <cryptohi.h> |
| 9 #include <keyhi.h> | 9 #include <keyhi.h> |
| 10 #include <nss.h> | 10 #include <nss.h> |
| 11 #include <pk11pub.h> | 11 #include <pk11pub.h> |
| 12 #include <prtime.h> | 12 #include <prtime.h> |
| 13 #include <seccomon.h> | 13 #include <seccomon.h> |
| 14 #include <secder.h> | 14 #include <secder.h> |
| 15 #include <sechash.h> | 15 #include <sechash.h> |
| 16 | 16 |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/pickle.h" | 19 #include "base/pickle.h" |
| 20 #include "base/stringprintf.h" |
| 20 #include "base/time.h" | 21 #include "base/time.h" |
| 21 #include "crypto/nss_util.h" | 22 #include "crypto/nss_util.h" |
| 22 #include "crypto/rsa_private_key.h" | 23 #include "crypto/rsa_private_key.h" |
| 23 #include "crypto/scoped_nss_types.h" | 24 #include "crypto/scoped_nss_types.h" |
| 24 #include "net/cert/x509_util_nss.h" | 25 #include "net/cert/x509_util_nss.h" |
| 25 | 26 |
| 26 namespace net { | 27 namespace net { |
| 27 | 28 |
| 28 void X509Certificate::Initialize() { | 29 void X509Certificate::Initialize() { |
| 29 x509_util::ParsePrincipal(&cert_handle_->subject, &subject_); | 30 x509_util::ParsePrincipal(&cert_handle_->subject, &subject_); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 59 } | 60 } |
| 60 | 61 |
| 61 std::string X509Certificate::GetDefaultNickname(CertType type) const { | 62 std::string X509Certificate::GetDefaultNickname(CertType type) const { |
| 62 if (!default_nickname_.empty()) | 63 if (!default_nickname_.empty()) |
| 63 return default_nickname_; | 64 return default_nickname_; |
| 64 | 65 |
| 65 std::string result; | 66 std::string result; |
| 66 if (type == USER_CERT && cert_handle_->slot) { | 67 if (type == USER_CERT && cert_handle_->slot) { |
| 67 // Find the private key for this certificate and see if it has a | 68 // Find the private key for this certificate and see if it has a |
| 68 // nickname. If there is a private key, and it has a nickname, then | 69 // nickname. If there is a private key, and it has a nickname, then |
| 69 // we return that nickname. | 70 // return that nickname. |
| 70 SECKEYPrivateKey* private_key = PK11_FindPrivateKeyFromCert( | 71 SECKEYPrivateKey* private_key = PK11_FindPrivateKeyFromCert( |
| 71 cert_handle_->slot, | 72 cert_handle_->slot, |
| 72 cert_handle_, | 73 cert_handle_, |
| 73 NULL); // wincx | 74 NULL); // wincx |
| 74 if (private_key) { | 75 if (private_key) { |
| 75 char* private_key_nickname = PK11_GetPrivateKeyNickname(private_key); | 76 char* private_key_nickname = PK11_GetPrivateKeyNickname(private_key); |
| 76 if (private_key_nickname) { | 77 if (private_key_nickname) { |
| 77 result = private_key_nickname; | 78 result = private_key_nickname; |
| 78 PORT_Free(private_key_nickname); | 79 PORT_Free(private_key_nickname); |
| 79 SECKEY_DestroyPrivateKey(private_key); | 80 SECKEY_DestroyPrivateKey(private_key); |
| 80 return result; | 81 return result; |
| 81 } | 82 } |
| 82 SECKEY_DestroyPrivateKey(private_key); | 83 SECKEY_DestroyPrivateKey(private_key); |
| 83 } | 84 } |
| 84 } | 85 } |
| 85 | 86 |
| 86 switch (type) { | 87 switch (type) { |
| 87 case CA_CERT: { | 88 case CA_CERT: { |
| 88 char* nickname = CERT_MakeCANickname(cert_handle_); | 89 char* nickname = CERT_MakeCANickname(cert_handle_); |
| 89 result = nickname; | 90 result = nickname; |
| 90 PORT_Free(nickname); | 91 PORT_Free(nickname); |
| 91 break; | 92 break; |
| 92 } | 93 } |
| 93 case USER_CERT: { | 94 case USER_CERT: |
| 94 // Create a nickname for a user certificate. | 95 // TODO(gspencer): Internationalize this. It's wrong to assume English |
| 95 // We use the scheme used by Firefox: | 96 // here. |
| 96 // --> <subject's common name>'s <issuer's common name> ID. | 97 result = base::StringPrintf("%s's %s ID", |
| 97 // TODO(gspencer): internationalize this: it's wrong to | 98 subject_.GetDisplayName().c_str(), |
| 98 // hard code English. | 99 issuer_.GetDisplayName().c_str()); |
| 99 | |
| 100 std::string username, ca_name; | |
| 101 char* temp_username = CERT_GetCommonName( | |
| 102 &cert_handle_->subject); | |
| 103 char* temp_ca_name = CERT_GetCommonName(&cert_handle_->issuer); | |
| 104 if (temp_username) { | |
| 105 username = temp_username; | |
| 106 PORT_Free(temp_username); | |
| 107 } | |
| 108 if (temp_ca_name) { | |
| 109 ca_name = temp_ca_name; | |
| 110 PORT_Free(temp_ca_name); | |
| 111 } | |
| 112 result = username + "'s " + ca_name + " ID"; | |
| 113 break; | 100 break; |
| 114 } | |
| 115 case SERVER_CERT: | 101 case SERVER_CERT: |
| 116 result = subject_.GetDisplayName(); | 102 result = subject_.GetDisplayName(); |
| 117 break; | 103 break; |
| 118 case UNKNOWN_CERT: | 104 case UNKNOWN_CERT: |
| 119 default: | 105 default: |
| 120 break; | 106 break; |
| 121 } | 107 } |
| 122 return result; | 108 return result; |
| 123 } | 109 } |
| 124 | 110 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 } | 279 } |
| 294 | 280 |
| 295 // static | 281 // static |
| 296 void X509Certificate::GetPublicKeyInfo(OSCertHandle cert_handle, | 282 void X509Certificate::GetPublicKeyInfo(OSCertHandle cert_handle, |
| 297 size_t* size_bits, | 283 size_t* size_bits, |
| 298 PublicKeyType* type) { | 284 PublicKeyType* type) { |
| 299 x509_util::GetPublicKeyInfo(cert_handle, size_bits, type); | 285 x509_util::GetPublicKeyInfo(cert_handle, size_bits, type); |
| 300 } | 286 } |
| 301 | 287 |
| 302 } // namespace net | 288 } // namespace net |
| OLD | NEW |