1 # Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 package TLSProxy
::Message
;
14 use constant TLS_MESSAGE_HEADER_LENGTH
=> 4;
18 MT_HELLO_REQUEST
=> 0,
21 MT_NEW_SESSION_TICKET
=> 4,
22 MT_ENCRYPTED_EXTENSIONS
=> 8,
24 MT_SERVER_KEY_EXCHANGE
=> 12,
25 MT_CERTIFICATE_REQUEST
=> 13,
26 MT_SERVER_HELLO_DONE
=> 14,
27 MT_CERTIFICATE_VERIFY
=> 15,
28 MT_CLIENT_KEY_EXCHANGE
=> 16,
30 MT_CERTIFICATE_STATUS
=> 22,
31 MT_COMPRESSED_CERTIFICATE
=> 25,
43 AL_DESC_CLOSE_NOTIFY
=> 0,
44 AL_DESC_UNEXPECTED_MESSAGE
=> 10,
45 AL_DESC_ILLEGAL_PARAMETER
=> 47,
46 AL_DESC_NO_RENEGOTIATION
=> 100
50 MT_HELLO_REQUEST
, "HelloRequest",
51 MT_CLIENT_HELLO
, "ClientHello",
52 MT_SERVER_HELLO
, "ServerHello",
53 MT_NEW_SESSION_TICKET
, "NewSessionTicket",
54 MT_ENCRYPTED_EXTENSIONS
, "EncryptedExtensions",
55 MT_CERTIFICATE
, "Certificate",
56 MT_SERVER_KEY_EXCHANGE
, "ServerKeyExchange",
57 MT_CERTIFICATE_REQUEST
, "CertificateRequest",
58 MT_SERVER_HELLO_DONE
, "ServerHelloDone",
59 MT_CERTIFICATE_VERIFY
, "CertificateVerify",
60 MT_CLIENT_KEY_EXCHANGE
, "ClientKeyExchange",
61 MT_FINISHED
, "Finished",
62 MT_CERTIFICATE_STATUS
, "CertificateStatus",
63 MT_COMPRESSED_CERTIFICATE
, "CompressedCertificate",
64 MT_NEXT_PROTO
, "NextProto"
69 EXT_MAX_FRAGMENT_LENGTH
=> 1,
70 EXT_STATUS_REQUEST
=> 5,
71 EXT_SUPPORTED_GROUPS
=> 10,
72 EXT_EC_POINT_FORMATS
=> 11,
78 EXT_CLIENT_CERT_TYPE
=> 19,
79 EXT_SERVER_CERT_TYPE
=> 20,
81 EXT_ENCRYPT_THEN_MAC
=> 22,
82 EXT_EXTENDED_MASTER_SECRET
=> 23,
83 EXT_COMPRESS_CERTIFICATE
=> 27,
84 EXT_SESSION_TICKET
=> 35,
87 EXT_SUPPORTED_VERSIONS
=> 43,
89 EXT_PSK_KEX_MODES
=> 45,
90 EXT_POST_HANDSHAKE_AUTH
=> 49,
91 EXT_SIG_ALGS_CERT
=> 50,
92 EXT_RENEGOTIATE
=> 65281,
94 EXT_CRYPTOPRO_BUG_EXTENSION
=> 0xfde8,
95 EXT_UNKNOWN
=> 0xfffe,
96 #Unknown extension that should appear last
97 EXT_FORCE_LAST
=> 0xffff
100 # SignatureScheme of TLS 1.3 from:
101 # https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-signaturescheme
102 # We have to manually grab the SHA224 equivalents from the old registry
104 SIG_ALG_RSA_PKCS1_SHA256
=> 0x0401,
105 SIG_ALG_RSA_PKCS1_SHA384
=> 0x0501,
106 SIG_ALG_RSA_PKCS1_SHA512
=> 0x0601,
107 SIG_ALG_ECDSA_SECP256R1_SHA256
=> 0x0403,
108 SIG_ALG_ECDSA_SECP384R1_SHA384
=> 0x0503,
109 SIG_ALG_ECDSA_SECP521R1_SHA512
=> 0x0603,
110 SIG_ALG_RSA_PSS_RSAE_SHA256
=> 0x0804,
111 SIG_ALG_RSA_PSS_RSAE_SHA384
=> 0x0805,
112 SIG_ALG_RSA_PSS_RSAE_SHA512
=> 0x0806,
113 SIG_ALG_ED25519
=> 0x0807,
114 SIG_ALG_ED448
=> 0x0808,
115 SIG_ALG_RSA_PSS_PSS_SHA256
=> 0x0809,
116 SIG_ALG_RSA_PSS_PSS_SHA384
=> 0x080a,
117 SIG_ALG_RSA_PSS_PSS_SHA512
=> 0x080b,
118 SIG_ALG_RSA_PKCS1_SHA1
=> 0x0201,
119 SIG_ALG_ECDSA_SHA1
=> 0x0203,
120 SIG_ALG_DSA_SHA1
=> 0x0202,
121 SIG_ALG_DSA_SHA256
=> 0x0402,
122 SIG_ALG_DSA_SHA384
=> 0x0502,
123 SIG_ALG_DSA_SHA512
=> 0x0602,
124 OSSL_SIG_ALG_RSA_PKCS1_SHA224
=> 0x0301,
125 OSSL_SIG_ALG_DSA_SHA224
=> 0x0302,
126 OSSL_SIG_ALG_ECDSA_SHA224
=> 0x0303
130 CIPHER_RSA_WITH_AES_128_CBC_SHA
=> 0x002f,
131 CIPHER_DHE_RSA_AES_128_SHA
=> 0x0033,
132 CIPHER_ADH_AES_128_SHA
=> 0x0034,
133 CIPHER_TLS13_AES_128_GCM_SHA256
=> 0x1301,
134 CIPHER_TLS13_AES_256_GCM_SHA384
=> 0x1302
145 my $startoffset = -1;
149 my @message_rec_list = ();
150 my @message_frag_lens = ();
152 my $successondata = 0;
164 @message_rec_list = ();
165 @message_frag_lens = ();
169 #Class method to extract messages from a record
173 my $serverin = shift;
178 @message_frag_lens = ();
180 if ($serverin != $server && length($payload) != 0) {
181 die "Changed peer, but we still have fragment data\n";
185 if ($record->content_type == TLSProxy
::Record
::RT_CCS
) {
186 if ($payload ne "") {
187 #We can't handle this yet
188 die "CCS received before message data complete\n";
190 if (!TLSProxy
::Proxy
->is_tls13()) {
192 TLSProxy
::Record
->server_encrypting(1);
194 TLSProxy
::Record
->client_encrypting(1);
197 } elsif ($record->content_type == TLSProxy
::Record
::RT_HANDSHAKE
) {
198 if ($record->len == 0 || $record->len_real == 0) {
199 print " Message truncated\n";
203 if (length $payload > 0) {
204 #We are continuing processing a message started in a previous
205 #record. Add this record to the list associated with this
207 push @message_rec_list, $record;
209 if ($messlen <= length($payload)) {
211 die "Internal error: invalid messlen: ".$messlen
212 ." payload length:".length($payload)."\n";
214 if (length($payload) + $record->decrypt_len >= $messlen) {
215 #We can complete the message with this record
216 $recoffset = $messlen - length($payload);
217 $payload .= substr($record->decrypt_data, 0, $recoffset);
218 push @message_frag_lens, $recoffset;
219 $message = create_message
($server, $mt, $payload,
221 push @messages, $message;
225 #This is just part of the total message
226 $payload .= $record->decrypt_data;
227 $recoffset = $record->decrypt_len;
228 push @message_frag_lens, $record->decrypt_len;
230 print " Partial message data read: ".$recoffset." bytes\n";
233 while ($record->decrypt_len > $recoffset) {
234 #We are at the start of a new message
235 if ($record->decrypt_len - $recoffset < 4) {
236 #Whilst technically probably valid we can't cope with this
237 die "End of record in the middle of a message header\n";
239 @message_rec_list = ($record);
242 ($mt, $lenhi, $lenlo) = unpack('CnC',
243 substr($record->decrypt_data,
245 $messlen = ($lenhi << 8) | $lenlo;
246 print " Message type: $message_type{$mt}\n";
247 print " Message Length: $messlen\n";
248 $startoffset = $recoffset;
252 if ($recoffset <= $record->decrypt_len) {
253 #Some payload data is present in this record
254 if ($record->decrypt_len - $recoffset >= $messlen) {
255 #We can complete the message with this record
256 $payload .= substr($record->decrypt_data, $recoffset,
258 $recoffset += $messlen;
259 push @message_frag_lens, $messlen;
260 $message = create_message
($server, $mt, $payload,
262 push @messages, $message;
266 #This is just part of the total message
267 $payload .= substr($record->decrypt_data, $recoffset,
268 $record->decrypt_len - $recoffset);
269 $recoffset = $record->decrypt_len;
270 push @message_frag_lens, $recoffset;
275 } elsif ($record->content_type == TLSProxy
::Record
::RT_APPLICATION_DATA
) {
276 print " [ENCRYPTED APPLICATION DATA]\n";
277 print " [".$record->decrypt_data."]\n";
279 if ($successondata) {
283 } elsif ($record->content_type == TLSProxy
::Record
::RT_ALERT
) {
284 my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
285 print " [$alertlev, $alertdesc]\n";
286 #A CloseNotify from the client indicates we have finished successfully
288 if (!$end && !$server && $alertlev == AL_LEVEL_WARN
289 && $alertdesc == AL_DESC_CLOSE_NOTIFY
) {
292 #Fatal or close notify alerts end the test
293 if ($alertlev == AL_LEVEL_FATAL
|| $alertdesc == AL_DESC_CLOSE_NOTIFY
) {
296 $alert = TLSProxy
::Alert
->new(
306 #Function to work out which sub-class we need to create and then
310 my ($server, $mt, $data, $startoffset) = @_;
313 #We only support ClientHello in this version...needs to be extended for
315 if ($mt == MT_CLIENT_HELLO
) {
316 $message = TLSProxy
::ClientHello
->new(
324 } elsif ($mt == MT_SERVER_HELLO
) {
325 $message = TLSProxy
::ServerHello
->new(
333 } elsif ($mt == MT_ENCRYPTED_EXTENSIONS
) {
334 $message = TLSProxy
::EncryptedExtensions
->new(
342 } elsif ($mt == MT_CERTIFICATE
) {
343 $message = TLSProxy
::Certificate
->new(
351 } elsif ($mt == MT_CERTIFICATE_REQUEST
) {
352 $message = TLSProxy
::CertificateRequest
->new(
360 } elsif ($mt == MT_CERTIFICATE_VERIFY
) {
361 $message = TLSProxy
::CertificateVerify
->new(
369 } elsif ($mt == MT_SERVER_KEY_EXCHANGE
) {
370 $message = TLSProxy
::ServerKeyExchange
->new(
378 } elsif ($mt == MT_NEW_SESSION_TICKET
) {
379 $message = TLSProxy
::NewSessionTicket
->new(
388 #Unknown message type
389 $message = TLSProxy
::Message
->new(
415 return !$success && $end;
431 $message_frag_lens) = @_;
438 startoffset
=> $startoffset,
439 message_frag_lens
=> $message_frag_lens,
443 return bless $self, $class;
450 $ciphersuite = shift;
455 #Update all the underlying records with the modified data from this message
456 #Note: Only supports TLSv1.3 and ETM encryption
462 my $numrecs = $#{$self->records};
464 $self->set_message_contents();
469 $lenlo = length($self->data) & 0xff;
470 $lenhi = length($self->data) >> 8;
471 $msgdata = pack('CnC', $self->mt, $lenhi, $lenlo).$self->data;
474 #The message is fully contained within one record
475 my ($rec) = @
{$self->records};
476 my $recdata = $rec->decrypt_data;
480 # We use empty message_frag_lens to indicates that pre-repacking,
481 # the message wasn't present. The first fragment length doesn't include
482 # the TLS header, so we need to check and compute the right length.
483 if (@
{$self->message_frag_lens}) {
484 $old_length = ${$self->message_frag_lens}[0] +
485 TLS_MESSAGE_HEADER_LENGTH
;
490 my $prefix = substr($recdata, 0, $self->startoffset);
491 my $suffix = substr($recdata, $self->startoffset + $old_length);
493 $rec->decrypt_data($prefix.($msgdata).($suffix));
494 # TODO(openssl-team): don't keep explicit lengths.
495 # (If a length override is ever needed to construct invalid packets,
496 # use an explicit override field instead.)
497 $rec->decrypt_len(length($rec->decrypt_data));
498 # Only support re-encryption for TLSv1.3 and ETM.
499 if ($rec->encrypted()) {
500 if (TLSProxy
::Proxy
->is_tls13()) {
501 #Add content type (1 byte) and 16 tag bytes
502 $rec->data($rec->decrypt_data
503 .pack("C", TLSProxy
::Record
::RT_HANDSHAKE
).("\0"x16
));
504 } elsif ($rec->etm()) {
505 my $data = $rec->decrypt_data;
507 my $padval = length($data) % 16;
508 $padval = 15 - $padval;
510 $data .= pack("C", $padval);
513 #Add MAC. Assumed to be 20 bytes
514 foreach my $macval (0..19) {
515 $data .= pack("C", $macval);
518 if ($rec->version() >= TLSProxy
::Record
::VERS_TLS_1_1
) {
520 $data = ("\0"x16
).$data;
524 die "Unsupported encryption: No ETM";
527 $rec->data($rec->decrypt_data);
529 $rec->len(length($rec->data));
531 #Update the fragment len in case we changed it above
532 ${$self->message_frag_lens}[0] = length($msgdata)
533 - TLS_MESSAGE_HEADER_LENGTH
;
537 #Note we don't currently support changing a fragmented message length
540 foreach my $rec (@
{$self->records}) {
541 my $recdata = $rec->decrypt_data;
543 #This is the first record
544 my $remainlen = length($recdata) - $self->startoffset;
545 $rec->data(substr($recdata, 0, $self->startoffset)
546 .substr(($msgdata), 0, $remainlen));
547 $datadone += $remainlen;
548 } elsif ($recctr + 1 == $numrecs) {
549 #This is the last record
550 $rec->data(substr($msgdata, $datadone));
552 #This is a middle record
553 $rec->data(substr($msgdata, $datadone, length($rec->data)));
554 $datadone += length($rec->data);
560 #To be overridden by sub-classes
561 sub set_message_contents
569 return $self->{server
};
572 #Read/write accessors
585 $self->{data
} = shift;
587 return $self->{data
};
593 $self->{records
} = shift;
595 return $self->{records
};
601 $self->{startoffset
} = shift;
603 return $self->{startoffset
};
605 sub message_frag_lens
609 $self->{message_frag_lens
} = shift;
611 return $self->{message_frag_lens
};
616 return TLS_MESSAGE_HEADER_LENGTH
+ length($self->data);
622 $self->{dupext
} = shift;
624 return $self->{dupext
};
630 $successondata = shift;
632 return $successondata;