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

Side by Side Diff: content/child/webcrypto/openssl/rsa_key_openssl.cc

Issue 512023002: Refactor the interface for generating keys. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Run git-cl format Created 6 years, 2 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/child/webcrypto/openssl/rsa_key_openssl.h" 5 #include "content/child/webcrypto/openssl/rsa_key_openssl.h"
6 6
7 #include <openssl/evp.h> 7 #include <openssl/evp.h>
8 #include <openssl/pkcs12.h> 8 #include <openssl/pkcs12.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/stl_util.h" 11 #include "base/stl_util.h"
12 #include "content/child/webcrypto/crypto_data.h" 12 #include "content/child/webcrypto/crypto_data.h"
13 #include "content/child/webcrypto/generate_key_result.h"
13 #include "content/child/webcrypto/jwk.h" 14 #include "content/child/webcrypto/jwk.h"
14 #include "content/child/webcrypto/openssl/key_openssl.h" 15 #include "content/child/webcrypto/openssl/key_openssl.h"
15 #include "content/child/webcrypto/status.h" 16 #include "content/child/webcrypto/status.h"
16 #include "content/child/webcrypto/webcrypto_util.h" 17 #include "content/child/webcrypto/webcrypto_util.h"
17 #include "crypto/openssl_util.h" 18 #include "crypto/openssl_util.h"
18 #include "crypto/scoped_openssl_types.h" 19 #include "crypto/scoped_openssl_types.h"
19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h"
20 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" 21 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
21 22
22 namespace content { 23 namespace content {
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 return CreateWebCryptoPublicKey(pkey.Pass(), 222 return CreateWebCryptoPublicKey(pkey.Pass(),
222 algorithm.id(), 223 algorithm.id(),
223 algorithm.rsaHashedImportParams()->hash(), 224 algorithm.rsaHashedImportParams()->hash(),
224 extractable, 225 extractable,
225 usage_mask, 226 usage_mask,
226 key); 227 key);
227 } 228 }
228 229
229 } // namespace 230 } // namespace
230 231
231 Status RsaHashedAlgorithm::VerifyKeyUsagesBeforeGenerateKeyPair( 232 Status RsaHashedAlgorithm::GenerateKey(
233 const blink::WebCryptoAlgorithm& algorithm,
234 bool extractable,
232 blink::WebCryptoKeyUsageMask combined_usage_mask, 235 blink::WebCryptoKeyUsageMask combined_usage_mask,
233 blink::WebCryptoKeyUsageMask* public_usage_mask, 236 GenerateKeyResult* result) const {
234 blink::WebCryptoKeyUsageMask* private_usage_mask) const {
235 Status status = CheckKeyCreationUsages( 237 Status status = CheckKeyCreationUsages(
236 all_public_key_usages_ | all_private_key_usages_, combined_usage_mask); 238 all_public_key_usages_ | all_private_key_usages_, combined_usage_mask);
237 if (status.IsError()) 239 if (status.IsError())
238 return status; 240 return status;
239 241
240 *public_usage_mask = combined_usage_mask & all_public_key_usages_; 242 const blink::WebCryptoKeyUsageMask public_usage_mask =
241 *private_usage_mask = combined_usage_mask & all_private_key_usages_; 243 combined_usage_mask & all_public_key_usages_;
244 const blink::WebCryptoKeyUsageMask private_usage_mask =
245 combined_usage_mask & all_private_key_usages_;
242 246
243 return Status::Success();
244 }
245
246 Status RsaHashedAlgorithm::GenerateKeyPair(
247 const blink::WebCryptoAlgorithm& algorithm,
248 bool extractable,
249 blink::WebCryptoKeyUsageMask public_usage_mask,
250 blink::WebCryptoKeyUsageMask private_usage_mask,
251 blink::WebCryptoKey* public_key,
252 blink::WebCryptoKey* private_key) const {
253 const blink::WebCryptoRsaHashedKeyGenParams* params = 247 const blink::WebCryptoRsaHashedKeyGenParams* params =
254 algorithm.rsaHashedKeyGenParams(); 248 algorithm.rsaHashedKeyGenParams();
255 249
256 unsigned int public_exponent = 0; 250 unsigned int public_exponent = 0;
257 unsigned int modulus_length_bits = 0; 251 unsigned int modulus_length_bits = 0;
258 Status status = 252 status =
259 GetRsaKeyGenParameters(params, &public_exponent, &modulus_length_bits); 253 GetRsaKeyGenParameters(params, &public_exponent, &modulus_length_bits);
260 if (status.IsError()) 254 if (status.IsError())
261 return status; 255 return status;
262 256
263 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 257 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
264 258
265 // Generate an RSA key pair. 259 // Generate an RSA key pair.
266 crypto::ScopedRSA rsa_private_key(RSA_new()); 260 crypto::ScopedRSA rsa_private_key(RSA_new());
267 crypto::ScopedBIGNUM bn(BN_new()); 261 crypto::ScopedBIGNUM bn(BN_new());
268 if (!rsa_private_key.get() || !bn.get() || 262 if (!rsa_private_key.get() || !bn.get() ||
(...skipping 14 matching lines...) Expand all
283 } 277 }
284 278
285 // Construct an EVP_PKEY for the public key. 279 // Construct an EVP_PKEY for the public key.
286 crypto::ScopedRSA rsa_public_key(RSAPublicKey_dup(rsa_private_key.get())); 280 crypto::ScopedRSA rsa_public_key(RSAPublicKey_dup(rsa_private_key.get()));
287 crypto::ScopedEVP_PKEY public_pkey(EVP_PKEY_new()); 281 crypto::ScopedEVP_PKEY public_pkey(EVP_PKEY_new());
288 if (!public_pkey || 282 if (!public_pkey ||
289 !EVP_PKEY_set1_RSA(public_pkey.get(), rsa_public_key.get())) { 283 !EVP_PKEY_set1_RSA(public_pkey.get(), rsa_public_key.get())) {
290 return Status::OperationError(); 284 return Status::OperationError();
291 } 285 }
292 286
287 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
288 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
289
293 // Note that extractable is unconditionally set to true. This is because per 290 // Note that extractable is unconditionally set to true. This is because per
294 // the WebCrypto spec generated public keys are always public. 291 // the WebCrypto spec generated public keys are always public.
295 status = CreateWebCryptoPublicKey(public_pkey.Pass(), 292 status = CreateWebCryptoPublicKey(public_pkey.Pass(),
296 algorithm.id(), 293 algorithm.id(),
297 params->hash(), 294 params->hash(),
298 true, 295 true,
299 public_usage_mask, 296 public_usage_mask,
300 public_key); 297 &public_key);
301 if (status.IsError()) 298 if (status.IsError())
302 return status; 299 return status;
303 300
304 return CreateWebCryptoPrivateKey(private_pkey.Pass(), 301 status = CreateWebCryptoPrivateKey(private_pkey.Pass(),
305 algorithm.id(), 302 algorithm.id(),
306 params->hash(), 303 params->hash(),
307 extractable, 304 extractable,
308 private_usage_mask, 305 private_usage_mask,
309 private_key); 306 &private_key);
307 if (status.IsError())
308 return status;
309
310 result->AssignKeyPair(public_key, private_key);
311 return Status::Success();
310 } 312 }
311 313
312 Status RsaHashedAlgorithm::VerifyKeyUsagesBeforeImportKey( 314 Status RsaHashedAlgorithm::VerifyKeyUsagesBeforeImportKey(
313 blink::WebCryptoKeyFormat format, 315 blink::WebCryptoKeyFormat format,
314 blink::WebCryptoKeyUsageMask usage_mask) const { 316 blink::WebCryptoKeyUsageMask usage_mask) const {
315 switch (format) { 317 switch (format) {
316 case blink::WebCryptoKeyFormatSpki: 318 case blink::WebCryptoKeyFormatSpki:
317 return CheckKeyCreationUsages(all_public_key_usages_, usage_mask); 319 return CheckKeyCreationUsages(all_public_key_usages_, usage_mask);
318 case blink::WebCryptoKeyFormatPkcs8: 320 case blink::WebCryptoKeyFormatPkcs8:
319 return CheckKeyCreationUsages(all_private_key_usages_, usage_mask); 321 return CheckKeyCreationUsages(all_private_key_usages_, usage_mask);
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 return Status::Success(); 501 return Status::Success();
500 502
501 default: 503 default:
502 return Status::ErrorUnexpected(); 504 return Status::ErrorUnexpected();
503 } 505 }
504 } 506 }
505 507
506 } // namespace webcrypto 508 } // namespace webcrypto
507 509
508 } // namespace content 510 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webcrypto/openssl/rsa_key_openssl.h ('k') | content/child/webcrypto/openssl/sym_key_openssl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698