summaryrefslogtreecommitdiffstats
path: root/net/quic/crypto/proof_source_chromium_openssl.cc
blob: c89f2d9d89bb5eda72a1f4f40a51850aec967ff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/quic/crypto/proof_source_chromium.h"

#include <openssl/digest.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>

#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "crypto/openssl_util.h"
#include "net/quic/crypto/crypto_protocol.h"
#include "net/ssl/scoped_openssl_types.h"

using std::string;
using std::vector;

namespace net {

ProofSourceChromium::ProofSourceChromium() {}

ProofSourceChromium::~ProofSourceChromium() {}

bool ProofSourceChromium::Initialize(const base::FilePath& cert_path,
                                     const base::FilePath& key_path) {
  crypto::EnsureOpenSSLInit();

  std::string cert_data;
  if (!base::ReadFileToString(cert_path, &cert_data)) {
    DLOG(FATAL) << "Unable to read certificates.";
    return false;
  }

  CertificateList certs_in_file =
      X509Certificate::CreateCertificateListFromBytes(
          cert_data.data(), cert_data.size(), X509Certificate::FORMAT_AUTO);

  if (certs_in_file.empty()) {
    DLOG(FATAL) << "No certificates.";
    return false;
  }

  for (const scoped_refptr<X509Certificate>& cert : certs_in_file) {
    std::string der_encoded_cert;
    if (!X509Certificate::GetDEREncoded(cert->os_cert_handle(),
                                        &der_encoded_cert)) {
      return false;
    }
    certificates_.push_back(der_encoded_cert);
  }

  std::string key_data;
  if (!base::ReadFileToString(key_path, &key_data)) {
    DLOG(FATAL) << "Unable to read key.";
    return false;
  }

  const uint8_t* p = reinterpret_cast<const uint8_t*>(key_data.data());
  std::vector<uint8_t> input(p, p + key_data.size());
  private_key_.reset(crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(input));
  if (private_key_.get() == nullptr) {
    DLOG(FATAL) << "Unable to create private key.";
    return false;
  }
  return true;
}

bool ProofSourceChromium::GetProof(const IPAddressNumber& server_ip,
                                   const string& hostname,
                                   const string& server_config,
                                   bool ecdsa_ok,
                                   const vector<string>** out_certs,
                                   string* out_signature) {
  DCHECK(private_key_.get()) << " this: " << this;

  crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
  crypto::ScopedEVP_MD_CTX sign_context(EVP_MD_CTX_create());
  EVP_PKEY_CTX* pkey_ctx;
  if (!EVP_DigestSignInit(sign_context.get(), &pkey_ctx, EVP_sha256(), nullptr,
                          private_key_->key()) ||
      !EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) ||
      !EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1) ||
      !EVP_DigestSignUpdate(sign_context.get(), reinterpret_cast<const uint8*>(
                                                    kProofSignatureLabel),
                            sizeof(kProofSignatureLabel)) ||
      !EVP_DigestSignUpdate(sign_context.get(), reinterpret_cast<const uint8*>(
                                                    server_config.data()),
                            server_config.size())) {
    return false;
  }

  // Determine the maximum length of the signature.
  size_t len = 0;
  if (!EVP_DigestSignFinal(sign_context.get(), nullptr, &len)) {
    return false;
  }
  std::vector<uint8_t> signature(len);
  // Sign it.
  if (!EVP_DigestSignFinal(sign_context.get(), vector_as_array(&signature),
                           &len)) {
    return false;
  }
  signature.resize(len);
  out_signature->assign(reinterpret_cast<const char*>(&signature[0]),
                        signature.size());
  *out_certs = &certificates_;
  VLOG(1) << "signature: "
          << base::HexEncode(out_signature->data(), out_signature->size());
  return true;
}

}  // namespace net