• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
23 namespace keymaster {
24 
Initialize()25 int 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 
34     ConfigureRequest req;
35     req.os_version = GetOsVersion();
36     req.os_patchlevel = GetOsPatchlevel();
37 
38     ConfigureResponse rsp;
39     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 
TrustyKeymaster()49 TrustyKeymaster::TrustyKeymaster() {}
50 
~TrustyKeymaster()51 TrustyKeymaster::~TrustyKeymaster() {
52     trusty_keymaster_disconnect();
53 }
54 
ForwardCommand(enum keymaster_command command,const Serializable & req,KeymasterResponse * rsp)55 static void ForwardCommand(enum keymaster_command command, const Serializable& req,
56                            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 
GetVersion(const GetVersionRequest & request,GetVersionResponse * response)65 void TrustyKeymaster::GetVersion(const GetVersionRequest& request, GetVersionResponse* response) {
66     ForwardCommand(KM_GET_VERSION, request, response);
67 }
68 
SupportedAlgorithms(const SupportedAlgorithmsRequest & request,SupportedAlgorithmsResponse * response)69 void TrustyKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
70                                           SupportedAlgorithmsResponse* response) {
71     ForwardCommand(KM_GET_SUPPORTED_ALGORITHMS, request, response);
72 }
73 
SupportedBlockModes(const SupportedBlockModesRequest & request,SupportedBlockModesResponse * response)74 void TrustyKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
75                                           SupportedBlockModesResponse* response) {
76     ForwardCommand(KM_GET_SUPPORTED_BLOCK_MODES, request, response);
77 }
78 
SupportedPaddingModes(const SupportedPaddingModesRequest & request,SupportedPaddingModesResponse * response)79 void TrustyKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
80                                             SupportedPaddingModesResponse* response) {
81     ForwardCommand(KM_GET_SUPPORTED_PADDING_MODES, request, response);
82 }
83 
SupportedDigests(const SupportedDigestsRequest & request,SupportedDigestsResponse * response)84 void TrustyKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
85                                        SupportedDigestsResponse* response) {
86     ForwardCommand(KM_GET_SUPPORTED_DIGESTS, request, response);
87 }
88 
SupportedImportFormats(const SupportedImportFormatsRequest & request,SupportedImportFormatsResponse * response)89 void TrustyKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
90                                              SupportedImportFormatsResponse* response) {
91     ForwardCommand(KM_GET_SUPPORTED_IMPORT_FORMATS, request, response);
92 }
93 
SupportedExportFormats(const SupportedExportFormatsRequest & request,SupportedExportFormatsResponse * response)94 void TrustyKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
95                                              SupportedExportFormatsResponse* response) {
96     ForwardCommand(KM_GET_SUPPORTED_EXPORT_FORMATS, request, response);
97 }
98 
AddRngEntropy(const AddEntropyRequest & request,AddEntropyResponse * response)99 void TrustyKeymaster::AddRngEntropy(const AddEntropyRequest& request,
100                                     AddEntropyResponse* response) {
101     ForwardCommand(KM_ADD_RNG_ENTROPY, request, response);
102 }
103 
Configure(const ConfigureRequest & request,ConfigureResponse * response)104 void TrustyKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
105     ForwardCommand(KM_CONFIGURE, request, response);
106 }
107 
GenerateKey(const GenerateKeyRequest & request,GenerateKeyResponse * response)108 void 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 
GetKeyCharacteristics(const GetKeyCharacteristicsRequest & request,GetKeyCharacteristicsResponse * response)120 void TrustyKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
121                                             GetKeyCharacteristicsResponse* response) {
122     ForwardCommand(KM_GET_KEY_CHARACTERISTICS, request, response);
123 }
124 
ImportKey(const ImportKeyRequest & request,ImportKeyResponse * response)125 void TrustyKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
126     ForwardCommand(KM_IMPORT_KEY, request, response);
127 }
128 
ImportWrappedKey(const ImportWrappedKeyRequest & request,ImportWrappedKeyResponse * response)129 void TrustyKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
130                                        ImportWrappedKeyResponse* response) {
131     ForwardCommand(KM_IMPORT_WRAPPED_KEY, request, response);
132 }
133 
ExportKey(const ExportKeyRequest & request,ExportKeyResponse * response)134 void TrustyKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
135     ForwardCommand(KM_EXPORT_KEY, request, response);
136 }
137 
AttestKey(const AttestKeyRequest & request,AttestKeyResponse * response)138 void TrustyKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
139     ForwardCommand(KM_ATTEST_KEY, request, response);
140 }
141 
UpgradeKey(const UpgradeKeyRequest & request,UpgradeKeyResponse * response)142 void TrustyKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
143     ForwardCommand(KM_UPGRADE_KEY, request, response);
144 }
145 
DeleteKey(const DeleteKeyRequest & request,DeleteKeyResponse * response)146 void TrustyKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
147     ForwardCommand(KM_DELETE_KEY, request, response);
148 }
149 
DeleteAllKeys(const DeleteAllKeysRequest & request,DeleteAllKeysResponse * response)150 void TrustyKeymaster::DeleteAllKeys(const DeleteAllKeysRequest& request,
151                                     DeleteAllKeysResponse* response) {
152     ForwardCommand(KM_DELETE_ALL_KEYS, request, response);
153 }
154 
BeginOperation(const BeginOperationRequest & request,BeginOperationResponse * response)155 void TrustyKeymaster::BeginOperation(const BeginOperationRequest& request,
156                                      BeginOperationResponse* response) {
157     ForwardCommand(KM_BEGIN_OPERATION, request, response);
158 }
159 
UpdateOperation(const UpdateOperationRequest & request,UpdateOperationResponse * response)160 void TrustyKeymaster::UpdateOperation(const UpdateOperationRequest& request,
161                                       UpdateOperationResponse* response) {
162     ForwardCommand(KM_UPDATE_OPERATION, request, response);
163 }
164 
FinishOperation(const FinishOperationRequest & request,FinishOperationResponse * response)165 void TrustyKeymaster::FinishOperation(const FinishOperationRequest& request,
166                                       FinishOperationResponse* response) {
167     ForwardCommand(KM_FINISH_OPERATION, request, response);
168 }
169 
AbortOperation(const AbortOperationRequest & request,AbortOperationResponse * response)170 void TrustyKeymaster::AbortOperation(const AbortOperationRequest& request,
171                                      AbortOperationResponse* response) {
172     ForwardCommand(KM_ABORT_OPERATION, request, response);
173 }
174 
175 /* Methods for Keymaster 4.0 functionality -- not yet implemented */
GetHmacSharingParameters()176 GetHmacSharingParametersResponse TrustyKeymaster::GetHmacSharingParameters() {
177     GetHmacSharingParametersResponse response;
178     response.error = KM_ERROR_UNIMPLEMENTED;
179     return response;
180 }
181 
ComputeSharedHmac(const ComputeSharedHmacRequest &)182 ComputeSharedHmacResponse TrustyKeymaster::ComputeSharedHmac(
183         const ComputeSharedHmacRequest& /* request */) {
184     ComputeSharedHmacResponse response;
185     response.error = KM_ERROR_UNIMPLEMENTED;
186     return response;
187 }
188 
VerifyAuthorization(const VerifyAuthorizationRequest &)189 VerifyAuthorizationResponse TrustyKeymaster::VerifyAuthorization(
190         const VerifyAuthorizationRequest& /* request */) {
191     VerifyAuthorizationResponse response;
192     response.error = KM_ERROR_UNIMPLEMENTED;
193     return response;
194 }
195 
196 }  // namespace keymaster
197