Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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_KEY_H_ |
| 18 | #define SYSTEM_KEYMASTER_KEY_H_ |
| 19 | |
Shawn Willden | b9d584d | 2015-01-22 16:35:00 -0700 | [diff] [blame] | 20 | #include <hardware/keymaster_defs.h> |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 21 | #include <keymaster/authorization_set.h> |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 22 | #include <keymaster/logger.h> |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 23 | |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 24 | #include "abstract_factory_registry.h" |
| 25 | #include "unencrypted_key_blob.h" |
| 26 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 27 | namespace keymaster { |
| 28 | |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 29 | class Key; |
| 30 | |
| 31 | /** |
| 32 | * KeyFactory is a pure interface whose subclasses know how to construct a specific subclass of Key. |
| 33 | * There is a one to one correspondence between Key subclasses and KeyFactory subclasses. |
| 34 | */ |
| 35 | class KeyFactory { |
| 36 | public: |
| 37 | virtual ~KeyFactory() {} |
| 38 | |
| 39 | // Required for registry |
| 40 | typedef keymaster_algorithm_t KeyType; |
| 41 | virtual keymaster_algorithm_t registry_key() const = 0; |
| 42 | |
| 43 | // Factory methods. |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 44 | virtual Key* GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error) = 0; |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 45 | virtual Key* ImportKey(const AuthorizationSet& key_description, |
| 46 | keymaster_key_format_t key_format, const uint8_t* key_data, |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 47 | size_t key_data_length, keymaster_error_t* error) = 0; |
| 48 | virtual Key* LoadKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) = 0; |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 49 | |
| 50 | // Informational methods. |
| 51 | virtual const keymaster_key_format_t* SupportedImportFormats(size_t* format_count) = 0; |
| 52 | virtual const keymaster_key_format_t* SupportedExportFormats(size_t* format_count) = 0; |
| 53 | }; |
| 54 | |
| 55 | typedef AbstractFactoryRegistry<KeyFactory> KeyFactoryRegistry; |
| 56 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 57 | class KeyBlob; |
| 58 | class Operation; |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 59 | class UnencryptedKeyBlob; |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 60 | |
| 61 | class Key { |
| 62 | public: |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 63 | virtual ~Key() {} |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Return a copy of raw key material, in the key's preferred binary format. |
| 67 | */ |
| 68 | virtual keymaster_error_t key_material(UniquePtr<uint8_t[]>*, size_t* size) const = 0; |
| 69 | |
| 70 | /** |
| 71 | * Return a copy of raw key material, in the specified format. |
| 72 | */ |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 73 | virtual keymaster_error_t formatted_key_material(keymaster_key_format_t format, |
| 74 | UniquePtr<uint8_t[]>* material, |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 75 | size_t* size) const = 0; |
| 76 | |
| 77 | const AuthorizationSet& authorizations() const { return authorizations_; } |
| 78 | |
| 79 | protected: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 80 | Key(const KeyBlob& blob); |
| 81 | Key(const AuthorizationSet& authorizations) : authorizations_(authorizations) {} |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | AuthorizationSet authorizations_; |
| 85 | }; |
| 86 | |
| 87 | } // namespace keymaster |
| 88 | |
| 89 | #endif // SYSTEM_KEYMASTER_KEY_H_ |