Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_VOLD_KEYMASTER_H |
| 18 | #define ANDROID_VOLD_KEYMASTER_H |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 19 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 20 | #include "KeyBuffer.h" |
| 21 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 22 | #include <memory> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 23 | #include <string> |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 24 | #include <utility> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 25 | |
Steven Moreland | 25e8b4b | 2017-05-01 12:45:32 -0700 | [diff] [blame] | 26 | #include <android-base/macros.h> |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 27 | #include <keymasterV4_0/Keymaster.h> |
| 28 | #include <keymasterV4_0/authorization_set.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace vold { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 32 | |
| 33 | namespace km = ::android::hardware::keymaster::V4_0; |
| 34 | using KmDevice = km::support::Keymaster; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 35 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 36 | // C++ wrappers to the Keymaster hidl interface. |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 37 | // This is tailored to the needs of KeyStorage, but could be extended to be |
| 38 | // a more general interface. |
| 39 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 40 | // Wrapper for a Keymaster operation handle representing an |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 41 | // ongoing Keymaster operation. Aborts the operation |
| 42 | // in the destructor if it is unfinished. Methods log failures |
| 43 | // to LOG(ERROR). |
| 44 | class KeymasterOperation { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 45 | public: |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 46 | ~KeymasterOperation(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 47 | // Is this instance valid? This is false if creation fails, and becomes |
| 48 | // false on finish or if an update fails. |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 49 | explicit operator bool() { return mError == km::ErrorCode::OK; } |
| 50 | km::ErrorCode errorCode() { return mError; } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 51 | // Call "update" repeatedly until all of the input is consumed, and |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 52 | // concatenate the output. Return true on success. |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 53 | template <class TI, class TO> |
| 54 | bool updateCompletely(TI& input, TO* output) { |
| 55 | if (output) output->clear(); |
| 56 | return updateCompletely(input.data(), input.size(), [&](const char* b, size_t n) { |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 57 | if (output) std::copy(b, b + n, std::back_inserter(*output)); |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 58 | }); |
| 59 | } |
| 60 | |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 61 | // Finish and write the output to this string, unless pointer is null. |
| 62 | bool finish(std::string* output); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 63 | // Move constructor |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 64 | KeymasterOperation(KeymasterOperation&& rhs) { *this = std::move(rhs); } |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 65 | // Construct an object in an error state for error returns |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 66 | KeymasterOperation() : mDevice{nullptr}, mOpHandle{0}, mError{km::ErrorCode::UNKNOWN_ERROR} {} |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 67 | // Move Assignment |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 68 | KeymasterOperation& operator=(KeymasterOperation&& rhs) { |
Shawn Willden | 3e02df8 | 2018-02-07 15:06:06 -0700 | [diff] [blame^] | 69 | mDevice = rhs.mDevice; |
| 70 | rhs.mDevice = nullptr; |
| 71 | |
| 72 | mOpHandle = rhs.mOpHandle; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 73 | rhs.mOpHandle = 0; |
Shawn Willden | 3e02df8 | 2018-02-07 15:06:06 -0700 | [diff] [blame^] | 74 | |
| 75 | mError = rhs.mError; |
| 76 | rhs.mError = km::ErrorCode::UNKNOWN_ERROR; |
| 77 | |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 78 | return *this; |
| 79 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 80 | |
| 81 | private: |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 82 | KeymasterOperation(KmDevice* d, uint64_t h) |
| 83 | : mDevice{d}, mOpHandle{h}, mError{km::ErrorCode::OK} {} |
| 84 | KeymasterOperation(km::ErrorCode error) : mDevice{nullptr}, mOpHandle{0}, mError{error} {} |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 85 | |
| 86 | bool updateCompletely(const char* input, size_t inputLen, |
| 87 | const std::function<void(const char*, size_t)> consumer); |
| 88 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 89 | KmDevice* mDevice; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 90 | uint64_t mOpHandle; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 91 | km::ErrorCode mError; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 92 | DISALLOW_COPY_AND_ASSIGN(KeymasterOperation); |
| 93 | friend class Keymaster; |
| 94 | }; |
| 95 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 96 | // Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not |
| 97 | // part of one. |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 98 | class Keymaster { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 99 | public: |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 100 | Keymaster(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 101 | // false if we failed to open the keymaster device. |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 102 | explicit operator bool() { return mDevice.get() != nullptr; } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 103 | // Generate a key in the keymaster from the given params. |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 104 | bool generateKey(const km::AuthorizationSet& inParams, std::string* key); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 105 | // If the keymaster supports it, permanently delete a key. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 106 | bool deleteKey(const std::string& key); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 107 | // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE. |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 108 | bool upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams, |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 109 | std::string* newKey); |
| 110 | // Begin a new cryptographic operation, collecting output parameters if pointer is non-null |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 111 | KeymasterOperation begin(km::KeyPurpose purpose, const std::string& key, |
| 112 | const km::AuthorizationSet& inParams, |
| 113 | const km::HardwareAuthToken& authToken, |
| 114 | km::AuthorizationSet* outParams); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 115 | bool isSecure(); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 116 | |
| 117 | private: |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 118 | std::unique_ptr<KmDevice> mDevice; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(Keymaster); |
| 120 | }; |
| 121 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 122 | } // namespace vold |
| 123 | } // namespace android |
| 124 | |
Paul Crowley | 3b71fc5 | 2017-10-09 10:55:21 -0700 | [diff] [blame] | 125 | // FIXME no longer needed now cryptfs is in C++. |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * The following functions provide C bindings to keymaster services |
| 129 | * needed by cryptfs scrypt. The compatibility check checks whether |
| 130 | * the keymaster implementation is considered secure, i.e., TEE backed. |
| 131 | * The create_key function generates an RSA key for signing. |
| 132 | * The sign_object function signes an object with the given keymaster |
| 133 | * key. |
| 134 | */ |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 135 | |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 136 | /* Return values for keymaster_sign_object_for_cryptfs_scrypt */ |
| 137 | |
| 138 | enum class KeymasterSignResult { |
| 139 | ok = 0, |
| 140 | error = -1, |
| 141 | upgrade = -2, |
| 142 | }; |
| 143 | |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 144 | int keymaster_compatibility_cryptfs_scrypt(); |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 145 | int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 146 | uint32_t ratelimit, uint8_t* key_buffer, |
| 147 | uint32_t key_buffer_size, uint32_t* key_out_size); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 148 | |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 149 | int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 150 | uint32_t ratelimit, const uint8_t* key_blob, |
| 151 | size_t key_blob_size, uint8_t* key_buffer, |
| 152 | uint32_t key_buffer_size, uint32_t* key_out_size); |
| 153 | |
| 154 | KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt( |
| 155 | const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object, |
| 156 | const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 157 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 158 | #endif |