1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "trusty_keymaster_hal"
#include <android-base/logging.h>
#include <keymaster/android_keymaster_messages.h>
#include <keymaster/keymaster_configuration.h>
#include <trusty_keymaster/TrustyKeymaster.h>
#include <trusty_keymaster/ipc/trusty_keymaster_ipc.h>
namespace keymaster {
int TrustyKeymaster::Initialize(KmVersion version) {
int err;
LOG(INFO) << "Initializing TrustyKeymaster as KmVersion: " << (int)version;
err = trusty_keymaster_connect();
if (err) {
LOG(ERROR) << "Failed to connect to trusty keymaster (1st try)" << err;
return err;
}
// Try GetVersion2 first.
GetVersion2Request versionReq;
versionReq.max_message_version = MessageVersion(version);
GetVersion2Response versionRsp = GetVersion2(versionReq);
if (versionRsp.error != KM_ERROR_OK) {
LOG(WARNING) << "TA appears not to support GetVersion2, falling back (err = "
<< versionRsp.error << ")";
err = trusty_keymaster_connect();
if (err) {
LOG(FATAL) << "Failed to connect to trusty keymaster (2nd try) " << err;
return err;
}
GetVersionRequest versionReq;
GetVersionResponse versionRsp;
GetVersion(versionReq, &versionRsp);
if (versionRsp.error != KM_ERROR_OK) {
LOG(FATAL) << "Failed to get TA version " << versionRsp.error;
return -1;
} else {
keymaster_error_t error;
message_version_ = NegotiateMessageVersion(versionRsp, &error);
if (error != KM_ERROR_OK) {
LOG(FATAL) << "Failed to negotiate message version " << error;
return -1;
}
}
} else {
message_version_ = NegotiateMessageVersion(versionReq, versionRsp);
}
ConfigureRequest req(message_version());
req.os_version = GetOsVersion();
req.os_patchlevel = GetOsPatchlevel();
ConfigureResponse rsp(message_version());
Configure(req, &rsp);
if (rsp.error != KM_ERROR_OK) {
LOG(FATAL) << "Failed to configure keymaster " << rsp.error;
return -1;
}
// Set the vendor patchlevel to value retrieved from system property (which
// requires SELinux permission).
ConfigureVendorPatchlevelRequest vendor_req(message_version());
vendor_req.vendor_patchlevel = GetVendorPatchlevel();
ConfigureVendorPatchlevelResponse vendor_rsp = ConfigureVendorPatchlevel(vendor_req);
if (vendor_rsp.error != KM_ERROR_OK) {
LOG(ERROR) << "Failed to configure keymaster vendor patchlevel: " << vendor_rsp.error;
// Don't fail if this message isn't understood.
}
return 0;
}
TrustyKeymaster::TrustyKeymaster() {}
TrustyKeymaster::~TrustyKeymaster() {
trusty_keymaster_disconnect();
}
static void ForwardCommand(enum keymaster_command command, const KeymasterMessage& req,
KeymasterResponse* rsp) {
keymaster_error_t err;
err = trusty_keymaster_send(command, req, rsp);
if (err != KM_ERROR_OK) {
LOG(ERROR) << "Cmd " << command << " returned error: " << err;
rsp->error = err;
}
}
void TrustyKeymaster::GetVersion(const GetVersionRequest& request, GetVersionResponse* response) {
ForwardCommand(KM_GET_VERSION, request, response);
}
void TrustyKeymaster::SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
SupportedAlgorithmsResponse* response) {
ForwardCommand(KM_GET_SUPPORTED_ALGORITHMS, request, response);
}
void TrustyKeymaster::SupportedBlockModes(const SupportedBlockModesRequest& request,
SupportedBlockModesResponse* response) {
ForwardCommand(KM_GET_SUPPORTED_BLOCK_MODES, request, response);
}
void TrustyKeymaster::SupportedPaddingModes(const SupportedPaddingModesRequest& request,
SupportedPaddingModesResponse* response) {
ForwardCommand(KM_GET_SUPPORTED_PADDING_MODES, request, response);
}
void TrustyKeymaster::SupportedDigests(const SupportedDigestsRequest& request,
SupportedDigestsResponse* response) {
ForwardCommand(KM_GET_SUPPORTED_DIGESTS, request, response);
}
void TrustyKeymaster::SupportedImportFormats(const SupportedImportFormatsRequest& request,
SupportedImportFormatsResponse* response) {
ForwardCommand(KM_GET_SUPPORTED_IMPORT_FORMATS, request, response);
}
void TrustyKeymaster::SupportedExportFormats(const SupportedExportFormatsRequest& request,
SupportedExportFormatsResponse* response) {
ForwardCommand(KM_GET_SUPPORTED_EXPORT_FORMATS, request, response);
}
void TrustyKeymaster::AddRngEntropy(const AddEntropyRequest& request,
AddEntropyResponse* response) {
ForwardCommand(KM_ADD_RNG_ENTROPY, request, response);
}
void TrustyKeymaster::Configure(const ConfigureRequest& request, ConfigureResponse* response) {
ForwardCommand(KM_CONFIGURE, request, response);
}
void TrustyKeymaster::GenerateKey(const GenerateKeyRequest& request,
GenerateKeyResponse* response) {
if (message_version_ < 4) {
// Pre-KeyMint we need to add TAG_CREATION_DATETIME if not provided by the caller.
GenerateKeyRequest datedRequest(request.message_version);
datedRequest.key_description = request.key_description;
if (!request.key_description.Contains(TAG_CREATION_DATETIME)) {
datedRequest.key_description.push_back(TAG_CREATION_DATETIME, java_time(time(NULL)));
}
ForwardCommand(KM_GENERATE_KEY, datedRequest, response);
} else {
ForwardCommand(KM_GENERATE_KEY, request, response);
}
}
void TrustyKeymaster::GenerateRkpKey(const GenerateRkpKeyRequest& request,
GenerateRkpKeyResponse* response) {
ForwardCommand(KM_GENERATE_RKP_KEY, request, response);
}
void TrustyKeymaster::GenerateCsr(const GenerateCsrRequest& request,
GenerateCsrResponse* response) {
ForwardCommand(KM_GENERATE_CSR, request, response);
}
void TrustyKeymaster::GenerateCsrV2(const GenerateCsrV2Request& request,
GenerateCsrV2Response* response) {
ForwardCommand(KM_GENERATE_CSR_V2, request, response);
}
void TrustyKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
GetKeyCharacteristicsResponse* response) {
ForwardCommand(KM_GET_KEY_CHARACTERISTICS, request, response);
}
void TrustyKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) {
ForwardCommand(KM_IMPORT_KEY, request, response);
}
void TrustyKeymaster::ImportWrappedKey(const ImportWrappedKeyRequest& request,
ImportWrappedKeyResponse* response) {
ForwardCommand(KM_IMPORT_WRAPPED_KEY, request, response);
}
void TrustyKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) {
ForwardCommand(KM_EXPORT_KEY, request, response);
}
void TrustyKeymaster::AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response) {
ForwardCommand(KM_ATTEST_KEY, request, response);
}
void TrustyKeymaster::UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response) {
ForwardCommand(KM_UPGRADE_KEY, request, response);
}
void TrustyKeymaster::DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response) {
ForwardCommand(KM_DELETE_KEY, request, response);
}
void TrustyKeymaster::DeleteAllKeys(const DeleteAllKeysRequest& request,
DeleteAllKeysResponse* response) {
ForwardCommand(KM_DELETE_ALL_KEYS, request, response);
}
void TrustyKeymaster::DestroyAttestationIds(const DestroyAttestationIdsRequest& request,
DestroyAttestationIdsResponse* response) {
ForwardCommand(KM_DESTROY_ATTESTATION_IDS, request, response);
}
void TrustyKeymaster::BeginOperation(const BeginOperationRequest& request,
BeginOperationResponse* response) {
ForwardCommand(KM_BEGIN_OPERATION, request, response);
}
void TrustyKeymaster::UpdateOperation(const UpdateOperationRequest& request,
UpdateOperationResponse* response) {
ForwardCommand(KM_UPDATE_OPERATION, request, response);
}
void TrustyKeymaster::FinishOperation(const FinishOperationRequest& request,
FinishOperationResponse* response) {
ForwardCommand(KM_FINISH_OPERATION, request, response);
}
void TrustyKeymaster::AbortOperation(const AbortOperationRequest& request,
AbortOperationResponse* response) {
ForwardCommand(KM_ABORT_OPERATION, request, response);
}
GetHmacSharingParametersResponse TrustyKeymaster::GetHmacSharingParameters() {
GetHmacSharingParametersRequest request(message_version());
GetHmacSharingParametersResponse response(message_version());
ForwardCommand(KM_GET_HMAC_SHARING_PARAMETERS, request, &response);
return response;
}
ComputeSharedHmacResponse TrustyKeymaster::ComputeSharedHmac(
const ComputeSharedHmacRequest& request) {
ComputeSharedHmacResponse response(message_version());
ForwardCommand(KM_COMPUTE_SHARED_HMAC, request, &response);
return response;
}
VerifyAuthorizationResponse TrustyKeymaster::VerifyAuthorization(
const VerifyAuthorizationRequest& request) {
VerifyAuthorizationResponse response(message_version());
ForwardCommand(KM_VERIFY_AUTHORIZATION, request, &response);
return response;
}
GetVersion2Response TrustyKeymaster::GetVersion2(const GetVersion2Request& request) {
GetVersion2Response response(message_version());
ForwardCommand(KM_GET_VERSION_2, request, &response);
return response;
}
EarlyBootEndedResponse TrustyKeymaster::EarlyBootEnded() {
EarlyBootEndedResponse response(message_version());
ForwardCommand(KM_EARLY_BOOT_ENDED, EarlyBootEndedRequest(message_version()), &response);
return response;
}
DeviceLockedResponse TrustyKeymaster::DeviceLocked(const DeviceLockedRequest& request) {
DeviceLockedResponse response(message_version());
ForwardCommand(KM_DEVICE_LOCKED, request, &response);
return response;
}
ConfigureVendorPatchlevelResponse TrustyKeymaster::ConfigureVendorPatchlevel(
const ConfigureVendorPatchlevelRequest& request) {
ConfigureVendorPatchlevelResponse response(message_version());
ForwardCommand(KM_CONFIGURE_VENDOR_PATCHLEVEL, request, &response);
return response;
}
GetRootOfTrustResponse TrustyKeymaster::GetRootOfTrust(const GetRootOfTrustRequest& request) {
GetRootOfTrustResponse response(message_version());
ForwardCommand(KM_GET_ROOT_OF_TRUST, request, &response);
return response;
}
GetHwInfoResponse TrustyKeymaster::GetHwInfo() {
GetHwInfoResponse response(message_version());
ForwardCommand(KM_GET_HW_INFO, GetHwInfoRequest(message_version()), &response);
return response;
}
} // namespace keymaster
|