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 | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 48 | explicit operator bool() { return mDevice != nullptr; } |
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 | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 52 | // Finish; pass nullptr for the "output" param. |
| 53 | bool finish(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 54 | // Finish and write the output to this string. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 55 | bool finishWithOutput(std::string* output); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 56 | // Move constructor |
| 57 | KeymasterOperation(KeymasterOperation&& rhs) { |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame^] | 58 | mOpHandle = std::move(rhs.mOpHandle); |
| 59 | mDevice = std::move(rhs.mDevice); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 60 | } |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 61 | |
| 62 | private: |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame^] | 63 | KeymasterOperation(std::shared_ptr<IKeymasterDevice> d, keymaster_operation_handle_t h) |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 64 | : mDevice{d}, mOpHandle{h} {} |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame^] | 65 | std::shared_ptr<IKeymasterDevice> mDevice; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 66 | keymaster_operation_handle_t mOpHandle; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 67 | DISALLOW_COPY_AND_ASSIGN(KeymasterOperation); |
| 68 | friend class Keymaster; |
| 69 | }; |
| 70 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame^] | 71 | // Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not |
| 72 | // part of one. |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 73 | class Keymaster { |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 74 | public: |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 75 | Keymaster(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 76 | // false if we failed to open the keymaster device. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 77 | explicit operator bool() { return mDevice != nullptr; } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 78 | // Generate a key in the keymaster from the given params. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 79 | bool generateKey(const AuthorizationSet& inParams, std::string* key); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 80 | // If the keymaster supports it, permanently delete a key. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 81 | bool deleteKey(const std::string& key); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 82 | // Begin a new cryptographic operation, collecting output parameters. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 83 | KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key, |
| 84 | const AuthorizationSet& inParams, AuthorizationSet* outParams); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 85 | // Begin a new cryptographic operation; don't collect output parameters. |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 86 | KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key, |
| 87 | const AuthorizationSet& inParams); |
| 88 | |
| 89 | private: |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame^] | 90 | std::shared_ptr<IKeymasterDevice> mDevice; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 91 | DISALLOW_COPY_AND_ASSIGN(Keymaster); |
| 92 | }; |
| 93 | |
| 94 | template <keymaster_tag_t Tag> |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 95 | inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder&& params, |
| 96 | TypedTag<KM_BYTES, Tag> tag, |
| 97 | const std::string& val) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 98 | return params.Authorization(tag, val.data(), val.size()); |
| 99 | } |
| 100 | |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 101 | template <keymaster_tag_t Tag> |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 102 | inline void addStringParam(AuthorizationSetBuilder* params, TypedTag<KM_BYTES, Tag> tag, |
| 103 | const std::string& val) { |
Paul Crowley | a051eb7 | 2016-03-08 16:08:32 -0800 | [diff] [blame] | 104 | params->Authorization(tag, val.data(), val.size()); |
Paul Crowley | 0572080 | 2016-02-08 15:55:41 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 107 | } // namespace vold |
| 108 | } // namespace android |
| 109 | |
| 110 | #endif |