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 "gatekeeper_responder.h"
17 
18 #include <android-base/logging.h>
19 #include <gatekeeper/gatekeeper_messages.h>
20 
21 namespace cuttlefish {
22 
GatekeeperResponder(cuttlefish::GatekeeperChannel & channel,gatekeeper::GateKeeper & gatekeeper)23 GatekeeperResponder::GatekeeperResponder(cuttlefish::GatekeeperChannel& channel,
24                                          gatekeeper::GateKeeper& gatekeeper)
25     : channel_(channel), gatekeeper_(gatekeeper) {}
26 
ProcessMessage()27 bool GatekeeperResponder::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* buffer_end = request->payload + request->payload_size;
35   switch(request->command) {
36     using namespace gatekeeper;
37     case ENROLL: {
38       EnrollRequest enroll_request;
39       auto rc = enroll_request.Deserialize(buffer, buffer_end);
40       if (rc != ERROR_NONE) {
41         LOG(ERROR) << "Failed to deserialize Enroll Request";
42         return false;
43       }
44       EnrollResponse response;
45       gatekeeper_.Enroll(enroll_request, &response);
46       return channel_.SendResponse(ENROLL, response);
47     }
48     case VERIFY: {
49       VerifyRequest verify_request;
50       auto rc = verify_request.Deserialize(buffer, buffer_end);
51       if (rc != ERROR_NONE) {
52         LOG(ERROR) << "Failed to deserialize Verify Request";
53         return false;
54       }
55       VerifyResponse response;
56       gatekeeper_.Verify(verify_request, &response);
57       return channel_.SendResponse(VERIFY, response);
58     }
59     default:
60       LOG(ERROR) << "Unrecognized message id " << request->command;
61       return false;
62   }
63 }
64 
65 }  // namespace cuttlefish
66