1 | # Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
|
---|
2 | #
|
---|
3 | # Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
4 | # this file except in compliance with the License. You can obtain a copy
|
---|
5 | # in the file LICENSE in the source distribution or at
|
---|
6 | # https://www.openssl.org/source/license.html
|
---|
7 |
|
---|
8 | use strict;
|
---|
9 |
|
---|
10 | package TLSProxy::Message;
|
---|
11 |
|
---|
12 | use TLSProxy::Alert;
|
---|
13 |
|
---|
14 | use constant DTLS_MESSAGE_HEADER_LENGTH => 12;
|
---|
15 | use constant TLS_MESSAGE_HEADER_LENGTH => 4;
|
---|
16 |
|
---|
17 | #Message types
|
---|
18 | use constant {
|
---|
19 | MT_HELLO_REQUEST => 0,
|
---|
20 | MT_CLIENT_HELLO => 1,
|
---|
21 | MT_SERVER_HELLO => 2,
|
---|
22 | MT_HELLO_VERIFY_REQUEST => 3,
|
---|
23 | MT_NEW_SESSION_TICKET => 4,
|
---|
24 | MT_ENCRYPTED_EXTENSIONS => 8,
|
---|
25 | MT_CERTIFICATE => 11,
|
---|
26 | MT_SERVER_KEY_EXCHANGE => 12,
|
---|
27 | MT_CERTIFICATE_REQUEST => 13,
|
---|
28 | MT_SERVER_HELLO_DONE => 14,
|
---|
29 | MT_CERTIFICATE_VERIFY => 15,
|
---|
30 | MT_CLIENT_KEY_EXCHANGE => 16,
|
---|
31 | MT_FINISHED => 20,
|
---|
32 | MT_CERTIFICATE_STATUS => 22,
|
---|
33 | MT_COMPRESSED_CERTIFICATE => 25,
|
---|
34 | MT_NEXT_PROTO => 67
|
---|
35 | };
|
---|
36 |
|
---|
37 | #Alert levels
|
---|
38 | use constant {
|
---|
39 | AL_LEVEL_WARN => 1,
|
---|
40 | AL_LEVEL_FATAL => 2
|
---|
41 | };
|
---|
42 |
|
---|
43 | #Alert descriptions
|
---|
44 | use constant {
|
---|
45 | AL_DESC_CLOSE_NOTIFY => 0,
|
---|
46 | AL_DESC_UNEXPECTED_MESSAGE => 10,
|
---|
47 | AL_DESC_BAD_RECORD_MAC => 20,
|
---|
48 | AL_DESC_ILLEGAL_PARAMETER => 47,
|
---|
49 | AL_DESC_PROTOCOL_VERSION => 70,
|
---|
50 | AL_DESC_NO_RENEGOTIATION => 100
|
---|
51 | };
|
---|
52 |
|
---|
53 | my %message_type = (
|
---|
54 | MT_HELLO_REQUEST, "HelloRequest",
|
---|
55 | MT_CLIENT_HELLO, "ClientHello",
|
---|
56 | MT_SERVER_HELLO, "ServerHello",
|
---|
57 | MT_HELLO_VERIFY_REQUEST, "HelloVerifyRequest",
|
---|
58 | MT_NEW_SESSION_TICKET, "NewSessionTicket",
|
---|
59 | MT_ENCRYPTED_EXTENSIONS, "EncryptedExtensions",
|
---|
60 | MT_CERTIFICATE, "Certificate",
|
---|
61 | MT_SERVER_KEY_EXCHANGE, "ServerKeyExchange",
|
---|
62 | MT_CERTIFICATE_REQUEST, "CertificateRequest",
|
---|
63 | MT_SERVER_HELLO_DONE, "ServerHelloDone",
|
---|
64 | MT_CERTIFICATE_VERIFY, "CertificateVerify",
|
---|
65 | MT_CLIENT_KEY_EXCHANGE, "ClientKeyExchange",
|
---|
66 | MT_FINISHED, "Finished",
|
---|
67 | MT_CERTIFICATE_STATUS, "CertificateStatus",
|
---|
68 | MT_COMPRESSED_CERTIFICATE, "CompressedCertificate",
|
---|
69 | MT_NEXT_PROTO, "NextProto"
|
---|
70 | );
|
---|
71 |
|
---|
72 | use constant {
|
---|
73 | EXT_SERVER_NAME => 0,
|
---|
74 | EXT_MAX_FRAGMENT_LENGTH => 1,
|
---|
75 | EXT_STATUS_REQUEST => 5,
|
---|
76 | EXT_SUPPORTED_GROUPS => 10,
|
---|
77 | EXT_EC_POINT_FORMATS => 11,
|
---|
78 | EXT_SRP => 12,
|
---|
79 | EXT_SIG_ALGS => 13,
|
---|
80 | EXT_USE_SRTP => 14,
|
---|
81 | EXT_ALPN => 16,
|
---|
82 | EXT_SCT => 18,
|
---|
83 | EXT_CLIENT_CERT_TYPE => 19,
|
---|
84 | EXT_SERVER_CERT_TYPE => 20,
|
---|
85 | EXT_PADDING => 21,
|
---|
86 | EXT_ENCRYPT_THEN_MAC => 22,
|
---|
87 | EXT_EXTENDED_MASTER_SECRET => 23,
|
---|
88 | EXT_COMPRESS_CERTIFICATE => 27,
|
---|
89 | EXT_SESSION_TICKET => 35,
|
---|
90 | EXT_KEY_SHARE => 51,
|
---|
91 | EXT_PSK => 41,
|
---|
92 | EXT_SUPPORTED_VERSIONS => 43,
|
---|
93 | EXT_COOKIE => 44,
|
---|
94 | EXT_PSK_KEX_MODES => 45,
|
---|
95 | EXT_POST_HANDSHAKE_AUTH => 49,
|
---|
96 | EXT_SIG_ALGS_CERT => 50,
|
---|
97 | EXT_RENEGOTIATE => 65281,
|
---|
98 | EXT_NPN => 13172,
|
---|
99 | EXT_CRYPTOPRO_BUG_EXTENSION => 0xfde8,
|
---|
100 | EXT_UNKNOWN => 0xfffe,
|
---|
101 | #Unknown extension that should appear last
|
---|
102 | EXT_FORCE_LAST => 0xffff
|
---|
103 | };
|
---|
104 |
|
---|
105 | # SignatureScheme of TLS 1.3 from:
|
---|
106 | # https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-signaturescheme
|
---|
107 | # We have to manually grab the SHA224 equivalents from the old registry
|
---|
108 | use constant {
|
---|
109 | SIG_ALG_RSA_PKCS1_SHA256 => 0x0401,
|
---|
110 | SIG_ALG_RSA_PKCS1_SHA384 => 0x0501,
|
---|
111 | SIG_ALG_RSA_PKCS1_SHA512 => 0x0601,
|
---|
112 | SIG_ALG_ECDSA_SECP256R1_SHA256 => 0x0403,
|
---|
113 | SIG_ALG_ECDSA_SECP384R1_SHA384 => 0x0503,
|
---|
114 | SIG_ALG_ECDSA_SECP521R1_SHA512 => 0x0603,
|
---|
115 | SIG_ALG_RSA_PSS_RSAE_SHA256 => 0x0804,
|
---|
116 | SIG_ALG_RSA_PSS_RSAE_SHA384 => 0x0805,
|
---|
117 | SIG_ALG_RSA_PSS_RSAE_SHA512 => 0x0806,
|
---|
118 | SIG_ALG_ED25519 => 0x0807,
|
---|
119 | SIG_ALG_ED448 => 0x0808,
|
---|
120 | SIG_ALG_RSA_PSS_PSS_SHA256 => 0x0809,
|
---|
121 | SIG_ALG_RSA_PSS_PSS_SHA384 => 0x080a,
|
---|
122 | SIG_ALG_RSA_PSS_PSS_SHA512 => 0x080b,
|
---|
123 | SIG_ALG_RSA_PKCS1_SHA1 => 0x0201,
|
---|
124 | SIG_ALG_ECDSA_SHA1 => 0x0203,
|
---|
125 | SIG_ALG_DSA_SHA1 => 0x0202,
|
---|
126 | SIG_ALG_DSA_SHA256 => 0x0402,
|
---|
127 | SIG_ALG_DSA_SHA384 => 0x0502,
|
---|
128 | SIG_ALG_DSA_SHA512 => 0x0602,
|
---|
129 | OSSL_SIG_ALG_RSA_PKCS1_SHA224 => 0x0301,
|
---|
130 | OSSL_SIG_ALG_DSA_SHA224 => 0x0302,
|
---|
131 | OSSL_SIG_ALG_ECDSA_SHA224 => 0x0303
|
---|
132 | };
|
---|
133 |
|
---|
134 | use constant {
|
---|
135 | CIPHER_RSA_WITH_AES_128_CBC_SHA => 0x002f,
|
---|
136 | CIPHER_DHE_RSA_AES_128_SHA => 0x0033,
|
---|
137 | CIPHER_ADH_AES_128_SHA => 0x0034,
|
---|
138 | CIPHER_TLS13_AES_128_GCM_SHA256 => 0x1301,
|
---|
139 | CIPHER_TLS13_AES_256_GCM_SHA384 => 0x1302
|
---|
140 | };
|
---|
141 |
|
---|
142 | use constant {
|
---|
143 | CLIENT => 0,
|
---|
144 | SERVER => 1
|
---|
145 | };
|
---|
146 |
|
---|
147 | my $payload = "";
|
---|
148 | my $messlen = -1;
|
---|
149 | my $mt;
|
---|
150 | my $startoffset = -1;
|
---|
151 | my $server = 0;
|
---|
152 | my $success = 0;
|
---|
153 | my $end = 0;
|
---|
154 | my @message_rec_list = ();
|
---|
155 | my @message_frag_lens = ();
|
---|
156 | my $ciphersuite = 0;
|
---|
157 | my $successondata = 0;
|
---|
158 | my $alert;
|
---|
159 |
|
---|
160 | sub clear
|
---|
161 | {
|
---|
162 | $payload = "";
|
---|
163 | $messlen = -1;
|
---|
164 | $startoffset = -1;
|
---|
165 | $server = 0;
|
---|
166 | $success = 0;
|
---|
167 | $end = 0;
|
---|
168 | $successondata = 0;
|
---|
169 | @message_rec_list = ();
|
---|
170 | @message_frag_lens = ();
|
---|
171 | $alert = undef;
|
---|
172 | }
|
---|
173 |
|
---|
174 | #Class method to extract messages from a record
|
---|
175 | sub get_messages
|
---|
176 | {
|
---|
177 | my $class = shift;
|
---|
178 | my $serverin = shift;
|
---|
179 | my $record = shift;
|
---|
180 | my $isdtls = shift;
|
---|
181 | my @messages = ();
|
---|
182 | my $message;
|
---|
183 |
|
---|
184 | @message_frag_lens = ();
|
---|
185 |
|
---|
186 | if ($serverin != $server && length($payload) != 0) {
|
---|
187 | die "Changed peer, but we still have fragment data\n";
|
---|
188 | }
|
---|
189 | $server = $serverin;
|
---|
190 |
|
---|
191 | if ($record->content_type == TLSProxy::Record::RT_CCS) {
|
---|
192 | if ($payload ne "") {
|
---|
193 | #We can't handle this yet
|
---|
194 | die "CCS received before message data complete\n";
|
---|
195 | }
|
---|
196 | if (!TLSProxy::Proxy->is_tls13()) {
|
---|
197 | if ($server) {
|
---|
198 | TLSProxy::Record->server_encrypting(1);
|
---|
199 | } else {
|
---|
200 | TLSProxy::Record->client_encrypting(1);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | } elsif ($record->content_type == TLSProxy::Record::RT_HANDSHAKE) {
|
---|
204 | if ($record->len == 0 || $record->len_real == 0) {
|
---|
205 | print " Message truncated\n";
|
---|
206 | } else {
|
---|
207 | my $recoffset = 0;
|
---|
208 |
|
---|
209 | if (length $payload > 0) {
|
---|
210 | #We are continuing processing a message started in a previous
|
---|
211 | #record. Add this record to the list associated with this
|
---|
212 | #message
|
---|
213 | push @message_rec_list, $record;
|
---|
214 |
|
---|
215 | if ($messlen <= length($payload)) {
|
---|
216 | #Shouldn't happen
|
---|
217 | die "Internal error: invalid messlen: ".$messlen
|
---|
218 | ." payload length:".length($payload)."\n";
|
---|
219 | }
|
---|
220 | if (length($payload) + $record->decrypt_len >= $messlen) {
|
---|
221 | #We can complete the message with this record
|
---|
222 | $recoffset = $messlen - length($payload);
|
---|
223 | $payload .= substr($record->decrypt_data, 0, $recoffset);
|
---|
224 | push @message_frag_lens, $recoffset;
|
---|
225 | if ($isdtls) {
|
---|
226 | # We must set $msgseq, $msgfrag, $msgfragoffs
|
---|
227 | die "Internal error: cannot handle partial dtls messages\n"
|
---|
228 | }
|
---|
229 | $message = create_message($server, $mt,
|
---|
230 | #$msgseq, $msgfrag, $msgfragoffs,
|
---|
231 | 0, 0, 0,
|
---|
232 | $payload, $startoffset, $isdtls);
|
---|
233 | push @messages, $message;
|
---|
234 |
|
---|
235 | $payload = "";
|
---|
236 | } else {
|
---|
237 | #This is just part of the total message
|
---|
238 | $payload .= $record->decrypt_data;
|
---|
239 | $recoffset = $record->decrypt_len;
|
---|
240 | push @message_frag_lens, $record->decrypt_len;
|
---|
241 | }
|
---|
242 | print " Partial message data read: ".$recoffset." bytes\n";
|
---|
243 | }
|
---|
244 |
|
---|
245 | while ($record->decrypt_len > $recoffset) {
|
---|
246 | #We are at the start of a new message
|
---|
247 | my $msgheaderlen = $isdtls ? DTLS_MESSAGE_HEADER_LENGTH
|
---|
248 | : TLS_MESSAGE_HEADER_LENGTH;
|
---|
249 | if ($record->decrypt_len - $recoffset < $msgheaderlen) {
|
---|
250 | #Whilst technically probably valid we can't cope with this
|
---|
251 | die "End of record in the middle of a message header\n";
|
---|
252 | }
|
---|
253 | @message_rec_list = ($record);
|
---|
254 | my $lenhi;
|
---|
255 | my $lenlo;
|
---|
256 | my $msgseq;
|
---|
257 | my $msgfrag;
|
---|
258 | my $msgfragoffs;
|
---|
259 | if ($isdtls) {
|
---|
260 | my $msgfraghi;
|
---|
261 | my $msgfraglo;
|
---|
262 | my $msgfragoffshi;
|
---|
263 | my $msgfragoffslo;
|
---|
264 | ($mt, $lenhi, $lenlo, $msgseq, $msgfraghi, $msgfraglo, $msgfragoffshi, $msgfragoffslo) =
|
---|
265 | unpack('CnCnnCnC', substr($record->decrypt_data, $recoffset));
|
---|
266 | $msgfrag = ($msgfraghi << 8) | $msgfraglo;
|
---|
267 | $msgfragoffs = ($msgfragoffshi << 8) | $msgfragoffslo;
|
---|
268 | } else {
|
---|
269 | ($mt, $lenhi, $lenlo) =
|
---|
270 | unpack('CnC', substr($record->decrypt_data, $recoffset));
|
---|
271 | }
|
---|
272 | $messlen = ($lenhi << 8) | $lenlo;
|
---|
273 | print " Message type: $message_type{$mt}($mt)\n";
|
---|
274 | print " Message Length: $messlen\n";
|
---|
275 | $startoffset = $recoffset;
|
---|
276 | $recoffset += $msgheaderlen;
|
---|
277 | $payload = "";
|
---|
278 |
|
---|
279 | if ($recoffset <= $record->decrypt_len) {
|
---|
280 | #Some payload data is present in this record
|
---|
281 | if ($record->decrypt_len - $recoffset >= $messlen) {
|
---|
282 | #We can complete the message with this record
|
---|
283 | $payload .= substr($record->decrypt_data, $recoffset,
|
---|
284 | $messlen);
|
---|
285 | $recoffset += $messlen;
|
---|
286 | push @message_frag_lens, $messlen;
|
---|
287 | $message = create_message($server, $mt, $msgseq,
|
---|
288 | $msgfrag, $msgfragoffs,
|
---|
289 | $payload, $startoffset, $isdtls);
|
---|
290 | push @messages, $message;
|
---|
291 |
|
---|
292 | $payload = "";
|
---|
293 | } else {
|
---|
294 | #This is just part of the total message
|
---|
295 | $payload .= substr($record->decrypt_data, $recoffset,
|
---|
296 | $record->decrypt_len - $recoffset);
|
---|
297 | $recoffset = $record->decrypt_len;
|
---|
298 | push @message_frag_lens, $recoffset;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | }
|
---|
302 | }
|
---|
303 | } elsif ($record->content_type == TLSProxy::Record::RT_APPLICATION_DATA) {
|
---|
304 | print " [ENCRYPTED APPLICATION DATA]\n";
|
---|
305 | print " [".$record->decrypt_data."]\n";
|
---|
306 |
|
---|
307 | if ($successondata) {
|
---|
308 | $success = 1;
|
---|
309 | $end = 1;
|
---|
310 | }
|
---|
311 | } elsif ($record->content_type == TLSProxy::Record::RT_ALERT) {
|
---|
312 | my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
|
---|
313 | print " [$alertlev, $alertdesc]\n";
|
---|
314 | #A CloseNotify from the client indicates we have finished successfully
|
---|
315 | #(we assume)
|
---|
316 | if (!$end && !$server && $alertlev == AL_LEVEL_WARN
|
---|
317 | && $alertdesc == AL_DESC_CLOSE_NOTIFY) {
|
---|
318 | $success = 1;
|
---|
319 | }
|
---|
320 | #Fatal or close notify alerts end the test
|
---|
321 | if ($alertlev == AL_LEVEL_FATAL || $alertdesc == AL_DESC_CLOSE_NOTIFY) {
|
---|
322 | $end = 1;
|
---|
323 | }
|
---|
324 | $alert = TLSProxy::Alert->new(
|
---|
325 | $server,
|
---|
326 | $record->encrypted,
|
---|
327 | $alertlev,
|
---|
328 | $alertdesc);
|
---|
329 | }
|
---|
330 |
|
---|
331 | return @messages;
|
---|
332 | }
|
---|
333 |
|
---|
334 | #Function to work out which sub-class we need to create and then
|
---|
335 | #construct it
|
---|
336 | sub create_message
|
---|
337 | {
|
---|
338 | my ($server, $mt, $msgseq, $msgfrag, $msgfragoffs, $data, $startoffset, $isdtls) = @_;
|
---|
339 | my $message;
|
---|
340 |
|
---|
341 | if ($mt == MT_CLIENT_HELLO) {
|
---|
342 | $message = TLSProxy::ClientHello->new(
|
---|
343 | $isdtls,
|
---|
344 | $server,
|
---|
345 | $msgseq,
|
---|
346 | $msgfrag,
|
---|
347 | $msgfragoffs,
|
---|
348 | $data,
|
---|
349 | [@message_rec_list],
|
---|
350 | $startoffset,
|
---|
351 | [@message_frag_lens]
|
---|
352 | );
|
---|
353 | $message->parse();
|
---|
354 | } elsif ($mt == MT_SERVER_HELLO) {
|
---|
355 | $message = TLSProxy::ServerHello->new(
|
---|
356 | $isdtls,
|
---|
357 | $server,
|
---|
358 | $msgseq,
|
---|
359 | $msgfrag,
|
---|
360 | $msgfragoffs,
|
---|
361 | $data,
|
---|
362 | [@message_rec_list],
|
---|
363 | $startoffset,
|
---|
364 | [@message_frag_lens]
|
---|
365 | );
|
---|
366 | $message->parse();
|
---|
367 | } elsif ($mt == MT_HELLO_VERIFY_REQUEST) {
|
---|
368 | $message = TLSProxy::HelloVerifyRequest->new(
|
---|
369 | $isdtls,
|
---|
370 | $server,
|
---|
371 | $msgseq,
|
---|
372 | $msgfrag,
|
---|
373 | $msgfragoffs,
|
---|
374 | $data,
|
---|
375 | [@message_rec_list],
|
---|
376 | $startoffset,
|
---|
377 | [@message_frag_lens]
|
---|
378 | );
|
---|
379 | $message->parse();
|
---|
380 | } elsif ($mt == MT_ENCRYPTED_EXTENSIONS) {
|
---|
381 | $message = TLSProxy::EncryptedExtensions->new(
|
---|
382 | $isdtls,
|
---|
383 | $server,
|
---|
384 | $msgseq,
|
---|
385 | $msgfrag,
|
---|
386 | $msgfragoffs,
|
---|
387 | $data,
|
---|
388 | [@message_rec_list],
|
---|
389 | $startoffset,
|
---|
390 | [@message_frag_lens]
|
---|
391 | );
|
---|
392 | $message->parse();
|
---|
393 | } elsif ($mt == MT_CERTIFICATE) {
|
---|
394 | $message = TLSProxy::Certificate->new(
|
---|
395 | $isdtls,
|
---|
396 | $server,
|
---|
397 | $msgseq,
|
---|
398 | $msgfrag,
|
---|
399 | $msgfragoffs,
|
---|
400 | $data,
|
---|
401 | [@message_rec_list],
|
---|
402 | $startoffset,
|
---|
403 | [@message_frag_lens]
|
---|
404 | );
|
---|
405 | $message->parse();
|
---|
406 | } elsif ($mt == MT_CERTIFICATE_REQUEST) {
|
---|
407 | $message = TLSProxy::CertificateRequest->new(
|
---|
408 | $isdtls,
|
---|
409 | $server,
|
---|
410 | $msgseq,
|
---|
411 | $msgfrag,
|
---|
412 | $msgfragoffs,
|
---|
413 | $data,
|
---|
414 | [@message_rec_list],
|
---|
415 | $startoffset,
|
---|
416 | [@message_frag_lens]
|
---|
417 | );
|
---|
418 | $message->parse();
|
---|
419 | } elsif ($mt == MT_CERTIFICATE_VERIFY) {
|
---|
420 | $message = TLSProxy::CertificateVerify->new(
|
---|
421 | $isdtls,
|
---|
422 | $server,
|
---|
423 | $msgseq,
|
---|
424 | $msgfrag,
|
---|
425 | $msgfragoffs,
|
---|
426 | $data,
|
---|
427 | [@message_rec_list],
|
---|
428 | $startoffset,
|
---|
429 | [@message_frag_lens]
|
---|
430 | );
|
---|
431 | $message->parse();
|
---|
432 | } elsif ($mt == MT_SERVER_KEY_EXCHANGE) {
|
---|
433 | $message = TLSProxy::ServerKeyExchange->new(
|
---|
434 | $isdtls,
|
---|
435 | $server,
|
---|
436 | $msgseq,
|
---|
437 | $msgfrag,
|
---|
438 | $msgfragoffs,
|
---|
439 | $data,
|
---|
440 | [@message_rec_list],
|
---|
441 | $startoffset,
|
---|
442 | [@message_frag_lens]
|
---|
443 | );
|
---|
444 | $message->parse();
|
---|
445 | } elsif ($mt == MT_NEW_SESSION_TICKET) {
|
---|
446 | if ($isdtls) {
|
---|
447 | $message = TLSProxy::NewSessionTicket->new_dtls(
|
---|
448 | $server,
|
---|
449 | $msgseq,
|
---|
450 | $msgfrag,
|
---|
451 | $msgfragoffs,
|
---|
452 | $data,
|
---|
453 | [@message_rec_list],
|
---|
454 | $startoffset,
|
---|
455 | [@message_frag_lens]
|
---|
456 | );
|
---|
457 | } else {
|
---|
458 | $message = TLSProxy::NewSessionTicket->new(
|
---|
459 | $server,
|
---|
460 | $data,
|
---|
461 | [@message_rec_list],
|
---|
462 | $startoffset,
|
---|
463 | [@message_frag_lens]
|
---|
464 | );
|
---|
465 | }
|
---|
466 | $message->parse();
|
---|
467 | } elsif ($mt == MT_NEXT_PROTO) {
|
---|
468 | $message = TLSProxy::NextProto->new(
|
---|
469 | $isdtls,
|
---|
470 | $server,
|
---|
471 | $msgseq,
|
---|
472 | $msgfrag,
|
---|
473 | $msgfragoffs,
|
---|
474 | $data,
|
---|
475 | [@message_rec_list],
|
---|
476 | $startoffset,
|
---|
477 | [@message_frag_lens]
|
---|
478 | );
|
---|
479 | $message->parse();
|
---|
480 | } else {
|
---|
481 | #Unknown message type
|
---|
482 | $message = TLSProxy::Message->new(
|
---|
483 | $isdtls,
|
---|
484 | $server,
|
---|
485 | $mt,
|
---|
486 | $msgseq,
|
---|
487 | $msgfrag,
|
---|
488 | $msgfragoffs,
|
---|
489 | $data,
|
---|
490 | [@message_rec_list],
|
---|
491 | $startoffset,
|
---|
492 | [@message_frag_lens]
|
---|
493 | );
|
---|
494 | }
|
---|
495 |
|
---|
496 | return $message;
|
---|
497 | }
|
---|
498 |
|
---|
499 | sub end
|
---|
500 | {
|
---|
501 | my $class = shift;
|
---|
502 | return $end;
|
---|
503 | }
|
---|
504 | sub success
|
---|
505 | {
|
---|
506 | my $class = shift;
|
---|
507 | return $success;
|
---|
508 | }
|
---|
509 | sub fail
|
---|
510 | {
|
---|
511 | my $class = shift;
|
---|
512 | return !$success && $end;
|
---|
513 | }
|
---|
514 |
|
---|
515 | sub alert
|
---|
516 | {
|
---|
517 | return $alert;
|
---|
518 | }
|
---|
519 |
|
---|
520 | sub new
|
---|
521 | {
|
---|
522 | my $class = shift;
|
---|
523 | my ($isdtls,
|
---|
524 | $server,
|
---|
525 | $mt,
|
---|
526 | $msgseq,
|
---|
527 | $msgfrag,
|
---|
528 | $msgfragoffs,
|
---|
529 | $data,
|
---|
530 | $records,
|
---|
531 | $startoffset,
|
---|
532 | $message_frag_lens) = @_;
|
---|
533 |
|
---|
534 | my $self = {
|
---|
535 | isdtls => $isdtls,
|
---|
536 | server => $server,
|
---|
537 | data => $data,
|
---|
538 | records => $records,
|
---|
539 | mt => $mt,
|
---|
540 | msgseq => $msgseq,
|
---|
541 | msgfrag => $msgfrag,
|
---|
542 | msgfragoffs => $msgfragoffs,
|
---|
543 | startoffset => $startoffset,
|
---|
544 | message_frag_lens => $message_frag_lens,
|
---|
545 | dupext => -1
|
---|
546 | };
|
---|
547 |
|
---|
548 | return bless $self, $class;
|
---|
549 | }
|
---|
550 |
|
---|
551 | sub ciphersuite
|
---|
552 | {
|
---|
553 | my $class = shift;
|
---|
554 | if (@_) {
|
---|
555 | $ciphersuite = shift;
|
---|
556 | }
|
---|
557 | return $ciphersuite;
|
---|
558 | }
|
---|
559 |
|
---|
560 | #Update all the underlying records with the modified data from this message
|
---|
561 | #Note: Only supports TLSv1.3 and ETM encryption
|
---|
562 | sub repack
|
---|
563 | {
|
---|
564 | my $self = shift;
|
---|
565 | my $msgdata;
|
---|
566 |
|
---|
567 | my $numrecs = $#{$self->records};
|
---|
568 |
|
---|
569 | $self->set_message_contents();
|
---|
570 |
|
---|
571 | my $lenlo = length($self->data) & 0xff;
|
---|
572 | my $lenhi = length($self->data) >> 8;
|
---|
573 |
|
---|
574 | if ($self->{isdtls}) {
|
---|
575 | my $msgfraghi = $self->msgfrag >> 8;
|
---|
576 | my $msgfraglo = $self->msgfrag & 0xff;
|
---|
577 | my $msgfragoffshi = $self->msgfragoffs >> 8;
|
---|
578 | my $msgfragoffslo = $self->msgfragoffs & 0xff;
|
---|
579 |
|
---|
580 | $msgdata = pack('CnCnnCnC', $self->mt, $lenhi, $lenlo, $self->msgseq,
|
---|
581 | $msgfraghi, $msgfraglo,
|
---|
582 | $msgfragoffshi, $msgfragoffslo).$self->data;
|
---|
583 | } else {
|
---|
584 | $msgdata = pack('CnC', $self->mt, $lenhi, $lenlo).$self->data;
|
---|
585 | }
|
---|
586 |
|
---|
587 | if ($numrecs == 0) {
|
---|
588 | #The message is fully contained within one record
|
---|
589 | my ($rec) = @{$self->records};
|
---|
590 | my $recdata = $rec->decrypt_data;
|
---|
591 |
|
---|
592 | my $old_length;
|
---|
593 | my $msg_header_len = $self->{isdtls} ? DTLS_MESSAGE_HEADER_LENGTH
|
---|
594 | : TLS_MESSAGE_HEADER_LENGTH;
|
---|
595 |
|
---|
596 | # We use empty message_frag_lens to indicates that pre-repacking,
|
---|
597 | # the message wasn't present. The first fragment length doesn't include
|
---|
598 | # the TLS header, so we need to check and compute the right length.
|
---|
599 | if (@{$self->message_frag_lens}) {
|
---|
600 | $old_length = ${$self->message_frag_lens}[0] + $msg_header_len;
|
---|
601 | } else {
|
---|
602 | $old_length = 0;
|
---|
603 | }
|
---|
604 |
|
---|
605 | my $prefix = substr($recdata, 0, $self->startoffset);
|
---|
606 | my $suffix = substr($recdata, $self->startoffset + $old_length);
|
---|
607 |
|
---|
608 | $rec->decrypt_data($prefix.($msgdata).($suffix));
|
---|
609 | # TODO(openssl-team): don't keep explicit lengths.
|
---|
610 | # (If a length override is ever needed to construct invalid packets,
|
---|
611 | # use an explicit override field instead.)
|
---|
612 | $rec->decrypt_len(length($rec->decrypt_data));
|
---|
613 | # Only support re-encryption for TLSv1.3 and ETM.
|
---|
614 | if ($rec->encrypted()) {
|
---|
615 | if (TLSProxy::Proxy->is_tls13()) {
|
---|
616 | #Add content type (1 byte) and 16 tag bytes
|
---|
617 | $rec->data($rec->decrypt_data
|
---|
618 | .pack("C", TLSProxy::Record::RT_HANDSHAKE).("\0"x16));
|
---|
619 | } elsif ($rec->etm()) {
|
---|
620 | my $data = $rec->decrypt_data;
|
---|
621 | #Add padding
|
---|
622 | my $padval = length($data) % 16;
|
---|
623 | $padval = 15 - $padval;
|
---|
624 | for (0..$padval) {
|
---|
625 | $data .= pack("C", $padval);
|
---|
626 | }
|
---|
627 |
|
---|
628 | #Add MAC. Assumed to be 20 bytes
|
---|
629 | foreach my $macval (0..19) {
|
---|
630 | $data .= pack("C", $macval);
|
---|
631 | }
|
---|
632 |
|
---|
633 | if ($rec->version() >= TLSProxy::Record::VERS_TLS_1_1) {
|
---|
634 | #Explicit IV
|
---|
635 | $data = ("\0"x16).$data;
|
---|
636 | }
|
---|
637 | $rec->data($data);
|
---|
638 | } else {
|
---|
639 | die "Unsupported encryption: No ETM";
|
---|
640 | }
|
---|
641 | } else {
|
---|
642 | $rec->data($rec->decrypt_data);
|
---|
643 | }
|
---|
644 | $rec->len(length($rec->data));
|
---|
645 |
|
---|
646 | #Update the fragment len in case we changed it above
|
---|
647 | ${$self->message_frag_lens}[0] = length($msgdata) - $msg_header_len;
|
---|
648 | return;
|
---|
649 | }
|
---|
650 |
|
---|
651 | #Note we don't currently support changing a fragmented message length
|
---|
652 | my $recctr = 0;
|
---|
653 | my $datadone = 0;
|
---|
654 | foreach my $rec (@{$self->records}) {
|
---|
655 | my $recdata = $rec->decrypt_data;
|
---|
656 | if ($recctr == 0) {
|
---|
657 | #This is the first record
|
---|
658 | my $remainlen = length($recdata) - $self->startoffset;
|
---|
659 | $rec->data(substr($recdata, 0, $self->startoffset)
|
---|
660 | .substr(($msgdata), 0, $remainlen));
|
---|
661 | $datadone += $remainlen;
|
---|
662 | } elsif ($recctr + 1 == $numrecs) {
|
---|
663 | #This is the last record
|
---|
664 | $rec->data(substr($msgdata, $datadone));
|
---|
665 | } else {
|
---|
666 | #This is a middle record
|
---|
667 | $rec->data(substr($msgdata, $datadone, length($rec->data)));
|
---|
668 | $datadone += length($rec->data);
|
---|
669 | }
|
---|
670 | $recctr++;
|
---|
671 | }
|
---|
672 | }
|
---|
673 |
|
---|
674 | #To be overridden by sub-classes
|
---|
675 | sub set_message_contents
|
---|
676 | {
|
---|
677 | }
|
---|
678 |
|
---|
679 | #Read only accessors
|
---|
680 | sub server
|
---|
681 | {
|
---|
682 | my $self = shift;
|
---|
683 | return $self->{server};
|
---|
684 | }
|
---|
685 |
|
---|
686 | #Read/write accessors
|
---|
687 | sub mt
|
---|
688 | {
|
---|
689 | my $self = shift;
|
---|
690 | if (@_) {
|
---|
691 | $self->{mt} = shift;
|
---|
692 | }
|
---|
693 | return $self->{mt};
|
---|
694 | }
|
---|
695 | sub msgseq
|
---|
696 | {
|
---|
697 | my $self = shift;
|
---|
698 | if (@_) {
|
---|
699 | $self->{msgseq} = shift;
|
---|
700 | }
|
---|
701 | return $self->{msgseq};
|
---|
702 | }
|
---|
703 | sub msgfrag
|
---|
704 | {
|
---|
705 | my $self = shift;
|
---|
706 | if (@_) {
|
---|
707 | $self->{msgfrag} = shift;
|
---|
708 | }
|
---|
709 | return $self->{msgfrag};
|
---|
710 | }
|
---|
711 | sub msgfragoffs
|
---|
712 | {
|
---|
713 | my $self = shift;
|
---|
714 | if (@_) {
|
---|
715 | $self->{msgfragoffs} = shift;
|
---|
716 | }
|
---|
717 | return $self->{msgfragoffs};
|
---|
718 | }
|
---|
719 | sub data
|
---|
720 | {
|
---|
721 | my $self = shift;
|
---|
722 | if (@_) {
|
---|
723 | $self->{data} = shift;
|
---|
724 | }
|
---|
725 | return $self->{data};
|
---|
726 | }
|
---|
727 | sub records
|
---|
728 | {
|
---|
729 | my $self = shift;
|
---|
730 | if (@_) {
|
---|
731 | $self->{records} = shift;
|
---|
732 | }
|
---|
733 | return $self->{records};
|
---|
734 | }
|
---|
735 | sub startoffset
|
---|
736 | {
|
---|
737 | my $self = shift;
|
---|
738 | if (@_) {
|
---|
739 | $self->{startoffset} = shift;
|
---|
740 | }
|
---|
741 | return $self->{startoffset};
|
---|
742 | }
|
---|
743 | sub message_frag_lens
|
---|
744 | {
|
---|
745 | my $self = shift;
|
---|
746 | if (@_) {
|
---|
747 | $self->{message_frag_lens} = shift;
|
---|
748 | }
|
---|
749 | return $self->{message_frag_lens};
|
---|
750 | }
|
---|
751 | sub encoded_length
|
---|
752 | {
|
---|
753 | my $self = shift;
|
---|
754 | my $msg_header_len = $self->{isdtls} ? DTLS_MESSAGE_HEADER_LENGTH
|
---|
755 | : TLS_MESSAGE_HEADER_LENGTH;
|
---|
756 | return $msg_header_len + length($self->data);
|
---|
757 | }
|
---|
758 | sub dupext
|
---|
759 | {
|
---|
760 | my $self = shift;
|
---|
761 | if (@_) {
|
---|
762 | $self->{dupext} = shift;
|
---|
763 | }
|
---|
764 | return $self->{dupext};
|
---|
765 | }
|
---|
766 | sub successondata
|
---|
767 | {
|
---|
768 | my $class = shift;
|
---|
769 | if (@_) {
|
---|
770 | $successondata = shift;
|
---|
771 | }
|
---|
772 | return $successondata;
|
---|
773 | }
|
---|
774 | 1;
|
---|