Home | History | Annotate | Download | only in attestation
      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.
      4 
      5 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h"
      6 
      7 #include <string>
      8 
      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"
     14 #include "url/gurl.h"
     15 
     16 namespace {
     17 
     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";
     21 
     22 }  // namespace
     23 
     24 namespace chromeos {
     25 namespace attestation {
     26 
     27 AttestationCAClient::AttestationCAClient() {}
     28 
     29 AttestationCAClient::~AttestationCAClient() {}
     30 
     31 void AttestationCAClient::SendEnrollRequest(const std::string& request,
     32                                             const DataCallback& on_response) {
     33   FetchURL(kEnrollRequestURL, request, on_response);
     34 }
     35 
     36 void AttestationCAClient::SendCertificateRequest(
     37     const std::string& request,
     38     const DataCallback& on_response) {
     39   FetchURL(kCertificateRequestURL, request, on_response);
     40 }
     41 
     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.";
     46     return;
     47   }
     48 
     49   DataCallback callback = iter->second;
     50   pending_requests_.erase(iter);
     51   scoped_ptr<const net::URLFetcher> scoped_source(source);
     52 
     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, "");
     58     return;
     59   }
     60 
     61   if (source->GetResponseCode() != net::HTTP_OK) {
     62     LOG(ERROR) << "Attestation CA sent an error response: "
     63                << source->GetResponseCode();
     64     callback.Run(false, "");
     65     return;
     66   }
     67 
     68   std::string response;
     69   bool result = source->GetResponseAsString(&response);
     70   DCHECK(result) << "Invalid fetcher setting.";
     71 
     72   // Run the callback last because it may delete |this|.
     73   callback.Run(true, response);
     74 }
     75 
     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,
     81                                                      GURL(url),
     82                                                      net::URLFetcher::POST,
     83                                                      this);
     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;
     90   fetcher->Start();
     91 }
     92 
     93 }  // namespace attestation
     94 }  // namespace chromeos
     95