Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "net/quic/crypto/proof_verifier_chromium.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/callback_helpers.h" | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "crypto/signature_verifier.h" | |
| 14 #include "net/base/net_errors.h" | |
| 15 #include "net/base/net_log.h" | |
| 16 #include "net/cert/asn1_util.h" | |
| 17 #include "net/cert/cert_status_flags.h" | |
| 18 #include "net/cert/cert_verifier.h" | |
| 19 #include "net/cert/cert_verify_result.h" | |
| 20 #include "net/cert/single_request_cert_verifier.h" | |
| 21 #include "net/cert/x509_certificate.h" | |
| 22 #include "net/cert/x509_util.h" | |
| 23 #include "net/quic/crypto/crypto_protocol.h" | |
| 24 #include "net/ssl/ssl_config_service.h" | |
| 25 | |
| 26 using base::StringPiece; | |
| 27 using base::StringPrintf; | |
| 28 using std::string; | |
| 29 using std::vector; | |
| 30 | |
| 31 namespace net { | |
| 32 | |
| 33 ProofVerifierChromium::ProofVerifierChromium(CertVerifier* cert_verifier) | |
| 34 : cert_verifier_(cert_verifier), | |
| 35 cert_(NULL), | |
|
wtc
2013/07/02 00:56:38
This initializer is not necessary because |cert_|
ramant (doing other things)
2013/07/02 14:19:50
Done.
| |
| 36 next_state_(STATE_NONE) { | |
| 37 } | |
| 38 | |
| 39 ProofVerifierChromium::~ProofVerifierChromium() { | |
| 40 verifier_.reset(); | |
| 41 | |
| 42 // Reset object state. | |
| 43 callback_.Reset(); | |
| 44 cert_verify_result_.Reset(); | |
| 45 } | |
| 46 | |
| 47 int ProofVerifierChromium::VerifyProof(const string& hostname, | |
| 48 const string& server_config, | |
| 49 const vector<string>& certs, | |
| 50 const string& signature, | |
| 51 const CompletionCallback& callback) { | |
| 52 error_details_.clear(); | |
| 53 | |
| 54 if (certs.empty()) { | |
| 55 DLOG(WARNING) << "Certs are empty."; | |
| 56 return ERR_FAILED; | |
| 57 } | |
| 58 | |
| 59 // Convert certs to X509Certificate. | |
| 60 vector<StringPiece> v(certs.size()); | |
|
wtc
2013/07/02 00:56:38
Nit: "v" seems too short. How about |pieces| or |c
ramant (doing other things)
2013/07/02 14:19:50
Done.
| |
| 61 for (unsigned i = 0; i < certs.size(); i++) { | |
| 62 v[i] = base::StringPiece(certs[i]); | |
| 63 } | |
| 64 cert_ = X509Certificate::CreateFromDERCertChain(v); | |
| 65 if (!cert_.get()) { | |
| 66 cert_verify_result_.Reset(); | |
| 67 cert_verify_result_.cert_status = CERT_STATUS_INVALID; | |
| 68 return ERR_CERT_INVALID; | |
|
wtc
2013/07/02 00:56:38
We need to return ERR_FAILED because ProofVerifier
ramant (doing other things)
2013/07/02 14:19:50
Done.
| |
| 69 } | |
| 70 | |
| 71 // We call VerifySignature first to avoid copying of server_config and | |
| 72 // signature. | |
| 73 if (!VerifySignature(server_config, signature, certs[0])) { | |
| 74 error_details_ = "Failed to verify signature of server config"; | |
| 75 DLOG(WARNING) << error_details_; | |
| 76 return ERR_FAILED; | |
| 77 } | |
| 78 | |
| 79 hostname_ = hostname; | |
| 80 callback_ = callback; | |
| 81 | |
| 82 return VerifyChain(); | |
| 83 } | |
| 84 | |
| 85 string ProofVerifierChromium::error_details() { | |
| 86 return error_details_; | |
| 87 } | |
| 88 | |
| 89 int ProofVerifierChromium::VerifyChain() { | |
| 90 next_state_ = STATE_VERIFY_CERT; | |
| 91 return DoLoop(OK); | |
|
wtc
2013/07/02 00:56:38
I suggest removing the VerifyChain() function and
ramant (doing other things)
2013/07/02 14:19:50
Done.
| |
| 92 } | |
| 93 | |
| 94 int ProofVerifierChromium::DoLoop(int last_result) { | |
| 95 int rv = last_result; | |
| 96 do { | |
| 97 State state = next_state_; | |
| 98 next_state_ = STATE_NONE; | |
| 99 switch (state) { | |
| 100 case STATE_VERIFY_CERT: | |
| 101 DCHECK(rv == OK); | |
| 102 rv = DoVerifyCert(rv); | |
| 103 break; | |
| 104 case STATE_VERIFY_CERT_COMPLETE: | |
| 105 rv = DoVerifyCertComplete(rv); | |
| 106 break; | |
| 107 case STATE_NONE: | |
| 108 default: | |
| 109 rv = ERR_UNEXPECTED; | |
| 110 LOG(DFATAL) << "unexpected state " << state; | |
| 111 break; | |
| 112 } | |
| 113 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | |
| 114 return rv; | |
| 115 } | |
| 116 | |
| 117 void ProofVerifierChromium::OnIOComplete(int result) { | |
| 118 int rv = DoLoop(result); | |
| 119 if (rv != ERR_IO_PENDING) { | |
| 120 base::ResetAndReturn(&callback_).Run(rv); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 int ProofVerifierChromium::DoVerifyCert(int result) { | |
| 125 next_state_ = STATE_VERIFY_CERT_COMPLETE; | |
| 126 | |
| 127 int flags = CertVerifier::VERIFY_REV_CHECKING_ENABLED | | |
| 128 CertVerifier::VERIFY_CERT_IO_ENABLED; | |
| 129 verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); | |
| 130 return verifier_->Verify( | |
| 131 cert_.get(), | |
| 132 hostname_, | |
| 133 flags, | |
| 134 SSLConfigService::GetCRLSet().get(), | |
| 135 &cert_verify_result_, | |
| 136 base::Bind(&ProofVerifierChromium::OnIOComplete, | |
| 137 base::Unretained(this)), | |
| 138 net_log_); | |
| 139 } | |
| 140 | |
| 141 int ProofVerifierChromium::DoVerifyCertComplete(int result) { | |
| 142 verifier_.reset(); | |
| 143 | |
| 144 if (result <= ERR_FAILED) { | |
| 145 error_details_ = StringPrintf("Failed to verify certificate chain: %s", | |
| 146 ErrorToString(result)); | |
| 147 DLOG(WARNING) << error_details_; | |
| 148 result = ERR_FAILED; | |
| 149 } | |
| 150 | |
| 151 // Exit DoLoop and return the result to the caller to ProofVerify. | |
|
wtc
2013/07/02 00:56:38
Typo: ProofVerify => VerifyProof
ramant (doing other things)
2013/07/02 14:19:50
Done.
| |
| 152 DCHECK_EQ(STATE_NONE, next_state_); | |
| 153 return result; | |
| 154 } | |
| 155 | |
| 156 bool ProofVerifierChromium::VerifySignature(const string& signed_data, | |
| 157 const string& signature, | |
| 158 const string& cert) { | |
| 159 StringPiece spki; | |
| 160 if (!asn1::ExtractSPKIFromDERCert(cert, &spki)) { | |
| 161 DLOG(WARNING) << "ExtractSPKIFromDERCert failed"; | |
| 162 return false; | |
| 163 } | |
| 164 | |
| 165 crypto::SignatureVerifier verifier; | |
| 166 | |
| 167 size_t size_bits; | |
| 168 X509Certificate::PublicKeyType type; | |
| 169 X509Certificate::GetPublicKeyInfo(cert_->os_cert_handle(), &size_bits, | |
| 170 &type); | |
| 171 if (type == X509Certificate::kPublicKeyTypeRSA) { | |
| 172 crypto::SignatureVerifier::HashAlgorithm hash_alg = | |
| 173 crypto::SignatureVerifier::SHA256; | |
| 174 crypto::SignatureVerifier::HashAlgorithm mask_hash_alg = hash_alg; | |
| 175 unsigned int hash_len = 32; // 32 is the length of a SHA-256 hash. | |
| 176 // TODO(wtc): change this to hash_len when we can change the wire format. | |
| 177 unsigned int salt_len = signature.size() - hash_len - 2; | |
| 178 | |
| 179 bool ok = verifier.VerifyInitRSAPSS( | |
| 180 hash_alg, mask_hash_alg, salt_len, | |
| 181 reinterpret_cast<const uint8*>(signature.data()), signature.size(), | |
| 182 reinterpret_cast<const uint8*>(spki.data()), spki.size()); | |
| 183 if (!ok) { | |
| 184 DLOG(WARNING) << "VerifyInitRSAPSS failed"; | |
| 185 return false; | |
| 186 } | |
| 187 } else if (type == X509Certificate::kPublicKeyTypeECDSA) { | |
| 188 // This is the algorithm ID for ECDSA with SHA-256. Parameters are ABSENT. | |
| 189 // RFC 5758: | |
| 190 // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2) | |
| 191 // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 } | |
| 192 // ... | |
| 193 // When the ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384, or | |
| 194 // ecdsa-with-SHA512 algorithm identifier appears in the algorithm field | |
| 195 // as an AlgorithmIdentifier, the encoding MUST omit the parameters | |
| 196 // field. That is, the AlgorithmIdentifier SHALL be a SEQUENCE of one | |
| 197 // component, the OID ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with- | |
| 198 // SHA384, or ecdsa-with-SHA512. | |
| 199 // See also RFC 5480, Appendix A. | |
| 200 static const uint8 kECDSAWithSHA256AlgorithmID[] = { | |
| 201 0x30, 0x0a, | |
| 202 0x06, 0x08, | |
| 203 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, | |
| 204 }; | |
| 205 | |
| 206 if (!verifier.VerifyInit( | |
| 207 kECDSAWithSHA256AlgorithmID, sizeof(kECDSAWithSHA256AlgorithmID), | |
| 208 reinterpret_cast<const uint8*>(signature.data()), | |
| 209 signature.size(), | |
| 210 reinterpret_cast<const uint8*>(spki.data()), | |
| 211 spki.size())) { | |
| 212 DLOG(WARNING) << "VerifyInit failed"; | |
| 213 return false; | |
| 214 } | |
| 215 } else { | |
| 216 LOG(ERROR) << "Unsupported public key type " << type; | |
| 217 return false; | |
| 218 } | |
| 219 | |
| 220 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(kProofSignatureLabel), | |
| 221 sizeof(kProofSignatureLabel)); | |
| 222 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(signed_data.data()), | |
| 223 signed_data.size()); | |
| 224 | |
| 225 if (!verifier.VerifyFinal()) { | |
| 226 DLOG(WARNING) << "VerifyFinal failed"; | |
| 227 return false; | |
| 228 } | |
| 229 | |
| 230 DLOG(WARNING) << "VerifyFinal success"; | |
|
wtc
2013/07/02 00:56:38
This should be DLOG(INFO), or should be removed.
ramant (doing other things)
2013/07/02 14:19:50
Done.
| |
| 231 return true; | |
| 232 } | |
| 233 | |
| 234 } // namespace net | |
| OLD | NEW |