Home | History | Annotate | Download | only in quic
      1 // Copyright (c) 2012 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 #ifndef NET_QUIC_QUIC_CRYPTO_CLIENT_STREAM_H_
      6 #define NET_QUIC_QUIC_CRYPTO_CLIENT_STREAM_H_
      7 
      8 #include <string>
      9 
     10 #include "net/quic/crypto/proof_verifier.h"
     11 #include "net/quic/crypto/quic_crypto_client_config.h"
     12 #include "net/quic/quic_config.h"
     13 #include "net/quic/quic_crypto_stream.h"
     14 #include "net/quic/quic_server_id.h"
     15 
     16 namespace net {
     17 
     18 class QuicClientSessionBase;
     19 
     20 namespace test {
     21 class CryptoTestUtils;
     22 }  // namespace test
     23 
     24 class NET_EXPORT_PRIVATE QuicCryptoClientStream : public QuicCryptoStream {
     25  public:
     26   QuicCryptoClientStream(const QuicServerId& server_id,
     27                          QuicClientSessionBase* session,
     28                          ProofVerifyContext* verify_context,
     29                          QuicCryptoClientConfig* crypto_config);
     30   virtual ~QuicCryptoClientStream();
     31 
     32   // CryptoFramerVisitorInterface implementation
     33   virtual void OnHandshakeMessage(
     34       const CryptoHandshakeMessage& message) OVERRIDE;
     35 
     36   // Performs a crypto handshake with the server. Returns true if the crypto
     37   // handshake is started successfully.
     38   // TODO(agl): this should probably return void.
     39   virtual bool CryptoConnect();
     40 
     41   // num_sent_client_hellos returns the number of client hello messages that
     42   // have been sent. If the handshake has completed then this is one greater
     43   // than the number of round-trips needed for the handshake.
     44   int num_sent_client_hellos() const;
     45 
     46  private:
     47   // ProofVerifierCallbackImpl is passed as the callback method to VerifyProof.
     48   // The ProofVerifier calls this class with the result of proof verification
     49   // when verification is performed asynchronously.
     50   class ProofVerifierCallbackImpl : public ProofVerifierCallback {
     51    public:
     52     explicit ProofVerifierCallbackImpl(QuicCryptoClientStream* stream);
     53     virtual ~ProofVerifierCallbackImpl();
     54 
     55     // ProofVerifierCallback interface.
     56     virtual void Run(bool ok,
     57                      const string& error_details,
     58                      scoped_ptr<ProofVerifyDetails>* details) OVERRIDE;
     59 
     60     // Cancel causes any future callbacks to be ignored. It must be called on
     61     // the same thread as the callback will be made on.
     62     void Cancel();
     63 
     64    private:
     65     QuicCryptoClientStream* stream_;
     66   };
     67 
     68   friend class test::CryptoTestUtils;
     69   friend class ProofVerifierCallbackImpl;
     70 
     71   enum State {
     72     STATE_IDLE,
     73     STATE_INITIALIZE,
     74     STATE_SEND_CHLO,
     75     STATE_RECV_REJ,
     76     STATE_VERIFY_PROOF,
     77     STATE_VERIFY_PROOF_COMPLETE,
     78     STATE_RECV_SHLO,
     79   };
     80 
     81   // DoHandshakeLoop performs a step of the handshake state machine. Note that
     82   // |in| may be NULL if the call did not result from a received message.
     83   void DoHandshakeLoop(const CryptoHandshakeMessage* in);
     84 
     85   // Called to set the proof of |cached| valid.  Also invokes the session's
     86   // OnProofValid() method.
     87   void SetCachedProofValid(QuicCryptoClientConfig::CachedState* cached);
     88 
     89   QuicClientSessionBase* client_session();
     90 
     91   State next_state_;
     92   // num_client_hellos_ contains the number of client hello messages that this
     93   // connection has sent.
     94   int num_client_hellos_;
     95 
     96   QuicCryptoClientConfig* const crypto_config_;
     97 
     98   // Client's connection nonce (4-byte timestamp + 28 random bytes)
     99   std::string nonce_;
    100   // Server's (hostname, port, is_https, privacy_mode) tuple.
    101   const QuicServerId server_id_;
    102 
    103   // Generation counter from QuicCryptoClientConfig's CachedState.
    104   uint64 generation_counter_;
    105 
    106   // proof_verify_callback_ contains the callback object that we passed to an
    107   // asynchronous proof verification. The ProofVerifier owns this object.
    108   ProofVerifierCallbackImpl* proof_verify_callback_;
    109 
    110   // These members are used to store the result of an asynchronous proof
    111   // verification. These members must not be used after
    112   // STATE_VERIFY_PROOF_COMPLETE.
    113   bool verify_ok_;
    114   string verify_error_details_;
    115   scoped_ptr<ProofVerifyDetails> verify_details_;
    116   scoped_ptr<ProofVerifyContext> verify_context_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(QuicCryptoClientStream);
    119 };
    120 
    121 }  // namespace net
    122 
    123 #endif  // NET_QUIC_QUIC_CRYPTO_CLIENT_STREAM_H_
    124