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"
14 #include "base/logging.h"
15 #include "crypto/nss_util.h"
16 #include "crypto/sha2.h"
17 #include "net/cert/signed_tree_head.h"
23 SECOidTag
GetNSSSigAlg(ct::DigitallySigned::SignatureAlgorithm alg
) {
25 case ct::DigitallySigned::SIG_ALGO_RSA
:
26 return SEC_OID_PKCS1_RSA_ENCRYPTION
;
27 case ct::DigitallySigned::SIG_ALGO_DSA
:
28 return SEC_OID_ANSIX9_DSA_SIGNATURE
;
29 case ct::DigitallySigned::SIG_ALGO_ECDSA
:
30 return SEC_OID_ANSIX962_EC_PUBLIC_KEY
;
31 case ct::DigitallySigned::SIG_ALGO_ANONYMOUS
:
34 return SEC_OID_UNKNOWN
;
38 SECOidTag
GetNSSHashAlg(ct::DigitallySigned::HashAlgorithm alg
) {
40 case ct::DigitallySigned::HASH_ALGO_MD5
:
42 case ct::DigitallySigned::HASH_ALGO_SHA1
:
44 case ct::DigitallySigned::HASH_ALGO_SHA224
:
45 return SEC_OID_SHA224
;
46 case ct::DigitallySigned::HASH_ALGO_SHA256
:
47 return SEC_OID_SHA256
;
48 case ct::DigitallySigned::HASH_ALGO_SHA384
:
49 return SEC_OID_SHA384
;
50 case ct::DigitallySigned::HASH_ALGO_SHA512
:
51 return SEC_OID_SHA512
;
52 case ct::DigitallySigned::HASH_ALGO_NONE
:
55 return SEC_OID_UNKNOWN
;
61 CTLogVerifier::~CTLogVerifier() {
63 SECKEY_DestroyPublicKey(public_key_
);
66 bool CTLogVerifier::Init(const base::StringPiece
& public_key
) {
69 crypto::EnsureNSSInit();
71 key_data
.data
= reinterpret_cast<unsigned char*>(
72 const_cast<char*>(public_key
.data()));
73 key_data
.len
= public_key
.size();
75 CERTSubjectPublicKeyInfo
* public_key_info
=
76 SECKEY_DecodeDERSubjectPublicKeyInfo(&key_data
);
77 if (!public_key_info
) {
78 DVLOG(1) << "Failed decoding public key.";
82 public_key_
= SECKEY_ExtractPublicKey(public_key_info
);
83 SECKEY_DestroySubjectPublicKeyInfo(public_key_info
);
86 DVLOG(1) << "Failed extracting public key.";
90 key_id_
= crypto::SHA256HashString(public_key
);
92 // Right now, only RSASSA-PKCS1v15 with SHA-256 and ECDSA with SHA-256 are
94 switch (SECKEY_GetPublicKeyType(public_key_
)) {
96 hash_algorithm_
= ct::DigitallySigned::HASH_ALGO_SHA256
;
97 signature_algorithm_
= ct::DigitallySigned::SIG_ALGO_RSA
;
100 hash_algorithm_
= ct::DigitallySigned::HASH_ALGO_SHA256
;
101 signature_algorithm_
= ct::DigitallySigned::SIG_ALGO_ECDSA
;
104 DVLOG(1) << "Unsupported key type: "
105 << SECKEY_GetPublicKeyType(public_key_
);
109 // Extra sanity check: Require RSA keys of at least 2048 bits.
110 if (signature_algorithm_
== ct::DigitallySigned::SIG_ALGO_RSA
&&
111 SECKEY_PublicKeyStrengthInBits(public_key_
) < 2048) {
112 DVLOG(1) << "Too small a public key.";
119 bool CTLogVerifier::VerifySignature(const base::StringPiece
& data_to_sign
,
120 const base::StringPiece
& signature
) {
122 sig_data
.data
= reinterpret_cast<unsigned char*>(const_cast<char*>(
124 sig_data
.len
= signature
.size();
126 SECStatus rv
= VFY_VerifyDataDirect(
127 reinterpret_cast<const unsigned char*>(data_to_sign
.data()),
128 data_to_sign
.size(), public_key_
, &sig_data
,
129 GetNSSSigAlg(signature_algorithm_
), GetNSSHashAlg(hash_algorithm_
),
131 DVLOG(1) << "Signature verification result: " << (rv
== SECSuccess
);
132 return rv
== SECSuccess
;