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