blob: 750a9d71cf147215c4a439471756e8dae2ba026e [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
Bonian Chen24d46bd2021-01-06 06:39:50 +000034 ConfigureRequest req;
Roberto Pereira24261972018-07-30 14:54:58 -070035 req.os_version = GetOsVersion();
36 req.os_patchlevel = GetOsPatchlevel();
37
Bonian Chen24d46bd2021-01-06 06:39:50 +000038 ConfigureResponse rsp;
Roberto Pereira24261972018-07-30 14:54:58 -070039 Configure(req, &rsp);
40
41 if (rsp.error != KM_ERROR_OK) {
42 ALOGE("Failed to configure keymaster %d", rsp.error);
43 return -1;
44 }
45
46 return 0;
47}
48
49TrustyKeymaster::TrustyKeymaster() {}
50
51TrustyKeymaster::~TrustyKeymaster() {
52 trusty_keymaster_disconnect();
53}
54
Bonian Chen24d46bd2021-01-06 06:39:50 +000055static void ForwardCommand(enum keymaster_command command, const Serializable& req,
Roberto Pereira24261972018-07-30 14:54:58 -070056 KeymasterResponse* rsp) {
57 keymaster_error_t err;
58 err = trusty_keymaster_send(command, req, rsp);
59 if (err != KM_ERROR_OK) {
60 ALOGE("Failed to send cmd %d err: %d", command, err);
61 rsp->error = err;
62 }
63}
64
65void TrustyKeymaster::GetVersion(const GetVersionRequest& request, GetVersionResponse* response) {
66 ForwardCommand(KM_GET_VERSION, request, response);
67}
68
69void TrustyKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
70 SupportedAlgorithmsResponse* response) {
71 ForwardCommand(KM_GET_SUPPORTED_ALGORITHMS, request, response);
72}
73
74void TrustyKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
75 SupportedBlockModesResponse* response) {
76 ForwardCommand(KM_GET_SUPPORTED_BLOCK_MODES, request, response);
77}
78
79void TrustyKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
80 SupportedPaddingModesResponse* response) {
81 ForwardCommand(KM_GET_SUPPORTED_PADDING_MODES, request, response);
82}
83
84void TrustyKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
85 SupportedDigestsResponse* response) {
86 ForwardCommand(KM_GET_SUPPORTED_DIGESTS, request, response);
87}
88
89void TrustyKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
90 SupportedImportFormatsResponse* response) {
91 ForwardCommand(KM_GET_SUPPORTED_IMPORT_FORMATS, request, response);
92}
93
94void TrustyKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
95 SupportedExportFormatsResponse* response) {
96 ForwardCommand(KM_GET_SUPPORTED_EXPORT_FORMATS, request, response);
97}
98
99void TrustyKeymaster::AddRngEntropy(const AddEntropyRequest& request,
100 AddEntropyResponse* response) {
101 ForwardCommand(KM_ADD_RNG_ENTROPY, request, response);
102}
103
104void TrustyKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
105 ForwardCommand(KM_CONFIGURE, request, response);
106}
107
108void TrustyKeymaster::GenerateKey(const GenerateKeyRequest& request,
109 GenerateKeyResponse* response) {
110 GenerateKeyRequest datedRequest(request.message_version);
111 datedRequest.key_description = request.key_description;
112
113 if (!request.key_description.Contains(TAG_CREATION_DATETIME)) {
114 datedRequest.key_description.push_back(TAG_CREATION_DATETIME, java_time(time(NULL)));
115 }
116
117 ForwardCommand(KM_GENERATE_KEY, datedRequest, response);
118}
119
120void TrustyKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
121 GetKeyCharacteristicsResponse* response) {
122 ForwardCommand(KM_GET_KEY_CHARACTERISTICS, request, response);
123}
124
125void TrustyKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
126 ForwardCommand(KM_IMPORT_KEY, request, response);
127}
128
129void TrustyKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
130 ImportWrappedKeyResponse* response) {
131 ForwardCommand(KM_IMPORT_WRAPPED_KEY, request, response);
132}
133
134void TrustyKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
135 ForwardCommand(KM_EXPORT_KEY, request, response);
136}
137
138void TrustyKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
139 ForwardCommand(KM_ATTEST_KEY, request, response);
140}
141
142void TrustyKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
143 ForwardCommand(KM_UPGRADE_KEY, request, response);
144}
145
146void TrustyKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
147 ForwardCommand(KM_DELETE_KEY, request, response);
148}
149
150void TrustyKeymaster::DeleteAllKeys(const DeleteAllKeysRequest& request,
151 DeleteAllKeysResponse* response) {
152 ForwardCommand(KM_DELETE_ALL_KEYS, request, response);
153}
154
155void TrustyKeymaster::BeginOperation(const BeginOperationRequest& request,
156 BeginOperationResponse* response) {
157 ForwardCommand(KM_BEGIN_OPERATION, request, response);
158}
159
160void TrustyKeymaster::UpdateOperation(const UpdateOperationRequest& request,
161 UpdateOperationResponse* response) {
162 ForwardCommand(KM_UPDATE_OPERATION, request, response);
163}
164
165void TrustyKeymaster::FinishOperation(const FinishOperationRequest& request,
166 FinishOperationResponse* response) {
167 ForwardCommand(KM_FINISH_OPERATION, request, response);
168}
169
170void TrustyKeymaster::AbortOperation(const AbortOperationRequest& request,
171 AbortOperationResponse* response) {
172 ForwardCommand(KM_ABORT_OPERATION, request, response);
173}
174
Roberto Pereira24261972018-07-30 14:54:58 -0700175GetHmacSharingParametersResponse TrustyKeymaster::GetHmacSharingParameters() {
Bonian Chen24d46bd2021-01-06 06:39:50 +0000176 // Empty buffer to allow ForwardCommand to have something to serialize
177 Buffer request;
178 GetHmacSharingParametersResponse response;
Matthew Maurerb321b412019-03-18 13:59:28 -0700179 ForwardCommand(KM_GET_HMAC_SHARING_PARAMETERS, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700180 return response;
181}
182
183ComputeSharedHmacResponse TrustyKeymaster::ComputeSharedHmac(
Matthew Maurerb321b412019-03-18 13:59:28 -0700184 const ComputeSharedHmacRequest& request) {
Bonian Chen24d46bd2021-01-06 06:39:50 +0000185 ComputeSharedHmacResponse response;
Matthew Maurerb321b412019-03-18 13:59:28 -0700186 ForwardCommand(KM_COMPUTE_SHARED_HMAC, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700187 return response;
188}
189
190VerifyAuthorizationResponse TrustyKeymaster::VerifyAuthorization(
Matthew Maurerb321b412019-03-18 13:59:28 -0700191 const VerifyAuthorizationRequest& request) {
Bonian Chen24d46bd2021-01-06 06:39:50 +0000192 VerifyAuthorizationResponse response;
Matthew Maurerb321b412019-03-18 13:59:28 -0700193 ForwardCommand(KM_VERIFY_AUTHORIZATION, request, &response);
Roberto Pereira24261972018-07-30 14:54:58 -0700194 return response;
195}
196
Roberto Pereira24261972018-07-30 14:54:58 -0700197} // namespace keymaster