blob: 784a6803d1ef72c423efe3faa028a9be7cdac6a1 [file] [log] [blame]
Tri Vo3ab6f052022-11-22 10:26:16 -08001/*
2 * Copyright (c) 2019, 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#define LOG_TAG "credstore"
18
19#include <atomic>
20
21#include <android-base/logging.h>
22#include <android/security/rkp/BnGetKeyCallback.h>
23#include <android/security/rkp/BnGetRegistrationCallback.h>
Seth Moore484010a2023-01-31 11:22:26 -080024#include <android/security/rkp/IGetKeyCallback.h>
Tri Vo3ab6f052022-11-22 10:26:16 -080025#include <android/security/rkp/IRemoteProvisioning.h>
26#include <binder/IServiceManager.h>
27#include <binder/Status.h>
28#include <vintf/VintfObject.h>
29
30#include "RemotelyProvisionedKey.h"
31
32namespace android {
33namespace security {
34namespace identity {
35namespace {
36
37using ::android::binder::Status;
38using ::android::hardware::security::keymint::IRemotelyProvisionedComponent;
39using ::android::hardware::security::keymint::RpcHardwareInfo;
40using ::android::security::rkp::BnGetKeyCallback;
41using ::android::security::rkp::BnGetRegistrationCallback;
Seth Moore484010a2023-01-31 11:22:26 -080042using ::android::security::rkp::IGetKeyCallback;
Tri Vo3ab6f052022-11-22 10:26:16 -080043using ::android::security::rkp::IRegistration;
44using ::android::security::rkp::IRemoteProvisioning;
45using ::android::security::rkp::RemotelyProvisionedKey;
46
Tri Vo71e8cc12023-01-17 15:37:50 -080047constexpr const char* kRemoteProvisioningServiceName = "remote_provisioning";
48
Tri Vo3ab6f052022-11-22 10:26:16 -080049std::optional<String16> findRpcNameById(std::string_view targetRpcId) {
50 auto deviceManifest = vintf::VintfObject::GetDeviceHalManifest();
51 auto instances = deviceManifest->getAidlInstances("android.hardware.security.keymint",
52 "IRemotelyProvisionedComponent");
53 for (const std::string& instance : instances) {
54 auto rpcName =
55 IRemotelyProvisionedComponent::descriptor + String16("/") + String16(instance.c_str());
56 sp<IRemotelyProvisionedComponent> rpc =
57 android::waitForService<IRemotelyProvisionedComponent>(rpcName);
58
59 auto rpcId = getRpcId(rpc);
60 if (!rpcId) {
61 continue;
62 }
63 if (*rpcId == targetRpcId) {
64 return rpcName;
65 }
66 }
67
68 LOG(ERROR) << "Remotely provisioned component with given unique ID: " << targetRpcId
69 << " not found";
70 return std::nullopt;
71}
72
73std::optional<String16> getRpcName(const sp<IRemotelyProvisionedComponent>& rpc) {
74 std::optional<std::string> targetRpcId = getRpcId(rpc);
75 if (!targetRpcId) {
76 return std::nullopt;
77 }
78 return findRpcNameById(*targetRpcId);
79}
80
81class GetKeyCallback : public BnGetKeyCallback {
82 public:
83 GetKeyCallback(std::promise<std::optional<RemotelyProvisionedKey>> keyPromise)
84 : keyPromise_(std::move(keyPromise)), called_() {}
85
86 Status onSuccess(const RemotelyProvisionedKey& key) override {
87 if (called_.test_and_set()) {
88 return Status::ok();
89 }
90 keyPromise_.set_value(key);
91 return Status::ok();
92 }
93 Status onCancel() override {
94 if (called_.test_and_set()) {
95 return Status::ok();
96 }
97 LOG(ERROR) << "GetKeyCallback cancelled";
98 keyPromise_.set_value(std::nullopt);
99 return Status::ok();
100 }
Seth Moore484010a2023-01-31 11:22:26 -0800101 Status onError(IGetKeyCallback::ErrorCode error, const String16& description) override {
Tri Vo3ab6f052022-11-22 10:26:16 -0800102 if (called_.test_and_set()) {
103 return Status::ok();
104 }
Seth Moore484010a2023-01-31 11:22:26 -0800105 LOG(ERROR) << "GetKeyCallback failed: " << static_cast<int>(error) << ", " << description;
Tri Vo3ab6f052022-11-22 10:26:16 -0800106 keyPromise_.set_value(std::nullopt);
107 return Status::ok();
108 }
109
110 private:
111 std::promise<std::optional<RemotelyProvisionedKey>> keyPromise_;
112 // This callback can only be called into once
113 std::atomic_flag called_;
114};
115
116class GetRegistrationCallback : public BnGetRegistrationCallback {
117 public:
118 GetRegistrationCallback(std::promise<std::optional<RemotelyProvisionedKey>> keyPromise,
119 uint32_t keyId)
120 : keyPromise_(std::move(keyPromise)), keyId_(keyId), called_() {}
121
122 Status onSuccess(const sp<IRegistration>& registration) override {
123 if (called_.test_and_set()) {
124 return Status::ok();
125 }
126 auto cb = sp<GetKeyCallback>::make(std::move(keyPromise_));
127 auto status = registration->getKey(keyId_, cb);
128 if (!status.isOk()) {
Seth Moore484010a2023-01-31 11:22:26 -0800129 cb->onError(IGetKeyCallback::ErrorCode::ERROR_UNKNOWN,
130 String16("Failed to register GetKeyCallback"));
Tri Vo3ab6f052022-11-22 10:26:16 -0800131 }
132 return Status::ok();
133 }
134 Status onCancel() override {
135 if (called_.test_and_set()) {
136 return Status::ok();
137 }
138 LOG(ERROR) << "GetRegistrationCallback cancelled";
139 keyPromise_.set_value(std::nullopt);
140 return Status::ok();
141 }
142 Status onError(const String16& error) override {
143 if (called_.test_and_set()) {
144 return Status::ok();
145 }
146 LOG(ERROR) << "GetRegistrationCallback failed: " << error;
147 keyPromise_.set_value(std::nullopt);
148 return Status::ok();
149 }
150
151 private:
152 std::promise<std::optional<RemotelyProvisionedKey>> keyPromise_;
153 int32_t keyId_;
154 // This callback can only be called into once
155 std::atomic_flag called_;
156};
157
158} // namespace
159
160std::optional<std::string> getRpcId(const sp<IRemotelyProvisionedComponent>& rpc) {
161 RpcHardwareInfo rpcHwInfo;
162 Status status = rpc->getHardwareInfo(&rpcHwInfo);
163 if (!status.isOk()) {
164 LOG(ERROR) << "Error getting remotely provisioned component hardware info: " << status;
165 return std::nullopt;
166 }
167
168 if (!rpcHwInfo.uniqueId) {
169 LOG(ERROR) << "Remotely provisioned component is missing a unique id, which is "
170 << "required for credential key remotely provisioned attestation keys. "
171 << "This is a bug in the vendor implementation.";
172 return std::nullopt;
173 }
174
175 return *rpcHwInfo.uniqueId;
176}
177
178std::optional<std::future<std::optional<RemotelyProvisionedKey>>>
179getRpcKeyFuture(const sp<IRemotelyProvisionedComponent>& rpc, int32_t keyId) {
180 std::promise<std::optional<RemotelyProvisionedKey>> keyPromise;
181 auto keyFuture = keyPromise.get_future();
182
183 auto rpcName = getRpcName(rpc);
184 if (!rpcName) {
185 LOG(ERROR) << "Failed to get IRemotelyProvisionedComponent name";
186 return std::nullopt;
187 }
188
189 sp<IRemoteProvisioning> remoteProvisioning =
Tri Vo71e8cc12023-01-17 15:37:50 -0800190 android::waitForService<IRemoteProvisioning>(String16(kRemoteProvisioningServiceName));
Tri Vo3ab6f052022-11-22 10:26:16 -0800191 if (!remoteProvisioning) {
192 LOG(ERROR) << "Failed to get IRemoteProvisioning HAL";
193 return std::nullopt;
194 }
195
196 auto cb = sp<GetRegistrationCallback>::make(std::move(keyPromise), keyId);
197 Status status = remoteProvisioning->getRegistration(*rpcName, cb);
198 if (!status.isOk()) {
199 LOG(ERROR) << "Failed getRegistration()";
200 return std::nullopt;
201 }
202
203 return keyFuture;
204}
205
206} // namespace identity
207} // namespace security
208} // namespace android