1 /* 2 * Copyright 2014 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 #pragma once 18 19 #include "android_keymaster_messages.h" 20 #include <keymaster/android_keymaster_messages.h> 21 #include <keymaster/authorization_set.h> 22 23 namespace keymaster { 24 25 class Key; 26 class KeyFactory; 27 class KeymasterContext; 28 class OperationTable; 29 30 /** 31 * This is the reference implementation of Keymaster. In addition to acting as a reference for 32 * other Keymaster implementers to check their assumptions against, it is used by Keystore as the 33 * default implementation when no secure implementation is available, and may be installed and 34 * executed in secure hardware as a secure implementation. 35 * 36 * Note that this class doesn't actually implement the Keymaster HAL interface, instead it 37 * implements an alternative API which is similar to and based upon the HAL, but uses C++ "message" 38 * classes which support serialization. 39 * 40 * For non-secure, pure software implementation there is a HAL translation layer that converts the 41 * HAL's parameters to and from the message representations, which are then passed in to this 42 * API. 43 * 44 * For secure implementation there is another HAL translation layer that serializes the messages to 45 * the TEE. In the TEE implementation there's another component which deserializes the messages, 46 * extracts the relevant parameters and calls this API. 47 */ 48 class AndroidKeymaster { 49 public: 50 AndroidKeymaster(KeymasterContext* context, size_t operation_table_size, 51 int32_t message_version = kDefaultMessageVersion); 52 virtual ~AndroidKeymaster(); 53 AndroidKeymaster(AndroidKeymaster&&); 54 55 void GetVersion(const GetVersionRequest& request, GetVersionResponse* response); 56 void SupportedAlgorithms(const SupportedAlgorithmsRequest& request, 57 SupportedAlgorithmsResponse* response); 58 void SupportedBlockModes(const SupportedBlockModesRequest& request, 59 SupportedBlockModesResponse* response); 60 void SupportedPaddingModes(const SupportedPaddingModesRequest& request, 61 SupportedPaddingModesResponse* response); 62 void SupportedDigests(const SupportedDigestsRequest& request, 63 SupportedDigestsResponse* response); 64 void SupportedImportFormats(const SupportedImportFormatsRequest& request, 65 SupportedImportFormatsResponse* response); 66 void SupportedExportFormats(const SupportedExportFormatsRequest& request, 67 SupportedExportFormatsResponse* response); 68 69 GetHmacSharingParametersResponse GetHmacSharingParameters(); 70 ComputeSharedHmacResponse ComputeSharedHmac(const ComputeSharedHmacRequest& request); 71 VerifyAuthorizationResponse VerifyAuthorization(const VerifyAuthorizationRequest& request); 72 void GenerateTimestampToken(GenerateTimestampTokenRequest& request, 73 GenerateTimestampTokenResponse* response); 74 void AddRngEntropy(const AddEntropyRequest& request, AddEntropyResponse* response); 75 void Configure(const ConfigureRequest& request, ConfigureResponse* response); 76 void GenerateKey(const GenerateKeyRequest& request, GenerateKeyResponse* response); 77 void GenerateRkpKey(const GenerateRkpKeyRequest& request, GenerateRkpKeyResponse* response); 78 void GenerateCsr(const GenerateCsrRequest& request, GenerateCsrResponse* response); 79 void GenerateCsrV2(const GenerateCsrV2Request& request, GenerateCsrV2Response* response); 80 void GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request, 81 GetKeyCharacteristicsResponse* response); 82 void ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response); 83 void ImportWrappedKey(const ImportWrappedKeyRequest& request, 84 ImportWrappedKeyResponse* response); 85 void ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response); 86 void AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response); 87 void UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response); 88 void DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response); 89 void DeleteAllKeys(const DeleteAllKeysRequest& request, DeleteAllKeysResponse* response); 90 void BeginOperation(const BeginOperationRequest& request, BeginOperationResponse* response); 91 void UpdateOperation(const UpdateOperationRequest& request, UpdateOperationResponse* response); 92 void FinishOperation(const FinishOperationRequest& request, FinishOperationResponse* response); 93 void AbortOperation(const AbortOperationRequest& request, AbortOperationResponse* response); 94 95 EarlyBootEndedResponse EarlyBootEnded(); 96 DeviceLockedResponse DeviceLocked(const DeviceLockedRequest& request); 97 GetVersion2Response GetVersion2(const GetVersion2Request& request); 98 ConfigureVendorPatchlevelResponse 99 ConfigureVendorPatchlevel(const ConfigureVendorPatchlevelRequest& request); 100 ConfigureBootPatchlevelResponse 101 ConfigureBootPatchlevel(const ConfigureBootPatchlevelRequest& request); 102 ConfigureVerifiedBootInfoResponse 103 ConfigureVerifiedBootInfo(const ConfigureVerifiedBootInfoRequest& request); 104 GetRootOfTrustResponse GetRootOfTrust(const GetRootOfTrustRequest& request); 105 GetHwInfoResponse GetHwInfo(); 106 SetAttestationIdsResponse SetAttestationIds(const SetAttestationIdsRequest& request); 107 SetAttestationIdsKM3Response SetAttestationIdsKM3(const SetAttestationIdsKM3Request& request); 108 109 bool has_operation(keymaster_operation_handle_t op_handle) const; 110 111 // Returns the message version negotiated in GetVersion2. All response messages should have 112 // this passed to their constructors. This is done automatically for the methods that return a 113 // response by value. The caller must do it for the methods that take a response pointer. message_version()114 int32_t message_version() const { return message_version_; } 115 116 private: 117 // Loads the KM key from `key_blob`, getting app ID and app data from `additional_params`, if 118 // needed. If loading the key fails for any reason (including failure of the version binding 119 // check), the returned UniquePtr is null and `*error` is set (`error` must not be null). 120 UniquePtr<Key> LoadKey(const keymaster_key_blob_t& key_blob, 121 const AuthorizationSet& additional_params, keymaster_error_t* error); 122 123 UniquePtr<KeymasterContext> context_; 124 UniquePtr<OperationTable> operation_table_; 125 126 // If the caller doesn't bother to use GetVersion2 or GetVersion to configure the message 127 // version, assume kDefaultVersion, i.e. assume the client and server always support the 128 // latest default, which is the latest, except when experimental features are being added. 129 uint32_t message_version_; 130 }; 131 132 } // namespace keymaster 133