Skip to content

Commit 8cc9837

Browse files
authored
Merge pull request #5700 from Rohde-Schwarz/chore/span_in_tls_msgs_and_exts
[std::span] For TLS message and extension parsing and usage
2 parents ed7e002 + d1c163a commit 8cc9837

31 files changed

Lines changed: 113 additions & 110 deletions

src/lib/tls/msg_cert_status.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Botan::TLS {
1313

14-
Certificate_Status::Certificate_Status(const std::vector<uint8_t>& buf, const Connection_Side /*side*/) {
14+
Certificate_Status::Certificate_Status(std::span<const uint8_t> buf, const Connection_Side /*side*/) {
1515
if(buf.size() < 5) {
1616
throw Decoding_Error("Invalid Certificate_Status message: too small");
1717
}

src/lib/tls/msg_cert_verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Botan::TLS {
1717
/*
1818
* Deserialize a Certificate Verify message
1919
*/
20-
Certificate_Verify::Certificate_Verify(const std::vector<uint8_t>& buf) {
20+
Certificate_Verify::Certificate_Verify(std::span<const uint8_t> buf) {
2121
TLS_Data_Reader reader("CertificateVerify", buf);
2222

2323
m_scheme = Signature_Scheme(reader.get_uint16_t());

src/lib/tls/msg_client_hello.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ std::vector<uint8_t> make_hello_random(RandomNumberGenerator& rng, Callbacks& cb
4343
return buf;
4444
}
4545

46-
Client_Hello_Internal::Client_Hello_Internal(const std::vector<uint8_t>& buf) {
46+
Client_Hello_Internal::Client_Hello_Internal(std::span<const uint8_t> buf) {
4747
/*
4848
Minimum possible client hello
4949
@@ -287,7 +287,7 @@ const std::vector<uint8_t>& Client_Hello::cookie() const {
287287
Client_Hello_12_Shim::Client_Hello_12_Shim(std::unique_ptr<Client_Hello_Internal> data) :
288288
Client_Hello(std::move(data)) {}
289289

290-
Client_Hello_12_Shim::Client_Hello_12_Shim(const std::vector<uint8_t>& buf) :
290+
Client_Hello_12_Shim::Client_Hello_12_Shim(std::span<const uint8_t> buf) :
291291
Client_Hello_12_Shim(std::make_unique<Client_Hello_Internal>(buf)) {}
292292

293293
} // namespace Botan::TLS

src/lib/tls/msg_server_hello.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ std::vector<uint8_t> make_server_hello_random(RandomNumberGenerator& rng,
4545
return random;
4646
}
4747

48-
Server_Hello_Internal::Server_Hello_Internal(const std::vector<uint8_t>& buf) {
48+
Server_Hello_Internal::Server_Hello_Internal(std::span<const uint8_t> buf) {
4949
if(buf.size() < 38) {
5050
throw Decoding_Error("Server_Hello: Packet corrupted");
5151
}
@@ -153,7 +153,7 @@ const Extensions& Server_Hello::extensions() const {
153153
return m_data->extensions();
154154
}
155155

156-
Server_Hello_12_Shim::Server_Hello_12_Shim(const std::vector<uint8_t>& buf) :
156+
Server_Hello_12_Shim::Server_Hello_12_Shim(std::span<const uint8_t> buf) :
157157
Server_Hello_12_Shim(std::make_unique<Server_Hello_Internal>(buf)) {}
158158

159159
Server_Hello_12_Shim::Server_Hello_12_Shim(std::unique_ptr<Server_Hello_Internal> data) :

src/lib/tls/tls12/msg_certificate_12.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ Certificate_12::~Certificate_12() = default;
2424
/**
2525
* Create a new Certificate message
2626
*/
27-
Certificate_12::Certificate_12(Handshake_IO& io, Handshake_Hash& hash, const std::vector<X509_Certificate>& cert_list) :
28-
m_certs(cert_list) {
27+
Certificate_12::Certificate_12(Handshake_IO& io, Handshake_Hash& hash, std::vector<X509_Certificate> cert_list) :
28+
m_certs(std::move(cert_list)) {
2929
hash.update(io.send(*this));
3030
}
3131

3232
/**
3333
* Deserialize a Certificate message
3434
*/
35-
Certificate_12::Certificate_12(const std::vector<uint8_t>& buf, const Policy& policy) {
35+
Certificate_12::Certificate_12(std::span<const uint8_t> buf, const Policy& policy) {
3636
if(buf.size() < 3) {
3737
throw Decoding_Error("Certificate: Message malformed");
3838
}

src/lib/tls/tls12/msg_certificate_req_12.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ uint8_t cert_type_name_to_code(std::string_view name) {
5959
Certificate_Request_12::Certificate_Request_12(Handshake_IO& io,
6060
Handshake_Hash& hash,
6161
const Policy& policy,
62-
const std::vector<X509_DN>& ca_certs) :
63-
m_names(ca_certs), m_cert_key_types({"RSA", "ECDSA"}) {
62+
std::vector<X509_DN> ca_certs) :
63+
m_names(std::move(ca_certs)), m_cert_key_types({"RSA", "ECDSA"}) {
6464
m_schemes = policy.acceptable_signature_schemes();
6565
// RFC 5246 7.4.4: supported_signature_algorithms<2..2^16-2>
6666
if(m_schemes.empty()) {
@@ -72,7 +72,7 @@ Certificate_Request_12::Certificate_Request_12(Handshake_IO& io,
7272
/**
7373
* Deserialize a Certificate Request message
7474
*/
75-
Certificate_Request_12::Certificate_Request_12(const std::vector<uint8_t>& buf) {
75+
Certificate_Request_12::Certificate_Request_12(std::span<const uint8_t> buf) {
7676
if(buf.size() < 4) {
7777
throw Decoding_Error("Certificate_Req: Bad certificate request");
7878
}

src/lib/tls/tls12/msg_client_hello_12.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ Client_Hello_12::Client_Hello_12(Handshake_IO& io,
113113
const Policy& policy,
114114
Callbacks& cb,
115115
RandomNumberGenerator& rng,
116-
const std::vector<uint8_t>& reneg_info,
116+
std::vector<uint8_t> reneg_info,
117117
const Client_Hello_12::Settings& client_settings,
118-
const std::vector<std::string>& next_protocols) {
118+
std::vector<std::string> next_protocols) {
119119
m_data->m_legacy_version = client_settings.protocol_version();
120120
m_data->m_random = make_hello_random(rng, cb, policy);
121121
m_data->m_suites = policy.ciphersuite_list(client_settings.protocol_version());
@@ -142,7 +142,8 @@ Client_Hello_12::Client_Hello_12(Handshake_IO& io,
142142

143143
m_data->extensions().add(new Session_Ticket_Extension());
144144

145-
m_data->extensions().add(new Renegotiation_Extension(reneg_info));
145+
const bool has_reneg_info = !reneg_info.empty();
146+
m_data->extensions().add(new Renegotiation_Extension(std::move(reneg_info)));
146147

147148
m_data->extensions().add(new Supported_Versions(m_data->legacy_version(), policy));
148149

@@ -165,8 +166,8 @@ Client_Hello_12::Client_Hello_12(Handshake_IO& io,
165166
m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
166167
}
167168

168-
if(reneg_info.empty() && !next_protocols.empty()) {
169-
m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
169+
if(!has_reneg_info && !next_protocols.empty()) {
170+
m_data->extensions().add(new Application_Layer_Protocol_Notification(std::move(next_protocols)));
170171
}
171172

172173
if(m_data->legacy_version().is_datagram_protocol()) {
@@ -188,9 +189,9 @@ Client_Hello_12::Client_Hello_12(Handshake_IO& io,
188189
const Policy& policy,
189190
Callbacks& cb,
190191
RandomNumberGenerator& rng,
191-
const std::vector<uint8_t>& reneg_info,
192+
std::vector<uint8_t> reneg_info,
192193
const Session_with_Handle& session,
193-
const std::vector<std::string>& next_protocols) {
194+
std::vector<std::string> next_protocols) {
194195
m_data->m_legacy_version = session.session.version();
195196
m_data->m_random = make_hello_random(rng, cb, policy);
196197

@@ -228,7 +229,8 @@ Client_Hello_12::Client_Hello_12(Handshake_IO& io,
228229
m_data->extensions().add(new Session_Ticket_Extension(session.handle.ticket().value()));
229230
}
230231

231-
m_data->extensions().add(new Renegotiation_Extension(reneg_info));
232+
const bool has_reneg_info = !reneg_info.empty();
233+
m_data->extensions().add(new Renegotiation_Extension(std::move(reneg_info)));
232234

233235
const std::string hostname = session.session.server_info().hostname();
234236

@@ -251,8 +253,8 @@ Client_Hello_12::Client_Hello_12(Handshake_IO& io,
251253
m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
252254
}
253255

254-
if(reneg_info.empty() && !next_protocols.empty()) {
255-
m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
256+
if(!has_reneg_info && !next_protocols.empty()) {
257+
m_data->extensions().add(new Application_Layer_Protocol_Notification(std::move(next_protocols)));
256258
}
257259

258260
// NOLINTEND(*-owning-memory)
@@ -262,7 +264,7 @@ Client_Hello_12::Client_Hello_12(Handshake_IO& io,
262264
hash.update(io.send(*this));
263265
}
264266

265-
Client_Hello_12::Client_Hello_12(const std::vector<uint8_t>& buf) :
267+
Client_Hello_12::Client_Hello_12(std::span<const uint8_t> buf) :
266268
Client_Hello_12(std::make_unique<Client_Hello_Internal>(buf)) {}
267269

268270
Client_Hello_12::Client_Hello_12(std::unique_ptr<Client_Hello_Internal> data) : Client_Hello_12_Shim(std::move(data)) {
@@ -284,7 +286,7 @@ Hello_Request::Hello_Request(Handshake_IO& io) {
284286
io.send(*this);
285287
}
286288

287-
Hello_Request::Hello_Request(const std::vector<uint8_t>& buf) {
289+
Hello_Request::Hello_Request(std::span<const uint8_t> buf) {
288290
if(!buf.empty()) {
289291
throw Decoding_Error("Bad Hello_Request, has non-zero size");
290292
}

src/lib/tls/tls12/msg_client_kex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Client_Key_Exchange::Client_Key_Exchange(Handshake_IO& io,
236236
/*
237237
* Read a Client Key Exchange message
238238
*/
239-
Client_Key_Exchange::Client_Key_Exchange(const std::vector<uint8_t>& contents,
239+
Client_Key_Exchange::Client_Key_Exchange(std::span<const uint8_t> contents,
240240
const Handshake_State& state,
241241
const Private_Key* server_rsa_kex_key,
242242
Credentials_Manager& creds,

src/lib/tls/tls12/msg_hello_verify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Botan::TLS {
1414

15-
Hello_Verify_Request::Hello_Verify_Request(const std::vector<uint8_t>& buf) {
15+
Hello_Verify_Request::Hello_Verify_Request(std::span<const uint8_t> buf) {
1616
if(buf.size() < 3) {
1717
throw Decoding_Error("Hello verify request too small");
1818
}
@@ -30,7 +30,7 @@ Hello_Verify_Request::Hello_Verify_Request(const std::vector<uint8_t>& buf) {
3030
m_cookie.assign(buf.begin() + 3, buf.end());
3131
}
3232

33-
Hello_Verify_Request::Hello_Verify_Request(const std::vector<uint8_t>& client_hello_bits,
33+
Hello_Verify_Request::Hello_Verify_Request(std::span<const uint8_t> client_hello_bits,
3434
std::string_view client_identity,
3535
const SymmetricKey& secret_key) {
3636
auto hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");

src/lib/tls/tls12/msg_server_hello_12.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Server_Hello_12::Server_Hello_12(Handshake_IO& io,
2424
const Policy& policy,
2525
Callbacks& cb,
2626
RandomNumberGenerator& rng,
27-
const std::vector<uint8_t>& reneg_info,
27+
std::vector<uint8_t> reneg_info,
2828
const Client_Hello_12& client_hello,
2929
const Server_Hello_12::Settings& server_settings,
3030
std::string_view next_protocol) :
@@ -59,7 +59,7 @@ Server_Hello_12::Server_Hello_12(Handshake_IO& io,
5959
}
6060

6161
if(client_hello.secure_renegotiation()) {
62-
m_data->extensions().add(new Renegotiation_Extension(reneg_info));
62+
m_data->extensions().add(new Renegotiation_Extension(std::move(reneg_info)));
6363
}
6464

6565
if(client_hello.supports_session_ticket() && server_settings.offer_session_ticket()) {
@@ -99,7 +99,7 @@ Server_Hello_12::Server_Hello_12(Handshake_IO& io,
9999
const Policy& policy,
100100
Callbacks& cb,
101101
RandomNumberGenerator& rng,
102-
const std::vector<uint8_t>& reneg_info,
102+
std::vector<uint8_t> reneg_info,
103103
const Client_Hello_12& client_hello,
104104
const Session& resumed_session,
105105
bool offer_session_ticket,
@@ -132,7 +132,7 @@ Server_Hello_12::Server_Hello_12(Handshake_IO& io,
132132
}
133133

134134
if(client_hello.secure_renegotiation()) {
135-
m_data->extensions().add(new Renegotiation_Extension(reneg_info));
135+
m_data->extensions().add(new Renegotiation_Extension(std::move(reneg_info)));
136136
}
137137

138138
if(client_hello.supports_session_ticket() && offer_session_ticket) {
@@ -145,7 +145,7 @@ Server_Hello_12::Server_Hello_12(Handshake_IO& io,
145145
hash.update(io.send(*this));
146146
}
147147

148-
Server_Hello_12::Server_Hello_12(const std::vector<uint8_t>& buf) :
148+
Server_Hello_12::Server_Hello_12(std::span<const uint8_t> buf) :
149149
Server_Hello_12(std::make_unique<Server_Hello_Internal>(buf)) {}
150150

151151
Server_Hello_12::Server_Hello_12(std::unique_ptr<Server_Hello_Internal> data) : Server_Hello_12_Shim(std::move(data)) {}
@@ -213,7 +213,7 @@ Server_Hello_Done::Server_Hello_Done(Handshake_IO& io, Handshake_Hash& hash) {
213213
/*
214214
* Deserialize a Server Hello Done message
215215
*/
216-
Server_Hello_Done::Server_Hello_Done(const std::vector<uint8_t>& buf) {
216+
Server_Hello_Done::Server_Hello_Done(std::span<const uint8_t> buf) {
217217
if(!buf.empty()) {
218218
throw Decoding_Error("Server_Hello_Done: Must be empty, and is not");
219219
}

0 commit comments

Comments
 (0)