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