1 # Copyright 2016-2018 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,
42 AL_DESC_CLOSE_NOTIFY
=> 0,
43 AL_DESC_UNEXPECTED_MESSAGE
=> 10,
44 AL_DESC_ILLEGAL_PARAMETER
=> 47,
45 AL_DESC_NO_RENEGOTIATION
=> 100
49 MT_HELLO_REQUEST
, "HelloRequest",
50 MT_CLIENT_HELLO
, "ClientHello",
51 MT_SERVER_HELLO
, "ServerHello",
52 MT_NEW_SESSION_TICKET
, "NewSessionTicket",
53 MT_ENCRYPTED_EXTENSIONS
, "EncryptedExtensions",
54 MT_CERTIFICATE
, "Certificate",
55 MT_SERVER_KEY_EXCHANGE
, "ServerKeyExchange",
56 MT_CERTIFICATE_REQUEST
, "CertificateRequest",
57 MT_SERVER_HELLO_DONE
, "ServerHelloDone",
58 MT_CERTIFICATE_VERIFY
, "CertificateVerify",
59 MT_CLIENT_KEY_EXCHANGE
, "ClientKeyExchange",
60 MT_FINISHED
, "Finished",
61 MT_CERTIFICATE_STATUS
, "CertificateStatus",
62 MT_NEXT_PROTO
, "NextProto"
67 EXT_MAX_FRAGMENT_LENGTH
=> 1,
68 EXT_STATUS_REQUEST
=> 5,
69 EXT_SUPPORTED_GROUPS
=> 10,
70 EXT_EC_POINT_FORMATS
=> 11,
77 EXT_ENCRYPT_THEN_MAC
=> 22,
78 EXT_EXTENDED_MASTER_SECRET
=> 23,
79 EXT_SESSION_TICKET
=> 35,
82 EXT_SUPPORTED_VERSIONS
=> 43,
84 EXT_PSK_KEX_MODES
=> 45,
85 EXT_POST_HANDSHAKE_AUTH
=> 49,
86 EXT_SIG_ALGS_CERT
=> 50,
87 EXT_RENEGOTIATE
=> 65281,
89 EXT_CRYPTOPRO_BUG_EXTENSION
=> 0xfde8,
90 EXT_UNKNOWN
=> 0xfffe,
91 #Unknown extension that should appear last
92 EXT_FORCE_LAST
=> 0xffff
95 # SignatureScheme of TLS 1.3 from:
96 # https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-signaturescheme
97 # We have to manually grab the SHA224 equivalents from the old registry
99 SIG_ALG_RSA_PKCS1_SHA256
=> 0x0401,
100 SIG_ALG_RSA_PKCS1_SHA384
=> 0x0501,
101 SIG_ALG_RSA_PKCS1_SHA512
=> 0x0601,
102 SIG_ALG_ECDSA_SECP256R1_SHA256
=> 0x0403,
103 SIG_ALG_ECDSA_SECP384R1_SHA384
=> 0x0503,
104 SIG_ALG_ECDSA_SECP521R1_SHA512
=> 0x0603,
105 SIG_ALG_RSA_PSS_RSAE_SHA256
=> 0x0804,
106 SIG_ALG_RSA_PSS_RSAE_SHA384
=> 0x0805,
107 SIG_ALG_RSA_PSS_RSAE_SHA512
=> 0x0806,
108 SIG_ALG_ED25519
=> 0x0807,
109 SIG_ALG_ED448
=> 0x0808,
110 SIG_ALG_RSA_PSS_PSS_SHA256
=> 0x0809,
111 SIG_ALG_RSA_PSS_PSS_SHA384
=> 0x080a,
112 SIG_ALG_RSA_PSS_PSS_SHA512
=> 0x080b,
113 SIG_ALG_RSA_PKCS1_SHA1
=> 0x0201,
114 SIG_ALG_ECDSA_SHA1
=> 0x0203,
115 SIG_ALG_DSA_SHA1
=> 0x0202,
116 SIG_ALG_DSA_SHA256
=> 0x0402,
117 SIG_ALG_DSA_SHA384
=> 0x0502,
118 SIG_ALG_DSA_SHA512
=> 0x0602,
119 OSSL_SIG_ALG_RSA_PKCS1_SHA224
=> 0x0301,
120 OSSL_SIG_ALG_DSA_SHA224
=> 0x0302,
121 OSSL_SIG_ALG_ECDSA_SHA224
=> 0x0303
125 CIPHER_RSA_WITH_AES_128_CBC_SHA
=> 0x002f,
126 CIPHER_DHE_RSA_AES_128_SHA
=> 0x0033,
127 CIPHER_ADH_AES_128_SHA
=> 0x0034,
128 CIPHER_TLS13_AES_128_GCM_SHA256
=> 0x1301,
129 CIPHER_TLS13_AES_256_GCM_SHA384
=> 0x1302
135 my $startoffset = -1;
139 my @message_rec_list = ();
140 my @message_frag_lens = ();
142 my $successondata = 0;
154 @message_rec_list = ();
155 @message_frag_lens = ();
159 #Class method to extract messages from a record
163 my $serverin = shift;
168 @message_frag_lens = ();
170 if ($serverin != $server && length($payload) != 0) {
171 die "Changed peer, but we still have fragment data\n";
175 if ($record->content_type == TLSProxy
::Record
::RT_CCS
) {
176 if ($payload ne "") {
177 #We can't handle this yet
178 die "CCS received before message data complete\n";
180 if (!TLSProxy
::Proxy
->is_tls13()) {
182 TLSProxy
::Record
->server_encrypting(1);
184 TLSProxy
::Record
->client_encrypting(1);
187 } elsif ($record->content_type == TLSProxy
::Record
::RT_HANDSHAKE
) {
188 if ($record->len == 0 || $record->len_real == 0) {
189 print " Message truncated\n";
193 if (length $payload > 0) {
194 #We are continuing processing a message started in a previous
195 #record. Add this record to the list associated with this
197 push @message_rec_list, $record;
199 if ($messlen <= length($payload)) {
201 die "Internal error: invalid messlen: ".$messlen
202 ." payload length:".length($payload)."\n";
204 if (length($payload) + $record->decrypt_len >= $messlen) {
205 #We can complete the message with this record
206 $recoffset = $messlen - length($payload);
207 $payload .= substr($record->decrypt_data, 0, $recoffset);
208 push @message_frag_lens, $recoffset;
209 $message = create_message
($server, $mt, $payload,
211 push @messages, $message;
215 #This is just part of the total message
216 $payload .= $record->decrypt_data;
217 $recoffset = $record->decrypt_len;
218 push @message_frag_lens, $record->decrypt_len;
220 print " Partial message data read: ".$recoffset." bytes\n";
223 while ($record->decrypt_len > $recoffset) {
224 #We are at the start of a new message
225 if ($record->decrypt_len - $recoffset < 4) {
226 #Whilst technically probably valid we can't cope with this
227 die "End of record in the middle of a message header\n";
229 @message_rec_list = ($record);
232 ($mt, $lenhi, $lenlo) = unpack('CnC',
233 substr($record->decrypt_data,
235 $messlen = ($lenhi << 8) | $lenlo;
236 print " Message type: $message_type{$mt}\n";
237 print " Message Length: $messlen\n";
238 $startoffset = $recoffset;
242 if ($recoffset <= $record->decrypt_len) {
243 #Some payload data is present in this record
244 if ($record->decrypt_len - $recoffset >= $messlen) {
245 #We can complete the message with this record
246 $payload .= substr($record->decrypt_data, $recoffset,
248 $recoffset += $messlen;
249 push @message_frag_lens, $messlen;
250 $message = create_message
($server, $mt, $payload,
252 push @messages, $message;
256 #This is just part of the total message
257 $payload .= substr($record->decrypt_data, $recoffset,
258 $record->decrypt_len - $recoffset);
259 $recoffset = $record->decrypt_len;
260 push @message_frag_lens, $recoffset;
265 } elsif ($record->content_type == TLSProxy
::Record
::RT_APPLICATION_DATA
) {
266 print " [ENCRYPTED APPLICATION DATA]\n";
267 print " [".$record->decrypt_data."]\n";
269 if ($successondata) {
273 } elsif ($record->content_type == TLSProxy
::Record
::RT_ALERT
) {
274 my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
275 print " [$alertlev, $alertdesc]\n";
276 #A CloseNotify from the client indicates we have finished successfully
278 if (!$end && !$server && $alertlev == AL_LEVEL_WARN
279 && $alertdesc == AL_DESC_CLOSE_NOTIFY
) {
282 #Fatal or close notify alerts end the test
283 if ($alertlev == AL_LEVEL_FATAL
|| $alertdesc == AL_DESC_CLOSE_NOTIFY
) {
286 $alert = TLSProxy
::Alert
->new(
296 #Function to work out which sub-class we need to create and then
300 my ($server, $mt, $data, $startoffset) = @_;
303 #We only support ClientHello in this version...needs to be extended for
305 if ($mt == MT_CLIENT_HELLO
) {
306 $message = TLSProxy
::ClientHello
->new(
314 } elsif ($mt == MT_SERVER_HELLO
) {
315 $message = TLSProxy
::ServerHello
->new(
323 } elsif ($mt == MT_ENCRYPTED_EXTENSIONS
) {
324 $message = TLSProxy
::EncryptedExtensions
->new(
332 } elsif ($mt == MT_CERTIFICATE
) {
333 $message = TLSProxy
::Certificate
->new(
341 } elsif ($mt == MT_CERTIFICATE_VERIFY
) {
342 $message = TLSProxy
::CertificateVerify
->new(
350 } elsif ($mt == MT_SERVER_KEY_EXCHANGE
) {
351 $message = TLSProxy
::ServerKeyExchange
->new(
359 } elsif ($mt == MT_NEW_SESSION_TICKET
) {
360 $message = TLSProxy
::NewSessionTicket
->new(
369 #Unknown message type
370 $message = TLSProxy
::Message
->new(
396 return !$success && $end;
412 $message_frag_lens) = @_;
419 startoffset
=> $startoffset,
420 message_frag_lens
=> $message_frag_lens,
424 return bless $self, $class;
431 $ciphersuite = shift;
436 #Update all the underlying records with the modified data from this message
437 #Note: Only supports re-encrypting for TLSv1.3
443 my $numrecs = $#{$self->records};
445 $self->set_message_contents();
450 $lenlo = length($self->data) & 0xff;
451 $lenhi = length($self->data) >> 8;
452 $msgdata = pack('CnC', $self->mt, $lenhi, $lenlo).$self->data;
455 #The message is fully contained within one record
456 my ($rec) = @
{$self->records};
457 my $recdata = $rec->decrypt_data;
461 # We use empty message_frag_lens to indicates that pre-repacking,
462 # the message wasn't present. The first fragment length doesn't include
463 # the TLS header, so we need to check and compute the right length.
464 if (@
{$self->message_frag_lens}) {
465 $old_length = ${$self->message_frag_lens}[0] +
466 TLS_MESSAGE_HEADER_LENGTH
;
471 my $prefix = substr($recdata, 0, $self->startoffset);
472 my $suffix = substr($recdata, $self->startoffset + $old_length);
474 $rec->decrypt_data($prefix.($msgdata).($suffix));
475 # TODO(openssl-team): don't keep explicit lengths.
476 # (If a length override is ever needed to construct invalid packets,
477 # use an explicit override field instead.)
478 $rec->decrypt_len(length($rec->decrypt_data));
479 $rec->len($rec->len + length($msgdata) - $old_length);
480 # Only support re-encryption for TLSv1.3.
481 if (TLSProxy
::Proxy
->is_tls13() && $rec->encrypted()) {
482 #Add content type (1 byte) and 16 tag bytes
483 $rec->data($rec->decrypt_data
484 .pack("C", TLSProxy
::Record
::RT_HANDSHAKE
).("\0"x16
));
486 $rec->data($rec->decrypt_data);
489 #Update the fragment len in case we changed it above
490 ${$self->message_frag_lens}[0] = length($msgdata)
491 - TLS_MESSAGE_HEADER_LENGTH
;
495 #Note we don't currently support changing a fragmented message length
498 foreach my $rec (@
{$self->records}) {
499 my $recdata = $rec->decrypt_data;
501 #This is the first record
502 my $remainlen = length($recdata) - $self->startoffset;
503 $rec->data(substr($recdata, 0, $self->startoffset)
504 .substr(($msgdata), 0, $remainlen));
505 $datadone += $remainlen;
506 } elsif ($recctr + 1 == $numrecs) {
507 #This is the last record
508 $rec->data(substr($msgdata, $datadone));
510 #This is a middle record
511 $rec->data(substr($msgdata, $datadone, length($rec->data)));
512 $datadone += length($rec->data);
518 #To be overridden by sub-classes
519 sub set_message_contents
527 return $self->{server
};
530 #Read/write accessors
543 $self->{data
} = shift;
545 return $self->{data
};
551 $self->{records
} = shift;
553 return $self->{records
};
559 $self->{startoffset
} = shift;
561 return $self->{startoffset
};
563 sub message_frag_lens
567 $self->{message_frag_lens
} = shift;
569 return $self->{message_frag_lens
};
574 return TLS_MESSAGE_HEADER_LENGTH
+ length($self->data);
580 $self->{dupext
} = shift;
582 return $self->{dupext
};
588 $successondata = shift;
590 return $successondata;