OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/gcd_private/privet_v3_session.h" | 5 #include "chrome/browser/extensions/api/gcd_private/privet_v3_session.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "chrome/browser/extensions/api/gcd_private/privet_v3_context_getter.h" |
13 #include "chrome/browser/local_discovery/privet_constants.h" | 14 #include "chrome/browser/local_discovery/privet_constants.h" |
14 #include "chrome/browser/local_discovery/privet_http.h" | 15 #include "chrome/browser/local_discovery/privet_http.h" |
15 #include "chrome/browser/local_discovery/privet_http_impl.h" | 16 #include "chrome/browser/local_discovery/privet_http_impl.h" |
16 #include "chrome/browser/local_discovery/privet_url_fetcher.h" | 17 #include "chrome/browser/local_discovery/privet_url_fetcher.h" |
17 #include "chrome/common/cloud_print/cloud_print_constants.h" | 18 #include "chrome/common/cloud_print/cloud_print_constants.h" |
18 #include "crypto/hmac.h" | 19 #include "crypto/hmac.h" |
19 #include "crypto/p224_spake.h" | 20 #include "crypto/p224_spake.h" |
20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
21 | 22 |
22 using local_discovery::PrivetURLFetcher; | 23 using local_discovery::PrivetURLFetcher; |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 url_fetcher_.reset(); | 203 url_fetcher_.reset(); |
203 } | 204 } |
204 | 205 |
205 void PrivetV3Session::FetcherDelegate::OnTimeout() { | 206 void PrivetV3Session::FetcherDelegate::OnTimeout() { |
206 LOG(ERROR) << "PrivetURLFetcher timeout, url: " << url_fetcher_->url(); | 207 LOG(ERROR) << "PrivetURLFetcher timeout, url: " << url_fetcher_->url(); |
207 ReplyAndDestroyItself(Result::STATUS_CONNECTIONERROR, | 208 ReplyAndDestroyItself(Result::STATUS_CONNECTIONERROR, |
208 base::DictionaryValue()); | 209 base::DictionaryValue()); |
209 } | 210 } |
210 | 211 |
211 PrivetV3Session::PrivetV3Session( | 212 PrivetV3Session::PrivetV3Session( |
212 const scoped_refptr<net::URLRequestContextGetter>& context_getter, | 213 const scoped_refptr<PrivetV3ContextGetter>& context_getter, |
213 const net::HostPortPair& host_port) | 214 const net::HostPortPair& host_port) |
214 : client_(new local_discovery::PrivetHTTPClientImpl("", | 215 : client_(new local_discovery::PrivetHTTPClientImpl("", |
215 host_port, | 216 host_port, |
216 context_getter)), | 217 context_getter)), |
217 weak_ptr_factory_(this) {} | 218 context_getter_(context_getter), |
| 219 weak_ptr_factory_(this) { |
| 220 CHECK(context_getter_); |
| 221 } |
218 | 222 |
219 PrivetV3Session::~PrivetV3Session() { | 223 PrivetV3Session::~PrivetV3Session() { |
220 Cancel(); | 224 Cancel(); |
221 } | 225 } |
222 | 226 |
223 void PrivetV3Session::Init(const InitCallback& callback) { | 227 void PrivetV3Session::Init(const InitCallback& callback) { |
224 DCHECK(fetchers_.empty()); | 228 DCHECK(fetchers_.empty()); |
225 DCHECK(!client_->IsInHttpsMode()); | 229 DCHECK(!client_->IsInHttpsMode()); |
226 DCHECK(session_id_.empty()); | 230 DCHECK(session_id_.empty()); |
227 DCHECK(privet_auth_token_.empty()); | 231 DCHECK(privet_auth_token_.empty()); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 } | 364 } |
361 | 365 |
362 std::string auth_code(hmac.DigestLength(), ' '); | 366 std::string auth_code(hmac.DigestLength(), ' '); |
363 if (!hmac.Sign(session_id_, | 367 if (!hmac.Sign(session_id_, |
364 reinterpret_cast<unsigned char*>(string_as_array(&auth_code)), | 368 reinterpret_cast<unsigned char*>(string_as_array(&auth_code)), |
365 auth_code.size())) { | 369 auth_code.size())) { |
366 LOG(FATAL) << "Signing failed"; | 370 LOG(FATAL) << "Signing failed"; |
367 return callback.Run(Result::STATUS_SESSIONERROR); | 371 return callback.Run(Result::STATUS_SESSIONERROR); |
368 } | 372 } |
369 | 373 |
370 // From now use only https with fixed certificate. | |
371 VLOG(1) << "Expected certificate: " << fingerprint; | 374 VLOG(1) << "Expected certificate: " << fingerprint; |
372 client_->SwitchToHttps(https_port_, hash); | 375 context_getter_->AddPairedHost( |
| 376 client_->GetHost(), hash, |
| 377 base::Bind(&PrivetV3Session::OnPairedHostAddedToContext, |
| 378 weak_ptr_factory_.GetWeakPtr(), auth_code, callback)); |
| 379 } |
| 380 |
| 381 void PrivetV3Session::OnPairedHostAddedToContext( |
| 382 const std::string& auth_code, |
| 383 const ResultCallback& callback) { |
| 384 // Now use https with fixed certificate. |
| 385 client_->SwitchToHttps(https_port_); |
373 | 386 |
374 std::string auth_code_base64; | 387 std::string auth_code_base64; |
375 base::Base64Encode(auth_code, &auth_code_base64); | 388 base::Base64Encode(auth_code, &auth_code_base64); |
376 | 389 |
377 base::DictionaryValue input; | 390 base::DictionaryValue input; |
378 input.SetString(kPrivetV3KeyAuthCode, auth_code_base64); | 391 input.SetString(kPrivetV3KeyAuthCode, auth_code_base64); |
379 input.SetString(kPrivetV3KeyMode, kPrivetV3KeyPairing); | 392 input.SetString(kPrivetV3KeyMode, kPrivetV3KeyPairing); |
380 input.SetString(kPrivetV3KeyRequestedScope, kPrivetV3Auto); | 393 input.SetString(kPrivetV3KeyRequestedScope, kPrivetV3Auto); |
381 | 394 |
382 // Now we can use SendMessage with certificate validation. | 395 // Now we can use SendMessage with certificate validation. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 void PrivetV3Session::Cancel() { | 475 void PrivetV3Session::Cancel() { |
463 // Cancel started unconfirmed sessions. | 476 // Cancel started unconfirmed sessions. |
464 if (session_id_.empty() || client_->IsInHttpsMode()) | 477 if (session_id_.empty() || client_->IsInHttpsMode()) |
465 return; | 478 return; |
466 base::DictionaryValue input; | 479 base::DictionaryValue input; |
467 input.SetString(kPrivetV3KeySessionId, session_id_); | 480 input.SetString(kPrivetV3KeySessionId, session_id_); |
468 StartPostRequest(kPrivetV3PairingCancelPath, input, MessageCallback()); | 481 StartPostRequest(kPrivetV3PairingCancelPath, input, MessageCallback()); |
469 } | 482 } |
470 | 483 |
471 } // namespace extensions | 484 } // namespace extensions |
OLD | NEW |