Shawn Willden | 2beb628 | 2015-05-20 16:36:24 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SYSTEM_KEYMASTER_KEYMASTER0_ENGINE_H_ |
| 18 | #define SYSTEM_KEYMASTER_KEYMASTER0_ENGINE_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | |
| 22 | #include <openssl/ec.h> |
| 23 | #include <openssl/engine.h> |
| 24 | #include <openssl/ex_data.h> |
| 25 | #include <openssl/rsa.h> |
| 26 | |
| 27 | #include <hardware/keymaster0.h> |
| 28 | #include <hardware/keymaster_defs.h> |
| 29 | |
| 30 | namespace keymaster { |
| 31 | |
| 32 | struct KeymasterKeyBlob; |
| 33 | |
| 34 | /* Keymaster0Engine is a BoringSSL ENGINE that implements RSA by forwarding the requested |
| 35 | * operations to a keymaster0 module. */ |
| 36 | class Keymaster0Engine { |
| 37 | public: |
| 38 | /** |
| 39 | * Create a Keymaster0Engine, wrapping the provided keymaster0_device. The engine takes |
| 40 | * ownership of the device, and will close it during destruction. |
| 41 | */ |
| 42 | Keymaster0Engine(const keymaster0_device_t* keymaster0_device); |
| 43 | ~Keymaster0Engine(); |
| 44 | |
| 45 | bool GenerateRsaKey(uint64_t public_exponent, uint32_t public_modulus, |
| 46 | KeymasterKeyBlob* key_material) const; |
| 47 | bool ImportRsaKey(keymaster_key_format_t key_format, const KeymasterKeyBlob& to_import, |
| 48 | KeymasterKeyBlob* imported_key_material) const; |
| 49 | |
| 50 | RSA* BlobToRsaKey(const KeymasterKeyBlob& blob) const; |
| 51 | |
| 52 | const keymaster_key_blob_t* rsa_get_blob(const RSA* rsa) const; |
| 53 | |
| 54 | EVP_PKEY* GetKeymaster0PublicKey(const KeymasterKeyBlob& blob) const; |
| 55 | |
| 56 | private: |
| 57 | Keymaster0Engine(const Keymaster0Engine&); // Uncopyable |
| 58 | void operator=(const Keymaster0Engine&); // Unassignable |
| 59 | |
| 60 | static int keyblob_dup(CRYPTO_EX_DATA* to, const CRYPTO_EX_DATA* from, void** from_d, int index, |
| 61 | long argl, void* argp); |
| 62 | static void keyblob_free(void* parent, void* ptr, CRYPTO_EX_DATA* data, int index, long argl, |
| 63 | void* argp); |
| 64 | static int rsa_private_transform(RSA* rsa, uint8_t* out, const uint8_t* in, size_t len); |
| 65 | |
| 66 | struct Malloc_Delete { |
| 67 | void operator()(void* p) { free(p); } |
| 68 | }; |
| 69 | |
| 70 | bool Keymaster0Sign(const void* signing_params, const keymaster_key_blob_t& key_blob, |
| 71 | const uint8_t* data, const size_t data_length, |
| 72 | std::unique_ptr<uint8_t[], Malloc_Delete>* signature, |
| 73 | size_t* signature_length) const; |
| 74 | |
| 75 | int RsaPrivateTransform(RSA* rsa, uint8_t* out, const uint8_t* in, size_t len) const; |
| 76 | |
| 77 | const keymaster0_device_t* keymaster0_device_; |
| 78 | ENGINE* const engine_; |
| 79 | int rsa_index_; |
| 80 | |
| 81 | static const RSA_METHOD rsa_method_; |
| 82 | static Keymaster0Engine* instance_; |
| 83 | }; |
| 84 | |
| 85 | } // namespace keymaster |
| 86 | |
| 87 | #endif // SYSTEM_KEYMASTER_KEYMASTER0_ENGINE_H_ |