/src/curl_fuzzer/proto_fuzzer/tls_session_archive.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al. |
3 | | * |
4 | | * SPDX-License-Identifier: curl |
5 | | */ |
6 | | |
7 | | /// @file |
8 | | /// @brief Bounded ownership for public TLS session export and import. |
9 | | |
10 | | #include "proto_fuzzer/tls_session_archive.h" |
11 | | |
12 | | #include <cstring> |
13 | | |
14 | | namespace proto_fuzzer { |
15 | | namespace { |
16 | | |
17 | | constexpr std::size_t kMaxSessionKeySize = 4 * 1024; |
18 | | constexpr std::size_t kMaxSessionHashSize = 128; |
19 | | constexpr std::size_t kMaxSessionDataSize = 16 * 1024; |
20 | | constexpr std::size_t kMaxAlpnSize = 255; |
21 | | |
22 | | } // namespace |
23 | | |
24 | | /// Copy the first bounded session offered by curl. Later callback entries are |
25 | | /// ignored so one fuzz input cannot retain work in proportion to cache size. |
26 | | CURLcode TlsSessionArchive::Capture(CURL* /*easy*/, void* user_data, const char* session_key, |
27 | | const unsigned char* salted_hash, std::size_t salted_hash_size, |
28 | | const unsigned char* session_data, std::size_t session_data_size, |
29 | | curl_off_t valid_until, int ietf_tls_id, const char* alpn, |
30 | 123k | std::size_t early_data_max) { |
31 | 123k | auto* archive = static_cast<TlsSessionArchive*>(user_data); |
32 | 123k | if (archive == nullptr || !archive->empty() || salted_hash == nullptr || salted_hash_size == 0 || |
33 | 77.7k | salted_hash_size > kMaxSessionHashSize || session_data == nullptr || session_data_size == 0 || |
34 | 77.7k | session_data_size > kMaxSessionDataSize) { |
35 | 45.3k | return CURLE_OK; |
36 | 45.3k | } |
37 | | |
38 | 77.7k | const std::size_t session_key_size = session_key == nullptr ? 0 : strnlen(session_key, kMaxSessionKeySize + 1); |
39 | 77.7k | const std::size_t alpn_size = alpn == nullptr ? 0 : strnlen(alpn, kMaxAlpnSize + 1); |
40 | 77.7k | if (session_key_size <= kMaxSessionKeySize && session_key != nullptr) { |
41 | 77.7k | archive->session_key_.assign(session_key, session_key_size); |
42 | 77.7k | } |
43 | | |
44 | 77.7k | archive->salted_hash_.assign(salted_hash, salted_hash + salted_hash_size); |
45 | 77.7k | archive->session_data_.assign(session_data, session_data + session_data_size); |
46 | 77.7k | archive->valid_until_ = valid_until; |
47 | 77.7k | archive->ietf_tls_id_ = ietf_tls_id; |
48 | 77.7k | if (alpn_size <= kMaxAlpnSize && alpn != nullptr) { |
49 | 67.0k | archive->alpn_.assign(alpn, alpn_size); |
50 | 67.0k | } |
51 | 77.7k | archive->early_data_max_ = early_data_max; |
52 | 77.7k | return CURLE_OK; |
53 | 123k | } |
54 | | |
55 | | /// Copy the first bounded session from an easy handle cache. |
56 | | /// @param easy Easy handle whose cache curl exports. |
57 | | /// @return The public export result. |
58 | 123k | CURLcode TlsSessionArchive::CaptureFrom(CURL* easy) { |
59 | 123k | return easy == nullptr ? CURLE_BAD_FUNCTION_ARGUMENT : curl_easy_ssls_export(easy, &TlsSessionArchive::Capture, this); |
60 | 123k | } |
61 | | |
62 | | /// Import the session without its clear peer key. |
63 | | /// @param easy Easy handle whose cache receives the session. |
64 | | /// @return The public import result. |
65 | 66.7k | CURLcode TlsSessionArchive::ImportBySaltedHash(CURL* easy) const { |
66 | 66.7k | if (easy == nullptr || empty() || salted_hash_.empty()) { |
67 | 2.70k | return CURLE_BAD_FUNCTION_ARGUMENT; |
68 | 2.70k | } |
69 | 64.0k | return curl_easy_ssls_import(easy, nullptr, salted_hash_.data(), salted_hash_.size(), session_data_.data(), |
70 | 64.0k | session_data_.size()); |
71 | 66.7k | } |
72 | | |
73 | | /// Import the session through the clear peer key returned by curl. |
74 | | /// @param easy Easy handle whose cache receives the session. |
75 | | /// @return The public import result. |
76 | 9.18k | CURLcode TlsSessionArchive::ImportBySessionKey(CURL* easy) const { |
77 | 9.18k | if (easy == nullptr || empty() || session_key_.empty()) { |
78 | 3.01k | return CURLE_BAD_FUNCTION_ARGUMENT; |
79 | 3.01k | } |
80 | 6.16k | return curl_easy_ssls_import(easy, session_key_.c_str(), nullptr, 0, session_data_.data(), session_data_.size()); |
81 | 9.18k | } |
82 | | |
83 | | /// Truncate the salted hash by one byte to cover deterministic rejection. |
84 | | /// @param easy Easy handle whose cache receives the malformed entry. |
85 | | /// @return The public import result. |
86 | 8.73k | CURLcode TlsSessionArchive::ImportMalformedSaltedHash(CURL* easy) const { |
87 | 8.73k | if (easy == nullptr || empty() || salted_hash_.empty()) { |
88 | 2.43k | return CURLE_BAD_FUNCTION_ARGUMENT; |
89 | 2.43k | } |
90 | 6.29k | return curl_easy_ssls_import(easy, nullptr, salted_hash_.data(), salted_hash_.size() - 1, session_data_.data(), |
91 | 6.29k | session_data_.size()); |
92 | 8.73k | } |
93 | | |
94 | | /// @return true when no session data was captured. |
95 | 331k | bool TlsSessionArchive::empty() const { return session_data_.empty(); } |
96 | | /// @return the copied clear peer key, or an empty string. |
97 | 0 | const std::string& TlsSessionArchive::session_key() const { return session_key_; } |
98 | | /// @return the copied salted peer hash. |
99 | 0 | const std::vector<unsigned char>& TlsSessionArchive::salted_hash() const { return salted_hash_; } |
100 | | /// @return the copied serialized TLS session. |
101 | 0 | const std::vector<unsigned char>& TlsSessionArchive::session_data() const { return session_data_; } |
102 | | /// @return the exported session expiry time. |
103 | 0 | curl_off_t TlsSessionArchive::valid_until() const { return valid_until_; } |
104 | | /// @return the exported IETF TLS protocol identifier. |
105 | 0 | int TlsSessionArchive::ietf_tls_id() const { return ietf_tls_id_; } |
106 | | /// @return the copied ALPN protocol name, or an empty string. |
107 | 0 | const std::string& TlsSessionArchive::alpn() const { return alpn_; } |
108 | | /// @return the exported maximum early-data size. |
109 | 0 | std::size_t TlsSessionArchive::early_data_max() const { return early_data_max_; } |
110 | | |
111 | | } // namespace proto_fuzzer |