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(
24 cuttlefish::GatekeeperChannel& channel, gatekeeper::GateKeeper& gatekeeper)
25 : channel_(channel), gatekeeper_(gatekeeper) {
26 }
27
ProcessMessage()28 bool GatekeeperResponder::ProcessMessage() {
29 auto request = channel_.ReceiveMessage();
30 if (!request) {
31 LOG(ERROR) << "Could not receive message";
32 return false;
33 }
34 const uint8_t* buffer = request->payload;
35 const uint8_t* buffer_end = request->payload + request->payload_size;
36 switch(request->cmd) {
37 using namespace gatekeeper;
38 case ENROLL: {
39 EnrollRequest enroll_request;
40 auto rc = enroll_request.Deserialize(buffer, buffer_end);
41 if (rc != ERROR_NONE) {
42 LOG(ERROR) << "Failed to deserialize Enroll Request";
43 return false;
44 }
45 EnrollResponse response;
46 gatekeeper_.Enroll(enroll_request, &response);
47 return channel_.SendResponse(ENROLL, response);
48 }
49 case VERIFY: {
50 VerifyRequest verify_request;
51 auto rc = verify_request.Deserialize(buffer, buffer_end);
52 if (rc != ERROR_NONE) {
53 LOG(ERROR) << "Failed to deserialize Verify Request";
54 return false;
55 }
56 VerifyResponse response;
57 gatekeeper_.Verify(verify_request, &response);
58 return channel_.SendResponse(VERIFY, response);
59 }
60 default:
61 LOG(ERROR) << "Unrecognized message id " << request->cmd;
62 return false;
63 }
64 }
65
66 } // namespace cuttlefish
67