1 /* Copyright (c) 2018, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #ifndef HEADER_TEST_STATE
16 #define HEADER_TEST_STATE
17 
18 #include <openssl/base.h>
19 
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "mock_quic_transport.h"
26 
27 struct TestState {
28   // Serialize writes |pending_session| and |msg_callback_text| to |out|, for
29   // use in split-handshake tests.  We don't try to serialize every bit of test
30   // state, but serializing |pending_session| is necessary to exercise session
31   // resumption, and |msg_callback_text| is especially useful.  In the general
32   // case, checks of state updated during the handshake can be skipped when
33   // |config->handoff|.
34   bool Serialize(CBB *out) const;
35 
36   // Deserialize returns a new |TestState| from data written by |Serialize|.
37   static std::unique_ptr<TestState> Deserialize(CBS *cbs, SSL_CTX *ctx);
38 
39   // async_bio is async BIO which pauses reads and writes.
40   BIO *async_bio = nullptr;
41   // packeted_bio is the packeted BIO which simulates read timeouts.
42   BIO *packeted_bio = nullptr;
43   std::unique_ptr<MockQuicTransport> quic_transport;
44   bool cert_ready = false;
45   bssl::UniquePtr<SSL_SESSION> session;
46   bssl::UniquePtr<SSL_SESSION> pending_session;
47   bool early_callback_called = false;
48   bool handshake_done = false;
49   // private_key is the underlying private key used when testing custom keys.
50   bssl::UniquePtr<EVP_PKEY> private_key;
51   // When private key methods are used, whether the private key was used.
52   bool used_private_key = false;
53   std::vector<uint8_t> private_key_result;
54   // private_key_retries is the number of times an asynchronous private key
55   // operation has been retried.
56   unsigned private_key_retries = 0;
57   bool got_new_session = false;
58   bssl::UniquePtr<SSL_SESSION> new_session;
59   bool ticket_decrypt_done = false;
60   bool alpn_select_done = false;
61   bool early_callback_ready = false;
62   bool custom_verify_ready = false;
63   std::string msg_callback_text;
64   bool msg_callback_ok = true;
65   // cert_verified is true if certificate verification has been driven to
66   // completion. This tests that the callback is not called again after this.
67   bool cert_verified = false;
68   int explicit_renegotiates = 0;
69   std::function<bool(const SSL_CLIENT_HELLO*)> get_handshake_hints_cb;
70   int last_message_received = -1;
71 };
72 
73 bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state);
74 
75 TestState *GetTestState(const SSL *ssl);
76 
77 struct timeval *GetClock();
78 
79 void AdvanceClock(unsigned seconds);
80 
81 void CopySessions(SSL_CTX *dest, const SSL_CTX *src);
82 
83 // SerializeContextState writes session material (sessions and ticket keys) from
84 // |ctx| into |cbb|.
85 bool SerializeContextState(SSL_CTX *ctx, CBB *cbb);
86 
87 // DeserializeContextState updates |out| with material previously serialized by
88 // SerializeContextState.
89 bool DeserializeContextState(CBS *in, SSL_CTX *out);
90 
91 #endif  // HEADER_TEST_STATE
92