Chromium Code Reviews| 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 "crypto/signature_verifier.h" | 5 #include "crypto/signature_verifier.h" |
| 6 | 6 |
| 7 #include <openssl/evp.h> | 7 #include <openssl/evp.h> |
| 8 #include <openssl/x509.h> | 8 #include <openssl/x509.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "crypto/openssl_util.h" | 15 #include "crypto/openssl_util.h" |
| 16 | 16 |
| 17 namespace crypto { | 17 namespace crypto { |
| 18 | 18 |
| 19 namespace { | |
| 20 | |
| 21 const EVP_MD* ToOpenSSLDigest(SignatureVerifier::HashAlgorithm hash_alg) { | |
| 22 switch (hash_alg) { | |
| 23 case SignatureVerifier::SHA1: | |
| 24 return EVP_sha1(); | |
| 25 case SignatureVerifier::SHA256: | |
| 26 return EVP_sha256(); | |
| 27 default: | |
|
agl
2013/06/26 15:21:10
Just a thought: if we omit the default case, would
Ryan Sleevi
2013/06/26 19:48:28
Yes, it would. So +1 to omitting.
wtc
2013/06/27 02:23:51
Done.
| |
| 28 return NULL; | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 19 struct SignatureVerifier::VerifyContext { | 34 struct SignatureVerifier::VerifyContext { |
| 20 ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> public_key; | |
| 21 ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> ctx; | 35 ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> ctx; |
| 22 }; | 36 }; |
| 23 | 37 |
| 24 SignatureVerifier::SignatureVerifier() | 38 SignatureVerifier::SignatureVerifier() |
| 25 : verify_context_(NULL) { | 39 : verify_context_(NULL) { |
| 26 } | 40 } |
| 27 | 41 |
| 28 SignatureVerifier::~SignatureVerifier() { | 42 SignatureVerifier::~SignatureVerifier() { |
| 29 Reset(); | 43 Reset(); |
| 30 } | 44 } |
| 31 | 45 |
| 32 bool SignatureVerifier::VerifyInit(const uint8* signature_algorithm, | 46 bool SignatureVerifier::CommonInit(const EVP_MD* digest, |
| 33 int signature_algorithm_len, | |
| 34 const uint8* signature, | 47 const uint8* signature, |
| 35 int signature_len, | 48 int signature_len, |
| 36 const uint8* public_key_info, | 49 const uint8* public_key_info, |
| 37 int public_key_info_len) { | 50 int public_key_info_len, |
| 51 EVP_PKEY_CTX** pkey_ctx) { | |
| 38 DCHECK(!verify_context_); | 52 DCHECK(!verify_context_); |
| 39 verify_context_ = new VerifyContext; | 53 verify_context_ = new VerifyContext; |
| 40 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 54 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 41 | 55 |
| 42 ScopedOpenSSL<X509_ALGOR, X509_ALGOR_free> algorithm( | |
| 43 d2i_X509_ALGOR(NULL, &signature_algorithm, signature_algorithm_len)); | |
| 44 if (!algorithm.get()) | |
| 45 return false; | |
| 46 | |
| 47 const EVP_MD* digest = EVP_get_digestbyobj(algorithm.get()->algorithm); | |
| 48 DCHECK(digest); | |
| 49 | |
| 50 signature_.assign(signature, signature + signature_len); | 56 signature_.assign(signature, signature + signature_len); |
| 51 | 57 |
| 52 // BIO_new_mem_buf is not const aware, but it does not modify the buffer. | 58 // BIO_new_mem_buf is not const aware, but it does not modify the buffer. |
| 53 char* data = reinterpret_cast<char*>(const_cast<uint8*>(public_key_info)); | 59 char* data = reinterpret_cast<char*>(const_cast<uint8*>(public_key_info)); |
| 54 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, | 60 ScopedOpenSSL<BIO, BIO_free_all> bio(BIO_new_mem_buf(data, |
| 55 public_key_info_len)); | 61 public_key_info_len)); |
| 56 if (!bio.get()) | 62 if (!bio.get()) |
| 57 return false; | 63 return false; |
| 58 | 64 |
| 59 verify_context_->public_key.reset(d2i_PUBKEY_bio(bio.get(), NULL)); | 65 ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> public_key( |
| 60 if (!verify_context_->public_key.get()) | 66 d2i_PUBKEY_bio(bio.get(), NULL)); |
| 67 if (!public_key.get()) | |
| 61 return false; | 68 return false; |
| 62 | 69 |
| 63 verify_context_->ctx.reset(EVP_MD_CTX_create()); | 70 verify_context_->ctx.reset(EVP_MD_CTX_create()); |
| 64 int rv = EVP_VerifyInit_ex(verify_context_->ctx.get(), digest, NULL); | 71 int rv = EVP_DigestVerifyInit(verify_context_->ctx.get(), pkey_ctx, |
| 72 digest, NULL, public_key.get()); | |
| 73 return rv == 1; | |
| 74 } | |
| 75 | |
| 76 bool SignatureVerifier::VerifyInit(const uint8* signature_algorithm, | |
| 77 int signature_algorithm_len, | |
| 78 const uint8* signature, | |
| 79 int signature_len, | |
| 80 const uint8* public_key_info, | |
| 81 int public_key_info_len) { | |
| 82 ScopedOpenSSL<X509_ALGOR, X509_ALGOR_free> algorithm( | |
|
Ryan Sleevi
2013/06/26 19:48:28
BUG? No OpenSSLErrStackTracer to wipe errors here
wtc
2013/06/27 02:23:51
Done.
| |
| 83 d2i_X509_ALGOR(NULL, &signature_algorithm, signature_algorithm_len)); | |
| 84 if (!algorithm.get()) | |
| 85 return false; | |
| 86 const EVP_MD* digest = EVP_get_digestbyobj(algorithm.get()->algorithm); | |
| 87 DCHECK(digest); | |
|
Ryan Sleevi
2013/06/26 19:48:28
Shouldn't this be a handled case?
It seems like i
wtc
2013/06/27 02:23:51
Done.
I assume |digest| can only be null if OpenS
| |
| 88 | |
| 89 return CommonInit(digest, signature, signature_len, public_key_info, | |
| 90 public_key_info_len, NULL); | |
| 91 } | |
| 92 | |
| 93 bool SignatureVerifier::VerifyInitRSAPSS(HashAlgorithm hash_alg, | |
| 94 HashAlgorithm mask_hash_alg, | |
| 95 int salt_len, | |
| 96 const uint8* signature, | |
| 97 int signature_len, | |
| 98 const uint8* public_key_info, | |
| 99 int public_key_info_len) { | |
| 100 const EVP_MD* digest = ToOpenSSLDigest(hash_alg); | |
|
Ryan Sleevi
2013/06/26 19:48:28
BUG? No OpenSSLErrStackTracer to wipe errors here
wtc
2013/06/27 02:23:51
Done.
| |
| 101 DCHECK(digest); | |
| 102 | |
| 103 EVP_PKEY_CTX* pkey_ctx; | |
| 104 if (!CommonInit(digest, signature, signature_len, public_key_info, | |
| 105 public_key_info_len, &pkey_ctx)) { | |
| 106 return false; | |
| 107 } | |
| 108 | |
| 109 int rv = EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING); | |
| 110 if (rv != 1) | |
| 111 return false; | |
| 112 rv = EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, | |
| 113 ToOpenSSLDigest(mask_hash_alg)); | |
| 114 if (rv != 1) | |
| 115 return false; | |
| 116 rv = EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, salt_len); | |
| 65 return rv == 1; | 117 return rv == 1; |
| 66 } | 118 } |
| 67 | 119 |
| 68 void SignatureVerifier::VerifyUpdate(const uint8* data_part, | 120 void SignatureVerifier::VerifyUpdate(const uint8* data_part, |
| 69 int data_part_len) { | 121 int data_part_len) { |
| 70 DCHECK(verify_context_); | 122 DCHECK(verify_context_); |
| 71 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 123 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 72 int rv = EVP_VerifyUpdate(verify_context_->ctx.get(), | 124 int rv = EVP_DigestVerifyUpdate(verify_context_->ctx.get(), |
| 73 data_part, data_part_len); | 125 data_part, data_part_len); |
| 74 DCHECK_EQ(rv, 1); | 126 DCHECK_EQ(rv, 1); |
| 75 } | 127 } |
| 76 | 128 |
| 77 bool SignatureVerifier::VerifyFinal() { | 129 bool SignatureVerifier::VerifyFinal() { |
| 78 DCHECK(verify_context_); | 130 DCHECK(verify_context_); |
| 79 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 131 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 80 int rv = EVP_VerifyFinal(verify_context_->ctx.get(), | 132 int rv = EVP_DigestVerifyFinal(verify_context_->ctx.get(), |
| 81 vector_as_array(&signature_), signature_.size(), | 133 vector_as_array(&signature_), |
| 82 verify_context_->public_key.get()); | 134 signature_.size()); |
| 83 DCHECK_GE(rv, 0); | 135 DCHECK_GE(rv, 0); |
| 84 Reset(); | 136 Reset(); |
| 85 return rv == 1; | 137 return rv == 1; |
| 86 } | 138 } |
| 87 | 139 |
| 88 void SignatureVerifier::Reset() { | 140 void SignatureVerifier::Reset() { |
| 89 delete verify_context_; | 141 delete verify_context_; |
| 90 verify_context_ = NULL; | 142 verify_context_ = NULL; |
| 91 signature_.clear(); | 143 signature_.clear(); |
| 92 } | 144 } |
| 93 | 145 |
| 94 } // namespace crypto | 146 } // namespace crypto |
| OLD | NEW |