Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: net/cert/ct_log_verifier_nss.cc

Issue 55953002: CT: Adding SCT verification functionality: A CTLogVerifier instance can verify SCTs signed by a sin… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #include "net/cert/ct_log_verifier.h"
6
7 #include <cryptohi.h>
8 #include <keyhi.h>
9 #include <nss.h>
10 #include <pk11pub.h>
11 #include <secitem.h>
12 #include <secoid.h>
13
14 #include "base/logging.h"
15 #include "crypto/nss_util.h"
16 #include "crypto/sha2.h"
17 #include "net/cert/signed_certificate_timestamp.h"
18
19 namespace net {
20
21 namespace {
22
23 SECOidTag GetNssSigAlg(ct::DigitallySigned::SignatureAlgorithm alg) {
wtc 2013/11/08 21:04:00 Nit: Nss => NSS
Eran M. (Google) 2013/11/12 12:01:28 Done.
24 switch (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:
32 // FALLTHROUGH
wtc 2013/11/08 21:04:00 This FALLTHROUGH comment and the FALLTHROUGH comme
Eran M. (Google) 2013/11/12 12:01:28 Done.
33 default:
34 NOTREACHED();
35 return SEC_OID_UNKNOWN;
36 }
37 }
38
39 SECOidTag GetNssHashAlg(ct::DigitallySigned::HashAlgorithm alg) {
40 switch (alg) {
41 case ct::DigitallySigned::HASH_ALGO_MD5:
42 return SEC_OID_MD5;
43 case ct::DigitallySigned::HASH_ALGO_SHA1:
44 return SEC_OID_SHA1;
45 case ct::DigitallySigned::HASH_ALGO_SHA224:
46 return SEC_OID_SHA224;
47 case ct::DigitallySigned::HASH_ALGO_SHA256:
48 return SEC_OID_SHA256;
49 case ct::DigitallySigned::HASH_ALGO_SHA384:
50 return SEC_OID_SHA384;
51 case ct::DigitallySigned::HASH_ALGO_SHA512:
52 return SEC_OID_SHA512;
53 case ct::DigitallySigned::HASH_ALGO_NONE:
54 // FALLTHROUGH
55 default:
56 NOTREACHED();
57 return SEC_OID_UNKNOWN;
58 }
59 }
60
61 } // namespace
62
63 CTLogVerifier::~CTLogVerifier() {
64 if (public_key_)
65 SECKEY_DestroyPublicKey(public_key_);
66 }
67
68 CTLogVerifier::CTLogVerifier() : public_key_(NULL) {}
wtc 2013/11/08 21:04:00 hash_algorithm_ and sig_algorithm_ should also be
Eran M. (Google) 2013/11/12 12:01:28 Done.
69
70 bool CTLogVerifier::Init(const base::StringPiece& public_key,
71 const base::StringPiece& description) {
72 SECItem key_data;
73
74 crypto::EnsureNSSInit();
75
76 key_data.data = reinterpret_cast<unsigned char*>(
77 const_cast<char*>(public_key.data()));
78 key_data.len = public_key.size();
79
80 CERTSubjectPublicKeyInfo* public_key_info =
81 SECKEY_DecodeDERSubjectPublicKeyInfo(&key_data);
82 if (!public_key_info) {
83 DVLOG(1) << "Failed decoding public key: " << public_key;
wtc 2013/11/08 21:04:00 public_key contains binary data, so it is not suit
Eran M. (Google) 2013/11/12 12:01:28 Done - removed.
84 return false;
85 }
86
87 public_key_ = SECKEY_ExtractPublicKey(public_key_info);
88 SECKEY_DestroySubjectPublicKeyInfo(public_key_info);
89
90 if (!public_key_) {
91 DVLOG(1) << "Failed extracting public key from " << public_key;
92 return false;
93 }
94
95 key_id_ = crypto::SHA256HashString(public_key);
96 description_ = description.as_string();
97
98
99 // Right now, only RSASSA-PKCS1v15 with SHA-256 and ECDSA with SHA-256 are
100 // supported.
wtc 2013/11/08 21:04:00 Nit: copy this comment to the _openssl.cc file. N
Eran M. (Google) 2013/11/12 12:01:28 Done.
101 switch (public_key_->keyType) {
Ryan Sleevi 2013/11/08 00:30:43 Use SECKEY_GetPublicKeyType()
Eran M. (Google) 2013/11/12 12:01:28 Done.
102 case rsaKey:
103 hash_algorithm_ = ct::DigitallySigned::HASH_ALGO_SHA256;
104 sig_algorithm_ = ct::DigitallySigned::SIG_ALGO_RSA;
105 break;
106 case ecKey:
107 hash_algorithm_ = ct::DigitallySigned::HASH_ALGO_SHA256;
108 sig_algorithm_ = ct::DigitallySigned::SIG_ALGO_ECDSA;
109 break;
110 default:
111 DVLOG(1) << "Unsupported key type: " << public_key_->keyType;
112 return false;
113 }
114
115 // Extra sanity check: Require RSA signatures of at least 2048 bits.
wtc 2013/11/08 21:04:00 Nit: signatures => keys Nit: copy this comment to
Eran M. (Google) 2013/11/12 12:01:28 Done.
116 if (sig_algorithm_ == ct::DigitallySigned::SIG_ALGO_RSA &&
117 SECKEY_PublicKeyStrengthInBits(public_key_) < 2048) {
118 DVLOG(1) << "Too small a public key.";
119 return false;
120 }
121
122 return true;
123 }
124
125 bool CTLogVerifier::VerifySignature(const base::StringPiece& data_to_sign,
126 const base::StringPiece& signature) {
127 SECItem sig_data;
128 sig_data.data = reinterpret_cast<unsigned char*>(const_cast<char*>(
129 signature.data()));
130 sig_data.len = signature.size();
131
132 SECStatus rv = VFY_VerifyDataDirect(
133 reinterpret_cast<const unsigned char*>(data_to_sign.data()),
134 data_to_sign.size(), public_key_, &sig_data,
135 GetNssSigAlg(sig_algorithm_), GetNssHashAlg(hash_algorithm_),
136 NULL, NULL);
137 DVLOG(1) << "Signature verification result: " << (rv == SECSuccess);
138 return rv == SECSuccess;
139 }
140
141 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698