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 | |
T.J. Mercier | aed61b1 | 2022-07-26 23:20:07 +0000 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Janis Danisevskis | eba6c56 | 2017-02-10 14:49:53 +0000 | [diff] [blame] | 22 | #include <assert.h> |
Shawn Willden | 0629810 | 2015-05-25 23:12:48 -0600 | [diff] [blame] | 23 | |
Shawn Willden | b9d584d | 2015-01-22 16:35:00 -0700 | [diff] [blame] | 24 | #include <hardware/keymaster_defs.h> |
Janis Danisevskis | eba6c56 | 2017-02-10 14:49:53 +0000 | [diff] [blame] | 25 | #include <keymaster/UniquePtr.h> |
Janis Danisevskis | 59c6af8 | 2017-05-31 15:05:53 -0700 | [diff] [blame] | 26 | #include <keymaster/android_keymaster_utils.h> |
Shawn Willden | deffcb7 | 2018-01-07 23:34:58 -0700 | [diff] [blame] | 27 | #include <keymaster/authorization_set.h> |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 28 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 29 | namespace keymaster { |
| 30 | |
Janis Danisevskis | 59c6af8 | 2017-05-31 15:05:53 -0700 | [diff] [blame] | 31 | class KeyFactory; |
| 32 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 33 | class Key { |
| 34 | public: |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 35 | virtual ~Key() {} |
Shawn Willden | deffcb7 | 2018-01-07 23:34:58 -0700 | [diff] [blame] | 36 | Key(const Key&) = delete; |
| 37 | void operator=(const Key&) = delete; |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 38 | |
| 39 | /** |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 40 | * Return a copy of raw key material, in the specified format. |
| 41 | */ |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 42 | virtual keymaster_error_t formatted_key_material(keymaster_key_format_t format, |
| 43 | UniquePtr<uint8_t[]>* material, |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 44 | size_t* size) const = 0; |
| 45 | |
Shawn Willden | deffcb7 | 2018-01-07 23:34:58 -0700 | [diff] [blame] | 46 | AuthProxy authorizations() const { return AuthProxy(hw_enforced_, sw_enforced_); } |
Janis Danisevskis | 59c6af8 | 2017-05-31 15:05:53 -0700 | [diff] [blame] | 47 | const AuthorizationSet& hw_enforced() const { return hw_enforced_; } |
| 48 | const AuthorizationSet& sw_enforced() const { return sw_enforced_; } |
| 49 | AuthorizationSet& hw_enforced() { return hw_enforced_; } |
| 50 | AuthorizationSet& sw_enforced() { return sw_enforced_; } |
| 51 | |
| 52 | const KeymasterKeyBlob& key_material() const { return key_material_; } |
| 53 | KeymasterKeyBlob& key_material() { return key_material_; } |
| 54 | |
Shawn Willden | deffcb7 | 2018-01-07 23:34:58 -0700 | [diff] [blame] | 55 | // Methods to move data out of the key. These could be overloads of the methods above, with ref |
| 56 | // qualifiers, but naming them differently makes it harder to accidentally make a temporary copy |
| 57 | // when we mean to move. |
T.J. Mercier | aed61b1 | 2022-07-26 23:20:07 +0000 | [diff] [blame] | 58 | AuthorizationSet&& hw_enforced_move() { return std::move(hw_enforced_); } |
| 59 | AuthorizationSet&& sw_enforced_move() { return std::move(sw_enforced_); } |
| 60 | KeymasterKeyBlob&& key_material_move() { return std::move(key_material_); } |
Shawn Willden | deffcb7 | 2018-01-07 23:34:58 -0700 | [diff] [blame] | 61 | |
Janis Danisevskis | 59c6af8 | 2017-05-31 15:05:53 -0700 | [diff] [blame] | 62 | const KeyFactory* key_factory() const { return key_factory_; } |
| 63 | const KeyFactory*& key_factory() { return key_factory_; } |
Shawn Willden | deffcb7 | 2018-01-07 23:34:58 -0700 | [diff] [blame] | 64 | |
Shawn Willden | 2865c25 | 2021-07-31 15:34:12 -0600 | [diff] [blame] | 65 | void set_secure_deletion_slot(uint32_t slot) { secure_deletion_slot_ = slot; } |
| 66 | uint32_t secure_deletion_slot() const { return secure_deletion_slot_; } |
| 67 | |
Janis Danisevskis | 59c6af8 | 2017-05-31 15:05:53 -0700 | [diff] [blame] | 68 | protected: |
| 69 | Key(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced, |
| 70 | const KeyFactory* key_factory) |
T.J. Mercier | aed61b1 | 2022-07-26 23:20:07 +0000 | [diff] [blame] | 71 | : hw_enforced_(std::move(hw_enforced)), sw_enforced_(std::move(sw_enforced)), |
Shawn Willden | deffcb7 | 2018-01-07 23:34:58 -0700 | [diff] [blame] | 72 | key_factory_(key_factory) {} |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 73 | |
| 74 | protected: |
Janis Danisevskis | 59c6af8 | 2017-05-31 15:05:53 -0700 | [diff] [blame] | 75 | AuthorizationSet hw_enforced_; |
| 76 | AuthorizationSet sw_enforced_; |
| 77 | KeymasterKeyBlob key_material_; |
| 78 | const KeyFactory* key_factory_; |
Shawn Willden | 2865c25 | 2021-07-31 15:34:12 -0600 | [diff] [blame] | 79 | uint32_t secure_deletion_slot_ = 0; |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } // namespace keymaster |
| 83 | |
| 84 | #endif // SYSTEM_KEYMASTER_KEY_H_ |