WiFiServiceImpl (Windows): Fixed wrong authentication type with WEP-PSK.
[chromium-blink-merge.git] / net / cert / ct_log_verifier_openssl.cc
blobaf875a58738be475c4ace28018723e57c1fa82ed
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/cert/ct_log_verifier.h"
7 #include <openssl/evp.h>
8 #include <openssl/x509.h>
10 #include "base/logging.h"
11 #include "crypto/openssl_util.h"
12 #include "crypto/scoped_openssl_types.h"
13 #include "crypto/sha2.h"
14 #include "net/cert/signed_tree_head.h"
16 namespace net {
18 namespace {
20 const EVP_MD* GetEvpAlg(ct::DigitallySigned::HashAlgorithm alg) {
21 switch (alg) {
22 case ct::DigitallySigned::HASH_ALGO_MD5:
23 return EVP_md5();
24 case ct::DigitallySigned::HASH_ALGO_SHA1:
25 return EVP_sha1();
26 case ct::DigitallySigned::HASH_ALGO_SHA224:
27 return EVP_sha224();
28 case ct::DigitallySigned::HASH_ALGO_SHA256:
29 return EVP_sha256();
30 case ct::DigitallySigned::HASH_ALGO_SHA384:
31 return EVP_sha384();
32 case ct::DigitallySigned::HASH_ALGO_SHA512:
33 return EVP_sha512();
34 case ct::DigitallySigned::HASH_ALGO_NONE:
35 default:
36 NOTREACHED();
37 return NULL;
41 } // namespace
43 CTLogVerifier::~CTLogVerifier() {
44 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
46 if (public_key_)
47 EVP_PKEY_free(public_key_);
50 bool CTLogVerifier::Init(const base::StringPiece& public_key) {
51 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
53 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(public_key.data());
54 const uint8_t* end = ptr + public_key.size();
55 public_key_ = d2i_PUBKEY(nullptr, &ptr, public_key.size());
56 if (!public_key_ || ptr != end)
57 return false;
59 key_id_ = crypto::SHA256HashString(public_key);
61 // Right now, only RSASSA-PKCS1v15 with SHA-256 and ECDSA with SHA-256 are
62 // supported.
63 switch (EVP_PKEY_type(public_key_->type)) {
64 case EVP_PKEY_RSA:
65 hash_algorithm_ = ct::DigitallySigned::HASH_ALGO_SHA256;
66 signature_algorithm_ = ct::DigitallySigned::SIG_ALGO_RSA;
67 break;
68 case EVP_PKEY_EC:
69 hash_algorithm_ = ct::DigitallySigned::HASH_ALGO_SHA256;
70 signature_algorithm_ = ct::DigitallySigned::SIG_ALGO_ECDSA;
71 break;
72 default:
73 DVLOG(1) << "Unsupported key type: " << EVP_PKEY_type(public_key_->type);
74 return false;
77 // Extra sanity check: Require RSA keys of at least 2048 bits.
78 // EVP_PKEY_size returns the size in bytes. 256 = 2048-bit RSA key.
79 if (signature_algorithm_ == ct::DigitallySigned::SIG_ALGO_RSA &&
80 EVP_PKEY_size(public_key_) < 256) {
81 DVLOG(1) << "Too small a public key.";
82 return false;
85 return true;
88 bool CTLogVerifier::VerifySignature(const base::StringPiece& data_to_sign,
89 const base::StringPiece& signature) {
90 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
92 const EVP_MD* hash_alg = GetEvpAlg(hash_algorithm_);
93 if (hash_alg == NULL)
94 return false;
96 EVP_MD_CTX ctx;
97 EVP_MD_CTX_init(&ctx);
99 bool ok = (
100 1 == EVP_DigestVerifyInit(&ctx, NULL, hash_alg, NULL, public_key_) &&
101 1 == EVP_DigestVerifyUpdate(
102 &ctx, data_to_sign.data(), data_to_sign.size()) &&
103 1 == EVP_DigestVerifyFinal(
104 &ctx,
105 reinterpret_cast<const uint8_t*>(signature.data()),
106 signature.size()));
108 EVP_MD_CTX_cleanup(&ctx);
109 return ok;
112 } // namespace net