blob: bd3f21985195759aac9339c22b9d9e23071a2c8a [file] [log] [blame]
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001/*
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_KEYMASTER_H
18#define ANDROID_VOLD_KEYMASTER_H
19
20#include <memory>
21#include <string>
22#include <utility>
23
24#include <keymaster/authorization_set.h>
codeworkx071526b2017-12-26 19:48:52 +010025#include "Utils.h"
Ethan Yonkerbd7492d2016-12-07 13:55:01 -060026
27namespace android {
28namespace vold {
29
30using namespace keymaster;
31
32// C++ wrappers to the Keymaster C interface.
33// This is tailored to the needs of KeyStorage, but could be extended to be
34// a more general interface.
35
36// Class that wraps a keymaster1_device_t or keymaster2_device_t and provides methods
37// they have in common. Also closes the device on destruction.
38class IKeymasterDevice;
39
40// Wrapper for a keymaster_operation_handle_t representing an
41// ongoing Keymaster operation. Aborts the operation
42// in the destructor if it is unfinished. Methods log failures
43// to LOG(ERROR).
44class KeymasterOperation {
45 public:
46 ~KeymasterOperation();
47 // Is this instance valid? This is false if creation fails, and becomes
48 // false on finish or if an update fails.
49 explicit operator bool() { return mDevice != nullptr; }
50 // Call "update" repeatedly until all of the input is consumed, and
51 // concatenate the output. Return true on success.
52 bool updateCompletely(const std::string& input, std::string* output);
53 // Finish; pass nullptr for the "output" param.
54 bool finish();
55 // Finish and write the output to this string.
56 bool finishWithOutput(std::string* output);
57 // Move constructor
58 KeymasterOperation(KeymasterOperation&& rhs) {
59 mOpHandle = std::move(rhs.mOpHandle);
60 mDevice = std::move(rhs.mDevice);
61 }
62
63 private:
64 KeymasterOperation(std::shared_ptr<IKeymasterDevice> d, keymaster_operation_handle_t h)
65 : mDevice{d}, mOpHandle{h} {}
66 std::shared_ptr<IKeymasterDevice> mDevice;
67 keymaster_operation_handle_t mOpHandle;
68 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
69 friend class Keymaster;
70};
71
72// Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
73// part of one.
74class Keymaster {
75 public:
76 Keymaster();
77 // false if we failed to open the keymaster device.
78 explicit operator bool() { return mDevice != nullptr; }
79 // Generate a key in the keymaster from the given params.
80 //bool generateKey(const AuthorizationSet& inParams, std::string* key);
81 // If the keymaster supports it, permanently delete a key.
82 bool deleteKey(const std::string& key);
83 // Begin a new cryptographic operation, collecting output parameters.
84 KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
85 const AuthorizationSet& inParams, AuthorizationSet* outParams);
86 // Begin a new cryptographic operation; don't collect output parameters.
87 KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
88 const AuthorizationSet& inParams);
89
90 private:
91 std::shared_ptr<IKeymasterDevice> mDevice;
92 DISALLOW_COPY_AND_ASSIGN(Keymaster);
93};
94
95template <keymaster_tag_t Tag>
96inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder&& params,
97 TypedTag<KM_BYTES, Tag> tag,
98 const std::string& val) {
99 return params.Authorization(tag, val.data(), val.size());
100}
101
102template <keymaster_tag_t Tag>
103inline void addStringParam(AuthorizationSetBuilder* params, TypedTag<KM_BYTES, Tag> tag,
104 const std::string& val) {
105 params->Authorization(tag, val.data(), val.size());
106}
107
108} // namespace vold
109} // namespace android
110
111#endif