eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 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 | |
eroman | ab1308a | 2015-10-01 19:38:04 | [diff] [blame] | 5 | #ifndef COMPONENTS_WEBCRYPTO_BLINK_KEY_HANDLE_H_ |
| 6 | #define COMPONENTS_WEBCRYPTO_BLINK_KEY_HANDLE_H_ |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 7 | |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <vector> |
| 11 | |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 12 | #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
tfarina | 29a3a174 | 2016-10-28 18:47:33 | [diff] [blame^] | 13 | #include "third_party/boringssl/src/include/openssl/base.h" |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 14 | |
eroman | ab1308a | 2015-10-01 19:38:04 | [diff] [blame] | 15 | // Blink keys (blink::WebCryptoKey) have an associated key handle |
| 16 | // (blink::WebCryptoKeyHandle) used to store custom data. This is where the |
| 17 | // underlying EVP_PKEY is stored for asymmetric keys, or an std::vector |
| 18 | // containing the bytes for symmetric keys. |
| 19 | // |
| 20 | // This file contains helpers for creating the key handles, and extracting |
| 21 | // properties from it. |
| 22 | |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 23 | namespace webcrypto { |
| 24 | |
| 25 | class CryptoData; |
| 26 | |
| 27 | // Returns a reference to the symmetric key data wrapped by the given Blink |
| 28 | // key. The returned reference is owned by |key|. This function must only be |
| 29 | // called on secret keys (HMAC, AES, etc). |
| 30 | const std::vector<uint8_t>& GetSymmetricKeyData(const blink::WebCryptoKey& key); |
| 31 | |
| 32 | // Returns the EVP_PKEY* wrapped by the given Blink key. The returned pointer |
| 33 | // is owned by |key|. This function must only be called on asymmetric keys |
| 34 | // (RSA, EC, etc). |
| 35 | EVP_PKEY* GetEVP_PKEY(const blink::WebCryptoKey& key); |
| 36 | |
| 37 | // Returns a reference to the serialized key data. This reference is owned by |
| 38 | // |key|. This function can be called for any key type. |
| 39 | const std::vector<uint8_t>& GetSerializedKeyData( |
| 40 | const blink::WebCryptoKey& key); |
| 41 | |
| 42 | // Creates a symmetric key handle that can be passed to Blink. The caller takes |
| 43 | // ownership of the returned pointer. |
| 44 | blink::WebCryptoKeyHandle* CreateSymmetricKeyHandle( |
| 45 | const CryptoData& key_bytes); |
| 46 | |
| 47 | // Creates an asymmetric key handle that can be passed to Blink. The caller |
| 48 | // takes |
| 49 | // ownership of the returned pointer. |
| 50 | // |
| 51 | // TODO(eroman): This should _move_ input serialized_key_data rather than |
| 52 | // create a copy, since all the callers are passing in vectors that are later |
| 53 | // thrown away anyway. |
| 54 | blink::WebCryptoKeyHandle* CreateAsymmetricKeyHandle( |
davidben | 7e772281 | 2016-10-11 00:29:06 | [diff] [blame] | 55 | bssl::UniquePtr<EVP_PKEY> pkey, |
eroman | d62cb47 | 2015-09-18 18:24:23 | [diff] [blame] | 56 | const std::vector<uint8_t>& serialized_key_data); |
| 57 | |
| 58 | } // namespace webcrypto |
| 59 | |
eroman | ab1308a | 2015-10-01 19:38:04 | [diff] [blame] | 60 | #endif // COMPONENTS_WEBCRYPTO_BLINK_KEY_HANDLE_H_ |