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 | #ifndef ANDROID_VOLD_KEYMASTER1_H |
| 18 | #define ANDROID_VOLD_KEYMASTER1_H |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include <hardware/hardware.h> |
| 23 | #include <hardware/keymaster1.h> |
| 24 | |
| 25 | #include <keymaster/authorization_set.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace vold { |
| 29 | |
| 30 | using namespace keymaster; |
| 31 | |
| 32 | // C++ wrappers to the keymaster1 C interface. |
| 33 | // This is tailored to the needs of KeyStorage, but could be extended to be |
| 34 | // a more general interface. |
| 35 | |
| 36 | |
| 37 | // Wrapper for a keymaster_operation_handle_t representing an |
| 38 | // ongoing Keymaster operation. Aborts the operation |
| 39 | // in the destructor if it is unfinished. Methods log failures |
| 40 | // to LOG(ERROR). |
| 41 | class KeymasterOperation { |
| 42 | public: |
| 43 | ~KeymasterOperation() { if (device) device->abort(device, op_handle); } |
| 44 | // Is this instance valid? This is false if creation fails, and becomes |
| 45 | // false on finish or if an update fails. |
| 46 | explicit operator bool() {return device != nullptr;} |
| 47 | // Call "update" repeatedly until all of the input is consumed, and |
| 48 | // concatenate the output. Return true on success. |
| 49 | bool UpdateCompletely(const std::string &input, std::string &output); |
| 50 | // Finish; pass nullptr for the "output" param. |
| 51 | bool Finish(); |
| 52 | // Finish and write the output to this string. |
| 53 | bool FinishWithOutput(std::string &output); |
| 54 | // Move constructor |
| 55 | KeymasterOperation(KeymasterOperation&& rhs) { |
| 56 | op_handle = rhs.op_handle; |
| 57 | device = rhs.device; |
| 58 | rhs.device = nullptr; |
| 59 | } |
| 60 | // Move assignation. |
| 61 | KeymasterOperation& operator=(KeymasterOperation&& rhs) { |
| 62 | op_handle = rhs.op_handle; |
| 63 | device = rhs.device; |
| 64 | rhs.device = nullptr; |
| 65 | return *this; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | KeymasterOperation(keymaster1_device_t *d, keymaster_operation_handle_t h): |
| 70 | device {d}, op_handle {h} {} |
| 71 | keymaster1_device_t *device; |
| 72 | keymaster_operation_handle_t op_handle; |
| 73 | DISALLOW_COPY_AND_ASSIGN(KeymasterOperation); |
| 74 | friend class Keymaster; |
| 75 | }; |
| 76 | |
| 77 | // Wrapper for a keymaster1_device_t representing an open connection |
| 78 | // to the keymaster, which is closed in the destructor. |
| 79 | class Keymaster { |
| 80 | public: |
| 81 | Keymaster(); |
| 82 | ~Keymaster() { if (device) keymaster1_close(device); } |
| 83 | // false if we failed to open the keymaster device. |
| 84 | explicit operator bool() {return device != nullptr;} |
| 85 | // Generate a key in the keymaster from the given params. |
| 86 | bool GenerateKey(const AuthorizationSet &in_params, std::string &key); |
| 87 | // If the keymaster supports it, permanently delete a key. |
| 88 | bool DeleteKey(const std::string &key); |
| 89 | // Begin a new cryptographic operation, collecting output parameters. |
| 90 | KeymasterOperation Begin( |
| 91 | keymaster_purpose_t purpose, |
| 92 | const std::string &key, |
| 93 | const AuthorizationSet &in_params, |
| 94 | AuthorizationSet &out_params); |
| 95 | // Begin a new cryptographic operation; don't collect output parameters. |
| 96 | KeymasterOperation Begin( |
| 97 | keymaster_purpose_t purpose, |
| 98 | const std::string &key, |
| 99 | const AuthorizationSet &in_params); |
| 100 | private: |
| 101 | keymaster1_device_t *device; |
| 102 | DISALLOW_COPY_AND_ASSIGN(Keymaster); |
| 103 | }; |
| 104 | |
| 105 | template <keymaster_tag_t Tag> |
| 106 | inline AuthorizationSetBuilder& AddStringParam(AuthorizationSetBuilder ¶ms, |
| 107 | TypedTag<KM_BYTES, Tag> tag, const std::string& val) { |
| 108 | return params.Authorization(tag, val.data(), val.size()); |
| 109 | } |
| 110 | |
| 111 | } // namespace vold |
| 112 | } // namespace android |
| 113 | |
| 114 | #endif |