Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(501)

Side by Side Diff: remoting/host/gnubby_utils.cc

Issue 138753005: Add gnubby authentication to remoting host (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change authorization socket flag name Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "remoting/host/gnubby_utils.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "base/base64.h"
11 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/values.h"
16 #include "remoting/base/logging.h"
17
18 namespace remoting {
19
20 namespace {
21
22 // Failure code to use when the code from the webapp response isn't available.
23 const int kGnubbyResponseFail = 1;
24
25 const int kSsh2AgentcGnubbySignRequest = 101; // 0x65
26 const int kSsh2AgentcGnubbySignResponse = 102; // 0x66
27
28 // Class to read gnubby blob data.
29 class BlobReader {
Sergey Ulanov 2014/02/09 22:29:54 Reuse Pickle from base/pickle.h?
psj 2014/02/10 22:57:22 Unfortunately not. Pickle pads to int boundaries a
30 public:
31 // Create a blob with the given data. Does not take ownership of the memory.
32 BlobReader(const char* data, int data_len);
33 virtual ~BlobReader();
34
35 // Read a byte from the blob. Returns true on success.
36 bool ReadByte(char* value);
37
38 // Read a four byte size from the blob. Returns true on success.
39 bool ReadSize(int* value);
40
41 // Read a size-prefixed blob. Returns true on success.
42 bool ReadBlobReader(scoped_ptr<BlobReader>* value);
43
44 // Read a size-prefixed string from the blob. Returns true on success.
45 bool ReadString(std::string* value);
46
47 private:
48 // The blob data.
49 const char* data_;
50
51 // The length of the blob data.
52 int data_len_;
53
54 // The current read index.
55 int index_;
56
57 DISALLOW_COPY_AND_ASSIGN(BlobReader);
58 };
59
60 // Class to write gnubby blob data.
61 class BlobWriter {
Sergey Ulanov 2014/02/09 22:29:54 ditto
psj 2014/02/10 22:57:22 ditto
62 public:
63 BlobWriter();
64 virtual ~BlobWriter();
65
66 // Write a byte to the blob.
67 void WriteByte(char value);
68
69 // Write a four byte size to the blob.
70 void WriteSize(int value);
71
72 // Write a size-prefixed blob to the blob.
73 void WriteBlobWriter(const BlobWriter& value);
74
75 // Write a size-prefixed string to the blob.
76 void WriteString(const std::string& value);
77
78 // Returns the blob data.
79 void GetData(std::string* data) const;
80
81 private:
82 // The blob data.
83 std::vector<char> data_;
84
85 DISALLOW_COPY_AND_ASSIGN(BlobWriter);
86 };
87
88 BlobReader::BlobReader(const char* data, int data_len)
89 : data_(data), data_len_(data_len), index_(0) {}
90
91 BlobReader::~BlobReader() {}
92
93 bool BlobReader::ReadByte(char* value) {
94 if (data_len_ - index_ < 0) {
95 *value = 0;
96 return false;
97 }
98 *value = data_[index_++];
99 return true;
100 }
101
102 bool BlobReader::ReadSize(int* value) {
103 if (data_len_ - (index_ + 4) < 0) {
104 *value = 0;
105 return false;
106 }
107 *value =
108 ((data_[index_] & 255) << 24) +
109 ((data_[index_ + 1] & 255) << 16) +
110 ((data_[index_ + 2] & 255) << 8) +
111 (data_[index_ + 3] & 255);
112 index_ += 4;
113 return true;
114 }
115
116 bool BlobReader::ReadBlobReader(scoped_ptr<BlobReader>* value) {
117 int blobSize;
118 if (!ReadSize(&blobSize) || data_len_ - (index_ + blobSize) < 0) {
119 value->reset();
120 return 0;
121 }
122 value->reset(new BlobReader(data_ + index_, blobSize));
123 index_ += blobSize;
124 return true;
125 }
126
127 bool BlobReader::ReadString(std::string* value) {
128 int length;
129 if (!ReadSize(&length) || data_len_ - (index_ + length) < 0) {
130 value->erase();
131 return 0;
132 }
133 value->assign(data_ + index_, length);
134 index_ += length;
135 return true;
136 }
137
138 BlobWriter::BlobWriter() {}
139
140 BlobWriter::~BlobWriter() {}
141
142 void BlobWriter::WriteByte(char value) {
143 data_.push_back(value);
144 }
145
146 void BlobWriter::WriteSize(int value) {
147 data_.push_back((value & 0xff000000) >> 24);
148 data_.push_back((value & 0xff0000) >> 16);
149 data_.push_back((value & 0xff00) >> 8);
150 data_.push_back(value & 0xff);
151 }
152
153 void BlobWriter::WriteBlobWriter(const BlobWriter& value) {
154 std::string data;
155 value.GetData(&data);
156 WriteString(data);
157 }
158
159 void BlobWriter::WriteString(const std::string& value) {
160 WriteSize(value.length());
161 for (std::string::const_iterator i = value.begin(); i != value.end(); ++i) {
162 data_.push_back(*i);
163 }
164 }
165
166 void BlobWriter::GetData(std::string* data) const {
167 data->assign(data_.data(), data_.size());
168 }
169
170 } // namespace
171
172 bool GnubbyUtils::GetJsonFromRequest(const char* data,
173 int data_len,
174 std::string* json) {
175 json->empty();
176
177 BlobReader ssh_request(data, data_len);
178 scoped_ptr<BlobReader> blob;
179 if (!ssh_request.ReadBlobReader(&blob)) {
180 return false;
181 }
182
183 char cmd = 0;
184 char timeout = 0;
185 int request_count = 0;
186 bool result = blob->ReadByte(&cmd);
187 result = result && blob->ReadByte(&timeout);
188 result = result && blob->ReadSize(&request_count);
189 if (!result || cmd != kSsh2AgentcGnubbySignRequest) {
190 return false;
191 }
192
193 base::DictionaryValue request;
194 request.SetString("type", "sign_helper_request");
195 request.SetInteger("timeout", timeout);
196
197 base::ListValue* sign_requests = new base::ListValue();
198 request.Set("signData", sign_requests);
199
200 for (int i = 0; i < request_count; ++i) {
201 scoped_ptr<BlobReader> sign_request;
202 std::string version;
203 std::string challenge_hash;
204 std::string origin_hash;
205 std::string key_handle;
206
207 result = result && blob->ReadBlobReader(&sign_request);
208 result = result && sign_request->ReadString(&version);
209 result = result && sign_request->ReadString(&challenge_hash);
210 result = result && sign_request->ReadString(&origin_hash);
211 result = result && sign_request->ReadString(&key_handle);
212 if (!result) {
213 return false;
214 }
215
216 std::string encoded_origin_hash;
217 std::string encoded_challenge_hash;
218 std::string encoded_key_handle;
219
220 WebSafeBase64Encode(origin_hash, &encoded_origin_hash);
221 WebSafeBase64Encode(challenge_hash, &encoded_challenge_hash);
222 WebSafeBase64Encode(key_handle, &encoded_key_handle);
223
224 base::DictionaryValue* request = new base::DictionaryValue();
225 request->SetString("appIdHash", encoded_origin_hash);
226 request->SetString("challengeHash", encoded_challenge_hash);
227 request->SetString("keyHandle", encoded_key_handle);
228 request->SetString("version", version);
229 sign_requests->Append(request);
230 }
231
232 base::JSONWriter::Write(&request, json);
233 return true;
234 }
235
236 void GnubbyUtils::GetResponseFromJson(std::string json, std::string* data) {
237 data->erase();
238
239 scoped_ptr<base::Value> json_value(base::JSONReader::Read(json));
240 base::DictionaryValue* reply;
241 if (json_value && json_value->GetAsDictionary(&reply)) {
242 BlobWriter response;
243 response.WriteByte(kSsh2AgentcGnubbySignResponse);
244
245 int code;
246 bool result = reply->GetInteger("code", &code);
247 if (result && code == 0) {
248 response.WriteSize(code);
249
250 std::string type;
251 base::DictionaryValue* reply_data;
252 result = reply->GetString("type", &type);
253 result = result && type == "sign_helper_reply";
254 result = result && reply->GetDictionary("responseData", &reply_data);
255 if (result) {
256 BlobWriter tmp;
257 std::string version;
258 result = reply_data->GetString("version", &version);
259 if (result) {
260 tmp.WriteString(version);
261 } else {
262 tmp.WriteSize(0);
263 }
264
265 std::string challenge_hash;
266 std::string app_id_hash;
267 std::string key_handle;
268 std::string signature_data;
269
270 result = DecodeDataFromDictionary(
271 *reply_data, "challengeHash", &challenge_hash);
272 result = result && DecodeDataFromDictionary(
273 *reply_data, "appIdHash", &app_id_hash);
274 result = result && DecodeDataFromDictionary(
275 *reply_data, "keyHandle", &key_handle);
276 result = result && DecodeDataFromDictionary(
277 *reply_data, "signatureData", &signature_data);
278
279 if (result) {
280 tmp.WriteString(challenge_hash);
281 tmp.WriteString(app_id_hash);
282 tmp.WriteString(key_handle);
283 tmp.WriteString(signature_data);
284 }
285 response.WriteBlobWriter(tmp);
286 }
287 } else {
288 response.WriteSize(kGnubbyResponseFail);
289 }
290
291 BlobWriter ssh_response;
292 ssh_response.WriteBlobWriter(response);
293 ssh_response.GetData(data);
294
295 if (!result) {
296 HOST_LOG << "Failed to create valid blob from json";
297 }
298 } else {
299 HOST_LOG << "Could not parse json: " << json;
300 }
301 }
302
303 void GnubbyUtils::WebSafeBase64Encode(const std::string& data,
304 std::string* encoded_data) {
305 base::Base64Encode(data, encoded_data);
306
307 std::replace(encoded_data->begin(), encoded_data->end(), '+', '-');
308 std::replace(encoded_data->begin(), encoded_data->end(), '/', '_');
309 encoded_data->erase(
310 std::remove(encoded_data->begin(), encoded_data->end(), '='),
311 encoded_data->end());
312 }
313
314 void GnubbyUtils::WebSafeBase64Decode(const std::string& encoded_data,
315 std::string* data) {
316 std::string temp(encoded_data);
317 std::replace(temp.begin(), temp.end(), '-', '+');
318 std::replace(temp.begin(), temp.end(), '_', '/');
319
320 int num_equals = temp.length() % 3;
321 for (int i = 0; i < num_equals; ++i) {
322 temp += "=";
323 }
324
325 base::Base64Decode(temp, data);
326 }
327
328 bool GnubbyUtils::DecodeDataFromDictionary(
329 const base::DictionaryValue& dictionary,
330 const std::string& path,
331 std::string* data) {
332 std::string encoded_data;
333 bool result = dictionary.GetString(path, &encoded_data);
334 if (result) {
335 WebSafeBase64Decode(encoded_data, data);
336 } else {
337 HOST_LOG << "Failed to get dictionary value " << path;
338 data->erase();
339 }
340 return result;
341 }
342
343 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698