1 // Copyright 2019 The BoringSSL Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 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