• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 
17 #include "common/libs/security/confui_sign.h"
18 
19 #include <android-base/logging.h>
20 
21 #include "common/libs/fs/shared_buf.h"
22 
23 namespace cuttlefish {
Send(SharedFD output,const confui::SignMessageError error,const std::vector<std::uint8_t> & payload)24 bool ConfUiSignerImpl::Send(SharedFD output,
25                             const confui::SignMessageError error,
26                             const std::vector<std::uint8_t>& payload) {
27 #define SET_FLAG_AND_RETURN_SEND \
28   sign_status_ |= kIoError;      \
29   return false
30 
31   // this looks redudant but makes sure that the byte-length of
32   // the error is guaranteed to be the same when it is received
33   confui::SignRawMessage msg;
34   msg.error_ = error;
35   auto n_written_err = WriteAllBinary(output, &(msg.error_));
36   if (n_written_err != sizeof(msg.error_)) {
37     sign_status_ |= kIoError;
38     SET_FLAG_AND_RETURN_SEND;
39   }
40   decltype(msg.payload_.size()) payload_size = payload.size();
41   auto n_written_payload_size = WriteAllBinary(output, &payload_size);
42   if (n_written_payload_size != sizeof(payload_size)) {
43     SET_FLAG_AND_RETURN_SEND;
44   }
45   const char* buf = reinterpret_cast<const char*>(payload.data());
46   auto n_written_payload = WriteAll(output, buf, payload.size());
47 
48   if (n_written_payload != payload.size()) {
49     SET_FLAG_AND_RETURN_SEND;
50   }
51   return true;
52 }
53 
Receive(SharedFD input)54 std::optional<confui::SignRawMessage> ConfUiSignerImpl::Receive(
55     SharedFD input) {
56   confui::SignRawMessage msg;
57 
58   auto n_read = ReadExactBinary(input, &(msg.error_));
59   if (n_read != sizeof(msg.error_)) {
60     sign_status_ |= kIoError;
61   }
62   if (msg.error_ != confui::SignMessageError::kOk) {
63     sign_status_ |= kLogicError;
64   }
65   if (!IsOk()) {
66     return std::nullopt;
67   }
68 
69   decltype(msg.payload_.size()) payload_size = 0;
70   n_read = ReadExactBinary(input, &payload_size);
71   if (n_read != sizeof(payload_size)) {
72     sign_status_ |= kIoError;
73     return std::nullopt;
74   }
75 
76   std::vector<std::uint8_t> buffer(payload_size);
77   char* buf_data = reinterpret_cast<char*>(buffer.data());
78   n_read = ReadExact(input, buf_data, payload_size);
79   if (n_read != payload_size) {
80     sign_status_ |= kIoError;
81     return std::nullopt;
82   }
83   msg.payload_.swap(buffer);
84   return {msg};
85 }
86 
Receive()87 std::optional<confui::SignRawMessage> ConfUiSignSender::Receive() {
88   return impl_.Receive(server_fd_);
89 }
90 
Send(const SignMessageError error,const std::vector<std::uint8_t> & encoded_hmac)91 bool ConfUiSignSender::Send(const SignMessageError error,
92                             const std::vector<std::uint8_t>& encoded_hmac) {
93   if (!impl_.IsOk()) {
94     return false;
95   }
96   impl_.Send(server_fd_, error, encoded_hmac);
97   return impl_.IsOk();
98 }
99 
Request(const std::vector<std::uint8_t> & message)100 bool ConfUiSignRequester::Request(const std::vector<std::uint8_t>& message) {
101   impl_.Send(client_fd_, confui::SignMessageError::kOk, message);
102   return impl_.IsOk();
103 }
104 
Receive()105 std::optional<confui::SignRawMessage> ConfUiSignRequester::Receive() {
106   if (!impl_.IsOk()) {
107     return std::nullopt;
108   }
109   return impl_.Receive(client_fd_);
110 }
111 }  // namespace cuttlefish
112