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> |
| 20 | |
| 21 | namespace android { |
| 22 | namespace vold { |
| 23 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 24 | bool KeymasterOperation::updateCompletely( |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 25 | const std::string &input, |
| 26 | std::string &output) { |
| 27 | output.clear(); |
| 28 | auto it = input.begin(); |
| 29 | while (it != input.end()) { |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 30 | size_t toRead = static_cast<size_t>(input.end() - it); |
| 31 | keymaster_blob_t inputBlob {reinterpret_cast<const uint8_t *>(&*it), toRead}; |
| 32 | keymaster_blob_t outputBlob; |
| 33 | size_t inputConsumed; |
| 34 | auto error = mDevice->update(mDevice, mOpHandle, |
| 35 | nullptr, &inputBlob, &inputConsumed, nullptr, &outputBlob); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 36 | if (error != KM_ERROR_OK) { |
| 37 | LOG(ERROR) << "update failed, code " << error; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 38 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 39 | return false; |
| 40 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 41 | output.append(reinterpret_cast<const char *>(outputBlob.data), outputBlob.data_length); |
| 42 | free(const_cast<uint8_t *>(outputBlob.data)); |
| 43 | if (inputConsumed > toRead) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 44 | LOG(ERROR) << "update reported too much input consumed"; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 45 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 46 | return false; |
| 47 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 48 | it += inputConsumed; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 53 | bool KeymasterOperation::finish() { |
| 54 | auto error = mDevice->finish(mDevice, mOpHandle, |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 55 | nullptr, nullptr, nullptr, nullptr); |
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 | if (error != KM_ERROR_OK) { |
| 58 | LOG(ERROR) << "finish failed, code " << error; |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 64 | bool KeymasterOperation::finishWithOutput(std::string &output) { |
| 65 | keymaster_blob_t outputBlob; |
| 66 | auto error = mDevice->finish(mDevice, mOpHandle, |
| 67 | nullptr, nullptr, nullptr, &outputBlob); |
| 68 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 69 | if (error != KM_ERROR_OK) { |
| 70 | LOG(ERROR) << "finish failed, code " << error; |
| 71 | return false; |
| 72 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 73 | output.assign(reinterpret_cast<const char *>(outputBlob.data), outputBlob.data_length); |
| 74 | free(const_cast<uint8_t *>(outputBlob.data)); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
| 78 | Keymaster::Keymaster() { |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 79 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 80 | const hw_module_t *module; |
| 81 | int ret = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &module); |
| 82 | if (ret != 0) { |
| 83 | LOG(ERROR) << "hw_get_module_by_class returned " << ret; |
| 84 | return; |
| 85 | } |
| 86 | // TODO: This will need to be updated to support keymaster2. |
| 87 | if (module->module_api_version != KEYMASTER_MODULE_API_VERSION_1_0) { |
| 88 | LOG(ERROR) << "module_api_version is " << module->module_api_version; |
| 89 | return; |
| 90 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 91 | ret = keymaster1_open(module, &mDevice); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 92 | if (ret != 0) { |
| 93 | LOG(ERROR) << "keymaster1_open returned " << ret; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 94 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 99 | bool Keymaster::generateKey( |
| 100 | const keymaster::AuthorizationSet &inParams, |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 101 | std::string &key) { |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 102 | keymaster_key_blob_t keyBlob; |
| 103 | auto error = mDevice->generate_key(mDevice, &inParams, &keyBlob, nullptr); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 104 | if (error != KM_ERROR_OK) { |
| 105 | LOG(ERROR) << "generate_key failed, code " << error; |
| 106 | return false; |
| 107 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 108 | key.assign(reinterpret_cast<const char *>(keyBlob.key_material), keyBlob.key_material_size); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 112 | bool Keymaster::deleteKey(const std::string &key) { |
| 113 | if (mDevice->delete_key == nullptr) return true; |
| 114 | keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() }; |
| 115 | auto error = mDevice->delete_key(mDevice, &keyBlob); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 116 | if (error != KM_ERROR_OK) { |
| 117 | LOG(ERROR) << "delete_key failed, code " << error; |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 123 | KeymasterOperation Keymaster::begin( |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 124 | keymaster_purpose_t purpose, |
| 125 | const std::string &key, |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 126 | const keymaster::AuthorizationSet &inParams, |
| 127 | keymaster::AuthorizationSet &outParams) { |
| 128 | keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() }; |
| 129 | keymaster_operation_handle_t mOpHandle; |
| 130 | keymaster_key_param_set_t outParams_set; |
| 131 | auto error = mDevice->begin(mDevice, purpose, |
| 132 | &keyBlob, &inParams, &outParams_set, &mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 133 | if (error != KM_ERROR_OK) { |
| 134 | LOG(ERROR) << "begin failed, code " << error; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 135 | return KeymasterOperation(nullptr, mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 136 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 137 | outParams.Clear(); |
| 138 | outParams.push_back(outParams_set); |
| 139 | keymaster_free_param_set(&outParams_set); |
| 140 | return KeymasterOperation(mDevice, mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 143 | KeymasterOperation Keymaster::begin( |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 144 | keymaster_purpose_t purpose, |
| 145 | const std::string &key, |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 146 | const keymaster::AuthorizationSet &inParams) { |
| 147 | keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() }; |
| 148 | keymaster_operation_handle_t mOpHandle; |
| 149 | auto error = mDevice->begin(mDevice, purpose, |
| 150 | &keyBlob, &inParams, nullptr, &mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 151 | if (error != KM_ERROR_OK) { |
| 152 | LOG(ERROR) << "begin failed, code " << error; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 153 | return KeymasterOperation(nullptr, mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 154 | } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 155 | return KeymasterOperation(mDevice, mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace vold |
| 159 | } // namespace android |