blob: ef5fc3fc2bbd504eb1c2eb5469d52029a2b72700 [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
82 return 0;
83}
84
85TrustyKeymaster::TrustyKeymaster() {}
86
87TrustyKeymaster::~TrustyKeymaster() {
88 trusty_keymaster_disconnect();
89}
90
Shawn Willden9323f412021-01-06 19:15:29 +000091static void ForwardCommand(enum keymaster_command command, const KeymasterMessage& req,
Roberto Pereira24261972018-07-30 14:54:58 -070092 KeymasterResponse* rsp) {
93 keymaster_error_t err;
94 err = trusty_keymaster_send(command, req, rsp);
95 if (err != KM_ERROR_OK) {
Shawn Willdenfed81d82021-04-22 13:32:56 -060096 LOG(ERROR) << "Cmd " << command << " returned error: " << err;
Roberto Pereira24261972018-07-30 14:54:58 -070097 rsp->error = err;
98 }
99}
100
101void TrustyKeymaster::GetVersion(const GetVersionRequest& request, GetVersionResponse* response) {
102 ForwardCommand(KM_GET_VERSION, request, response);
103}
104
105void TrustyKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
106 SupportedAlgorithmsResponse* response) {
107 ForwardCommand(KM_GET_SUPPORTED_ALGORITHMS, request, response);
108}
109
110void TrustyKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
111 SupportedBlockModesResponse* response) {
112 ForwardCommand(KM_GET_SUPPORTED_BLOCK_MODES, request, response);
113}
114
115void TrustyKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
116 SupportedPaddingModesResponse* response) {
117 ForwardCommand(KM_GET_SUPPORTED_PADDING_MODES, request, response);
118}
119
120void TrustyKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
121 SupportedDigestsResponse* response) {
122 ForwardCommand(KM_GET_SUPPORTED_DIGESTS, request, response);
123}
124
125void TrustyKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
126 SupportedImportFormatsResponse* response) {
127 ForwardCommand(KM_GET_SUPPORTED_IMPORT_FORMATS, request, response);
128}
129
130void TrustyKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
131 SupportedExportFormatsResponse* response) {
132 ForwardCommand(KM_GET_SUPPORTED_EXPORT_FORMATS, request, response);
133}
134
135void TrustyKeymaster::AddRngEntropy(const AddEntropyRequest& request,
136 AddEntropyResponse* response) {
137 ForwardCommand(KM_ADD_RNG_ENTROPY, request, response);
138}
139
140void TrustyKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
141 ForwardCommand(KM_CONFIGURE, request, response);
142}
143
144void TrustyKeymaster::GenerateKey(const GenerateKeyRequest& request,
145 GenerateKeyResponse* response) {
Shawn Willdenfed81d82021-04-22 13:32:56 -0600146 if (message_version_ < 4) {
147 // Pre-KeyMint we need to add TAG_CREATION_DATETIME if not provided by the caller.
148 GenerateKeyRequest datedRequest(request.message_version);
149 datedRequest.key_description = request.key_description;
Roberto Pereira24261972018-07-30 14:54:58 -0700150
Shawn Willdenfed81d82021-04-22 13:32:56 -0600151 if (!request.key_description.Contains(TAG_CREATION_DATETIME)) {
152 datedRequest.key_description.push_back(TAG_CREATION_DATETIME, java_time(time(NULL)));
153 }
154
155 ForwardCommand(KM_GENERATE_KEY, datedRequest, response);
156 } else {
157 ForwardCommand(KM_GENERATE_KEY, request, response);
Roberto Pereira24261972018-07-30 14:54:58 -0700158 }
Roberto Pereira24261972018-07-30 14:54:58 -0700159}
160
161void TrustyKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
162 GetKeyCharacteristicsResponse* response) {
163 ForwardCommand(KM_GET_KEY_CHARACTERISTICS, request, response);
164}
165
166void TrustyKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
167 ForwardCommand(KM_IMPORT_KEY, request, response);
168}
169
170void TrustyKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
171 ImportWrappedKeyResponse* response) {
172 ForwardCommand(KM_IMPORT_WRAPPED_KEY, request, response);
173}
174
175void TrustyKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
176 ForwardCommand(KM_EXPORT_KEY, request, response);
177}
178
179void TrustyKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
180 ForwardCommand(KM_ATTEST_KEY, request, response);
181}
182
183void TrustyKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
184 ForwardCommand(KM_UPGRADE_KEY, request, response);
185}
186
187void TrustyKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
188 ForwardCommand(KM_DELETE_KEY, request, response);
189}
190
191void TrustyKeymaster::DeleteAllKeys(const DeleteAllKeysRequest& request,
192 DeleteAllKeysResponse* response) {
193 ForwardCommand(KM_DELETE_ALL_KEYS, request, response);
194}
195
196void TrustyKeymaster::BeginOperation(const BeginOperationRequest& request,
197 BeginOperationResponse* response) {
198 ForwardCommand(KM_BEGIN_OPERATION, request, response);
199}
200
201void TrustyKeymaster::UpdateOperation(const UpdateOperationRequest& request,
202 UpdateOperationResponse* response) {
203 ForwardCommand(KM_UPDATE_OPERATION, request, response);
204}
205
206void TrustyKeymaster::FinishOperation(const FinishOperationRequest& request,
207 FinishOperationResponse* response) {
208 ForwardCommand(KM_FINISH_OPERATION, request, response);
209}
210
211void TrustyKeymaster::AbortOperation(const AbortOperationRequest& request,
212 AbortOperationResponse* response) {
213 ForwardCommand(KM_ABORT_OPERATION, request, response);
214}
215
Roberto Pereira24261972018-07-30 14:54:58 -0700216GetHmacSharingParametersResponse TrustyKeymaster::GetHmacSharingParameters() {
Shawn Willden9323f412021-01-06 19:15:29 +0000217 GetHmacSharingParametersRequest request(message_version());
218 GetHmacSharingParametersResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700219 ForwardCommand(KM_GET_HMAC_SHARING_PARAMETERS, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700220 return response;
221}
222
223ComputeSharedHmacResponse TrustyKeymaster::ComputeSharedHmac(
Matthew Maurerb321b412019-03-18 13:59:28 -0700224 const ComputeSharedHmacRequest& request) {
Shawn Willden9323f412021-01-06 19:15:29 +0000225 ComputeSharedHmacResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700226 ForwardCommand(KM_COMPUTE_SHARED_HMAC, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700227 return response;
228}
229
230VerifyAuthorizationResponse TrustyKeymaster::VerifyAuthorization(
Matthew Maurerb321b412019-03-18 13:59:28 -0700231 const VerifyAuthorizationRequest& request) {
Shawn Willden9323f412021-01-06 19:15:29 +0000232 VerifyAuthorizationResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700233 ForwardCommand(KM_VERIFY_AUTHORIZATION, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700234 return response;
235}
236
Shawn Willden9323f412021-01-06 19:15:29 +0000237GetVersion2Response TrustyKeymaster::GetVersion2(const GetVersion2Request& request) {
238 GetVersion2Response response(message_version());
239 ForwardCommand(KM_GET_VERSION_2, request, &response);
240 return response;
241}
242
Shawn Willdenfed81d82021-04-22 13:32:56 -0600243EarlyBootEndedResponse TrustyKeymaster::EarlyBootEnded() {
244 EarlyBootEndedResponse response(message_version());
245 ForwardCommand(KM_EARLY_BOOT_ENDED, EarlyBootEndedRequest(message_version()), &response);
246 return response;
247}
248
249DeviceLockedResponse TrustyKeymaster::DeviceLocked(const DeviceLockedRequest& request) {
250 DeviceLockedResponse response(message_version());
251 ForwardCommand(KM_DEVICE_LOCKED, request, &response);
252 return response;
253}
254
Roberto Pereira24261972018-07-30 14:54:58 -0700255} // namespace keymaster