asymmetric_key_util.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_WEBCRYPTO_ALGORITHMS_ASYMMETRIC_KEY_UTIL_H_
  5. #define COMPONENTS_WEBCRYPTO_ALGORITHMS_ASYMMETRIC_KEY_UTIL_H_
  6. #include "base/containers/span.h"
  7. #include "third_party/blink/public/platform/web_crypto_algorithm.h"
  8. #include "third_party/blink/public/platform/web_crypto_key.h"
  9. #include "third_party/boringssl/src/include/openssl/base.h"
  10. // This file contains functions shared by multiple asymmetric key algorithms.
  11. namespace webcrypto {
  12. class Status;
  13. // Exports an EVP_PKEY public key to the SPKI format.
  14. Status ExportPKeySpki(EVP_PKEY* key, std::vector<uint8_t>* buffer);
  15. // Exports an EVP_PKEY private key to the PKCS8 format.
  16. Status ExportPKeyPkcs8(EVP_PKEY* key, std::vector<uint8_t>* buffer);
  17. // Creates a WebCrypto public key given an EVP_PKEY. This step includes
  18. // exporting the key to SPKI format, for use by serialization later.
  19. Status CreateWebCryptoPublicKey(bssl::UniquePtr<EVP_PKEY> public_key,
  20. const blink::WebCryptoKeyAlgorithm& algorithm,
  21. bool extractable,
  22. blink::WebCryptoKeyUsageMask usages,
  23. blink::WebCryptoKey* key);
  24. // Creates a WebCrypto private key given an EVP_PKEY. This step includes
  25. // exporting the key to PKCS8 format, for use by serialization later.
  26. Status CreateWebCryptoPrivateKey(bssl::UniquePtr<EVP_PKEY> private_key,
  27. const blink::WebCryptoKeyAlgorithm& algorithm,
  28. bool extractable,
  29. blink::WebCryptoKeyUsageMask usages,
  30. blink::WebCryptoKey* key);
  31. // Imports SPKI bytes to an EVP_PKEY for a public key. The resulting asymmetric
  32. // key may be invalid, and should be verified using something like
  33. // RSA_check_key(). The only validation performed by this function is to ensure
  34. // the key type matched |expected_pkey_id|.
  35. Status ImportUnverifiedPkeyFromSpki(base::span<const uint8_t> key_data,
  36. int expected_pkey_id,
  37. bssl::UniquePtr<EVP_PKEY>* pkey);
  38. // Imports PKCS8 bytes to an EVP_PKEY for a private key. The resulting
  39. // asymmetric key may be invalid, and should be verified using something like
  40. // RSA_check_key(). The only validation performed by this function is to ensure
  41. // the key type matched |expected_pkey_id|.
  42. Status ImportUnverifiedPkeyFromPkcs8(base::span<const uint8_t> key_data,
  43. int expected_pkey_id,
  44. bssl::UniquePtr<EVP_PKEY>* pkey);
  45. // Splits the combined usages given to GenerateKey() into the respective usages
  46. // for the public key and private key. Returns an error if the usages are
  47. // invalid.
  48. Status GetUsagesForGenerateAsymmetricKey(
  49. blink::WebCryptoKeyUsageMask combined_usages,
  50. blink::WebCryptoKeyUsageMask all_public_usages,
  51. blink::WebCryptoKeyUsageMask all_private_usages,
  52. blink::WebCryptoKeyUsageMask* public_usages,
  53. blink::WebCryptoKeyUsageMask* private_usages);
  54. } // namespace webcrypto
  55. #endif // COMPONENTS_WEBCRYPTO_ALGORITHMS_ASYMMETRIC_KEY_UTIL_H_