| OLD | NEW |
| 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 "components/webcrypto/algorithms/ec.h" | 5 #include "components/webcrypto/algorithms/ec.h" |
| 6 | 6 |
| 7 #include <openssl/ec.h> | 7 #include <openssl/ec.h> |
| 8 #include <openssl/ec_key.h> | 8 #include <openssl/ec_key.h> |
| 9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 283 |
| 284 status = CreateWebCryptoPrivateKey(std::move(private_pkey), key_algorithm, | 284 status = CreateWebCryptoPrivateKey(std::move(private_pkey), key_algorithm, |
| 285 extractable, private_usages, &private_key); | 285 extractable, private_usages, &private_key); |
| 286 if (status.IsError()) | 286 if (status.IsError()) |
| 287 return status; | 287 return status; |
| 288 | 288 |
| 289 result->AssignKeyPair(public_key, private_key); | 289 result->AssignKeyPair(public_key, private_key); |
| 290 return Status::Success(); | 290 return Status::Success(); |
| 291 } | 291 } |
| 292 | 292 |
| 293 Status EcAlgorithm::VerifyKeyUsagesBeforeImportKey( | 293 Status EcAlgorithm::ImportKey(blink::WebCryptoKeyFormat format, |
| 294 blink::WebCryptoKeyFormat format, | 294 const CryptoData& key_data, |
| 295 blink::WebCryptoKeyUsageMask usages) const { | 295 const blink::WebCryptoAlgorithm& algorithm, |
| 296 return VerifyUsagesBeforeImportAsymmetricKey(format, all_public_key_usages_, | 296 bool extractable, |
| 297 all_private_key_usages_, usages); | 297 blink::WebCryptoKeyUsageMask usages, |
| 298 blink::WebCryptoKey* key) const { |
| 299 switch (format) { |
| 300 case blink::WebCryptoKeyFormatPkcs8: |
| 301 return ImportKeyPkcs8(key_data, algorithm, extractable, usages, key); |
| 302 case blink::WebCryptoKeyFormatSpki: |
| 303 return ImportKeySpki(key_data, algorithm, extractable, usages, key); |
| 304 case blink::WebCryptoKeyFormatJwk: |
| 305 return ImportKeyJwk(key_data, algorithm, extractable, usages, key); |
| 306 default: |
| 307 return Status::ErrorUnsupportedImportKeyFormat(); |
| 308 } |
| 309 } |
| 310 |
| 311 Status EcAlgorithm::ExportKey(blink::WebCryptoKeyFormat format, |
| 312 const blink::WebCryptoKey& key, |
| 313 std::vector<uint8_t>* buffer) const { |
| 314 switch (format) { |
| 315 case blink::WebCryptoKeyFormatPkcs8: |
| 316 return ExportKeyPkcs8(key, buffer); |
| 317 case blink::WebCryptoKeyFormatSpki: |
| 318 return ExportKeySpki(key, buffer); |
| 319 case blink::WebCryptoKeyFormatJwk: |
| 320 return ExportKeyJwk(key, buffer); |
| 321 default: |
| 322 return Status::ErrorUnsupportedExportKeyFormat(); |
| 323 } |
| 298 } | 324 } |
| 299 | 325 |
| 300 Status EcAlgorithm::ImportKeyPkcs8(const CryptoData& key_data, | 326 Status EcAlgorithm::ImportKeyPkcs8(const CryptoData& key_data, |
| 301 const blink::WebCryptoAlgorithm& algorithm, | 327 const blink::WebCryptoAlgorithm& algorithm, |
| 302 bool extractable, | 328 bool extractable, |
| 303 blink::WebCryptoKeyUsageMask usages, | 329 blink::WebCryptoKeyUsageMask usages, |
| 304 blink::WebCryptoKey* key) const { | 330 blink::WebCryptoKey* key) const { |
| 305 crypto::ScopedEVP_PKEY private_key; | 331 Status status = CheckKeyCreationUsages(all_private_key_usages_, usages); |
| 306 Status status = | |
| 307 ImportUnverifiedPkeyFromPkcs8(key_data, EVP_PKEY_EC, &private_key); | |
| 308 if (status.IsError()) | 332 if (status.IsError()) |
| 309 return status; | 333 return status; |
| 310 | 334 |
| 335 crypto::ScopedEVP_PKEY private_key; |
| 336 status = ImportUnverifiedPkeyFromPkcs8(key_data, EVP_PKEY_EC, &private_key); |
| 337 if (status.IsError()) |
| 338 return status; |
| 339 |
| 311 const blink::WebCryptoEcKeyImportParams* params = | 340 const blink::WebCryptoEcKeyImportParams* params = |
| 312 algorithm.ecKeyImportParams(); | 341 algorithm.ecKeyImportParams(); |
| 313 | 342 |
| 314 status = VerifyEcKeyAfterSpkiOrPkcs8Import(private_key.get(), | 343 status = VerifyEcKeyAfterSpkiOrPkcs8Import(private_key.get(), |
| 315 params->namedCurve()); | 344 params->namedCurve()); |
| 316 if (status.IsError()) | 345 if (status.IsError()) |
| 317 return status; | 346 return status; |
| 318 | 347 |
| 319 return CreateWebCryptoPrivateKey(std::move(private_key), | 348 return CreateWebCryptoPrivateKey(std::move(private_key), |
| 320 blink::WebCryptoKeyAlgorithm::createEc( | 349 blink::WebCryptoKeyAlgorithm::createEc( |
| 321 algorithm.id(), params->namedCurve()), | 350 algorithm.id(), params->namedCurve()), |
| 322 extractable, usages, key); | 351 extractable, usages, key); |
| 323 } | 352 } |
| 324 | 353 |
| 325 Status EcAlgorithm::ImportKeySpki(const CryptoData& key_data, | 354 Status EcAlgorithm::ImportKeySpki(const CryptoData& key_data, |
| 326 const blink::WebCryptoAlgorithm& algorithm, | 355 const blink::WebCryptoAlgorithm& algorithm, |
| 327 bool extractable, | 356 bool extractable, |
| 328 blink::WebCryptoKeyUsageMask usages, | 357 blink::WebCryptoKeyUsageMask usages, |
| 329 blink::WebCryptoKey* key) const { | 358 blink::WebCryptoKey* key) const { |
| 330 crypto::ScopedEVP_PKEY public_key; | 359 Status status = CheckKeyCreationUsages(all_public_key_usages_, usages); |
| 331 Status status = | |
| 332 ImportUnverifiedPkeyFromSpki(key_data, EVP_PKEY_EC, &public_key); | |
| 333 if (status.IsError()) | 360 if (status.IsError()) |
| 334 return status; | 361 return status; |
| 335 | 362 |
| 363 crypto::ScopedEVP_PKEY public_key; |
| 364 status = ImportUnverifiedPkeyFromSpki(key_data, EVP_PKEY_EC, &public_key); |
| 365 if (status.IsError()) |
| 366 return status; |
| 367 |
| 336 const blink::WebCryptoEcKeyImportParams* params = | 368 const blink::WebCryptoEcKeyImportParams* params = |
| 337 algorithm.ecKeyImportParams(); | 369 algorithm.ecKeyImportParams(); |
| 338 | 370 |
| 339 status = | 371 status = |
| 340 VerifyEcKeyAfterSpkiOrPkcs8Import(public_key.get(), params->namedCurve()); | 372 VerifyEcKeyAfterSpkiOrPkcs8Import(public_key.get(), params->namedCurve()); |
| 341 if (status.IsError()) | 373 if (status.IsError()) |
| 342 return status; | 374 return status; |
| 343 | 375 |
| 344 return CreateWebCryptoPublicKey(std::move(public_key), | 376 return CreateWebCryptoPublicKey(std::move(public_key), |
| 345 blink::WebCryptoKeyAlgorithm::createEc( | 377 blink::WebCryptoKeyAlgorithm::createEc( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 return status; | 413 return status; |
| 382 if (jwk_crv != params->namedCurve()) | 414 if (jwk_crv != params->namedCurve()) |
| 383 return Status::ErrorJwkIncorrectCrv(); | 415 return Status::ErrorJwkIncorrectCrv(); |
| 384 | 416 |
| 385 // Only private keys have a "d" parameter. The key may still be invalid, but | 417 // Only private keys have a "d" parameter. The key may still be invalid, but |
| 386 // tentatively decide if it is a public or private key. | 418 // tentatively decide if it is a public or private key. |
| 387 bool is_private_key = jwk.HasMember("d"); | 419 bool is_private_key = jwk.HasMember("d"); |
| 388 | 420 |
| 389 // Now that the key type is known, verify the usages. | 421 // Now that the key type is known, verify the usages. |
| 390 if (is_private_key) { | 422 if (is_private_key) { |
| 391 status = CheckPrivateKeyCreationUsages(all_private_key_usages_, usages); | 423 status = CheckKeyCreationUsages(all_private_key_usages_, usages); |
| 392 } else { | 424 } else { |
| 393 status = CheckPublicKeyCreationUsages(all_public_key_usages_, usages); | 425 status = CheckKeyCreationUsages(all_public_key_usages_, usages); |
| 394 } | 426 } |
| 395 | 427 |
| 396 if (status.IsError()) | 428 if (status.IsError()) |
| 397 return status; | 429 return status; |
| 398 | 430 |
| 399 // Create an EC_KEY. | 431 // Create an EC_KEY. |
| 400 crypto::ScopedEC_KEY ec; | 432 crypto::ScopedEC_KEY ec; |
| 401 status = CreateEC_KEY(params->namedCurve(), &ec); | 433 status = CreateEC_KEY(params->namedCurve(), &ec); |
| 402 if (status.IsError()) | 434 if (status.IsError()) |
| 403 return status; | 435 return status; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 | 600 |
| 569 if (algorithm.ecParams()->namedCurve() != | 601 if (algorithm.ecParams()->namedCurve() != |
| 570 key->algorithm().ecParams()->namedCurve()) { | 602 key->algorithm().ecParams()->namedCurve()) { |
| 571 return Status::ErrorUnexpected(); | 603 return Status::ErrorUnexpected(); |
| 572 } | 604 } |
| 573 | 605 |
| 574 return Status::Success(); | 606 return Status::Success(); |
| 575 } | 607 } |
| 576 | 608 |
| 577 } // namespace webcrypto | 609 } // namespace webcrypto |
| OLD | NEW |