1 /* Copyright (c) 2019, 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_MOCK_QUIC_TRANSPORT
16 #define HEADER_MOCK_QUIC_TRANSPORT
17 
18 #include <openssl/base.h>
19 #include <openssl/bio.h>
20 #include <openssl/ssl.h>
21 
22 #include <vector>
23 
24 class MockQuicTransport {
25  public:
26   explicit MockQuicTransport(bssl::UniquePtr<BIO> bio, SSL *ssl);
27 
28   bool SetReadSecret(enum ssl_encryption_level_t level,
29                      const SSL_CIPHER *cipher, const uint8_t *secret,
30                      size_t secret_len);
31   bool SetWriteSecret(enum ssl_encryption_level_t level,
32                       const SSL_CIPHER *cipher, const uint8_t *secret,
33                       size_t secret_len);
34 
35   bool ReadHandshake();
36   bool WriteHandshakeData(enum ssl_encryption_level_t level,
37                           const uint8_t *data, size_t len);
38   // Returns the number of bytes read.
39   int ReadApplicationData(uint8_t *out, size_t max_out);
40   bool WriteApplicationData(const uint8_t *in, size_t len);
41   bool Flush();
42   bool SendAlert(enum ssl_encryption_level_t level, uint8_t alert);
43 
44  private:
45   // Reads a record header from |bio_| and returns whether the record was read
46   // successfully. As part of reading the header, this function checks that the
47   // cipher suite and secret in the header are correct. On success, the TLS
48   // record type is put in |*out_type|, the encryption level is put in
49   // |*out_level|, the length of the TLS record is put in |*out_len|, and the
50   // next thing to be read from |bio_| is |*out_len| bytes of the TLS record.
51   bool ReadHeader(uint8_t *out_type, enum ssl_encryption_level_t *out_level,
52                   size_t *out_len);
53 
54   // Writes a MockQuicTransport record to |bio_| at encryption level |level|
55   // with record type |type| and a TLS record payload of length |len| from
56   // |data|.
57   bool WriteRecord(enum ssl_encryption_level_t level, uint8_t type,
58                    const uint8_t *data, size_t len);
59 
60   bssl::UniquePtr<BIO> bio_;
61 
62   std::vector<uint8_t> pending_app_data_;
63   size_t app_data_offset_;
64 
65   struct EncryptionLevel {
66     uint16_t cipher;
67     std::vector<uint8_t> secret;
68   };
69 
70   std::vector<EncryptionLevel> read_levels_;
71   std::vector<EncryptionLevel> write_levels_;
72 
73   SSL *ssl_;  // Unowned.
74 };
75 
76 
77 #endif  // HEADER_MOCK_QUIC_TRANSPORT
78