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 | |
| 24 | #include <keymaster/authorization_set.h> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace vold { |
| 28 | |
| 29 | using namespace keymaster; |
| 30 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 31 | // C++ wrappers to the Keymaster C interface. |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 32 | // This is tailored to the needs of KeyStorage, but could be extended to be |
| 33 | // a more general interface. |
| 34 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 35 | // Class that wraps a keymaster1_device_t or keymaster2_device_t and provides methods |
| 36 | // they have in common. Also closes the device on destruction. |
| 37 | class IKeymasterDevice; |
| 38 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 39 | // Wrapper for a keymaster_operation_handle_t representing an |
| 40 | // ongoing Keymaster operation. Aborts the operation |
| 41 | // in the destructor if it is unfinished. Methods log failures |
| 42 | // to LOG(ERROR). |
| 43 | class KeymasterOperation { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 44 | public: |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 45 | ~KeymasterOperation(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 46 | // Is this instance valid? This is false if creation fails, and becomes |
| 47 | // false on finish or if an update fails. |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 48 | explicit operator bool() { return mError == KM_ERROR_OK; } |
| 49 | keymaster_error_t error() { return mError; } |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 50 | // Call "update" repeatedly until all of the input is consumed, and |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 51 | // concatenate the output. Return true on success. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 52 | bool updateCompletely(const std::string& input, std::string* output); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 53 | // Finish and write the output to this string, unless pointer is null. |
| 54 | bool finish(std::string* output); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 55 | // Move constructor |
| 56 | KeymasterOperation(KeymasterOperation&& rhs) { |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 57 | mDevice = std::move(rhs.mDevice); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 58 | mOpHandle = std::move(rhs.mOpHandle); |
| 59 | mError = std::move(rhs.mError); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 60 | } |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 61 | // Construct an object in an error state for error returns |
| 62 | KeymasterOperation() : KeymasterOperation(KM_ERROR_UNKNOWN_ERROR) {} |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 63 | |
| 64 | private: |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 65 | KeymasterOperation(std::shared_ptr<IKeymasterDevice> d, keymaster_operation_handle_t h) |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 66 | : mDevice{d}, mOpHandle{h}, mError {KM_ERROR_OK} {} |
| 67 | KeymasterOperation(keymaster_error_t error) |
| 68 | : mDevice{nullptr}, mOpHandle{static_cast<keymaster_operation_handle_t>(0)}, |
| 69 | mError {error} {} |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 70 | std::shared_ptr<IKeymasterDevice> mDevice; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 71 | keymaster_operation_handle_t mOpHandle; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 72 | keymaster_error_t mError; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 73 | DISALLOW_COPY_AND_ASSIGN(KeymasterOperation); |
| 74 | friend class Keymaster; |
| 75 | }; |
| 76 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 77 | // Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not |
| 78 | // part of one. |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 79 | class Keymaster { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 80 | public: |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 81 | Keymaster(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 82 | // false if we failed to open the keymaster device. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 83 | explicit operator bool() { return mDevice != nullptr; } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 84 | // Generate a key in the keymaster from the given params. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 85 | bool generateKey(const AuthorizationSet& inParams, std::string* key); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 86 | // If the keymaster supports it, permanently delete a key. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 87 | bool deleteKey(const std::string& key); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 88 | // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE. |
| 89 | bool upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams, |
| 90 | std::string* newKey); |
| 91 | // Begin a new cryptographic operation, collecting output parameters if pointer is non-null |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 92 | KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key, |
| 93 | const AuthorizationSet& inParams, AuthorizationSet* outParams); |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 94 | |
| 95 | private: |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 96 | std::shared_ptr<IKeymasterDevice> mDevice; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(Keymaster); |
| 98 | }; |
| 99 | |
| 100 | template <keymaster_tag_t Tag> |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 101 | inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder&& params, |
| 102 | TypedTag<KM_BYTES, Tag> tag, |
| 103 | const std::string& val) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 104 | return params.Authorization(tag, val.data(), val.size()); |
| 105 | } |
| 106 | |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 107 | template <keymaster_tag_t Tag> |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 108 | inline void addStringParam(AuthorizationSetBuilder* params, TypedTag<KM_BYTES, Tag> tag, |
| 109 | const std::string& val) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 110 | params->Authorization(tag, val.data(), val.size()); |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 113 | } // namespace vold |
| 114 | } // namespace android |
| 115 | |
| 116 | #endif |