 Chromium Code Reviews
 Chromium Code Reviews Issue 17385010:
  OpenSSL/NSS implementation of ProofVerfifier.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 17385010:
  OpenSSL/NSS implementation of ProofVerfifier.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: net/quic/crypto/proof_verifier_chromium.cc | 
| diff --git a/net/quic/crypto/proof_verifier_chromium.cc b/net/quic/crypto/proof_verifier_chromium.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..d3c184f5e84c9aa83e58c38d70709063d3de614b | 
| --- /dev/null | 
| +++ b/net/quic/crypto/proof_verifier_chromium.cc | 
| @@ -0,0 +1,234 @@ | 
| +// Copyright 2013 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include "net/quic/crypto/proof_verifier_chromium.h" | 
| + | 
| +#include "base/bind.h" | 
| +#include "base/bind_helpers.h" | 
| +#include "base/callback_helpers.h" | 
| +#include "base/compiler_specific.h" | 
| +#include "base/logging.h" | 
| +#include "base/strings/stringprintf.h" | 
| +#include "crypto/signature_verifier.h" | 
| +#include "net/base/net_errors.h" | 
| +#include "net/base/net_log.h" | 
| +#include "net/cert/asn1_util.h" | 
| +#include "net/cert/cert_status_flags.h" | 
| +#include "net/cert/cert_verifier.h" | 
| +#include "net/cert/cert_verify_result.h" | 
| +#include "net/cert/single_request_cert_verifier.h" | 
| +#include "net/cert/x509_certificate.h" | 
| +#include "net/cert/x509_util.h" | 
| +#include "net/quic/crypto/crypto_protocol.h" | 
| +#include "net/ssl/ssl_config_service.h" | 
| + | 
| +using base::StringPiece; | 
| +using base::StringPrintf; | 
| +using std::string; | 
| +using std::vector; | 
| + | 
| +namespace net { | 
| + | 
| +ProofVerifierChromium::ProofVerifierChromium(CertVerifier* cert_verifier) | 
| + : cert_verifier_(cert_verifier), | 
| + 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.
 | 
| + next_state_(STATE_NONE) { | 
| +} | 
| + | 
| +ProofVerifierChromium::~ProofVerifierChromium() { | 
| + verifier_.reset(); | 
| + | 
| + // Reset object state. | 
| + callback_.Reset(); | 
| + cert_verify_result_.Reset(); | 
| +} | 
| + | 
| +int ProofVerifierChromium::VerifyProof(const string& hostname, | 
| + const string& server_config, | 
| + const vector<string>& certs, | 
| + const string& signature, | 
| + const CompletionCallback& callback) { | 
| + error_details_.clear(); | 
| + | 
| + if (certs.empty()) { | 
| + DLOG(WARNING) << "Certs are empty."; | 
| + return ERR_FAILED; | 
| + } | 
| + | 
| + // Convert certs to X509Certificate. | 
| + 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.
 | 
| + for (unsigned i = 0; i < certs.size(); i++) { | 
| + v[i] = base::StringPiece(certs[i]); | 
| + } | 
| + cert_ = X509Certificate::CreateFromDERCertChain(v); | 
| + if (!cert_.get()) { | 
| + cert_verify_result_.Reset(); | 
| + cert_verify_result_.cert_status = CERT_STATUS_INVALID; | 
| + 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.
 | 
| + } | 
| + | 
| + // We call VerifySignature first to avoid copying of server_config and | 
| + // signature. | 
| + if (!VerifySignature(server_config, signature, certs[0])) { | 
| + error_details_ = "Failed to verify signature of server config"; | 
| + DLOG(WARNING) << error_details_; | 
| + return ERR_FAILED; | 
| + } | 
| + | 
| + hostname_ = hostname; | 
| + callback_ = callback; | 
| + | 
| + return VerifyChain(); | 
| +} | 
| + | 
| +string ProofVerifierChromium::error_details() { | 
| + return error_details_; | 
| +} | 
| + | 
| +int ProofVerifierChromium::VerifyChain() { | 
| + next_state_ = STATE_VERIFY_CERT; | 
| + 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.
 | 
| +} | 
| + | 
| +int ProofVerifierChromium::DoLoop(int last_result) { | 
| + int rv = last_result; | 
| + do { | 
| + State state = next_state_; | 
| + next_state_ = STATE_NONE; | 
| + switch (state) { | 
| + case STATE_VERIFY_CERT: | 
| + DCHECK(rv == OK); | 
| + rv = DoVerifyCert(rv); | 
| + break; | 
| + case STATE_VERIFY_CERT_COMPLETE: | 
| + rv = DoVerifyCertComplete(rv); | 
| + break; | 
| + case STATE_NONE: | 
| + default: | 
| + rv = ERR_UNEXPECTED; | 
| + LOG(DFATAL) << "unexpected state " << state; | 
| + break; | 
| + } | 
| + } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 
| + return rv; | 
| +} | 
| + | 
| +void ProofVerifierChromium::OnIOComplete(int result) { | 
| + int rv = DoLoop(result); | 
| + if (rv != ERR_IO_PENDING) { | 
| + base::ResetAndReturn(&callback_).Run(rv); | 
| + } | 
| +} | 
| + | 
| +int ProofVerifierChromium::DoVerifyCert(int result) { | 
| + next_state_ = STATE_VERIFY_CERT_COMPLETE; | 
| + | 
| + int flags = CertVerifier::VERIFY_REV_CHECKING_ENABLED | | 
| + CertVerifier::VERIFY_CERT_IO_ENABLED; | 
| + verifier_.reset(new SingleRequestCertVerifier(cert_verifier_)); | 
| + return verifier_->Verify( | 
| + cert_.get(), | 
| + hostname_, | 
| + flags, | 
| + SSLConfigService::GetCRLSet().get(), | 
| + &cert_verify_result_, | 
| + base::Bind(&ProofVerifierChromium::OnIOComplete, | 
| + base::Unretained(this)), | 
| + net_log_); | 
| +} | 
| + | 
| +int ProofVerifierChromium::DoVerifyCertComplete(int result) { | 
| + verifier_.reset(); | 
| + | 
| + if (result <= ERR_FAILED) { | 
| + error_details_ = StringPrintf("Failed to verify certificate chain: %s", | 
| + ErrorToString(result)); | 
| + DLOG(WARNING) << error_details_; | 
| + result = ERR_FAILED; | 
| + } | 
| + | 
| + // 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.
 | 
| + DCHECK_EQ(STATE_NONE, next_state_); | 
| + return result; | 
| +} | 
| + | 
| +bool ProofVerifierChromium::VerifySignature(const string& signed_data, | 
| + const string& signature, | 
| + const string& cert) { | 
| + StringPiece spki; | 
| + if (!asn1::ExtractSPKIFromDERCert(cert, &spki)) { | 
| + DLOG(WARNING) << "ExtractSPKIFromDERCert failed"; | 
| + return false; | 
| + } | 
| + | 
| + crypto::SignatureVerifier verifier; | 
| + | 
| + size_t size_bits; | 
| + X509Certificate::PublicKeyType type; | 
| + X509Certificate::GetPublicKeyInfo(cert_->os_cert_handle(), &size_bits, | 
| + &type); | 
| + if (type == X509Certificate::kPublicKeyTypeRSA) { | 
| + crypto::SignatureVerifier::HashAlgorithm hash_alg = | 
| + crypto::SignatureVerifier::SHA256; | 
| + crypto::SignatureVerifier::HashAlgorithm mask_hash_alg = hash_alg; | 
| + unsigned int hash_len = 32; // 32 is the length of a SHA-256 hash. | 
| + // TODO(wtc): change this to hash_len when we can change the wire format. | 
| + unsigned int salt_len = signature.size() - hash_len - 2; | 
| + | 
| + bool ok = verifier.VerifyInitRSAPSS( | 
| + hash_alg, mask_hash_alg, salt_len, | 
| + reinterpret_cast<const uint8*>(signature.data()), signature.size(), | 
| + reinterpret_cast<const uint8*>(spki.data()), spki.size()); | 
| + if (!ok) { | 
| + DLOG(WARNING) << "VerifyInitRSAPSS failed"; | 
| + return false; | 
| + } | 
| + } else if (type == X509Certificate::kPublicKeyTypeECDSA) { | 
| + // This is the algorithm ID for ECDSA with SHA-256. Parameters are ABSENT. | 
| + // RFC 5758: | 
| + // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2) | 
| + // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 } | 
| + // ... | 
| + // When the ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with-SHA384, or | 
| + // ecdsa-with-SHA512 algorithm identifier appears in the algorithm field | 
| + // as an AlgorithmIdentifier, the encoding MUST omit the parameters | 
| + // field. That is, the AlgorithmIdentifier SHALL be a SEQUENCE of one | 
| + // component, the OID ecdsa-with-SHA224, ecdsa-with-SHA256, ecdsa-with- | 
| + // SHA384, or ecdsa-with-SHA512. | 
| + // See also RFC 5480, Appendix A. | 
| + static const uint8 kECDSAWithSHA256AlgorithmID[] = { | 
| + 0x30, 0x0a, | 
| + 0x06, 0x08, | 
| + 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, | 
| + }; | 
| + | 
| + if (!verifier.VerifyInit( | 
| + kECDSAWithSHA256AlgorithmID, sizeof(kECDSAWithSHA256AlgorithmID), | 
| + reinterpret_cast<const uint8*>(signature.data()), | 
| + signature.size(), | 
| + reinterpret_cast<const uint8*>(spki.data()), | 
| + spki.size())) { | 
| + DLOG(WARNING) << "VerifyInit failed"; | 
| + return false; | 
| + } | 
| + } else { | 
| + LOG(ERROR) << "Unsupported public key type " << type; | 
| + return false; | 
| + } | 
| + | 
| + verifier.VerifyUpdate(reinterpret_cast<const uint8*>(kProofSignatureLabel), | 
| + sizeof(kProofSignatureLabel)); | 
| + verifier.VerifyUpdate(reinterpret_cast<const uint8*>(signed_data.data()), | 
| + signed_data.size()); | 
| + | 
| + if (!verifier.VerifyFinal()) { | 
| + DLOG(WARNING) << "VerifyFinal failed"; | 
| + return false; | 
| + } | 
| + | 
| + 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.
 | 
| + return true; | 
| +} | 
| + | 
| +} // namespace net |