1 // Copyright (c) 2013 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.
5 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h"
9 #include "chrome/browser/browser_process.h"
10 #include "net/base/load_flags.h"
11 #include "net/http/http_status_code.h"
12 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_request_status.h"
18 const char kCertificateRequestURL
[] = "https://chromeos-ca.gstatic.com/sign";
19 const char kEnrollRequestURL
[] = "https://chromeos-ca.gstatic.com/enroll";
20 const char kMimeContentType
[] = "application/octet-stream";
25 namespace attestation
{
27 AttestationCAClient::AttestationCAClient() {}
29 AttestationCAClient::~AttestationCAClient() {}
31 void AttestationCAClient::SendEnrollRequest(const std::string
& request
,
32 const DataCallback
& on_response
) {
33 FetchURL(kEnrollRequestURL
, request
, on_response
);
36 void AttestationCAClient::SendCertificateRequest(
37 const std::string
& request
,
38 const DataCallback
& on_response
) {
39 FetchURL(kCertificateRequestURL
, request
, on_response
);
42 void AttestationCAClient::OnURLFetchComplete(const net::URLFetcher
* source
) {
43 FetcherCallbackMap::iterator iter
= pending_requests_
.find(source
);
44 if (iter
== pending_requests_
.end()) {
45 LOG(WARNING
) << "Callback from unknown source.";
49 DataCallback callback
= iter
->second
;
50 pending_requests_
.erase(iter
);
51 scoped_ptr
<const net::URLFetcher
> scoped_source(source
);
53 if (source
->GetStatus().status() != net::URLRequestStatus::SUCCESS
) {
54 LOG(ERROR
) << "Attestation CA request failed, status: "
55 << source
->GetStatus().status() << ", error: "
56 << source
->GetStatus().error();
57 callback
.Run(false, "");
61 if (source
->GetResponseCode() != net::HTTP_OK
) {
62 LOG(ERROR
) << "Attestation CA sent an error response: "
63 << source
->GetResponseCode();
64 callback
.Run(false, "");
69 bool result
= source
->GetResponseAsString(&response
);
70 DCHECK(result
) << "Invalid fetcher setting.";
72 // Run the callback last because it may delete |this|.
73 callback
.Run(true, response
);
76 void AttestationCAClient::FetchURL(const std::string
& url
,
77 const std::string
& request
,
78 const DataCallback
& on_response
) {
79 // The first argument allows the use of TestURLFetcherFactory in tests.
80 net::URLFetcher
* fetcher
= net::URLFetcher::Create(0,
82 net::URLFetcher::POST
,
84 fetcher
->SetRequestContext(g_browser_process
->system_request_context());
85 fetcher
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
86 net::LOAD_DO_NOT_SAVE_COOKIES
|
87 net::LOAD_DISABLE_CACHE
);
88 fetcher
->SetUploadData(kMimeContentType
, request
);
89 pending_requests_
[fetcher
] = on_response
;
93 } // namespace attestation
94 } // namespace chromeos