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 "remote_keymaster.h"
18 
19 #include <android-base/logging.h>
20 #include <keymaster/android_keymaster_messages.h>
21 #include <keymaster/keymaster_configuration.h>
22 
23 namespace keymaster {
24 
RemoteKeymaster(cuttlefish::KeymasterChannel * channel)25 RemoteKeymaster::RemoteKeymaster(cuttlefish::KeymasterChannel* channel)
26     : channel_(channel) {}
27 
~RemoteKeymaster()28 RemoteKeymaster::~RemoteKeymaster() {
29 }
30 
ForwardCommand(AndroidKeymasterCommand command,const Serializable & req,KeymasterResponse * rsp)31 void RemoteKeymaster::ForwardCommand(AndroidKeymasterCommand command, const Serializable& req,
32                                      KeymasterResponse* rsp) {
33     if (!channel_->SendRequest(command, req)) {
34         LOG(ERROR) << "Failed to send keymaster message: " << command;
35         rsp->error = KM_ERROR_UNKNOWN_ERROR;
36         return;
37     }
38     auto response = channel_->ReceiveMessage();
39     if (!response) {
40         LOG(ERROR) << "Failed to receive keymaster response: " << command;
41         rsp->error = KM_ERROR_UNKNOWN_ERROR;
42         return;
43     }
44     const uint8_t* buffer = response->payload;
45     const uint8_t* buffer_end = response->payload + response->payload_size;
46     if (!rsp->Deserialize(&buffer, buffer_end)) {
47         LOG(ERROR) << "Failed to deserialize keymaster response: " << command;
48         rsp->error = KM_ERROR_UNKNOWN_ERROR;
49         return;
50     }
51 }
52 
Initialize()53 bool RemoteKeymaster::Initialize() {
54 
55     ConfigureRequest req;
56     req.os_version = GetOsVersion();
57     req.os_patchlevel = GetOsPatchlevel();
58 
59     ConfigureResponse rsp;
60     Configure(req, &rsp);
61 
62     if (rsp.error != KM_ERROR_OK) {
63         LOG(ERROR) << "Failed to configure keymaster: " << rsp.error;
64         return false;
65     }
66 
67     return true;
68 }
69 
GetVersion(const GetVersionRequest & request,GetVersionResponse * response)70 void RemoteKeymaster::GetVersion(const GetVersionRequest& request, GetVersionResponse* response) {
71     ForwardCommand(GET_VERSION, request, response);
72 }
73 
SupportedAlgorithms(const SupportedAlgorithmsRequest & request,SupportedAlgorithmsResponse * response)74 void RemoteKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
75                                           SupportedAlgorithmsResponse* response) {
76     ForwardCommand(GET_SUPPORTED_ALGORITHMS, request, response);
77 }
78 
SupportedBlockModes(const SupportedBlockModesRequest & request,SupportedBlockModesResponse * response)79 void RemoteKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
80                                           SupportedBlockModesResponse* response) {
81     ForwardCommand(GET_SUPPORTED_BLOCK_MODES, request, response);
82 }
83 
SupportedPaddingModes(const SupportedPaddingModesRequest & request,SupportedPaddingModesResponse * response)84 void RemoteKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
85                                             SupportedPaddingModesResponse* response) {
86     ForwardCommand(GET_SUPPORTED_PADDING_MODES, request, response);
87 }
88 
SupportedDigests(const SupportedDigestsRequest & request,SupportedDigestsResponse * response)89 void RemoteKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
90                                        SupportedDigestsResponse* response) {
91     ForwardCommand(GET_SUPPORTED_DIGESTS, request, response);
92 }
93 
SupportedImportFormats(const SupportedImportFormatsRequest & request,SupportedImportFormatsResponse * response)94 void RemoteKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
95                                              SupportedImportFormatsResponse* response) {
96     ForwardCommand(GET_SUPPORTED_IMPORT_FORMATS, request, response);
97 }
98 
SupportedExportFormats(const SupportedExportFormatsRequest & request,SupportedExportFormatsResponse * response)99 void RemoteKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
100                                              SupportedExportFormatsResponse* response) {
101     ForwardCommand(GET_SUPPORTED_EXPORT_FORMATS, request, response);
102 }
103 
AddRngEntropy(const AddEntropyRequest & request,AddEntropyResponse * response)104 void RemoteKeymaster::AddRngEntropy(const AddEntropyRequest& request,
105                                     AddEntropyResponse* response) {
106     ForwardCommand(ADD_RNG_ENTROPY, request, response);
107 }
108 
Configure(const ConfigureRequest & request,ConfigureResponse * response)109 void RemoteKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
110     ForwardCommand(CONFIGURE, request, response);
111 }
112 
GenerateKey(const GenerateKeyRequest & request,GenerateKeyResponse * response)113 void RemoteKeymaster::GenerateKey(const GenerateKeyRequest& request,
114                                   GenerateKeyResponse* response) {
115     GenerateKeyRequest datedRequest(request.message_version);
116     datedRequest.key_description = request.key_description;
117 
118     if (!request.key_description.Contains(TAG_CREATION_DATETIME)) {
119         datedRequest.key_description.push_back(TAG_CREATION_DATETIME, java_time(time(NULL)));
120     }
121 
122     ForwardCommand(GENERATE_KEY, datedRequest, response);
123 }
124 
GetKeyCharacteristics(const GetKeyCharacteristicsRequest & request,GetKeyCharacteristicsResponse * response)125 void RemoteKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
126                                             GetKeyCharacteristicsResponse* response) {
127     ForwardCommand(GET_KEY_CHARACTERISTICS, request, response);
128 }
129 
ImportKey(const ImportKeyRequest & request,ImportKeyResponse * response)130 void RemoteKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
131     ForwardCommand(IMPORT_KEY, request, response);
132 }
133 
ImportWrappedKey(const ImportWrappedKeyRequest & request,ImportWrappedKeyResponse * response)134 void RemoteKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
135                                        ImportWrappedKeyResponse* response) {
136     ForwardCommand(IMPORT_WRAPPED_KEY, request, response);
137 }
138 
ExportKey(const ExportKeyRequest & request,ExportKeyResponse * response)139 void RemoteKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
140     ForwardCommand(EXPORT_KEY, request, response);
141 }
142 
AttestKey(const AttestKeyRequest & request,AttestKeyResponse * response)143 void RemoteKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
144     ForwardCommand(ATTEST_KEY, request, response);
145 }
146 
UpgradeKey(const UpgradeKeyRequest & request,UpgradeKeyResponse * response)147 void RemoteKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
148     ForwardCommand(UPGRADE_KEY, request, response);
149 }
150 
DeleteKey(const DeleteKeyRequest & request,DeleteKeyResponse * response)151 void RemoteKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
152     ForwardCommand(DELETE_KEY, request, response);
153 }
154 
DeleteAllKeys(const DeleteAllKeysRequest & request,DeleteAllKeysResponse * response)155 void RemoteKeymaster::DeleteAllKeys(const DeleteAllKeysRequest& request,
156                                     DeleteAllKeysResponse* response) {
157     ForwardCommand(DELETE_ALL_KEYS, request, response);
158 }
159 
BeginOperation(const BeginOperationRequest & request,BeginOperationResponse * response)160 void RemoteKeymaster::BeginOperation(const BeginOperationRequest& request,
161                                      BeginOperationResponse* response) {
162     ForwardCommand(BEGIN_OPERATION, request, response);
163 }
164 
UpdateOperation(const UpdateOperationRequest & request,UpdateOperationResponse * response)165 void RemoteKeymaster::UpdateOperation(const UpdateOperationRequest& request,
166                                       UpdateOperationResponse* response) {
167     ForwardCommand(UPDATE_OPERATION, request, response);
168 }
169 
FinishOperation(const FinishOperationRequest & request,FinishOperationResponse * response)170 void RemoteKeymaster::FinishOperation(const FinishOperationRequest& request,
171                                       FinishOperationResponse* response) {
172     ForwardCommand(FINISH_OPERATION, request, response);
173 }
174 
AbortOperation(const AbortOperationRequest & request,AbortOperationResponse * response)175 void RemoteKeymaster::AbortOperation(const AbortOperationRequest& request,
176                                      AbortOperationResponse* response) {
177     ForwardCommand(ABORT_OPERATION, request, response);
178 }
179 
GetHmacSharingParameters()180 GetHmacSharingParametersResponse RemoteKeymaster::GetHmacSharingParameters() {
181     // Unused empty buffer to allow ForwardCommand to have something to serialize
182     Buffer request;
183     GetHmacSharingParametersResponse response;
184     ForwardCommand(GET_HMAC_SHARING_PARAMETERS, request, &response);
185     return response;
186 }
187 
ComputeSharedHmac(const ComputeSharedHmacRequest & request)188 ComputeSharedHmacResponse RemoteKeymaster::ComputeSharedHmac(
189         const ComputeSharedHmacRequest& request) {
190     ComputeSharedHmacResponse response;
191     ForwardCommand(COMPUTE_SHARED_HMAC, request, &response);
192     return response;
193 }
194 
VerifyAuthorization(const VerifyAuthorizationRequest & request)195 VerifyAuthorizationResponse RemoteKeymaster::VerifyAuthorization(
196         const VerifyAuthorizationRequest& request) {
197     VerifyAuthorizationResponse response;
198     ForwardCommand(VERIFY_AUTHORIZATION, request, &response);
199     return response;
200 }
201 
DeviceLocked(const DeviceLockedRequest & request)202 DeviceLockedResponse RemoteKeymaster::DeviceLocked(
203         const DeviceLockedRequest& request) {
204     DeviceLockedResponse response;
205     ForwardCommand(DEVICE_LOCKED, request, &response);
206     return response;
207 }
208 
EarlyBootEnded()209 EarlyBootEndedResponse RemoteKeymaster::EarlyBootEnded() {
210     // Unused empty buffer to allow ForwardCommand to have something to serialize
211     Buffer request;
212     EarlyBootEndedResponse response;
213     ForwardCommand(EARLY_BOOT_ENDED, request, &response);
214     return response;
215 }
216 
217 }  // namespace keymaster
218