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