blob: d8c488e3cab8f6658b2f315efb72b3b84882970f [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 */
Eric Biggersd86a8ab2021-06-15 11:34:00 -070016#ifndef ANDROID_VOLD_KEYSTORE_H
17#define ANDROID_VOLD_KEYSTORE_H
Paul Crowley1ef25582016-01-21 20:26:12 +000018
Pavel Grafove2e2d302017-08-01 17:15:53 +010019#include "KeyBuffer.h"
20
Paul Crowley0323afd2016-03-15 17:04:39 -070021#include <memory>
Paul Crowley1ef25582016-01-21 20:26:12 +000022#include <string>
Paul Crowley0323afd2016-03-15 17:04:39 -070023#include <utility>
Paul Crowley1ef25582016-01-21 20:26:12 +000024
Steven Moreland25e8b4b2017-05-01 12:45:32 -070025#include <android-base/macros.h>
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080026#include <keymint_support/authorization_set.h>
27#include <keymint_support/keymint_tags.h>
28
29#include <aidl/android/hardware/security/keymint/ErrorCode.h>
30#include <aidl/android/system/keystore2/IKeystoreService.h>
31#include <android/binder_manager.h>
Paul Crowley1ef25582016-01-21 20:26:12 +000032
33namespace android {
34namespace vold {
Shawn Willden35351812018-01-22 09:08:32 -070035
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080036namespace ks2 = ::aidl::android::system::keystore2;
37namespace km = ::aidl::android::hardware::security::keymint;
Shawn Willdenae8f06f2020-01-16 13:21:42 -070038
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080039// C++ wrappers to the Keystore2 AIDL interface.
Paul Crowley1ef25582016-01-21 20:26:12 +000040// This is tailored to the needs of KeyStorage, but could be extended to be
41// a more general interface.
42
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080043// Wrapper for a Keystore2 operation handle representing an
44// ongoing Keystore2 operation. Aborts the operation
Paul Crowley1ef25582016-01-21 20:26:12 +000045// in the destructor if it is unfinished. Methods log failures
46// to LOG(ERROR).
Eric Biggersd86a8ab2021-06-15 11:34:00 -070047class KeystoreOperation {
Paul Crowleydf528a72016-03-09 09:31:37 -080048 public:
Eric Biggersd86a8ab2021-06-15 11:34:00 -070049 ~KeystoreOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +000050 // Is this instance valid? This is false if creation fails, and becomes
51 // false on finish or if an update fails.
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080052 explicit operator bool() const { return (bool)ks2Operation; }
53 km::ErrorCode getErrorCode() const { return errorCode; }
54 std::optional<std::string> getUpgradedBlob() const { return upgradedBlob; }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000055 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000056 // concatenate the output. Return true on success.
Pavel Grafove2e2d302017-08-01 17:15:53 +010057 template <class TI, class TO>
58 bool updateCompletely(TI& input, TO* output) {
59 if (output) output->clear();
60 return updateCompletely(input.data(), input.size(), [&](const char* b, size_t n) {
Shawn Willden785365b2018-01-20 09:37:36 -070061 if (output) std::copy(b, b + n, std::back_inserter(*output));
Pavel Grafove2e2d302017-08-01 17:15:53 +010062 });
63 }
64
Paul Crowleydff8c722016-05-16 08:14:56 -070065 // Finish and write the output to this string, unless pointer is null.
66 bool finish(std::string* output);
Paul Crowley1ef25582016-01-21 20:26:12 +000067 // Move constructor
Eric Biggersd86a8ab2021-06-15 11:34:00 -070068 KeystoreOperation(KeystoreOperation&& rhs) { *this = std::move(rhs); }
Paul Crowleydff8c722016-05-16 08:14:56 -070069 // Construct an object in an error state for error returns
Eric Biggersd86a8ab2021-06-15 11:34:00 -070070 KeystoreOperation() { errorCode = km::ErrorCode::UNKNOWN_ERROR; }
Janis Danisevskis015ec302017-01-31 11:31:08 +000071 // Move Assignment
Eric Biggersd86a8ab2021-06-15 11:34:00 -070072 KeystoreOperation& operator=(KeystoreOperation&& rhs) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080073 ks2Operation = rhs.ks2Operation;
74 rhs.ks2Operation = nullptr;
Shawn Willden3e02df82018-02-07 15:06:06 -070075
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080076 upgradedBlob = rhs.upgradedBlob;
77 rhs.upgradedBlob = std::nullopt;
Shawn Willden3e02df82018-02-07 15:06:06 -070078
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080079 errorCode = rhs.errorCode;
80 rhs.errorCode = km::ErrorCode::UNKNOWN_ERROR;
Shawn Willden3e02df82018-02-07 15:06:06 -070081
Janis Danisevskis015ec302017-01-31 11:31:08 +000082 return *this;
83 }
Paul Crowleydf528a72016-03-09 09:31:37 -080084
85 private:
Eric Biggersd86a8ab2021-06-15 11:34:00 -070086 KeystoreOperation(std::shared_ptr<ks2::IKeystoreOperation> ks2Op,
87 std::optional<std::vector<uint8_t>> blob)
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080088 : ks2Operation{ks2Op}, errorCode{km::ErrorCode::OK} {
89 if (blob)
90 upgradedBlob = std::optional(std::string(blob->begin(), blob->end()));
91 else
92 upgradedBlob = std::nullopt;
93 }
94
Eric Biggersd86a8ab2021-06-15 11:34:00 -070095 KeystoreOperation(km::ErrorCode errCode) : errorCode{errCode} {}
Pavel Grafove2e2d302017-08-01 17:15:53 +010096
97 bool updateCompletely(const char* input, size_t inputLen,
98 const std::function<void(const char*, size_t)> consumer);
99
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800100 std::shared_ptr<ks2::IKeystoreOperation> ks2Operation;
101 std::optional<std::string> upgradedBlob;
102 km::ErrorCode errorCode;
Eric Biggersd86a8ab2021-06-15 11:34:00 -0700103 DISALLOW_COPY_AND_ASSIGN(KeystoreOperation);
104 friend class Keystore;
Paul Crowley1ef25582016-01-21 20:26:12 +0000105};
106
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800107// Wrapper for keystore2 methods that vold uses.
Eric Biggersd86a8ab2021-06-15 11:34:00 -0700108class Keystore {
Paul Crowleydf528a72016-03-09 09:31:37 -0800109 public:
Eric Biggersd86a8ab2021-06-15 11:34:00 -0700110 Keystore();
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800111 // false if we failed to get a keystore2 security level.
112 explicit operator bool() { return (bool)securityLevel; }
113 // Generate a key using keystore2 from the given params.
Shawn Willden35351812018-01-22 09:08:32 -0700114 bool generateKey(const km::AuthorizationSet& inParams, std::string* key);
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800115 // Exports a keystore2 key with STORAGE_KEY tag wrapped with a per-boot ephemeral key
Eric Biggersd86a8ab2021-06-15 11:34:00 -0700116 bool exportKey(const KeyBuffer& ksKey, std::string* key);
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800117 // If supported, permanently delete a key from the keymint device it belongs to.
Paul Crowleydf528a72016-03-09 09:31:37 -0800118 bool deleteKey(const std::string& key);
Paul Crowleydff8c722016-05-16 08:14:56 -0700119 // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
Eric Biggersd86a8ab2021-06-15 11:34:00 -0700120 // If the key was upgraded as a result of a call to this method, the returned KeystoreOperation
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800121 // also stores the upgraded key blob.
Eric Biggersd86a8ab2021-06-15 11:34:00 -0700122 KeystoreOperation begin(const std::string& key, const km::AuthorizationSet& inParams,
123 km::AuthorizationSet* outParams);
Paul Crowleydf528a72016-03-09 09:31:37 -0800124
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800125 // Tell all Keymint devices that early boot has ended and early boot-only keys can no longer
Shawn Willden50397a72020-04-01 10:02:16 -0600126 // be created or used.
127 static void earlyBootEnded();
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700128
Paul Crowley1e6a5f52021-08-06 15:16:10 -0700129 // Tell all Keymint devices to delete all rollback-protected keys.
130 static void deleteAllKeys();
131
Paul Crowleydf528a72016-03-09 09:31:37 -0800132 private:
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800133 std::shared_ptr<ks2::IKeystoreSecurityLevel> securityLevel;
Eric Biggersd86a8ab2021-06-15 11:34:00 -0700134 DISALLOW_COPY_AND_ASSIGN(Keystore);
Paul Crowley1ef25582016-01-21 20:26:12 +0000135};
136
Paul Crowley1ef25582016-01-21 20:26:12 +0000137} // namespace vold
138} // namespace android
139
Paul Crowley1ef25582016-01-21 20:26:12 +0000140#endif