OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/base/keygen_handler.h" | 5 #include "net/base/keygen_handler.h" |
6 | 6 |
| 7 #include <openssl/bytestring.h> |
| 8 #include <openssl/digest.h> |
| 9 #include <openssl/evp.h> |
7 #include <openssl/mem.h> | 10 #include <openssl/mem.h> |
8 #include <openssl/ssl.h> | 11 #include <stdint.h> |
9 | 12 |
| 13 #include "base/base64.h" |
| 14 #include "base/location.h" |
10 #include "base/logging.h" | 15 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/strings/string_piece.h" |
| 18 #include "crypto/auto_cbb.h" |
12 #include "crypto/openssl_util.h" | 19 #include "crypto/openssl_util.h" |
13 #include "crypto/rsa_private_key.h" | 20 #include "crypto/rsa_private_key.h" |
14 #include "crypto/scoped_openssl_types.h" | 21 #include "crypto/scoped_openssl_types.h" |
15 #include "net/base/openssl_private_key_store.h" | 22 #include "net/base/openssl_private_key_store.h" |
16 | 23 |
17 namespace net { | 24 namespace net { |
18 | 25 |
19 std::string KeygenHandler::GenKeyAndSignChallenge() { | 26 std::string KeygenHandler::GenKeyAndSignChallenge() { |
20 scoped_ptr<crypto::RSAPrivateKey> key( | 27 scoped_ptr<crypto::RSAPrivateKey> key( |
21 crypto::RSAPrivateKey::Create(key_size_in_bits_)); | 28 crypto::RSAPrivateKey::Create(key_size_in_bits_)); |
22 EVP_PKEY* pkey = key->key(); | 29 EVP_PKEY* pkey = key->key(); |
23 | 30 |
24 if (stores_key_) | 31 if (stores_key_) |
25 OpenSSLPrivateKeyStore::StoreKeyPair(url_, pkey); | 32 OpenSSLPrivateKeyStore::StoreKeyPair(url_, pkey); |
26 | 33 |
27 crypto::ScopedOpenSSL<NETSCAPE_SPKI, NETSCAPE_SPKI_free> spki( | 34 // Serialize the following structure, from |
28 NETSCAPE_SPKI_new()); | 35 // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen. |
29 ASN1_STRING_set(spki.get()->spkac->challenge, | 36 // |
30 challenge_.data(), challenge_.size()); | 37 // PublicKeyAndChallenge ::= SEQUENCE { |
31 NETSCAPE_SPKI_set_pubkey(spki.get(), pkey); | 38 // spki SubjectPublicKeyInfo, |
32 // Using MD5 as this is what is required in HTML5, even though the SPKI | 39 // challenge IA5STRING |
33 // structure does allow the use of a SHA-1 signature. | 40 // } |
34 NETSCAPE_SPKI_sign(spki.get(), pkey, EVP_md5()); | 41 // |
35 char* spkistr = NETSCAPE_SPKI_b64_encode(spki.get()); | 42 // SignedPublicKeyAndChallenge ::= SEQUENCE { |
| 43 // publicKeyAndChallenge PublicKeyAndChallenge, |
| 44 // signatureAlgorithm AlgorithmIdentifier, |
| 45 // signature BIT STRING |
| 46 // } |
| 47 // |
| 48 // The signature is over the PublicKeyAndChallenge. |
36 | 49 |
37 std::string result(spkistr); | 50 // TODO(davidben): If we gain another consumer, factor this code out into |
38 OPENSSL_free(spkistr); | 51 // shared logic, sharing OID definitions with the verifier, to support signing |
| 52 // other X.509-style structures. |
39 | 53 |
| 54 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 55 |
| 56 // Serialize up to the PublicKeyAndChallenge. |
| 57 crypto::AutoCBB cbb; |
| 58 CBB spkac, public_key_and_challenge, challenge; |
| 59 if (!CBB_init(cbb.get(), 0) || |
| 60 !CBB_add_asn1(cbb.get(), &spkac, CBS_ASN1_SEQUENCE) || |
| 61 !CBB_add_asn1(&spkac, &public_key_and_challenge, CBS_ASN1_SEQUENCE) || |
| 62 !EVP_marshal_public_key(&public_key_and_challenge, pkey) || |
| 63 !CBB_add_asn1(&public_key_and_challenge, &challenge, |
| 64 CBS_ASN1_IA5STRING) || |
| 65 !CBB_add_bytes(&challenge, |
| 66 reinterpret_cast<const uint8_t*>(challenge_.data()), |
| 67 challenge_.size()) || |
| 68 !CBB_flush(&spkac)) { |
| 69 return std::string(); |
| 70 } |
| 71 |
| 72 // Hash what's been written so far. |
| 73 crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); |
| 74 if (!EVP_DigestSignInit(ctx.get(), nullptr, EVP_md5(), nullptr, pkey) || |
| 75 !EVP_DigestSignUpdate(ctx.get(), CBB_data(&spkac), CBB_len(&spkac))) { |
| 76 return std::string(); |
| 77 } |
| 78 |
| 79 // The DER encoding of 1.2.840.113549.1.1.4, MD5 with RSA encryption. |
| 80 static const uint8_t kMd5WithRsaEncryption[] = { |
| 81 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x04, |
| 82 }; |
| 83 |
| 84 // Write the signatureAlgorithm. |
| 85 CBB algorithm, oid, null; |
| 86 if (!CBB_add_asn1(&spkac, &algorithm, CBS_ASN1_SEQUENCE) || |
| 87 !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) || |
| 88 !CBB_add_bytes(&oid, kMd5WithRsaEncryption, |
| 89 sizeof(kMd5WithRsaEncryption)) || |
| 90 !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL)) { |
| 91 return std::string(); |
| 92 } |
| 93 |
| 94 // Compute and write the signature. Note that X.509 signatures, although |
| 95 // always byte strings for RSA, are encoded as BIT STRINGS with a multiple of |
| 96 // 8 bits. |
| 97 CBB sig_bitstring; |
| 98 uint8_t* sig; |
| 99 size_t sig_len; |
| 100 if (!CBB_add_asn1(&spkac, &sig_bitstring, CBS_ASN1_BITSTRING) || |
| 101 !CBB_add_u8(&sig_bitstring, 0 /* no unused bits */) || |
| 102 // Determine the maximum length of the signature. |
| 103 !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len) || |
| 104 // Reserve |sig_len| bytes and write the signature to |spkac|. |
| 105 !CBB_reserve(&sig_bitstring, &sig, sig_len) || |
| 106 !EVP_DigestSignFinal(ctx.get(), sig, &sig_len) || |
| 107 !CBB_did_write(&sig_bitstring, sig_len)) { |
| 108 return std::string(); |
| 109 } |
| 110 |
| 111 // Finally, the structure is base64-encoded. |
| 112 uint8_t* der; |
| 113 size_t der_len; |
| 114 if (!CBB_finish(cbb.get(), &der, &der_len)) { |
| 115 return std::string(); |
| 116 } |
| 117 std::string result; |
| 118 base::Base64Encode( |
| 119 base::StringPiece(reinterpret_cast<const char*>(der), der_len), &result); |
| 120 OPENSSL_free(der); |
40 return result; | 121 return result; |
41 } | 122 } |
42 | 123 |
43 } // namespace net | 124 } // namespace net |
44 | |
OLD | NEW |