Index: net/base/keygen_handler_openssl.cc |
diff --git a/net/base/keygen_handler_openssl.cc b/net/base/keygen_handler_openssl.cc |
index bc34012c1e8dd6ffe2e03f6d57278a130becbaf8..5e979e08375d7f47837cb86a3b590aa4465d5e50 100644 |
--- a/net/base/keygen_handler_openssl.cc |
+++ b/net/base/keygen_handler_openssl.cc |
@@ -4,11 +4,18 @@ |
#include "net/base/keygen_handler.h" |
+#include <openssl/bytestring.h> |
+#include <openssl/digest.h> |
+#include <openssl/evp.h> |
#include <openssl/mem.h> |
-#include <openssl/ssl.h> |
+#include <stdint.h> |
+#include "base/base64.h" |
+#include "base/location.h" |
#include "base/logging.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/strings/string_piece.h" |
+#include "crypto/auto_cbb.h" |
#include "crypto/openssl_util.h" |
#include "crypto/rsa_private_key.h" |
#include "crypto/scoped_openssl_types.h" |
@@ -24,21 +31,81 @@ std::string KeygenHandler::GenKeyAndSignChallenge() { |
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()); |
+ // Serialize the following structure, from |
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/keygen. |
+ // |
+ // PublicKeyAndChallenge ::= SEQUENCE { |
+ // spki SubjectPublicKeyInfo, |
+ // challenge IA5STRING |
+ // } |
+ // |
+ // SignedPublicKeyAndChallenge ::= SEQUENCE { |
+ // publicKeyAndChallenge PublicKeyAndChallenge, |
+ // signatureAlgorithm AlgorithmIdentifier, |
+ // signature BIT STRING |
+ // } |
+ // |
+ // The signature is over the PublicKeyAndChallenge. |
- std::string result(spkistr); |
- OPENSSL_free(spkistr); |
+ crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
+ // Serialize up to the PublicKeyAndChallenge. |
+ crypto::AutoCBB cbb; |
+ CBB spkac, public_key_and_challenge, challenge; |
+ if (!CBB_init(cbb.get(), 0) || |
+ !CBB_add_asn1(cbb.get(), &spkac, CBS_ASN1_SEQUENCE) || |
+ !CBB_add_asn1(&spkac, &public_key_and_challenge, CBS_ASN1_SEQUENCE) || |
+ !EVP_marshal_public_key(&public_key_and_challenge, pkey) || |
+ !CBB_add_asn1(&public_key_and_challenge, &challenge, |
+ CBS_ASN1_IA5STRING) || |
+ !CBB_add_bytes(&challenge, |
+ reinterpret_cast<const uint8_t*>(challenge_.data()), |
+ 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
|
+ !CBB_flush(&spkac)) { |
+ return std::string(); |
+ } |
+ |
+ // Hash what's been written so far. |
+ crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); |
+ if (!EVP_DigestSignInit(ctx.get(), nullptr, EVP_md5(), nullptr, pkey) || |
+ !EVP_DigestSignUpdate(ctx.get(), CBB_data(&spkac), CBB_len(&spkac))) { |
+ return std::string(); |
+ } |
+ |
+ // The DER encoding of the AlgorithmIdentifier for MD5 with RSA. |
+ static const uint8_t kMd5WithRsaEncryption[] = { |
+ 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, |
+ 0xf7, 0x0d, 0x01, 0x01, 0x04, 0x05, 0x00, |
+ }; |
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
|
+ |
+ // Write the algorithm identifier and then the signature. |
+ CBB sig_bitstring; |
+ uint8_t* sig; |
+ size_t sig_len; |
+ if (!CBB_add_bytes(&spkac, kMd5WithRsaEncryption, |
+ sizeof(kMd5WithRsaEncryption)) || |
+ !CBB_add_asn1(&spkac, &sig_bitstring, CBS_ASN1_BITSTRING) || |
+ !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.
|
+ // Determine the maximum length of the signature. |
+ !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len) || |
+ // Reserve |sig_len| bytes and write the signature to |spkac|. |
+ !CBB_reserve(&sig_bitstring, &sig, sig_len) || |
+ !EVP_DigestSignFinal(ctx.get(), sig, &sig_len) || |
+ !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
|
+ return std::string(); |
+ } |
+ |
+ // Finally, the structure is base64-encoded. |
+ uint8_t* der; |
+ size_t der_len; |
+ if (!CBB_finish(cbb.get(), &der, &der_len)) { |
+ return std::string(); |
+ } |
+ std::string result; |
+ base::Base64Encode( |
+ base::StringPiece(reinterpret_cast<const char*>(der), der_len), &result); |
+ OPENSSL_free(der); |
return result; |
} |
} // namespace net |
- |