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/sha2.h"
18 const EVP_MD
* GetEvpAlg(ct::DigitallySigned::HashAlgorithm alg
) {
20 case ct::DigitallySigned::HASH_ALGO_MD5
:
22 case ct::DigitallySigned::HASH_ALGO_SHA1
:
24 case ct::DigitallySigned::HASH_ALGO_SHA224
:
26 case ct::DigitallySigned::HASH_ALGO_SHA256
:
28 case ct::DigitallySigned::HASH_ALGO_SHA384
:
30 case ct::DigitallySigned::HASH_ALGO_SHA512
:
32 case ct::DigitallySigned::HASH_ALGO_NONE
:
41 CTLogVerifier::~CTLogVerifier() {
42 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
45 EVP_PKEY_free(public_key_
);
48 CTLogVerifier::CTLogVerifier()
49 : hash_algorithm_(ct::DigitallySigned::HASH_ALGO_NONE
),
50 signature_algorithm_(ct::DigitallySigned::SIG_ALGO_ANONYMOUS
),
53 bool CTLogVerifier::Init(const base::StringPiece
& public_key
,
54 const base::StringPiece
& description
) {
55 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
57 crypto::ScopedOpenSSL
<BIO
, BIO_free_all
> bio(
58 BIO_new_mem_buf(const_cast<char*>(public_key
.data()), public_key
.size()));
62 public_key_
= d2i_PUBKEY_bio(bio
.get(), NULL
);
66 key_id_
= crypto::SHA256HashString(public_key
);
67 description_
= description
.as_string();
69 // Right now, only RSASSA-PKCS1v15 with SHA-256 and ECDSA with SHA-256 are
71 switch (EVP_PKEY_type(public_key_
->type
)) {
73 hash_algorithm_
= ct::DigitallySigned::HASH_ALGO_SHA256
;
74 signature_algorithm_
= ct::DigitallySigned::SIG_ALGO_RSA
;
77 hash_algorithm_
= ct::DigitallySigned::HASH_ALGO_SHA256
;
78 signature_algorithm_
= ct::DigitallySigned::SIG_ALGO_ECDSA
;
81 DVLOG(1) << "Unsupported key type: " << EVP_PKEY_type(public_key_
->type
);
85 // Extra sanity check: Require RSA keys of at least 2048 bits.
86 // EVP_PKEY_size returns the size in bytes. 256 = 2048-bit RSA key.
87 if (signature_algorithm_
== ct::DigitallySigned::SIG_ALGO_RSA
&&
88 EVP_PKEY_size(public_key_
) < 256) {
89 DVLOG(1) << "Too small a public key.";
96 bool CTLogVerifier::VerifySignature(const base::StringPiece
& data_to_sign
,
97 const base::StringPiece
& signature
) {
98 crypto::OpenSSLErrStackTracer
err_tracer(FROM_HERE
);
100 const EVP_MD
* hash_alg
= GetEvpAlg(hash_algorithm_
);
101 if (hash_alg
== NULL
)
105 EVP_MD_CTX_init(&ctx
);
108 1 == EVP_DigestVerifyInit(&ctx
, NULL
, hash_alg
, NULL
, public_key_
) &&
109 1 == EVP_DigestVerifyUpdate(
110 &ctx
, data_to_sign
.data(), data_to_sign
.size()) &&
111 1 == EVP_DigestVerifyFinal(
113 reinterpret_cast<unsigned char*>(const_cast<char*>(signature
.data())),
116 EVP_MD_CTX_cleanup(&ctx
);