Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: chrome/browser/extensions/api/platform_keys/platform_keys_api.cc

Issue 884073002: Implement chrome.platformKeys.getKeyPair(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cert_impl2
Patch Set: Updated histogram. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/extensions/api/platform_keys/platform_keys_api.h" 5 #include "chrome/browser/extensions/api/platform_keys/platform_keys_api.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/values.h"
11 #include "chrome/browser/chromeos/platform_keys/platform_keys.h" 12 #include "chrome/browser/chromeos/platform_keys/platform_keys.h"
12 #include "chrome/browser/chromeos/platform_keys/platform_keys_service.h" 13 #include "chrome/browser/chromeos/platform_keys/platform_keys_service.h"
13 #include "chrome/browser/chromeos/platform_keys/platform_keys_service_factory.h" 14 #include "chrome/browser/chromeos/platform_keys/platform_keys_service_factory.h"
14 #include "chrome/common/extensions/api/platform_keys_internal.h" 15 #include "chrome/common/extensions/api/platform_keys_internal.h"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 #include "net/cert/x509_certificate.h" 17 #include "net/cert/x509_certificate.h"
17 18
18 namespace extensions { 19 namespace extensions {
19 20
20 namespace api_pk = api::platform_keys; 21 namespace api_pk = api::platform_keys;
21 namespace api_pki = api::platform_keys_internal; 22 namespace api_pki = api::platform_keys_internal;
22 23
24 namespace {
25
26 const char kErrorAlgorithmNotSupported[] = "Algorithm not supported.";
27 const char kErrorInvalidX509Cert[] =
28 "Certificate is not a valid X.509 certificate.";
29
30 struct PublicKeyInfo {
31 // The X.509 Subject Public Key Info of the key in DER encoding.
32 std::string public_key_spki_der;
33
34 // The type of the key.
35 net::X509Certificate::PublicKeyType key_type =
36 net::X509Certificate::kPublicKeyTypeUnknown;
37
38 // The size of the key in bits.
39 size_t key_size_bits = 0;
40 };
41
42 // Builds a partial WebCrypto Algorithm object from the parameters available in
43 // |key_info|, which must the info of an RSA key. This doesn't include sign/hash
44 // parameters and thus isn't complete.
45 // platform_keys::GetPublicKey() enforced the public exponent 65537.
46 void BuildWebCryptoRSAAlgorithmDictionary(const PublicKeyInfo& key_info,
47 base::DictionaryValue* algorithm) {
48 CHECK_EQ(net::X509Certificate::kPublicKeyTypeRSA, key_info.key_type);
49 algorithm->SetStringWithoutPathExpansion("name", "RSASSA-PKCS1-v1_5");
50 algorithm->SetIntegerWithoutPathExpansion("modulusLength",
51 key_info.key_size_bits);
52
53 // Equals 65537.
54 const char defaultPublicExponent[] = {0x01, 0x00, 0x01};
55 algorithm->SetWithoutPathExpansion(
56 "publicExponent",
57 base::BinaryValue::CreateWithCopiedBuffer(
58 defaultPublicExponent, arraysize(defaultPublicExponent)));
59 }
60
61 } // namespace
62
23 namespace platform_keys { 63 namespace platform_keys {
24 64
25 const char kErrorInvalidToken[] = "The token is not valid."; 65 const char kErrorInvalidToken[] = "The token is not valid.";
26 const char kErrorAlgorithmNotSupported[] = "Algorithm not supported.";
27 const char kTokenIdUser[] = "user"; 66 const char kTokenIdUser[] = "user";
28 const char kTokenIdSystem[] = "system"; 67 const char kTokenIdSystem[] = "system";
29 68
30 // Returns whether |token_id| references a known Token. 69 // Returns whether |token_id| references a known Token.
31 bool ValidateToken(const std::string& token_id, 70 bool ValidateToken(const std::string& token_id,
32 std::string* platform_keys_token_id) { 71 std::string* platform_keys_token_id) {
33 platform_keys_token_id->clear(); 72 platform_keys_token_id->clear();
34 if (token_id == kTokenIdUser) { 73 if (token_id == kTokenIdUser) {
35 *platform_keys_token_id = chromeos::platform_keys::kTokenIdUser; 74 *platform_keys_token_id = chromeos::platform_keys::kTokenIdUser;
36 return true; 75 return true;
(...skipping 10 matching lines...) Expand all
47 if (platform_keys_token_id == chromeos::platform_keys::kTokenIdUser) 86 if (platform_keys_token_id == chromeos::platform_keys::kTokenIdUser)
48 return kTokenIdUser; 87 return kTokenIdUser;
49 if (platform_keys_token_id == chromeos::platform_keys::kTokenIdSystem) 88 if (platform_keys_token_id == chromeos::platform_keys::kTokenIdSystem)
50 return kTokenIdSystem; 89 return kTokenIdSystem;
51 90
52 return std::string(); 91 return std::string();
53 } 92 }
54 93
55 } // namespace platform_keys 94 } // namespace platform_keys
56 95
96 PlatformKeysInternalGetPublicKeyFunction::
97 ~PlatformKeysInternalGetPublicKeyFunction() {
98 }
99
100 ExtensionFunction::ResponseAction
101 PlatformKeysInternalGetPublicKeyFunction::Run() {
102 scoped_ptr<api_pki::GetPublicKey::Params> params(
103 api_pki::GetPublicKey::Params::Create(*args_));
104 EXTENSION_FUNCTION_VALIDATE(params);
105
106 const std::vector<char>& cert_der = params->certificate;
107 if (cert_der.empty())
108 return RespondNow(Error(kErrorInvalidX509Cert));
109 scoped_refptr<net::X509Certificate> cert_x509 =
110 net::X509Certificate::CreateFromBytes(vector_as_array(&cert_der),
111 cert_der.size());
112 if (!cert_x509)
113 return RespondNow(Error(kErrorInvalidX509Cert));
114
115 PublicKeyInfo key_info;
116 if (!chromeos::platform_keys::GetPublicKey(
117 cert_x509, &key_info.public_key_spki_der, &key_info.key_type,
118 &key_info.key_size_bits) ||
119 key_info.key_type != net::X509Certificate::kPublicKeyTypeRSA) {
120 return RespondNow(Error(kErrorAlgorithmNotSupported));
121 }
122
123 api_pki::GetPublicKey::Results::Algorithm algorithm;
124 BuildWebCryptoRSAAlgorithmDictionary(key_info,
125 &algorithm.additional_properties);
126
127 return RespondNow(ArgumentList(api_pki::GetPublicKey::Results::Create(
128 std::vector<char>(key_info.public_key_spki_der.begin(),
129 key_info.public_key_spki_der.end()),
130 algorithm)));
131 }
132
57 PlatformKeysInternalSelectClientCertificatesFunction:: 133 PlatformKeysInternalSelectClientCertificatesFunction::
58 ~PlatformKeysInternalSelectClientCertificatesFunction() { 134 ~PlatformKeysInternalSelectClientCertificatesFunction() {
59 } 135 }
60 136
61 ExtensionFunction::ResponseAction 137 ExtensionFunction::ResponseAction
62 PlatformKeysInternalSelectClientCertificatesFunction::Run() { 138 PlatformKeysInternalSelectClientCertificatesFunction::Run() {
63 scoped_ptr<api_pki::SelectClientCertificates::Params> params( 139 scoped_ptr<api_pki::SelectClientCertificates::Params> params(
64 api_pki::SelectClientCertificates::Params::Create(*args_)); 140 api_pki::SelectClientCertificates::Params::Create(*args_));
65 EXTENSION_FUNCTION_VALIDATE(params); 141 EXTENSION_FUNCTION_VALIDATE(params);
66 142
(...skipping 21 matching lines...) Expand all
88 OnSelectedCertificates(scoped_ptr<net::CertificateList> matches, 164 OnSelectedCertificates(scoped_ptr<net::CertificateList> matches,
89 const std::string& error_message) { 165 const std::string& error_message) {
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
91 if (!error_message.empty()) { 167 if (!error_message.empty()) {
92 Respond(Error(error_message)); 168 Respond(Error(error_message));
93 return; 169 return;
94 } 170 }
95 DCHECK(matches); 171 DCHECK(matches);
96 std::vector<linked_ptr<api_pk::Match>> result_matches; 172 std::vector<linked_ptr<api_pk::Match>> result_matches;
97 for (const scoped_refptr<net::X509Certificate>& match : *matches) { 173 for (const scoped_refptr<net::X509Certificate>& match : *matches) {
174 PublicKeyInfo key_info;
175 if (!chromeos::platform_keys::GetPublicKey(
176 match, &key_info.public_key_spki_der, &key_info.key_type,
177 &key_info.key_size_bits)) {
178 LOG(ERROR) << "Could not retrieve public key info.";
179 continue;
180 }
181 if (key_info.key_type != net::X509Certificate::kPublicKeyTypeRSA) {
182 LOG(ERROR) << "Skipping unsupported certificate with non-RSA key.";
183 continue;
184 }
185
98 linked_ptr<api_pk::Match> result_match(new api_pk::Match); 186 linked_ptr<api_pk::Match> result_match(new api_pk::Match);
99 std::string der_encoded_cert; 187 std::string der_encoded_cert;
100 net::X509Certificate::GetDEREncoded(match->os_cert_handle(), 188 net::X509Certificate::GetDEREncoded(match->os_cert_handle(),
101 &der_encoded_cert); 189 &der_encoded_cert);
102 result_match->certificate.assign(der_encoded_cert.begin(), 190 result_match->certificate.assign(der_encoded_cert.begin(),
103 der_encoded_cert.end()); 191 der_encoded_cert.end());
192
193 BuildWebCryptoRSAAlgorithmDictionary(
194 key_info, &result_match->key_algorithm.additional_properties);
104 result_matches.push_back(result_match); 195 result_matches.push_back(result_match);
105 } 196 }
106 Respond(ArgumentList( 197 Respond(ArgumentList(
107 api_pki::SelectClientCertificates::Results::Create(result_matches))); 198 api_pki::SelectClientCertificates::Results::Create(result_matches)));
108 } 199 }
109 200
110 PlatformKeysInternalSignFunction::~PlatformKeysInternalSignFunction() { 201 PlatformKeysInternalSignFunction::~PlatformKeysInternalSignFunction() {
111 } 202 }
112 203
113 ExtensionFunction::ResponseAction PlatformKeysInternalSignFunction::Run() { 204 ExtensionFunction::ResponseAction PlatformKeysInternalSignFunction::Run() {
114 scoped_ptr<api_pki::Sign::Params> params( 205 scoped_ptr<api_pki::Sign::Params> params(
115 api_pki::Sign::Params::Create(*args_)); 206 api_pki::Sign::Params::Create(*args_));
116 EXTENSION_FUNCTION_VALIDATE(params); 207 EXTENSION_FUNCTION_VALIDATE(params);
117 std::string platform_keys_token_id; 208 std::string platform_keys_token_id;
118 if (!platform_keys::ValidateToken(params->token_id, &platform_keys_token_id)) 209 if (!params->token_id.empty() &&
210 !platform_keys::ValidateToken(params->token_id,
211 &platform_keys_token_id)) {
119 return RespondNow(Error(platform_keys::kErrorInvalidToken)); 212 return RespondNow(Error(platform_keys::kErrorInvalidToken));
120 213 }
121 chromeos::platform_keys::HashAlgorithm hash_algorithm;
122 if (params->hash_algorithm_name == "SHA-1")
123 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA1;
124 else if (params->hash_algorithm_name == "SHA-256")
125 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA256;
126 else if (params->hash_algorithm_name == "SHA-384")
127 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA384;
128 else if (params->hash_algorithm_name == "SHA-512")
129 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA512;
130 else
131 return RespondNow(Error(platform_keys::kErrorAlgorithmNotSupported));
132 214
133 chromeos::PlatformKeysService* service = 215 chromeos::PlatformKeysService* service =
134 chromeos::PlatformKeysServiceFactory::GetForBrowserContext( 216 chromeos::PlatformKeysServiceFactory::GetForBrowserContext(
135 browser_context()); 217 browser_context());
136 DCHECK(service); 218 DCHECK(service);
137 219
138 service->Sign( 220 if (params->hash_algorithm_name == "none") {
139 platform_keys_token_id, 221 service->SignRSAPKCS1Raw(
140 std::string(params->public_key.begin(), params->public_key.end()), 222 platform_keys_token_id,
141 hash_algorithm, std::string(params->data.begin(), params->data.end()), 223 std::string(params->data.begin(), params->data.end()),
142 extension_id(), 224 std::string(params->public_key.begin(), params->public_key.end()),
143 base::Bind(&PlatformKeysInternalSignFunction::OnSigned, this)); 225 extension_id(),
226 base::Bind(&PlatformKeysInternalSignFunction::OnSigned, this));
227 } else {
228 chromeos::platform_keys::HashAlgorithm hash_algorithm;
229 if (params->hash_algorithm_name == "SHA-1") {
230 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA1;
231 } else if (params->hash_algorithm_name == "SHA-256") {
232 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA256;
233 } else if (params->hash_algorithm_name == "SHA-384") {
234 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA384;
235 } else if (params->hash_algorithm_name == "SHA-512") {
236 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA512;
237 } else {
238 return RespondNow(Error(kErrorAlgorithmNotSupported));
239 }
240 service->SignRSAPKCS1Digest(
241 platform_keys_token_id,
242 std::string(params->data.begin(), params->data.end()),
243 std::string(params->public_key.begin(), params->public_key.end()),
244 hash_algorithm, extension_id(),
245 base::Bind(&PlatformKeysInternalSignFunction::OnSigned, this));
246 }
247
144 return RespondLater(); 248 return RespondLater();
145 } 249 }
146 250
147 void PlatformKeysInternalSignFunction::OnSigned( 251 void PlatformKeysInternalSignFunction::OnSigned(
148 const std::string& signature, 252 const std::string& signature,
149 const std::string& error_message) { 253 const std::string& error_message) {
150 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 254 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
151 if (error_message.empty()) 255 if (error_message.empty())
152 Respond(ArgumentList(api_pki::Sign::Results::Create( 256 Respond(ArgumentList(api_pki::Sign::Results::Create(
153 std::vector<char>(signature.begin(), signature.end())))); 257 std::vector<char>(signature.begin(), signature.end()))));
154 else 258 else
155 Respond(Error(error_message)); 259 Respond(Error(error_message));
156 } 260 }
157 261
158 } // namespace extensions 262 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698