blob: aef16029fd1bf2f01081cbd70435270e3b775442 [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
Pavel Grafove2e2d302017-08-01 17:15:53 +010020#include "KeyBuffer.h"
21
Paul Crowley0323afd2016-03-15 17:04:39 -070022#include <memory>
Paul Crowley1ef25582016-01-21 20:26:12 +000023#include <string>
Paul Crowley0323afd2016-03-15 17:04:39 -070024#include <utility>
Paul Crowley1ef25582016-01-21 20:26:12 +000025
Steven Moreland25e8b4b2017-05-01 12:45:32 -070026#include <android-base/macros.h>
Shawn Willden785365b2018-01-20 09:37:36 -070027#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
Shawn Willdenf4527742017-11-09 15:59:39 -070028
29#include "authorization_set.h"
Paul Crowley1ef25582016-01-21 20:26:12 +000030
31namespace android {
32namespace vold {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010033using ::android::hardware::keymaster::V3_0::IKeymasterDevice;
34using ::keystore::ErrorCode;
35using ::keystore::KeyPurpose;
36using ::keystore::AuthorizationSet;
Paul Crowley1ef25582016-01-21 20:26:12 +000037
Janis Danisevskis8e537b82016-10-26 14:27:10 +010038// C++ wrappers to the Keymaster hidl interface.
Paul Crowley1ef25582016-01-21 20:26:12 +000039// This is tailored to the needs of KeyStorage, but could be extended to be
40// a more general interface.
41
Janis Danisevskis8e537b82016-10-26 14:27:10 +010042// Wrapper for a Keymaster operation handle representing an
Paul Crowley1ef25582016-01-21 20:26:12 +000043// ongoing Keymaster operation. Aborts the operation
44// in the destructor if it is unfinished. Methods log failures
45// to LOG(ERROR).
46class KeymasterOperation {
Paul Crowleydf528a72016-03-09 09:31:37 -080047 public:
Paul Crowley0323afd2016-03-15 17:04:39 -070048 ~KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +000049 // Is this instance valid? This is false if creation fails, and becomes
50 // false on finish or if an update fails.
Janis Danisevskis8e537b82016-10-26 14:27:10 +010051 explicit operator bool() { return mError == ErrorCode::OK; }
Wei Wang4375f1b2017-02-24 17:43:01 -080052 ErrorCode errorCode() { return mError; }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000053 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000054 // concatenate the output. Return true on success.
Pavel Grafove2e2d302017-08-01 17:15:53 +010055 template <class TI, class TO>
56 bool updateCompletely(TI& input, TO* output) {
57 if (output) output->clear();
58 return updateCompletely(input.data(), input.size(), [&](const char* b, size_t n) {
Shawn Willden785365b2018-01-20 09:37:36 -070059 if (output) std::copy(b, b + n, std::back_inserter(*output));
Pavel Grafove2e2d302017-08-01 17:15:53 +010060 });
61 }
62
Paul Crowleydff8c722016-05-16 08:14:56 -070063 // Finish and write the output to this string, unless pointer is null.
64 bool finish(std::string* output);
Paul Crowley1ef25582016-01-21 20:26:12 +000065 // Move constructor
66 KeymasterOperation(KeymasterOperation&& rhs) {
Paul Crowley0323afd2016-03-15 17:04:39 -070067 mDevice = std::move(rhs.mDevice);
Paul Crowleydff8c722016-05-16 08:14:56 -070068 mOpHandle = std::move(rhs.mOpHandle);
69 mError = std::move(rhs.mError);
Paul Crowley1ef25582016-01-21 20:26:12 +000070 }
Paul Crowleydff8c722016-05-16 08:14:56 -070071 // Construct an object in an error state for error returns
Shawn Willden785365b2018-01-20 09:37:36 -070072 KeymasterOperation() : mDevice{nullptr}, mOpHandle{0}, mError{ErrorCode::UNKNOWN_ERROR} {}
Janis Danisevskis015ec302017-01-31 11:31:08 +000073 // Move Assignment
Shawn Willden785365b2018-01-20 09:37:36 -070074 KeymasterOperation& operator=(KeymasterOperation&& rhs) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000075 mDevice = std::move(rhs.mDevice);
76 mOpHandle = std::move(rhs.mOpHandle);
77 mError = std::move(rhs.mError);
78 rhs.mError = ErrorCode::UNKNOWN_ERROR;
79 rhs.mOpHandle = 0;
80 return *this;
81 }
Paul Crowleydf528a72016-03-09 09:31:37 -080082
83 private:
Janis Danisevskis8e537b82016-10-26 14:27:10 +010084 KeymasterOperation(const sp<IKeymasterDevice>& d, uint64_t h)
Shawn Willden785365b2018-01-20 09:37:36 -070085 : mDevice{d}, mOpHandle{h}, mError{ErrorCode::OK} {}
86 KeymasterOperation(ErrorCode error) : mDevice{nullptr}, mOpHandle{0}, mError{error} {}
Pavel Grafove2e2d302017-08-01 17:15:53 +010087
88 bool updateCompletely(const char* input, size_t inputLen,
89 const std::function<void(const char*, size_t)> consumer);
90
Janis Danisevskis8e537b82016-10-26 14:27:10 +010091 sp<IKeymasterDevice> mDevice;
92 uint64_t mOpHandle;
93 ErrorCode mError;
Paul Crowley1ef25582016-01-21 20:26:12 +000094 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
95 friend class Keymaster;
96};
97
Paul Crowley0323afd2016-03-15 17:04:39 -070098// Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
99// part of one.
Paul Crowley1ef25582016-01-21 20:26:12 +0000100class Keymaster {
Paul Crowleydf528a72016-03-09 09:31:37 -0800101 public:
Paul Crowley1ef25582016-01-21 20:26:12 +0000102 Keymaster();
Paul Crowley1ef25582016-01-21 20:26:12 +0000103 // false if we failed to open the keymaster device.
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100104 explicit operator bool() { return mDevice.get() != nullptr; }
Paul Crowley1ef25582016-01-21 20:26:12 +0000105 // Generate a key in the keymaster from the given params.
Paul Crowleydf528a72016-03-09 09:31:37 -0800106 bool generateKey(const AuthorizationSet& inParams, std::string* key);
Paul Crowley1ef25582016-01-21 20:26:12 +0000107 // If the keymaster supports it, permanently delete a key.
Paul Crowleydf528a72016-03-09 09:31:37 -0800108 bool deleteKey(const std::string& key);
Paul Crowleydff8c722016-05-16 08:14:56 -0700109 // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE.
110 bool upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
111 std::string* newKey);
112 // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100113 KeymasterOperation begin(KeyPurpose purpose, const std::string& key,
Paul Crowleydf528a72016-03-09 09:31:37 -0800114 const AuthorizationSet& inParams, AuthorizationSet* outParams);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000115 bool isSecure();
Paul Crowleydf528a72016-03-09 09:31:37 -0800116
117 private:
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100118 sp<hardware::keymaster::V3_0::IKeymasterDevice> mDevice;
Paul Crowley1ef25582016-01-21 20:26:12 +0000119 DISALLOW_COPY_AND_ASSIGN(Keymaster);
120};
121
Paul Crowley1ef25582016-01-21 20:26:12 +0000122} // namespace vold
123} // namespace android
124
Paul Crowley3b71fc52017-10-09 10:55:21 -0700125// FIXME no longer needed now cryptfs is in C++.
Janis Danisevskis015ec302017-01-31 11:31:08 +0000126
127/*
128 * The following functions provide C bindings to keymaster services
129 * needed by cryptfs scrypt. The compatibility check checks whether
130 * the keymaster implementation is considered secure, i.e., TEE backed.
131 * The create_key function generates an RSA key for signing.
132 * The sign_object function signes an object with the given keymaster
133 * key.
134 */
Janis Danisevskis015ec302017-01-31 11:31:08 +0000135
Paul Crowley73473332017-11-21 15:43:51 -0800136/* Return values for keymaster_sign_object_for_cryptfs_scrypt */
137
138enum class KeymasterSignResult {
139 ok = 0,
140 error = -1,
141 upgrade = -2,
142};
143
Janis Danisevskis015ec302017-01-31 11:31:08 +0000144int keymaster_compatibility_cryptfs_scrypt();
Shawn Willden785365b2018-01-20 09:37:36 -0700145int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
146 uint32_t ratelimit, uint8_t* key_buffer,
147 uint32_t key_buffer_size, uint32_t* key_out_size);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000148
Paul Crowley73473332017-11-21 15:43:51 -0800149int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
150 uint32_t ratelimit, const uint8_t* key_blob,
151 size_t key_blob_size, uint8_t* key_buffer,
152 uint32_t key_buffer_size, uint32_t* key_out_size);
153
154KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
155 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
156 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000157
Paul Crowley1ef25582016-01-21 20:26:12 +0000158#endif