github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/tao/tao_rpc.cc (about) 1 // File: tao_rpc.cc 2 // Author: Tom Roeder <tmroeder@google.com> 3 // 4 // Description: RPC client stub for channel-based Tao implementations. 5 // 6 // Copyright (c) 2013, Google Inc. All rights reserved. 7 // 8 // Licensed under the Apache License, Version 2.0 (the "License"); 9 // you may not use this file except in compliance with the License. 10 // You may obtain a copy of the License at 11 // 12 // http://www.apache.org/licenses/LICENSE-2.0 13 // 14 // Unless required by applicable law or agreed to in writing, software 15 // distributed under the License is distributed on an "AS IS" BASIS, 16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 // See the License for the specific language governing permissions and 18 // limitations under the License. 19 #include "tao/tao_rpc.h" 20 21 #include <glog/logging.h> 22 23 #include "tao/fd_message_channel.h" 24 25 namespace tao { 26 27 bool TaoRPC::GetTaoName(string *name) { 28 TaoRPCRequest rpc; 29 return Request("Tao.GetTaoName", rpc, name, nullptr /* policy */, nullptr); 30 } 31 32 bool TaoRPC::ExtendTaoName(const string &subprin) { 33 TaoRPCRequest rpc; 34 rpc.set_data(subprin); 35 return Request("Tao.ExtendTaoName", rpc, nullptr /* data */, 36 nullptr /* policy */, nullptr); 37 } 38 39 bool TaoRPC::GetRandomBytes(size_t size, string *bytes) { 40 TaoRPCRequest rpc; 41 rpc.set_size(size); 42 return Request("Tao.GetRandomBytes", rpc, bytes, nullptr /* policy */, nullptr); 43 } 44 45 bool TaoRPC::GetSharedSecret(size_t size, const string &policy, string *bytes) { 46 TaoRPCRequest rpc; 47 rpc.set_size(size); 48 rpc.set_policy(policy); 49 return Request("Tao.GetSharedSecret", rpc, bytes, nullptr /* policy */, nullptr); 50 } 51 52 bool TaoRPC::Attest(const string &message, string *attestation) { 53 TaoRPCRequest rpc; 54 rpc.set_data(message); 55 return Request("Tao.Attest", rpc, attestation, nullptr /* policy */, nullptr); 56 } 57 58 bool TaoRPC::Seal(const string &data, const string &policy, string *sealed) { 59 TaoRPCRequest rpc; 60 rpc.set_data(data); 61 rpc.set_policy(policy); 62 return Request("Tao.Seal", rpc, sealed, nullptr /* policy */, nullptr); 63 } 64 65 bool TaoRPC::Unseal(const string &sealed, string *data, string *policy) { 66 TaoRPCRequest rpc; 67 rpc.set_data(sealed); 68 return Request("Tao.Unseal", rpc, data, policy, nullptr); 69 } 70 71 bool TaoRPC::InitCounter(const string& label, int64_t& c) { 72 TaoRPCRequest rpc; 73 rpc.set_label(label); 74 rpc.set_counter(c); 75 return Request("Tao.InitCounter", rpc, nullptr, nullptr, nullptr); 76 } 77 78 bool TaoRPC::GetCounter(const string& label, int64_t* c) { 79 TaoRPCRequest rpc; 80 rpc.set_label(label); 81 return Request("Tao.GetCounter", rpc, nullptr, nullptr, c); 82 } 83 84 bool TaoRPC::RollbackProtectedSeal(const string& label, const string &data, const string &policy, string *sealed) { 85 TaoRPCRequest rpc; 86 rpc.set_label(label); 87 rpc.set_policy(policy); 88 rpc.set_data(data); 89 return Request("Tao.RollbackProtectedSeal", rpc, sealed, nullptr, nullptr); 90 } 91 92 bool TaoRPC::RollbackProtectedUnseal(const string &sealed, string *data, string *policy) { 93 TaoRPCRequest rpc; 94 rpc.set_data(sealed); 95 return Request("Tao.RollbackProtectedUnseal", rpc, data, policy, nullptr); 96 } 97 98 bool TaoRPC::Request(const string &op, const TaoRPCRequest &req, string *data, 99 string *policy, int64_t* counter) { 100 ProtoRPCRequestHeader reqHdr; 101 ProtoRPCResponseHeader respHdr; 102 reqHdr.set_op(op); 103 reqHdr.set_seq(++last_seq_); 104 TaoRPCResponse resp; 105 bool eof; 106 if (!channel_->SendMessage(reqHdr)) { 107 failure_msg_ = "Channel send header failed"; 108 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 109 return false; 110 } 111 if (!channel_->SendMessage(req)) { 112 failure_msg_ = "Channel send failed"; 113 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 114 return false; 115 } 116 if (!channel_->ReceiveMessage(&respHdr, &eof)) { 117 failure_msg_ = "Channel receive header failed"; 118 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 119 return false; 120 } 121 if (eof) { 122 failure_msg_ = "Channel is closed"; 123 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 124 return false; 125 } 126 if (respHdr.has_error()) { 127 failure_msg_ = respHdr.error(); 128 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 129 string discard; 130 channel_->ReceiveString(&discard, &eof); 131 return false; 132 } 133 if (!channel_->ReceiveMessage(&resp, &eof)) { 134 failure_msg_ = "Channel receive failed"; 135 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 136 return false; 137 } 138 if (eof) { 139 failure_msg_ = "Channel is closed"; 140 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 141 return false; 142 } 143 if (respHdr.op() != op) { 144 failure_msg_ = "Unexpected operation in response"; 145 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 146 return false; 147 } 148 if (respHdr.seq() != reqHdr.seq()) { 149 failure_msg_ = "Unexpected sequence number in response"; 150 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 151 return false; 152 } 153 if (data != nullptr) { 154 if (!resp.has_data()) { 155 failure_msg_ = "Malformed response (missing data)"; 156 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 157 return false; 158 } 159 data->assign(resp.data()); 160 } 161 if (policy != nullptr) { 162 if (!resp.has_policy()) { 163 failure_msg_ = "Malformed response (missing policy)"; 164 LOG(ERROR) << "RPC to Tao host failed: " << failure_msg_; 165 return false; 166 } 167 policy->assign(resp.policy()); 168 } 169 if (counter != nullptr) { 170 *counter = resp.counter(); 171 } 172 return true; 173 } 174 175 bool TaoRPC::SerializeToString(string *params) const { 176 string channel_params; 177 if (!channel_->SerializeToString(&channel_params)) { 178 LOG(ERROR) << "Could not serialize TaoRPC"; 179 return false; 180 } 181 params->assign("tao::TaoRPC+" + channel_params); 182 return true; 183 } 184 185 TaoRPC *TaoRPC::DeserializeFromString(const string ¶ms) { 186 stringstream in(params); 187 skip(in, "tao::TaoRPC+"); 188 if (!in) return nullptr; // not for us 189 string channel_params; 190 getline(in, channel_params, '\0'); 191 // Try each known channel type. 192 MessageChannel *channel; 193 channel = FDMessageChannel::DeserializeFromString(channel_params); 194 if (channel != nullptr) return new TaoRPC(channel); 195 LOG(ERROR) << "Unknown channel serialized for TaoRPC"; 196 return nullptr; 197 } 198 199 } // namespace tao