blob: e77940a1fdf76ec11f7a1467aa4bf3701a6e0400 [file] [log] [blame]
Roberto Pereira24261972018-07-30 14:54:58 -07001/*
2 * Copyright 2018 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
Shawn Willdenfed81d82021-04-22 13:32:56 -060017#define LOG_TAG "trusty_keymaster_hal"
18#include <android-base/logging.h>
19
Roberto Pereira24261972018-07-30 14:54:58 -070020#include <keymaster/android_keymaster_messages.h>
21#include <keymaster/keymaster_configuration.h>
22#include <trusty_keymaster/TrustyKeymaster.h>
23#include <trusty_keymaster/ipc/trusty_keymaster_ipc.h>
24
25namespace keymaster {
26
Shawn Willdenfed81d82021-04-22 13:32:56 -060027int TrustyKeymaster::Initialize(KmVersion version) {
Roberto Pereira24261972018-07-30 14:54:58 -070028 int err;
29
Shawn Willdenfed81d82021-04-22 13:32:56 -060030 LOG(INFO) << "Initializing TrustyKeymaster as KmVersion: " << (int)version;
31
Roberto Pereira24261972018-07-30 14:54:58 -070032 err = trusty_keymaster_connect();
33 if (err) {
Shawn Willdenfed81d82021-04-22 13:32:56 -060034 LOG(ERROR) << "Failed to connect to trusty keymaster (1st try)" << err;
Roberto Pereira24261972018-07-30 14:54:58 -070035 return err;
36 }
37
Shawn Willden9323f412021-01-06 19:15:29 +000038 // Try GetVersion2 first.
39 GetVersion2Request versionReq;
Shawn Willdenfed81d82021-04-22 13:32:56 -060040 versionReq.max_message_version = MessageVersion(version);
Shawn Willden9323f412021-01-06 19:15:29 +000041 GetVersion2Response versionRsp = GetVersion2(versionReq);
42 if (versionRsp.error != KM_ERROR_OK) {
Shawn Willdenfed81d82021-04-22 13:32:56 -060043 LOG(WARNING) << "TA appears not to support GetVersion2, falling back (err = "
44 << versionRsp.error << ")";
Shawn Willden9323f412021-01-06 19:15:29 +000045
Matthew Maurerc4abbe62021-01-20 13:19:13 -080046 err = trusty_keymaster_connect();
47 if (err) {
Shawn Willdenfed81d82021-04-22 13:32:56 -060048 LOG(FATAL) << "Failed to connect to trusty keymaster (2nd try) " << err;
Matthew Maurerc4abbe62021-01-20 13:19:13 -080049 return err;
50 }
51
Shawn Willden9323f412021-01-06 19:15:29 +000052 GetVersionRequest versionReq;
53 GetVersionResponse versionRsp;
54 GetVersion(versionReq, &versionRsp);
55 if (versionRsp.error != KM_ERROR_OK) {
Shawn Willdenfed81d82021-04-22 13:32:56 -060056 LOG(FATAL) << "Failed to get TA version " << versionRsp.error;
Shawn Willden9323f412021-01-06 19:15:29 +000057 return -1;
58 } else {
59 keymaster_error_t error;
60 message_version_ = NegotiateMessageVersion(versionRsp, &error);
61 if (error != KM_ERROR_OK) {
Shawn Willdenfed81d82021-04-22 13:32:56 -060062 LOG(FATAL) << "Failed to negotiate message version " << error;
Shawn Willden9323f412021-01-06 19:15:29 +000063 return -1;
64 }
65 }
66 } else {
67 message_version_ = NegotiateMessageVersion(versionReq, versionRsp);
68 }
69
70 ConfigureRequest req(message_version());
Roberto Pereira24261972018-07-30 14:54:58 -070071 req.os_version = GetOsVersion();
72 req.os_patchlevel = GetOsPatchlevel();
73
Shawn Willden9323f412021-01-06 19:15:29 +000074 ConfigureResponse rsp(message_version());
Roberto Pereira24261972018-07-30 14:54:58 -070075 Configure(req, &rsp);
76
77 if (rsp.error != KM_ERROR_OK) {
Shawn Willdenfed81d82021-04-22 13:32:56 -060078 LOG(FATAL) << "Failed to configure keymaster " << rsp.error;
Roberto Pereira24261972018-07-30 14:54:58 -070079 return -1;
80 }
81
David Drysdalee7697d72021-07-13 12:17:17 +010082 // Set the vendor patchlevel to value retrieved from system property (which
83 // requires SELinux permission).
84 ConfigureVendorPatchlevelRequest vendor_req(message_version());
85 vendor_req.vendor_patchlevel = GetVendorPatchlevel();
86 ConfigureVendorPatchlevelResponse vendor_rsp = ConfigureVendorPatchlevel(vendor_req);
87 if (vendor_rsp.error != KM_ERROR_OK) {
88 LOG(ERROR) << "Failed to configure keymaster vendor patchlevel: " << vendor_rsp.error;
89 // Don't fail if this message isn't understood.
90 }
91
Roberto Pereira24261972018-07-30 14:54:58 -070092 return 0;
93}
94
95TrustyKeymaster::TrustyKeymaster() {}
96
97TrustyKeymaster::~TrustyKeymaster() {
98 trusty_keymaster_disconnect();
99}
100
Shawn Willden9323f412021-01-06 19:15:29 +0000101static void ForwardCommand(enum keymaster_command command, const KeymasterMessage& req,
Roberto Pereira24261972018-07-30 14:54:58 -0700102 KeymasterResponse* rsp) {
103 keymaster_error_t err;
104 err = trusty_keymaster_send(command, req, rsp);
105 if (err != KM_ERROR_OK) {
Shawn Willdenfed81d82021-04-22 13:32:56 -0600106 LOG(ERROR) << "Cmd " << command << " returned error: " << err;
Roberto Pereira24261972018-07-30 14:54:58 -0700107 rsp->error = err;
108 }
109}
110
111void TrustyKeymaster::GetVersion(const GetVersionRequest& request, GetVersionResponse* response) {
112 ForwardCommand(KM_GET_VERSION, request, response);
113}
114
Jim Blacklerfc6e3d32021-10-11 13:38:59 +0000115void TrustyKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
116 SupportedAlgorithmsResponse* response) {
117 ForwardCommand(KM_GET_SUPPORTED_ALGORITHMS, request, response);
118}
119
120void TrustyKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
121 SupportedBlockModesResponse* response) {
122 ForwardCommand(KM_GET_SUPPORTED_BLOCK_MODES, request, response);
123}
124
125void TrustyKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
126 SupportedPaddingModesResponse* response) {
127 ForwardCommand(KM_GET_SUPPORTED_PADDING_MODES, request, response);
128}
129
130void TrustyKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
131 SupportedDigestsResponse* response) {
132 ForwardCommand(KM_GET_SUPPORTED_DIGESTS, request, response);
133}
134
135void TrustyKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
136 SupportedImportFormatsResponse* response) {
137 ForwardCommand(KM_GET_SUPPORTED_IMPORT_FORMATS, request, response);
138}
139
140void TrustyKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
141 SupportedExportFormatsResponse* response) {
142 ForwardCommand(KM_GET_SUPPORTED_EXPORT_FORMATS, request, response);
143}
144
Roberto Pereira24261972018-07-30 14:54:58 -0700145void TrustyKeymaster::AddRngEntropy(const AddEntropyRequest& request,
146 AddEntropyResponse* response) {
147 ForwardCommand(KM_ADD_RNG_ENTROPY, request, response);
148}
149
150void TrustyKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
151 ForwardCommand(KM_CONFIGURE, request, response);
152}
153
154void TrustyKeymaster::GenerateKey(const GenerateKeyRequest& request,
155 GenerateKeyResponse* response) {
Shawn Willdenfed81d82021-04-22 13:32:56 -0600156 if (message_version_ < 4) {
157 // Pre-KeyMint we need to add TAG_CREATION_DATETIME if not provided by the caller.
158 GenerateKeyRequest datedRequest(request.message_version);
159 datedRequest.key_description = request.key_description;
Roberto Pereira24261972018-07-30 14:54:58 -0700160
Shawn Willdenfed81d82021-04-22 13:32:56 -0600161 if (!request.key_description.Contains(TAG_CREATION_DATETIME)) {
162 datedRequest.key_description.push_back(TAG_CREATION_DATETIME, java_time(time(NULL)));
163 }
164
165 ForwardCommand(KM_GENERATE_KEY, datedRequest, response);
166 } else {
167 ForwardCommand(KM_GENERATE_KEY, request, response);
Roberto Pereira24261972018-07-30 14:54:58 -0700168 }
Roberto Pereira24261972018-07-30 14:54:58 -0700169}
170
Max Bires95b5b042021-06-09 17:40:54 -0700171void TrustyKeymaster::GenerateRkpKey(const GenerateRkpKeyRequest& request,
172 GenerateRkpKeyResponse* response) {
173 ForwardCommand(KM_GENERATE_RKP_KEY, request, response);
174}
175
176void TrustyKeymaster::GenerateCsr(const GenerateCsrRequest& request,
177 GenerateCsrResponse* response) {
178 ForwardCommand(KM_GENERATE_CSR, request, response);
179}
180
Roberto Pereira24261972018-07-30 14:54:58 -0700181void TrustyKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
182 GetKeyCharacteristicsResponse* response) {
183 ForwardCommand(KM_GET_KEY_CHARACTERISTICS, request, response);
184}
185
186void TrustyKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
187 ForwardCommand(KM_IMPORT_KEY, request, response);
188}
189
190void TrustyKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
191 ImportWrappedKeyResponse* response) {
192 ForwardCommand(KM_IMPORT_WRAPPED_KEY, request, response);
193}
194
195void TrustyKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
196 ForwardCommand(KM_EXPORT_KEY, request, response);
197}
198
199void TrustyKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
200 ForwardCommand(KM_ATTEST_KEY, request, response);
201}
202
203void TrustyKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
204 ForwardCommand(KM_UPGRADE_KEY, request, response);
205}
206
207void TrustyKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
208 ForwardCommand(KM_DELETE_KEY, request, response);
209}
210
211void TrustyKeymaster::DeleteAllKeys(const DeleteAllKeysRequest& request,
212 DeleteAllKeysResponse* response) {
213 ForwardCommand(KM_DELETE_ALL_KEYS, request, response);
214}
215
216void TrustyKeymaster::BeginOperation(const BeginOperationRequest& request,
217 BeginOperationResponse* response) {
218 ForwardCommand(KM_BEGIN_OPERATION, request, response);
219}
220
221void TrustyKeymaster::UpdateOperation(const UpdateOperationRequest& request,
222 UpdateOperationResponse* response) {
223 ForwardCommand(KM_UPDATE_OPERATION, request, response);
224}
225
226void TrustyKeymaster::FinishOperation(const FinishOperationRequest& request,
227 FinishOperationResponse* response) {
228 ForwardCommand(KM_FINISH_OPERATION, request, response);
229}
230
231void TrustyKeymaster::AbortOperation(const AbortOperationRequest& request,
232 AbortOperationResponse* response) {
233 ForwardCommand(KM_ABORT_OPERATION, request, response);
234}
235
Roberto Pereira24261972018-07-30 14:54:58 -0700236GetHmacSharingParametersResponse TrustyKeymaster::GetHmacSharingParameters() {
Shawn Willden9323f412021-01-06 19:15:29 +0000237 GetHmacSharingParametersRequest request(message_version());
238 GetHmacSharingParametersResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700239 ForwardCommand(KM_GET_HMAC_SHARING_PARAMETERS, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700240 return response;
241}
242
243ComputeSharedHmacResponse TrustyKeymaster::ComputeSharedHmac(
Matthew Maurerb321b412019-03-18 13:59:28 -0700244 const ComputeSharedHmacRequest& request) {
Shawn Willden9323f412021-01-06 19:15:29 +0000245 ComputeSharedHmacResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700246 ForwardCommand(KM_COMPUTE_SHARED_HMAC, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700247 return response;
248}
249
250VerifyAuthorizationResponse TrustyKeymaster::VerifyAuthorization(
Matthew Maurerb321b412019-03-18 13:59:28 -0700251 const VerifyAuthorizationRequest& request) {
Shawn Willden9323f412021-01-06 19:15:29 +0000252 VerifyAuthorizationResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700253 ForwardCommand(KM_VERIFY_AUTHORIZATION, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700254 return response;
255}
256
Shawn Willden9323f412021-01-06 19:15:29 +0000257GetVersion2Response TrustyKeymaster::GetVersion2(const GetVersion2Request& request) {
258 GetVersion2Response response(message_version());
259 ForwardCommand(KM_GET_VERSION_2, request, &response);
260 return response;
261}
262
Shawn Willdenfed81d82021-04-22 13:32:56 -0600263EarlyBootEndedResponse TrustyKeymaster::EarlyBootEnded() {
264 EarlyBootEndedResponse response(message_version());
265 ForwardCommand(KM_EARLY_BOOT_ENDED, EarlyBootEndedRequest(message_version()), &response);
266 return response;
267}
268
269DeviceLockedResponse TrustyKeymaster::DeviceLocked(const DeviceLockedRequest& request) {
270 DeviceLockedResponse response(message_version());
271 ForwardCommand(KM_DEVICE_LOCKED, request, &response);
272 return response;
273}
274
David Drysdalee7697d72021-07-13 12:17:17 +0100275ConfigureVendorPatchlevelResponse TrustyKeymaster::ConfigureVendorPatchlevel(
276 const ConfigureVendorPatchlevelRequest& request) {
277 ConfigureVendorPatchlevelResponse response(message_version());
278 ForwardCommand(KM_CONFIGURE_VENDOR_PATCHLEVEL, request, &response);
279 return response;
280}
281
Shawn Willden396bc3f2022-06-07 13:17:48 -0600282GetRootOfTrustResponse TrustyKeymaster::GetRootOfTrust(const GetRootOfTrustRequest& request) {
283 GetRootOfTrustResponse response(message_version());
284 ForwardCommand(KM_GET_ROOT_OF_TRUST, request, &response);
285 return response;
286}
287
Roberto Pereira24261972018-07-30 14:54:58 -0700288} // namespace keymaster