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 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 20 | #include <memory> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 21 | #include <string> |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 22 | #include <utility> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 23 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 24 | #include <android/hardware/keymaster/3.0/IKeymasterDevice.h> |
| 25 | #include <keystore/authorization_set.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace vold { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 29 | using ::android::hardware::keymaster::V3_0::IKeymasterDevice; |
| 30 | using ::keystore::ErrorCode; |
| 31 | using ::keystore::KeyPurpose; |
| 32 | using ::keystore::AuthorizationSet; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 33 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 34 | // C++ wrappers to the Keymaster hidl interface. |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 35 | // This is tailored to the needs of KeyStorage, but could be extended to be |
| 36 | // a more general interface. |
| 37 | |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 38 | // Wrapper for a Keymaster operation handle representing an |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 39 | // ongoing Keymaster operation. Aborts the operation |
| 40 | // in the destructor if it is unfinished. Methods log failures |
| 41 | // to LOG(ERROR). |
| 42 | class KeymasterOperation { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 43 | public: |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 44 | ~KeymasterOperation(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 45 | // Is this instance valid? This is false if creation fails, and becomes |
| 46 | // false on finish or if an update fails. |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 47 | explicit operator bool() { return mError == ErrorCode::OK; } |
| 48 | ErrorCode error() { return mError; } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 49 | // Call "update" repeatedly until all of the input is consumed, and |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 50 | // concatenate the output. Return true on success. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 51 | bool updateCompletely(const std::string& input, std::string* output); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 52 | // Finish and write the output to this string, unless pointer is null. |
| 53 | bool finish(std::string* output); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 54 | // Move constructor |
| 55 | KeymasterOperation(KeymasterOperation&& rhs) { |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 56 | mDevice = std::move(rhs.mDevice); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 57 | mOpHandle = std::move(rhs.mOpHandle); |
| 58 | mError = std::move(rhs.mError); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 59 | } |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 60 | // Construct an object in an error state for error returns |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 61 | KeymasterOperation() |
| 62 | : mDevice{nullptr}, mOpHandle{static_cast<uint64_t>(0)}, |
| 63 | mError {ErrorCode::UNKNOWN_ERROR} {} |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 64 | |
| 65 | private: |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 66 | KeymasterOperation(const sp<IKeymasterDevice>& d, uint64_t h) |
| 67 | : mDevice{d}, mOpHandle{h}, mError {ErrorCode::OK} {} |
| 68 | KeymasterOperation(ErrorCode error) |
| 69 | : mDevice{nullptr}, mOpHandle{0}, |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 70 | mError {error} {} |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 71 | sp<IKeymasterDevice> mDevice; |
| 72 | uint64_t mOpHandle; |
| 73 | ErrorCode mError; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 74 | DISALLOW_COPY_AND_ASSIGN(KeymasterOperation); |
| 75 | friend class Keymaster; |
| 76 | }; |
| 77 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 78 | // Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not |
| 79 | // part of one. |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 80 | class Keymaster { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 81 | public: |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 82 | Keymaster(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 83 | // false if we failed to open the keymaster device. |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 84 | explicit operator bool() { return mDevice.get() != nullptr; } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 85 | // Generate a key in the keymaster from the given params. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 86 | bool generateKey(const AuthorizationSet& inParams, std::string* key); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 87 | // If the keymaster supports it, permanently delete a key. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 88 | bool deleteKey(const std::string& key); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 89 | // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE. |
| 90 | bool upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams, |
| 91 | std::string* newKey); |
| 92 | // 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^] | 93 | KeymasterOperation begin(KeyPurpose purpose, const std::string& key, |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 94 | const AuthorizationSet& inParams, AuthorizationSet* outParams); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 95 | |
| 96 | private: |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame^] | 97 | sp<hardware::keymaster::V3_0::IKeymasterDevice> mDevice; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 98 | DISALLOW_COPY_AND_ASSIGN(Keymaster); |
| 99 | }; |
| 100 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 101 | } // namespace vold |
| 102 | } // namespace android |
| 103 | |
| 104 | #endif |