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 | |
| 17 | #include "Keymaster.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
Shawn Willden | 35f0f22 | 2020-01-16 13:21:42 -0700 | [diff] [blame] | 20 | #include <keymasterV4_1/authorization_set.h> |
| 21 | #include <keymasterV4_1/keymaster_utils.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace vold { |
| 25 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 26 | using ::android::hardware::hidl_string; |
| 27 | using ::android::hardware::hidl_vec; |
Shawn Willden | c1903ad | 2018-03-30 17:06:14 -0600 | [diff] [blame] | 28 | using ::android::hardware::keymaster::V4_0::SecurityLevel; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 29 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 30 | KeymasterOperation::~KeymasterOperation() { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 31 | if (mDevice) mDevice->abort(mOpHandle); |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 34 | bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 35 | const std::function<void(const char*, size_t)> consumer) { |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 36 | uint32_t inputConsumed = 0; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 37 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 38 | km::ErrorCode km_error; |
| 39 | auto hidlCB = [&](km::ErrorCode ret, uint32_t inputConsumedDelta, |
| 40 | const hidl_vec<km::KeyParameter>& /*ignored*/, |
| 41 | const hidl_vec<uint8_t>& _output) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 42 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 43 | if (km_error != km::ErrorCode::OK) return; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 44 | inputConsumed += inputConsumedDelta; |
| 45 | consumer(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 48 | while (inputConsumed != inputLen) { |
| 49 | size_t toRead = static_cast<size_t>(inputLen - inputConsumed); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 50 | auto inputBlob = km::support::blob2hidlVec( |
| 51 | reinterpret_cast<const uint8_t*>(&input[inputConsumed]), toRead); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 52 | auto error = mDevice->update(mOpHandle, hidl_vec<km::KeyParameter>(), inputBlob, |
| 53 | km::HardwareAuthToken(), km::VerificationToken(), hidlCB); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 54 | if (!error.isOk()) { |
| 55 | LOG(ERROR) << "update failed: " << error.description(); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 56 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 59 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 60 | LOG(ERROR) << "update failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 61 | mDevice = nullptr; |
| 62 | return false; |
| 63 | } |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 64 | if (inputConsumed > inputLen) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 65 | LOG(ERROR) << "update reported too much input consumed"; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 66 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 73 | bool KeymasterOperation::finish(std::string* output) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 74 | km::ErrorCode km_error; |
| 75 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& /*ignored*/, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 76 | const hidl_vec<uint8_t>& _output) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 77 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 78 | if (km_error != km::ErrorCode::OK) return; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 79 | if (output) output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 80 | }; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 81 | auto error = mDevice->finish(mOpHandle, hidl_vec<km::KeyParameter>(), hidl_vec<uint8_t>(), |
| 82 | hidl_vec<uint8_t>(), km::HardwareAuthToken(), |
| 83 | km::VerificationToken(), hidlCb); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 84 | mDevice = nullptr; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 85 | if (!error.isOk()) { |
| 86 | LOG(ERROR) << "finish failed: " << error.description(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 87 | return false; |
| 88 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 89 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 90 | LOG(ERROR) << "finish failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 91 | return false; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 92 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Shawn Willden | 2807536 | 2018-05-09 08:12:10 -0600 | [diff] [blame] | 96 | /* static */ bool Keymaster::hmacKeyGenerated = false; |
| 97 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 98 | Keymaster::Keymaster() { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 99 | auto devices = KmDevice::enumerateAvailableDevices(); |
Shawn Willden | 2807536 | 2018-05-09 08:12:10 -0600 | [diff] [blame] | 100 | if (!hmacKeyGenerated) { |
| 101 | KmDevice::performHmacKeyAgreement(devices); |
| 102 | hmacKeyGenerated = true; |
| 103 | } |
Shawn Willden | c1903ad | 2018-03-30 17:06:14 -0600 | [diff] [blame] | 104 | for (auto& dev : devices) { |
Shawn Willden | e763ed2 | 2018-05-17 15:24:56 -0600 | [diff] [blame] | 105 | // Do not use StrongBox for device encryption / credential encryption. If a security chip |
| 106 | // is present it will have Weaver, which already strengthens CE. We get no additional |
| 107 | // benefit from using StrongBox here, so skip it. |
Shawn Willden | c1903ad | 2018-03-30 17:06:14 -0600 | [diff] [blame] | 108 | if (dev->halVersion().securityLevel != SecurityLevel::STRONGBOX) { |
| 109 | mDevice = std::move(dev); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if (!mDevice) return; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 114 | auto& version = mDevice->halVersion(); |
| 115 | LOG(INFO) << "Using " << version.keymasterName << " from " << version.authorName |
| 116 | << " for encryption. Security level: " << toString(version.securityLevel) |
| 117 | << ", HAL: " << mDevice->descriptor() << "/" << mDevice->instanceName(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 120 | bool Keymaster::generateKey(const km::AuthorizationSet& inParams, std::string* key) { |
| 121 | km::ErrorCode km_error; |
| 122 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& keyBlob, |
| 123 | const km::KeyCharacteristics& /*ignored*/) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 124 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 125 | if (km_error != km::ErrorCode::OK) return; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 126 | if (key) key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb); |
| 130 | if (!error.isOk()) { |
| 131 | LOG(ERROR) << "generate_key failed: " << error.description(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 132 | return false; |
| 133 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 134 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 135 | LOG(ERROR) << "generate_key failed, code " << int32_t(km_error); |
| 136 | return false; |
| 137 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 141 | bool Keymaster::exportKey(const KeyBuffer& kmKey, std::string* key) { |
| 142 | auto kmKeyBlob = km::support::blob2hidlVec(std::string(kmKey.data(), kmKey.size())); |
| 143 | km::ErrorCode km_error; |
| 144 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& exportedKeyBlob) { |
| 145 | km_error = ret; |
| 146 | if (km_error != km::ErrorCode::OK) return; |
| 147 | if (key) |
| 148 | key->assign(reinterpret_cast<const char*>(&exportedKeyBlob[0]), exportedKeyBlob.size()); |
| 149 | }; |
| 150 | auto error = mDevice->exportKey(km::KeyFormat::RAW, kmKeyBlob, {}, {}, hidlCb); |
| 151 | if (!error.isOk()) { |
| 152 | LOG(ERROR) << "export_key failed: " << error.description(); |
| 153 | return false; |
| 154 | } |
| 155 | if (km_error != km::ErrorCode::OK) { |
| 156 | LOG(ERROR) << "export_key failed, code " << int32_t(km_error); |
| 157 | return false; |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 162 | bool Keymaster::deleteKey(const std::string& key) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 163 | auto keyBlob = km::support::blob2hidlVec(key); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 164 | auto error = mDevice->deleteKey(keyBlob); |
| 165 | if (!error.isOk()) { |
| 166 | LOG(ERROR) << "delete_key failed: " << error.description(); |
| 167 | return false; |
| 168 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 169 | if (error != km::ErrorCode::OK) { |
| 170 | LOG(ERROR) << "delete_key failed, code " << int32_t(km::ErrorCode(error)); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 176 | bool Keymaster::upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams, |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 177 | std::string* newKey) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 178 | auto oldKeyBlob = km::support::blob2hidlVec(oldKey); |
| 179 | km::ErrorCode km_error; |
| 180 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 181 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 182 | if (km_error != km::ErrorCode::OK) return; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 183 | if (newKey) |
| 184 | newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]), |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 185 | upgradedKeyBlob.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 186 | }; |
| 187 | auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb); |
| 188 | if (!error.isOk()) { |
| 189 | LOG(ERROR) << "upgrade_key failed: " << error.description(); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 190 | return false; |
| 191 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 192 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 193 | LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error); |
| 194 | return false; |
| 195 | } |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 199 | KeymasterOperation Keymaster::begin(km::KeyPurpose purpose, const std::string& key, |
| 200 | const km::AuthorizationSet& inParams, |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 201 | km::AuthorizationSet* outParams) { |
| 202 | auto keyBlob = km::support::blob2hidlVec(key); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 203 | uint64_t mOpHandle; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 204 | km::ErrorCode km_error; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 205 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 206 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& _outParams, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 207 | uint64_t operationHandle) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 208 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 209 | if (km_error != km::ErrorCode::OK) return; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 210 | if (outParams) *outParams = _outParams; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 211 | mOpHandle = operationHandle; |
| 212 | }; |
| 213 | |
Satya Tangirala | e136171 | 2021-03-15 15:33:08 -0700 | [diff] [blame^] | 214 | auto error = |
| 215 | mDevice->begin(purpose, keyBlob, inParams.hidl_data(), km::HardwareAuthToken(), hidlCb); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 216 | if (!error.isOk()) { |
| 217 | LOG(ERROR) << "begin failed: " << error.description(); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 218 | return KeymasterOperation(km::ErrorCode::UNKNOWN_ERROR); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 219 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 220 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 221 | LOG(ERROR) << "begin failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 222 | return KeymasterOperation(km_error); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 223 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 224 | return KeymasterOperation(mDevice.get(), mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 225 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 226 | |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 227 | bool Keymaster::isSecure() { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 228 | return mDevice->halVersion().securityLevel != km::SecurityLevel::SOFTWARE; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 229 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 230 | |
Shawn Willden | 2b1ff5a | 2020-01-16 14:08:36 -0700 | [diff] [blame] | 231 | void Keymaster::earlyBootEnded() { |
Shawn Willden | 28eddbd | 2020-04-01 10:02:16 -0600 | [diff] [blame] | 232 | auto devices = KmDevice::enumerateAvailableDevices(); |
| 233 | for (auto& dev : devices) { |
| 234 | auto error = dev->earlyBootEnded(); |
| 235 | if (!error.isOk()) { |
| 236 | LOG(ERROR) << "earlyBootEnded call failed: " << error.description() << " for " |
| 237 | << dev->halVersion().keymasterName; |
| 238 | } |
| 239 | km::V4_1_ErrorCode km_error = error; |
| 240 | if (km_error != km::V4_1_ErrorCode::OK && km_error != km::V4_1_ErrorCode::UNIMPLEMENTED) { |
| 241 | LOG(ERROR) << "Error reporting early boot ending to keymaster: " |
| 242 | << static_cast<int32_t>(km_error) << " for " |
| 243 | << dev->halVersion().keymasterName; |
| 244 | } |
Shawn Willden | 2b1ff5a | 2020-01-16 14:08:36 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 248 | } // namespace vold |
| 249 | } // namespace android |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 250 | |
| 251 | using namespace ::android::vold; |
| 252 | |
| 253 | int keymaster_compatibility_cryptfs_scrypt() { |
Janis Danisevskis | e7152c3 | 2017-03-08 11:02:30 -0800 | [diff] [blame] | 254 | Keymaster dev; |
| 255 | if (!dev) { |
| 256 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 257 | return -1; |
| 258 | } |
| 259 | return dev.isSecure(); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 262 | static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size, |
| 263 | uint32_t* out_size) { |
| 264 | if (!buffer || !out_size) { |
| 265 | LOG(ERROR) << "Missing target pointers"; |
| 266 | return false; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 267 | } |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 268 | *out_size = towrite.size(); |
| 269 | if (buffer_size < towrite.size()) { |
| 270 | LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size(); |
| 271 | return false; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 272 | } |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 273 | memset(buffer, '\0', buffer_size); |
| 274 | std::copy(towrite.begin(), towrite.end(), buffer); |
| 275 | return true; |
| 276 | } |
| 277 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 278 | static km::AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 279 | uint32_t ratelimit) { |
| 280 | return km::AuthorizationSetBuilder() |
| 281 | .RsaSigningKey(rsa_key_size, rsa_exponent) |
| 282 | .NoDigestOrPadding() |
| 283 | .Authorization(km::TAG_BLOB_USAGE_REQUIREMENTS, km::KeyBlobUsageRequirements::STANDALONE) |
| 284 | .Authorization(km::TAG_NO_AUTH_REQUIRED) |
| 285 | .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 289 | uint32_t ratelimit, uint8_t* key_buffer, |
| 290 | uint32_t key_buffer_size, uint32_t* key_out_size) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 291 | if (key_out_size) { |
| 292 | *key_out_size = 0; |
| 293 | } |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 294 | Keymaster dev; |
| 295 | if (!dev) { |
| 296 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 297 | return -1; |
| 298 | } |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 299 | std::string key; |
| 300 | if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1; |
| 301 | if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 306 | uint32_t ratelimit, const uint8_t* key_blob, |
| 307 | size_t key_blob_size, uint8_t* key_buffer, |
| 308 | uint32_t key_buffer_size, uint32_t* key_out_size) { |
| 309 | if (key_out_size) { |
| 310 | *key_out_size = 0; |
| 311 | } |
| 312 | Keymaster dev; |
| 313 | if (!dev) { |
| 314 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 315 | return -1; |
| 316 | } |
| 317 | std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 318 | std::string new_key; |
| 319 | if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key)) |
| 320 | return -1; |
| 321 | if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1; |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt( |
| 326 | const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object, |
| 327 | const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) { |
| 328 | Keymaster dev; |
| 329 | if (!dev) { |
| 330 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 331 | return KeymasterSignResult::error; |
| 332 | } |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 333 | if (!key_blob || !object || !signature_buffer || !signature_buffer_size) { |
| 334 | LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument"; |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 335 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 338 | km::AuthorizationSet outParams; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 339 | std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 340 | std::string input(reinterpret_cast<const char*>(object), object_size); |
| 341 | std::string output; |
| 342 | KeymasterOperation op; |
| 343 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 344 | auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding(); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 345 | while (true) { |
Satya Tangirala | e136171 | 2021-03-15 15:33:08 -0700 | [diff] [blame^] | 346 | op = dev.begin(km::KeyPurpose::SIGN, key, paramBuilder, &outParams); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 347 | if (op.errorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 348 | sleep(ratelimit); |
| 349 | continue; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 350 | } else |
| 351 | break; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 354 | if (op.errorCode() == km::ErrorCode::KEY_REQUIRES_UPGRADE) { |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 355 | LOG(ERROR) << "Keymaster key requires upgrade"; |
| 356 | return KeymasterSignResult::upgrade; |
| 357 | } |
| 358 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 359 | if (op.errorCode() != km::ErrorCode::OK) { |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 360 | LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode()); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 361 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (!op.updateCompletely(input, &output)) { |
| 365 | LOG(ERROR) << "Error sending data to keymaster signature transaction: " |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 366 | << uint32_t(op.errorCode()); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 367 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | if (!op.finish(&output)) { |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 371 | LOG(ERROR) << "Error finalizing keymaster signature transaction: " |
| 372 | << int32_t(op.errorCode()); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 373 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size())); |
| 377 | if (*signature_buffer == nullptr) { |
| 378 | LOG(ERROR) << "Error allocation buffer for keymaster signature"; |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 379 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 380 | } |
| 381 | *signature_buffer_size = output.size(); |
| 382 | std::copy(output.data(), output.data() + output.size(), *signature_buffer); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 383 | return KeymasterSignResult::ok; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 384 | } |