| 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 #include "crypto/scoped_openssl_types.h" | 16 #include "crypto/scoped_openssl_types.h" |
| 17 | 17 |
| 18 namespace crypto { | 18 namespace crypto { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const EVP_MD* ToOpenSSLDigest(SignatureVerifier::HashAlgorithm hash_alg) { | 22 const EVP_MD* ToOpenSSLDigest(SignatureVerifier::HashAlgorithm hash_alg) { |
| 23 switch (hash_alg) { | 23 switch (hash_alg) { |
| 24 case SignatureVerifier::SHA1: | 24 case SignatureVerifier::SHA1: |
| 25 return EVP_sha1(); | 25 return EVP_sha1(); |
| 26 case SignatureVerifier::SHA256: | 26 case SignatureVerifier::SHA256: |
| 27 return EVP_sha256(); | 27 return EVP_sha256(); |
| 28 } | 28 } |
| 29 return EVP_md_null(); | 29 return NULL; |
| 30 } | 30 } |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 struct SignatureVerifier::VerifyContext { | 34 struct SignatureVerifier::VerifyContext { |
| 35 ScopedEVP_MD_CTX ctx; | 35 ScopedEVP_MD_CTX ctx; |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 SignatureVerifier::SignatureVerifier() | 38 SignatureVerifier::SignatureVerifier() |
| 39 : verify_context_(NULL) { | 39 : verify_context_(NULL) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 } | 73 } |
| 74 | 74 |
| 75 bool SignatureVerifier::VerifyInitRSAPSS(HashAlgorithm hash_alg, | 75 bool SignatureVerifier::VerifyInitRSAPSS(HashAlgorithm hash_alg, |
| 76 HashAlgorithm mask_hash_alg, | 76 HashAlgorithm mask_hash_alg, |
| 77 int salt_len, | 77 int salt_len, |
| 78 const uint8* signature, | 78 const uint8* signature, |
| 79 int signature_len, | 79 int signature_len, |
| 80 const uint8* public_key_info, | 80 const uint8* public_key_info, |
| 81 int public_key_info_len) { | 81 int public_key_info_len) { |
| 82 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 82 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 83 const EVP_MD* digest = ToOpenSSLDigest(hash_alg); | 83 const EVP_MD* const digest = ToOpenSSLDigest(hash_alg); |
| 84 DCHECK(digest); | 84 DCHECK(digest); |
| 85 if (!digest) { |
| 86 return false; |
| 87 } |
| 85 | 88 |
| 86 EVP_PKEY_CTX* pkey_ctx; | 89 EVP_PKEY_CTX* pkey_ctx; |
| 87 if (!CommonInit(digest, signature, signature_len, public_key_info, | 90 if (!CommonInit(digest, signature, signature_len, public_key_info, |
| 88 public_key_info_len, &pkey_ctx)) { | 91 public_key_info_len, &pkey_ctx)) { |
| 89 return false; | 92 return false; |
| 90 } | 93 } |
| 91 | 94 |
| 92 int rv = EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING); | 95 int rv = EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING); |
| 93 if (rv != 1) | 96 if (rv != 1) |
| 94 return false; | 97 return false; |
| 95 rv = EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, | 98 const EVP_MD* const mgf_digest = ToOpenSSLDigest(mask_hash_alg); |
| 96 ToOpenSSLDigest(mask_hash_alg)); | 99 DCHECK(mgf_digest); |
| 100 if (!mgf_digest) { |
| 101 return false; |
| 102 } |
| 103 rv = EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, mgf_digest); |
| 97 if (rv != 1) | 104 if (rv != 1) |
| 98 return false; | 105 return false; |
| 99 rv = EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, salt_len); | 106 rv = EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, salt_len); |
| 100 return rv == 1; | 107 return rv == 1; |
| 101 } | 108 } |
| 102 | 109 |
| 103 void SignatureVerifier::VerifyUpdate(const uint8* data_part, | 110 void SignatureVerifier::VerifyUpdate(const uint8* data_part, |
| 104 int data_part_len) { | 111 int data_part_len) { |
| 105 DCHECK(verify_context_); | 112 DCHECK(verify_context_); |
| 106 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 113 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 return rv == 1; | 157 return rv == 1; |
| 151 } | 158 } |
| 152 | 159 |
| 153 void SignatureVerifier::Reset() { | 160 void SignatureVerifier::Reset() { |
| 154 delete verify_context_; | 161 delete verify_context_; |
| 155 verify_context_ = NULL; | 162 verify_context_ = NULL; |
| 156 signature_.clear(); | 163 signature_.clear(); |
| 157 } | 164 } |
| 158 | 165 |
| 159 } // namespace crypto | 166 } // namespace crypto |
| OLD | NEW |