blob: 893a6d1c035597a823e89403e5b12ef905083251 [file] [log] [blame]
Paul Crowley1ef25582016-01-21 20:26:12 +00001/*
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 Crowley0323afd2016-03-15 17:04:39 -070017#ifndef ANDROID_VOLD_KEYMASTER_H
18#define ANDROID_VOLD_KEYMASTER_H
Paul Crowley1ef25582016-01-21 20:26:12 +000019
Paul Crowley0323afd2016-03-15 17:04:39 -070020#include <memory>
Paul Crowley1ef25582016-01-21 20:26:12 +000021#include <string>
Paul Crowley0323afd2016-03-15 17:04:39 -070022#include <utility>
Paul Crowley1ef25582016-01-21 20:26:12 +000023
Janis Danisevskis8e537b82016-10-26 14:27:10 +010024#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
25#include <keystore/authorization_set.h>
Paul Crowley1ef25582016-01-21 20:26:12 +000026
27namespace android {
28namespace vold {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010029using ::android::hardware::keymaster::V3_0::IKeymasterDevice;
30using ::keystore::ErrorCode;
31using ::keystore::KeyPurpose;
32using ::keystore::AuthorizationSet;
Paul Crowley1ef25582016-01-21 20:26:12 +000033
Janis Danisevskis8e537b82016-10-26 14:27:10 +010034// C++ wrappers to the Keymaster hidl interface.
Paul Crowley1ef25582016-01-21 20:26:12 +000035// This is tailored to the needs of KeyStorage, but could be extended to be
36// a more general interface.
37
Janis Danisevskis8e537b82016-10-26 14:27:10 +010038// Wrapper for a Keymaster operation handle representing an
Paul Crowley1ef25582016-01-21 20:26:12 +000039// ongoing Keymaster operation. Aborts the operation
40// in the destructor if it is unfinished. Methods log failures
41// to LOG(ERROR).
42class KeymasterOperation {
Paul Crowleydf528a72016-03-09 09:31:37 -080043 public:
Paul Crowley0323afd2016-03-15 17:04:39 -070044 ~KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +000045 // Is this instance valid? This is false if creation fails, and becomes
46 // false on finish or if an update fails.
Janis Danisevskis8e537b82016-10-26 14:27:10 +010047 explicit operator bool() { return mError == ErrorCode::OK; }
48 ErrorCode error() { return mError; }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000049 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000050 // concatenate the output. Return true on success.
Paul Crowleydf528a72016-03-09 09:31:37 -080051 bool updateCompletely(const std::string& input, std::string* output);
Paul Crowleydff8c722016-05-16 08:14:56 -070052 // Finish and write the output to this string, unless pointer is null.
53 bool finish(std::string* output);
Paul Crowley1ef25582016-01-21 20:26:12 +000054 // Move constructor
55 KeymasterOperation(KeymasterOperation&& rhs) {
Paul Crowley0323afd2016-03-15 17:04:39 -070056 mDevice = std::move(rhs.mDevice);
Paul Crowleydff8c722016-05-16 08:14:56 -070057 mOpHandle = std::move(rhs.mOpHandle);
58 mError = std::move(rhs.mError);
Paul Crowley1ef25582016-01-21 20:26:12 +000059 }
Paul Crowleydff8c722016-05-16 08:14:56 -070060 // Construct an object in an error state for error returns
Janis Danisevskis8e537b82016-10-26 14:27:10 +010061 KeymasterOperation()
62 : mDevice{nullptr}, mOpHandle{static_cast<uint64_t>(0)},
63 mError {ErrorCode::UNKNOWN_ERROR} {}
Paul Crowleydf528a72016-03-09 09:31:37 -080064
65 private:
Janis Danisevskis8e537b82016-10-26 14:27:10 +010066 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 Crowleydff8c722016-05-16 08:14:56 -070070 mError {error} {}
Janis Danisevskis8e537b82016-10-26 14:27:10 +010071 sp<IKeymasterDevice> mDevice;
72 uint64_t mOpHandle;
73 ErrorCode mError;
Paul Crowley1ef25582016-01-21 20:26:12 +000074 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
75 friend class Keymaster;
76};
77
Paul Crowley0323afd2016-03-15 17:04:39 -070078// Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
79// part of one.
Paul Crowley1ef25582016-01-21 20:26:12 +000080class Keymaster {
Paul Crowleydf528a72016-03-09 09:31:37 -080081 public:
Paul Crowley1ef25582016-01-21 20:26:12 +000082 Keymaster();
Paul Crowley1ef25582016-01-21 20:26:12 +000083 // false if we failed to open the keymaster device.
Janis Danisevskis8e537b82016-10-26 14:27:10 +010084 explicit operator bool() { return mDevice.get() != nullptr; }
Paul Crowley1ef25582016-01-21 20:26:12 +000085 // Generate a key in the keymaster from the given params.
Paul Crowleydf528a72016-03-09 09:31:37 -080086 bool generateKey(const AuthorizationSet& inParams, std::string* key);
Paul Crowley1ef25582016-01-21 20:26:12 +000087 // If the keymaster supports it, permanently delete a key.
Paul Crowleydf528a72016-03-09 09:31:37 -080088 bool deleteKey(const std::string& key);
Paul Crowleydff8c722016-05-16 08:14:56 -070089 // 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 Danisevskis8e537b82016-10-26 14:27:10 +010093 KeymasterOperation begin(KeyPurpose purpose, const std::string& key,
Paul Crowleydf528a72016-03-09 09:31:37 -080094 const AuthorizationSet& inParams, AuthorizationSet* outParams);
Paul Crowleydf528a72016-03-09 09:31:37 -080095
96 private:
Janis Danisevskis8e537b82016-10-26 14:27:10 +010097 sp<hardware::keymaster::V3_0::IKeymasterDevice> mDevice;
Paul Crowley1ef25582016-01-21 20:26:12 +000098 DISALLOW_COPY_AND_ASSIGN(Keymaster);
99};
100
Paul Crowley1ef25582016-01-21 20:26:12 +0000101} // namespace vold
102} // namespace android
103
104#endif