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> |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 20 | #include <keystore/keymaster_tags.h> |
| 21 | #include <keystore/authorization_set.h> |
| 22 | #include <keystore/keystore_hidl_support.h> |
| 23 | |
| 24 | using namespace ::keystore; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace vold { |
| 28 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 29 | KeymasterOperation::~KeymasterOperation() { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 30 | if (mDevice.get()) mDevice->abort(mOpHandle); |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 33 | bool KeymasterOperation::updateCompletely(const std::string& input, std::string* output) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 34 | output->clear(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 35 | auto it = input.begin(); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 36 | uint32_t inputConsumed; |
| 37 | |
| 38 | ErrorCode km_error; |
| 39 | auto hidlCB = [&] (ErrorCode ret, uint32_t _inputConsumed, |
| 40 | const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) { |
| 41 | km_error = ret; |
| 42 | if (km_error != ErrorCode::OK) return; |
| 43 | inputConsumed = _inputConsumed; |
| 44 | if (output) |
| 45 | output->append(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
| 46 | }; |
| 47 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 48 | while (it != input.end()) { |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 49 | size_t toRead = static_cast<size_t>(input.end() - it); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 50 | auto inputBlob = blob2hidlVec(reinterpret_cast<const uint8_t*>(&*it), toRead); |
| 51 | auto error = mDevice->update(mOpHandle, hidl_vec<KeyParameter>(), inputBlob, hidlCB); |
| 52 | if (!error.isOk()) { |
| 53 | LOG(ERROR) << "update failed: " << error.description(); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 54 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 57 | if (km_error != ErrorCode::OK) { |
| 58 | LOG(ERROR) << "update failed, code " << uint32_t(km_error); |
| 59 | mDevice = nullptr; |
| 60 | return false; |
| 61 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 62 | if (inputConsumed > toRead) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 63 | LOG(ERROR) << "update reported too much input consumed"; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 64 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 67 | it += inputConsumed; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 72 | bool KeymasterOperation::finish(std::string* output) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 73 | ErrorCode km_error; |
| 74 | auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& /*ignored*/, |
| 75 | const hidl_vec<uint8_t>& _output) { |
| 76 | km_error = ret; |
| 77 | if (km_error != ErrorCode::OK) return; |
| 78 | if (output) |
| 79 | output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
| 80 | }; |
| 81 | auto error = mDevice->finish(mOpHandle, hidl_vec<KeyParameter>(), hidl_vec<uint8_t>(), |
| 82 | hidl_vec<uint8_t>(), hidlCb); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 83 | mDevice = nullptr; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 84 | if (!error.isOk()) { |
| 85 | LOG(ERROR) << "finish failed: " << error.description(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 86 | return false; |
| 87 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 88 | if (km_error != ErrorCode::OK) { |
| 89 | LOG(ERROR) << "finish failed, code " << uint32_t(km_error); |
| 90 | return false; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 91 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | Keymaster::Keymaster() { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 96 | mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService("keymaster"); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 99 | bool Keymaster::generateKey(const AuthorizationSet& inParams, std::string* key) { |
| 100 | ErrorCode km_error; |
| 101 | auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& keyBlob, |
| 102 | const KeyCharacteristics& /*ignored*/) { |
| 103 | km_error = ret; |
| 104 | if (km_error != ErrorCode::OK) return; |
| 105 | if (key) |
| 106 | key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size()); |
| 107 | }; |
| 108 | |
| 109 | auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb); |
| 110 | if (!error.isOk()) { |
| 111 | LOG(ERROR) << "generate_key failed: " << error.description(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 112 | return false; |
| 113 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 114 | if (km_error != ErrorCode::OK) { |
| 115 | LOG(ERROR) << "generate_key failed, code " << int32_t(km_error); |
| 116 | return false; |
| 117 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 121 | bool Keymaster::deleteKey(const std::string& key) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 122 | auto keyBlob = blob2hidlVec(key); |
| 123 | auto error = mDevice->deleteKey(keyBlob); |
| 124 | if (!error.isOk()) { |
| 125 | LOG(ERROR) << "delete_key failed: " << error.description(); |
| 126 | return false; |
| 127 | } |
| 128 | if (ErrorCode(error) != ErrorCode::OK) { |
| 129 | LOG(ERROR) << "delete_key failed, code " << uint32_t(ErrorCode(error)); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 135 | bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams, |
| 136 | std::string* newKey) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 137 | auto oldKeyBlob = blob2hidlVec(oldKey); |
| 138 | ErrorCode km_error; |
| 139 | auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) { |
| 140 | km_error = ret; |
| 141 | if (km_error != ErrorCode::OK) return; |
| 142 | if (newKey) |
| 143 | newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]), |
| 144 | upgradedKeyBlob.size()); |
| 145 | }; |
| 146 | auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb); |
| 147 | if (!error.isOk()) { |
| 148 | LOG(ERROR) << "upgrade_key failed: " << error.description(); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 149 | return false; |
| 150 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 151 | if (km_error != ErrorCode::OK) { |
| 152 | LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error); |
| 153 | return false; |
| 154 | } |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 158 | KeymasterOperation Keymaster::begin(KeyPurpose purpose, const std::string& key, |
| 159 | const AuthorizationSet& inParams, |
| 160 | AuthorizationSet* outParams) { |
| 161 | auto keyBlob = blob2hidlVec(key); |
| 162 | uint64_t mOpHandle; |
| 163 | ErrorCode km_error; |
| 164 | |
| 165 | auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& _outParams, |
| 166 | uint64_t operationHandle) { |
| 167 | km_error = ret; |
| 168 | if (km_error != ErrorCode::OK) return; |
| 169 | if (outParams) |
| 170 | *outParams = _outParams; |
| 171 | mOpHandle = operationHandle; |
| 172 | }; |
| 173 | |
| 174 | auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), hidlCb); |
| 175 | if (!error.isOk()) { |
| 176 | LOG(ERROR) << "begin failed: " << error.description(); |
| 177 | return KeymasterOperation(ErrorCode::UNKNOWN_ERROR); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 178 | } |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 179 | if (km_error != ErrorCode::OK) { |
| 180 | LOG(ERROR) << "begin failed, code " << uint32_t(km_error); |
| 181 | return KeymasterOperation(km_error); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 182 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 183 | return KeymasterOperation(mDevice, mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | } // namespace vold |
| 187 | } // namespace android |