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