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 crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
38 OPENSSL_free(spkistr); | |
39 | 51 |
52 // Serialize up to the PublicKeyAndChallenge. | |
53 crypto::AutoCBB cbb; | |
54 CBB spkac, public_key_and_challenge, challenge; | |
55 if (!CBB_init(cbb.get(), 0) || | |
56 !CBB_add_asn1(cbb.get(), &spkac, CBS_ASN1_SEQUENCE) || | |
57 !CBB_add_asn1(&spkac, &public_key_and_challenge, CBS_ASN1_SEQUENCE) || | |
58 !EVP_marshal_public_key(&public_key_and_challenge, pkey) || | |
59 !CBB_add_asn1(&public_key_and_challenge, &challenge, | |
60 CBS_ASN1_IA5STRING) || | |
61 !CBB_add_bytes(&challenge, | |
62 reinterpret_cast<const uint8_t*>(challenge_.data()), | |
63 challenge_.size()) || | |
Ryan Sleevi
2016/03/03 18:32:06
Is this fine if challenge_ is empty? I suspect any
davidben
2016/03/03 19:58:15
If challenge is empty, we'll emit an empty IA5STRI
Ryan Sleevi
2016/03/03 20:27:41
Specifically, by using .data() rather than .c_str(
davidben
2016/03/03 21:59:36
If you pass CBB_add_bytes a length of zero, it's n
| |
64 !CBB_flush(&spkac)) { | |
65 return std::string(); | |
66 } | |
67 | |
68 // Hash what's been written so far. | |
69 crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); | |
70 if (!EVP_DigestSignInit(ctx.get(), nullptr, EVP_md5(), nullptr, pkey) || | |
71 !EVP_DigestSignUpdate(ctx.get(), CBB_data(&spkac), CBB_len(&spkac))) { | |
72 return std::string(); | |
73 } | |
74 | |
75 // The DER encoding of the AlgorithmIdentifier for MD5 with RSA. | |
76 static const uint8_t kMd5WithRsaEncryption[] = { | |
77 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, | |
78 0xf7, 0x0d, 0x01, 0x01, 0x04, 0x05, 0x00, | |
79 }; | |
Ryan Sleevi
2016/03/03 18:32:06
Surely there's a less-gross way than hardcoding -
davidben
2016/03/03 19:58:15
There's OBJ_nid2cbb that would work. I didn't use
Ryan Sleevi
2016/03/03 20:27:41
I do not think that would be a good state - it wou
davidben
2016/03/03 21:59:36
The string wouldn't be duplicated. That's largely
Ryan Sleevi
2016/03/03 22:08:43
I think we're in agreement here. Specifically, my
davidben
2016/03/03 22:25:40
Yup. I haven't done that here since signature_algo
| |
80 | |
81 // Write the algorithm identifier and then the signature. | |
82 CBB sig_bitstring; | |
83 uint8_t* sig; | |
84 size_t sig_len; | |
85 if (!CBB_add_bytes(&spkac, kMd5WithRsaEncryption, | |
86 sizeof(kMd5WithRsaEncryption)) || | |
87 !CBB_add_asn1(&spkac, &sig_bitstring, CBS_ASN1_BITSTRING) || | |
88 !CBB_add_u8(&sig_bitstring, 0 /* padding */) || | |
Ryan Sleevi
2016/03/03 18:32:06
? Why is this necessary? Because the assumption is
davidben
2016/03/03 19:58:15
I think we've had this conversation before. :-) It
Ryan Sleevi
2016/03/03 20:27:41
That was much more than needed, but still doesn't
davidben
2016/03/03 21:59:36
Yes, this is that byte. I tend to call it the "pha
Ryan Sleevi
2016/03/03 22:08:43
Yup. That's all I was trying to understand - to ma
davidben
2016/03/03 22:25:40
Done.
| |
89 // Determine the maximum length of the signature. | |
90 !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len) || | |
91 // Reserve |sig_len| bytes and write the signature to |spkac|. | |
92 !CBB_reserve(&sig_bitstring, &sig, sig_len) || | |
93 !EVP_DigestSignFinal(ctx.get(), sig, &sig_len) || | |
94 !CBB_did_write(&sig_bitstring, sig_len)) { | |
Ryan Sleevi
2016/03/03 18:32:06
This feels like such a gross way to do it. Is this
davidben
2016/03/03 19:58:15
Which piece of boilerplate in particular?
If you
Ryan Sleevi
2016/03/03 20:27:41
Specifically, that the act of transcribing a Signa
davidben
2016/03/03 21:59:36
I don't believe I've ever had to write this before
Ryan Sleevi
2016/03/03 22:08:43
Oh, I thought you were saying you had a second cop
davidben
2016/03/03 22:25:40
Oh! No, I mean, the Mac code has an existing separ
| |
95 return std::string(); | |
96 } | |
97 | |
98 // Finally, the structure is base64-encoded. | |
99 uint8_t* der; | |
100 size_t der_len; | |
101 if (!CBB_finish(cbb.get(), &der, &der_len)) { | |
102 return std::string(); | |
103 } | |
104 std::string result; | |
105 base::Base64Encode( | |
106 base::StringPiece(reinterpret_cast<const char*>(der), der_len), &result); | |
107 OPENSSL_free(der); | |
40 return result; | 108 return result; |
41 } | 109 } |
42 | 110 |
43 } // namespace net | 111 } // namespace net |
44 | |
OLD | NEW |