1 //
2 // Copyright (C) 2020 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 #include "keymaster_responder.h"
17
18 #include <android-base/logging.h>
19 #include <keymaster/android_keymaster_messages.h>
20
21 namespace cuttlefish {
22
KeymasterResponder(cuttlefish::KeymasterChannel & channel,keymaster::AndroidKeymaster & keymaster)23 KeymasterResponder::KeymasterResponder(cuttlefish::KeymasterChannel& channel,
24 keymaster::AndroidKeymaster& keymaster)
25 : channel_(channel), keymaster_(keymaster) {}
26
ProcessMessage()27 bool KeymasterResponder::ProcessMessage() {
28 auto request = channel_.ReceiveMessage();
29 if (!request) {
30 LOG(ERROR) << "Could not receive message";
31 return false;
32 }
33 const uint8_t* buffer = request->payload;
34 const uint8_t* end = request->payload + request->payload_size;
35 switch (request->cmd) {
36 using namespace keymaster;
37 #define HANDLE_MESSAGE(ENUM_NAME, METHOD_NAME) \
38 case ENUM_NAME: { \
39 METHOD_NAME##Request request(keymaster_.message_version()); \
40 if (!request.Deserialize(&buffer, end)) { \
41 LOG(ERROR) << "Failed to deserialize " #METHOD_NAME "Request"; \
42 return false; \
43 } \
44 METHOD_NAME##Response response(keymaster_.message_version()); \
45 keymaster_.METHOD_NAME(request, &response); \
46 return channel_.SendResponse(ENUM_NAME, response); \
47 }
48 HANDLE_MESSAGE(GENERATE_KEY, GenerateKey)
49 HANDLE_MESSAGE(BEGIN_OPERATION, BeginOperation)
50 HANDLE_MESSAGE(UPDATE_OPERATION, UpdateOperation)
51 HANDLE_MESSAGE(FINISH_OPERATION, FinishOperation)
52 HANDLE_MESSAGE(ABORT_OPERATION, AbortOperation)
53 HANDLE_MESSAGE(IMPORT_KEY, ImportKey)
54 HANDLE_MESSAGE(EXPORT_KEY, ExportKey)
55 HANDLE_MESSAGE(GET_VERSION, GetVersion)
56 HANDLE_MESSAGE(GET_SUPPORTED_ALGORITHMS, SupportedAlgorithms)
57 HANDLE_MESSAGE(GET_SUPPORTED_BLOCK_MODES, SupportedBlockModes)
58 HANDLE_MESSAGE(GET_SUPPORTED_PADDING_MODES, SupportedPaddingModes)
59 HANDLE_MESSAGE(GET_SUPPORTED_DIGESTS, SupportedDigests)
60 HANDLE_MESSAGE(GET_SUPPORTED_IMPORT_FORMATS, SupportedImportFormats)
61 HANDLE_MESSAGE(GET_SUPPORTED_EXPORT_FORMATS, SupportedExportFormats)
62 HANDLE_MESSAGE(GET_KEY_CHARACTERISTICS, GetKeyCharacteristics)
63 HANDLE_MESSAGE(ATTEST_KEY, AttestKey)
64 HANDLE_MESSAGE(UPGRADE_KEY, UpgradeKey)
65 HANDLE_MESSAGE(CONFIGURE, Configure)
66 HANDLE_MESSAGE(DELETE_KEY, DeleteKey)
67 HANDLE_MESSAGE(DELETE_ALL_KEYS, DeleteAllKeys)
68 HANDLE_MESSAGE(IMPORT_WRAPPED_KEY, ImportWrappedKey)
69 HANDLE_MESSAGE(GENERATE_RKP_KEY, GenerateRkpKey)
70 HANDLE_MESSAGE(GENERATE_CSR, GenerateCsr)
71 HANDLE_MESSAGE(GENERATE_CSR_V2, GenerateCsrV2)
72 HANDLE_MESSAGE(GENERATE_TIMESTAMP_TOKEN, GenerateTimestampToken)
73 #undef HANDLE_MESSAGE
74 #define HANDLE_MESSAGE_W_RETURN(ENUM_NAME, METHOD_NAME) \
75 case ENUM_NAME: { \
76 METHOD_NAME##Request request(keymaster_.message_version()); \
77 if (!request.Deserialize(&buffer, end)) { \
78 LOG(ERROR) << "Failed to deserialize " #METHOD_NAME "Request"; \
79 return false; \
80 } \
81 auto response = keymaster_.METHOD_NAME(request); \
82 return channel_.SendResponse(ENUM_NAME, response); \
83 }
84 HANDLE_MESSAGE_W_RETURN(COMPUTE_SHARED_HMAC, ComputeSharedHmac)
85 HANDLE_MESSAGE_W_RETURN(VERIFY_AUTHORIZATION, VerifyAuthorization)
86 HANDLE_MESSAGE_W_RETURN(DEVICE_LOCKED, DeviceLocked)
87 HANDLE_MESSAGE_W_RETURN(GET_VERSION_2, GetVersion2)
88 HANDLE_MESSAGE_W_RETURN(CONFIGURE_VENDOR_PATCHLEVEL,
89 ConfigureVendorPatchlevel)
90 HANDLE_MESSAGE_W_RETURN(CONFIGURE_BOOT_PATCHLEVEL, ConfigureBootPatchlevel)
91 HANDLE_MESSAGE_W_RETURN(CONFIGURE_VERIFIED_BOOT_INFO,
92 ConfigureVerifiedBootInfo)
93 HANDLE_MESSAGE_W_RETURN(GET_ROOT_OF_TRUST, GetRootOfTrust)
94 HANDLE_MESSAGE_W_RETURN(SET_ATTESTATION_IDS, SetAttestationIds)
95 HANDLE_MESSAGE_W_RETURN(SET_ATTESTATION_IDS_KM3, SetAttestationIdsKM3)
96 #undef HANDLE_MESSAGE_W_RETURN
97 #define HANDLE_MESSAGE_W_RETURN_NO_ARG(ENUM_NAME, METHOD_NAME) \
98 case ENUM_NAME: { \
99 auto response = keymaster_.METHOD_NAME(); \
100 return channel_.SendResponse(ENUM_NAME, response); \
101 }
102 HANDLE_MESSAGE_W_RETURN_NO_ARG(GET_HMAC_SHARING_PARAMETERS,
103 GetHmacSharingParameters)
104 HANDLE_MESSAGE_W_RETURN_NO_ARG(EARLY_BOOT_ENDED, EarlyBootEnded)
105 HANDLE_MESSAGE_W_RETURN_NO_ARG(GET_HW_INFO, GetHwInfo)
106 #undef HANDLE_MESSAGE_W_RETURN_NO_ARG
107 case ADD_RNG_ENTROPY: {
108 AddEntropyRequest request(keymaster_.message_version());
109 if (!request.Deserialize(&buffer, end)) {
110 LOG(ERROR) << "Failed to deserialize AddEntropyRequest";
111 return false;
112 }
113 AddEntropyResponse response(keymaster_.message_version());
114 ;
115 keymaster_.AddRngEntropy(request, &response);
116 return channel_.SendResponse(ADD_RNG_ENTROPY, response);
117 }
118 case DESTROY_ATTESTATION_IDS:
119 // Cuttlefish doesn't support ID attestation.
120 default:
121 LOG(ERROR) << "Unknown request type: " << request->cmd;
122 return false;
123 }
124 }
125
126 } // namespace cuttlefish
127