summaryrefslogtreecommitdiffstats
path: root/net/base/keygen_handler_openssl.cc
blob: edd0eb110b4e5b131f34151dab7ab13a862378d3 (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
// Copyright (c) 2011 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/base/keygen_handler.h"

#include <openssl/ssl.h>

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "crypto/openssl_util.h"
#include "crypto/rsa_private_key.h"
#include "net/base/openssl_private_key_store.h"

namespace net {

std::string KeygenHandler::GenKeyAndSignChallenge() {
  scoped_ptr<crypto::RSAPrivateKey> key(
      crypto::RSAPrivateKey::Create(key_size_in_bits_));
  EVP_PKEY* pkey = key->key();

  if (stores_key_)
    OpenSSLPrivateKeyStore::StoreKeyPair(url_, pkey);

  crypto::ScopedOpenSSL<NETSCAPE_SPKI, NETSCAPE_SPKI_free> spki(
       NETSCAPE_SPKI_new());
  ASN1_STRING_set(spki.get()->spkac->challenge,
                  challenge_.data(), challenge_.size());
  NETSCAPE_SPKI_set_pubkey(spki.get(), pkey);
  // Using MD5 as this is what is required in HTML5, even though the SPKI
  // structure does allow the use of a SHA-1 signature.
  NETSCAPE_SPKI_sign(spki.get(), pkey, EVP_md5());
  char* spkistr = NETSCAPE_SPKI_b64_encode(spki.get());

  std::string result(spkistr);
  OPENSSL_free(spkistr);

  return result;
}

}  // namespace net