• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TIMESTAMP_TOKEN, GenerateTimestampToken)
72 #undef HANDLE_MESSAGE
73 #define HANDLE_MESSAGE_W_RETURN(ENUM_NAME, METHOD_NAME)              \
74   case ENUM_NAME: {                                                  \
75     METHOD_NAME##Request request(keymaster_.message_version());      \
76     if (!request.Deserialize(&buffer, end)) {                        \
77       LOG(ERROR) << "Failed to deserialize " #METHOD_NAME "Request"; \
78       return false;                                                  \
79     }                                                                \
80     auto response = keymaster_.METHOD_NAME(request);                 \
81     return channel_.SendResponse(ENUM_NAME, response);               \
82   }
83     HANDLE_MESSAGE_W_RETURN(COMPUTE_SHARED_HMAC, ComputeSharedHmac)
84     HANDLE_MESSAGE_W_RETURN(VERIFY_AUTHORIZATION, VerifyAuthorization)
85     HANDLE_MESSAGE_W_RETURN(DEVICE_LOCKED, DeviceLocked)
86     HANDLE_MESSAGE_W_RETURN(GET_VERSION_2, GetVersion2)
87     HANDLE_MESSAGE_W_RETURN(CONFIGURE_VENDOR_PATCHLEVEL,
88                             ConfigureVendorPatchlevel)
89     HANDLE_MESSAGE_W_RETURN(CONFIGURE_BOOT_PATCHLEVEL, ConfigureBootPatchlevel)
90     HANDLE_MESSAGE_W_RETURN(CONFIGURE_VERIFIED_BOOT_INFO,
91                             ConfigureVerifiedBootInfo)
92     HANDLE_MESSAGE_W_RETURN(GET_ROOT_OF_TRUST, GetRootOfTrust)
93 #undef HANDLE_MESSAGE_W_RETURN
94 #define HANDLE_MESSAGE_W_RETURN_NO_ARG(ENUM_NAME, METHOD_NAME) \
95   case ENUM_NAME: {                                            \
96     auto response = keymaster_.METHOD_NAME();                  \
97     return channel_.SendResponse(ENUM_NAME, response);         \
98   }
99     HANDLE_MESSAGE_W_RETURN_NO_ARG(GET_HMAC_SHARING_PARAMETERS,
100                                    GetHmacSharingParameters)
101     HANDLE_MESSAGE_W_RETURN_NO_ARG(EARLY_BOOT_ENDED, EarlyBootEnded)
102 #undef HANDLE_MESSAGE_W_RETURN_NO_ARG
103     case ADD_RNG_ENTROPY: {
104       AddEntropyRequest request(keymaster_.message_version());
105       if (!request.Deserialize(&buffer, end)) {
106         LOG(ERROR) << "Failed to deserialize AddEntropyRequest";
107         return false;
108       }
109       AddEntropyResponse response(keymaster_.message_version());
110       ;
111       keymaster_.AddRngEntropy(request, &response);
112       return channel_.SendResponse(ADD_RNG_ENTROPY, response);
113     }
114     case DESTROY_ATTESTATION_IDS:
115       // Cuttlefish doesn't support ID attestation.
116     default:
117       LOG(ERROR) << "Unknown request type: " << request->cmd;
118       return false;
119   }
120 }
121 
122 }  // namespace cuttlefish
123