OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ssl/ssl_platform_key_util.h" | 5 #include "net/ssl/ssl_platform_key_util.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 base::LazyInstance<SSLPlatformKeyTaskRunner>::Leaky g_platform_key_task_runner = | 43 base::LazyInstance<SSLPlatformKeyTaskRunner>::Leaky g_platform_key_task_runner = |
44 LAZY_INSTANCE_INITIALIZER; | 44 LAZY_INSTANCE_INITIALIZER; |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 scoped_refptr<base::SingleThreadTaskRunner> GetSSLPlatformKeyTaskRunner() { | 48 scoped_refptr<base::SingleThreadTaskRunner> GetSSLPlatformKeyTaskRunner() { |
49 return g_platform_key_task_runner.Get().task_runner(); | 49 return g_platform_key_task_runner.Get().task_runner(); |
50 } | 50 } |
51 | 51 |
52 bool GetClientCertInfo(const X509Certificate* certificate, | 52 bool GetClientCertInfo(const X509Certificate* certificate, |
53 SSLPrivateKey::Type* out_type, | 53 int* out_type, |
54 size_t* out_max_length) { | 54 size_t* out_max_length) { |
55 crypto::OpenSSLErrStackTracer tracker(FROM_HERE); | 55 crypto::OpenSSLErrStackTracer tracker(FROM_HERE); |
56 | 56 |
57 std::string der_encoded; | 57 std::string der_encoded; |
58 base::StringPiece spki; | 58 base::StringPiece spki; |
59 if (!X509Certificate::GetDEREncoded(certificate->os_cert_handle(), | 59 if (!X509Certificate::GetDEREncoded(certificate->os_cert_handle(), |
60 &der_encoded) || | 60 &der_encoded) || |
61 !asn1::ExtractSPKIFromDERCert(der_encoded, &spki)) { | 61 !asn1::ExtractSPKIFromDERCert(der_encoded, &spki)) { |
62 LOG(ERROR) << "Could not extract SPKI from certificate."; | 62 LOG(ERROR) << "Could not extract SPKI from certificate."; |
63 return false; | 63 return false; |
64 } | 64 } |
65 | 65 |
66 CBS cbs; | 66 CBS cbs; |
67 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(spki.data()), spki.size()); | 67 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(spki.data()), spki.size()); |
68 bssl::UniquePtr<EVP_PKEY> key(EVP_parse_public_key(&cbs)); | 68 bssl::UniquePtr<EVP_PKEY> key(EVP_parse_public_key(&cbs)); |
69 if (!key || CBS_len(&cbs) != 0) { | 69 if (!key || CBS_len(&cbs) != 0) { |
70 LOG(ERROR) << "Could not parse public key."; | 70 LOG(ERROR) << "Could not parse public key."; |
71 return false; | 71 return false; |
72 } | 72 } |
73 | 73 |
74 int key_type = EVP_PKEY_id(key.get()); | 74 *out_type = EVP_PKEY_id(key.get()); |
75 switch (key_type) { | |
76 case EVP_PKEY_RSA: | |
77 *out_type = SSLPrivateKey::Type::RSA; | |
78 break; | |
79 | |
80 case EVP_PKEY_EC: { | |
81 EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(key.get()); | |
82 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); | |
83 switch (curve) { | |
84 case NID_X9_62_prime256v1: | |
85 *out_type = SSLPrivateKey::Type::ECDSA_P256; | |
86 break; | |
87 case NID_secp384r1: | |
88 *out_type = SSLPrivateKey::Type::ECDSA_P384; | |
89 break; | |
90 case NID_secp521r1: | |
91 *out_type = SSLPrivateKey::Type::ECDSA_P521; | |
92 break; | |
93 default: | |
94 LOG(ERROR) << "Unsupported curve type " << curve; | |
95 return false; | |
96 } | |
97 break; | |
98 } | |
99 | |
100 default: | |
101 LOG(ERROR) << "Unsupported key type " << key_type; | |
102 return false; | |
103 } | |
104 | |
105 *out_max_length = EVP_PKEY_size(key.get()); | 75 *out_max_length = EVP_PKEY_size(key.get()); |
106 return true; | 76 return true; |
107 } | 77 } |
108 | 78 |
109 } // namespace net | 79 } // namespace net |
OLD | NEW |