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