blob: 569003129849f57ee0a790eab9a3a854cc616f3c [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
17#include <cutils/log.h>
18#include <keymaster/android_keymaster_messages.h>
19#include <keymaster/keymaster_configuration.h>
20#include <trusty_keymaster/TrustyKeymaster.h>
21#include <trusty_keymaster/ipc/trusty_keymaster_ipc.h>
22
23namespace keymaster {
24
25int TrustyKeymaster::Initialize() {
26 int err;
27
28 err = trusty_keymaster_connect();
29 if (err) {
30 ALOGE("Failed to connect to trusty keymaster %d", err);
31 return err;
32 }
33
Shawn Willden9323f412021-01-06 19:15:29 +000034 // Try GetVersion2 first.
35 GetVersion2Request versionReq;
36 GetVersion2Response versionRsp = GetVersion2(versionReq);
37 if (versionRsp.error != KM_ERROR_OK) {
38 ALOGW("TA appears not to support GetVersion2, falling back (err = %d)", versionRsp.error);
39
40 GetVersionRequest versionReq;
41 GetVersionResponse versionRsp;
42 GetVersion(versionReq, &versionRsp);
43 if (versionRsp.error != KM_ERROR_OK) {
44 ALOGE("Failed to get TA version %d", versionRsp.error);
45 return -1;
46 } else {
47 keymaster_error_t error;
48 message_version_ = NegotiateMessageVersion(versionRsp, &error);
49 if (error != KM_ERROR_OK) {
50 ALOGE("Failed to negotiate message version %d", error);
51 return -1;
52 }
53 }
54 } else {
55 message_version_ = NegotiateMessageVersion(versionReq, versionRsp);
56 }
57
58 ConfigureRequest req(message_version());
Roberto Pereira24261972018-07-30 14:54:58 -070059 req.os_version = GetOsVersion();
60 req.os_patchlevel = GetOsPatchlevel();
61
Shawn Willden9323f412021-01-06 19:15:29 +000062 ConfigureResponse rsp(message_version());
Roberto Pereira24261972018-07-30 14:54:58 -070063 Configure(req, &rsp);
64
65 if (rsp.error != KM_ERROR_OK) {
66 ALOGE("Failed to configure keymaster %d", rsp.error);
67 return -1;
68 }
69
70 return 0;
71}
72
73TrustyKeymaster::TrustyKeymaster() {}
74
75TrustyKeymaster::~TrustyKeymaster() {
76 trusty_keymaster_disconnect();
77}
78
Shawn Willden9323f412021-01-06 19:15:29 +000079static void ForwardCommand(enum keymaster_command command, const KeymasterMessage& req,
Roberto Pereira24261972018-07-30 14:54:58 -070080 KeymasterResponse* rsp) {
81 keymaster_error_t err;
82 err = trusty_keymaster_send(command, req, rsp);
83 if (err != KM_ERROR_OK) {
84 ALOGE("Failed to send cmd %d err: %d", command, err);
85 rsp->error = err;
86 }
87}
88
89void TrustyKeymaster::GetVersion(const GetVersionRequest& request, GetVersionResponse* response) {
90 ForwardCommand(KM_GET_VERSION, request, response);
91}
92
93void TrustyKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
94 SupportedAlgorithmsResponse* response) {
95 ForwardCommand(KM_GET_SUPPORTED_ALGORITHMS, request, response);
96}
97
98void TrustyKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
99 SupportedBlockModesResponse* response) {
100 ForwardCommand(KM_GET_SUPPORTED_BLOCK_MODES, request, response);
101}
102
103void TrustyKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
104 SupportedPaddingModesResponse* response) {
105 ForwardCommand(KM_GET_SUPPORTED_PADDING_MODES, request, response);
106}
107
108void TrustyKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
109 SupportedDigestsResponse* response) {
110 ForwardCommand(KM_GET_SUPPORTED_DIGESTS, request, response);
111}
112
113void TrustyKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
114 SupportedImportFormatsResponse* response) {
115 ForwardCommand(KM_GET_SUPPORTED_IMPORT_FORMATS, request, response);
116}
117
118void TrustyKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
119 SupportedExportFormatsResponse* response) {
120 ForwardCommand(KM_GET_SUPPORTED_EXPORT_FORMATS, request, response);
121}
122
123void TrustyKeymaster::AddRngEntropy(const AddEntropyRequest& request,
124 AddEntropyResponse* response) {
125 ForwardCommand(KM_ADD_RNG_ENTROPY, request, response);
126}
127
128void TrustyKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
129 ForwardCommand(KM_CONFIGURE, request, response);
130}
131
132void TrustyKeymaster::GenerateKey(const GenerateKeyRequest& request,
133 GenerateKeyResponse* response) {
134 GenerateKeyRequest datedRequest(request.message_version);
135 datedRequest.key_description = request.key_description;
136
137 if (!request.key_description.Contains(TAG_CREATION_DATETIME)) {
138 datedRequest.key_description.push_back(TAG_CREATION_DATETIME, java_time(time(NULL)));
139 }
140
141 ForwardCommand(KM_GENERATE_KEY, datedRequest, response);
142}
143
144void TrustyKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
145 GetKeyCharacteristicsResponse* response) {
146 ForwardCommand(KM_GET_KEY_CHARACTERISTICS, request, response);
147}
148
149void TrustyKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
150 ForwardCommand(KM_IMPORT_KEY, request, response);
151}
152
153void TrustyKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
154 ImportWrappedKeyResponse* response) {
155 ForwardCommand(KM_IMPORT_WRAPPED_KEY, request, response);
156}
157
158void TrustyKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
159 ForwardCommand(KM_EXPORT_KEY, request, response);
160}
161
162void TrustyKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
163 ForwardCommand(KM_ATTEST_KEY, request, response);
164}
165
166void TrustyKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
167 ForwardCommand(KM_UPGRADE_KEY, request, response);
168}
169
170void TrustyKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
171 ForwardCommand(KM_DELETE_KEY, request, response);
172}
173
174void TrustyKeymaster::DeleteAllKeys(const DeleteAllKeysRequest& request,
175 DeleteAllKeysResponse* response) {
176 ForwardCommand(KM_DELETE_ALL_KEYS, request, response);
177}
178
179void TrustyKeymaster::BeginOperation(const BeginOperationRequest& request,
180 BeginOperationResponse* response) {
181 ForwardCommand(KM_BEGIN_OPERATION, request, response);
182}
183
184void TrustyKeymaster::UpdateOperation(const UpdateOperationRequest& request,
185 UpdateOperationResponse* response) {
186 ForwardCommand(KM_UPDATE_OPERATION, request, response);
187}
188
189void TrustyKeymaster::FinishOperation(const FinishOperationRequest& request,
190 FinishOperationResponse* response) {
191 ForwardCommand(KM_FINISH_OPERATION, request, response);
192}
193
194void TrustyKeymaster::AbortOperation(const AbortOperationRequest& request,
195 AbortOperationResponse* response) {
196 ForwardCommand(KM_ABORT_OPERATION, request, response);
197}
198
Roberto Pereira24261972018-07-30 14:54:58 -0700199GetHmacSharingParametersResponse TrustyKeymaster::GetHmacSharingParameters() {
Shawn Willden9323f412021-01-06 19:15:29 +0000200 GetHmacSharingParametersRequest request(message_version());
201 GetHmacSharingParametersResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700202 ForwardCommand(KM_GET_HMAC_SHARING_PARAMETERS, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700203 return response;
204}
205
206ComputeSharedHmacResponse TrustyKeymaster::ComputeSharedHmac(
Matthew Maurerb321b412019-03-18 13:59:28 -0700207 const ComputeSharedHmacRequest& request) {
Shawn Willden9323f412021-01-06 19:15:29 +0000208 ComputeSharedHmacResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700209 ForwardCommand(KM_COMPUTE_SHARED_HMAC, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700210 return response;
211}
212
213VerifyAuthorizationResponse TrustyKeymaster::VerifyAuthorization(
Matthew Maurerb321b412019-03-18 13:59:28 -0700214 const VerifyAuthorizationRequest& request) {
Shawn Willden9323f412021-01-06 19:15:29 +0000215 VerifyAuthorizationResponse response(message_version());
Matthew Maurerb321b412019-03-18 13:59:28 -0700216 ForwardCommand(KM_VERIFY_AUTHORIZATION, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700217 return response;
218}
219
Shawn Willden9323f412021-01-06 19:15:29 +0000220GetVersion2Response TrustyKeymaster::GetVersion2(const GetVersion2Request& request) {
221 GetVersion2Response response(message_version());
222 ForwardCommand(KM_GET_VERSION_2, request, &response);
223 return response;
224}
225
Roberto Pereira24261972018-07-30 14:54:58 -0700226} // namespace keymaster