| 1 |
// Copyright (c) 2018, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "settings_writer.h" |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
#include <openssl/ssl.h> |
| 9 |
|
| 10 |
#include "fuzzer_tags.h" |
| 11 |
#include "test_config.h" |
| 12 |
|
| 13 |
|
| 14 |
SettingsWriter::SettingsWriter() {} |
| 15 |
|
| 16 |
bool SettingsWriter::Init(int i, const TestConfig *config, |
| 17 |
SSL_SESSION *session) { |
| 18 |
if (config->write_settings.empty()) { |
| 19 |
return true; |
| 20 |
} |
| 21 |
// Treat write_settings as a path prefix for each connection in the run. |
| 22 |
char buf[DECIMAL_SIZE(int)]; |
| 23 |
snprintf(buf, sizeof(buf), "%d", i); |
| 24 |
path_ = config->write_settings + buf; |
| 25 |
|
| 26 |
if (!CBB_init(cbb_.get(), 64)) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
if (session != nullptr) { |
| 31 |
uint8_t *data; |
| 32 |
size_t len; |
| 33 |
if (!SSL_SESSION_to_bytes(session, &data, &len)) { |
| 34 |
return false; |
| 35 |
} |
| 36 |
bssl::UniquePtr<uint8_t> free_data(data); |
| 37 |
CBB child; |
| 38 |
if (!CBB_add_u16(cbb_.get(), kSessionTag) || |
| 39 |
!CBB_add_u24_length_prefixed(cbb_.get(), &child) || |
| 40 |
!CBB_add_bytes(&child, data, len) || !CBB_flush(cbb_.get())) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if (config->is_server && |
| 46 |
(config->require_any_client_certificate || config->verify_peer) && |
| 47 |
!CBB_add_u16(cbb_.get(), kRequestClientCert)) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
bool SettingsWriter::Commit() { |
| 55 |
if (path_.empty()) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
uint8_t *settings; |
| 60 |
size_t settings_len; |
| 61 |
if (!CBB_add_u16(cbb_.get(), kDataTag) || |
| 62 |
!CBB_finish(cbb_.get(), &settings, &settings_len)) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
bssl::UniquePtr<uint8_t> free_settings(settings); |
| 66 |
|
| 67 |
struct FileCloser { |
| 68 |
void operator()(FILE *f) const { fclose(f); } |
| 69 |
}; |
| 70 |
using ScopedFILE = std::unique_ptr<FILE, FileCloser>; |
| 71 |
ScopedFILE file(fopen(path_.c_str(), "w")); |
| 72 |
if (!file) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
return fwrite(settings, settings_len, 1, file.get()) == 1; |
| 77 |
} |
| 78 |
|
| 79 |
bool SettingsWriter::WriteHandoff(bssl::Span<const uint8_t> handoff) { |
| 80 |
return WriteData(kHandoffTag, handoff); |
| 81 |
} |
| 82 |
|
| 83 |
bool SettingsWriter::WriteHandback(bssl::Span<const uint8_t> handback) { |
| 84 |
return WriteData(kHandbackTag, handback); |
| 85 |
} |
| 86 |
|
| 87 |
bool SettingsWriter::WriteHints(bssl::Span<const uint8_t> hints) { |
| 88 |
return WriteData(kHintsTag, hints); |
| 89 |
} |
| 90 |
|
| 91 |
bool SettingsWriter::WriteData(uint16_t tag, bssl::Span<const uint8_t> data) { |
| 92 |
if (path_.empty()) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
CBB child; |
| 97 |
if (!CBB_add_u16(cbb_.get(), tag) || |
| 98 |
!CBB_add_u24_length_prefixed(cbb_.get(), &child) || |
| 99 |
!CBB_add_bytes(&child, data.data(), data.size()) || |
| 100 |
!CBB_flush(cbb_.get())) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
return true; |
| 104 |
} |