1 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 # Licensed under the OpenSSL license (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
;
12 use constant TLS_MESSAGE_HEADER_LENGTH
=> 4;
16 MT_HELLO_REQUEST
=> 0,
19 MT_NEW_SESSION_TICKET
=> 4,
20 MT_HELLO_RETRY_REQUEST
=> 6,
21 MT_ENCRYPTED_EXTENSIONS
=> 8,
23 MT_SERVER_KEY_EXCHANGE
=> 12,
24 MT_CERTIFICATE_REQUEST
=> 13,
25 MT_SERVER_HELLO_DONE
=> 14,
26 MT_CERTIFICATE_VERIFY
=> 15,
27 MT_CLIENT_KEY_EXCHANGE
=> 16,
29 MT_CERTIFICATE_STATUS
=> 22,
41 AL_DESC_CLOSE_NOTIFY
=> 0,
42 AL_DESC_UNEXPECTED_MESSAGE
=> 10,
43 AL_DESC_NO_RENEGOTIATION
=> 100
47 MT_HELLO_REQUEST
, "HelloRequest",
48 MT_CLIENT_HELLO
, "ClientHello",
49 MT_SERVER_HELLO
, "ServerHello",
50 MT_NEW_SESSION_TICKET
, "NewSessionTicket",
51 MT_HELLO_RETRY_REQUEST
, "HelloRetryRequest",
52 MT_ENCRYPTED_EXTENSIONS
, "EncryptedExtensions",
53 MT_CERTIFICATE
, "Certificate",
54 MT_SERVER_KEY_EXCHANGE
, "ServerKeyExchange",
55 MT_CERTIFICATE_REQUEST
, "CertificateRequest",
56 MT_SERVER_HELLO_DONE
, "ServerHelloDone",
57 MT_CERTIFICATE_VERIFY
, "CertificateVerify",
58 MT_CLIENT_KEY_EXCHANGE
, "ClientKeyExchange",
59 MT_FINISHED
, "Finished",
60 MT_CERTIFICATE_STATUS
, "CertificateStatus",
61 MT_NEXT_PROTO
, "NextProto"
66 EXT_STATUS_REQUEST
=> 5,
67 EXT_SUPPORTED_GROUPS
=> 10,
68 EXT_EC_POINT_FORMATS
=> 11,
75 EXT_ENCRYPT_THEN_MAC
=> 22,
76 EXT_EXTENDED_MASTER_SECRET
=> 23,
77 EXT_SESSION_TICKET
=> 35,
80 EXT_SUPPORTED_VERSIONS
=> 43,
82 EXT_PSK_KEX_MODES
=> 45,
83 EXT_RENEGOTIATE
=> 65281,
85 # This extension is an unofficial extension only ever written by OpenSSL
86 # (i.e. not read), and even then only when enabled. We use it to test
87 # handling of duplicate extensions.
88 EXT_DUPLICATE_EXTENSION
=> 0xfde8,
89 EXT_UNKNOWN
=> 0xfffe,
90 #Unknown extension that should appear last
91 EXT_FORCE_LAST
=> 0xffff
94 # SignatureScheme of TLS 1.3, from
95 # https://tools.ietf.org/html/draft-ietf-tls-tls13-20#appendix-B.3.1.3
96 # TODO(TLS1.3) update link to IANA registry after publication
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_SHA256
=> 0x0804,
106 SIG_ALG_RSA_PSS_SHA384
=> 0x0805,
107 SIG_ALG_RSA_PSS_SHA512
=> 0x0806,
108 SIG_ALG_ED25519
=> 0x0807,
109 SIG_ALG_ED448
=> 0x0808,
110 SIG_ALG_RSA_PKCS1_SHA1
=> 0x0201,
111 SIG_ALG_ECDSA_SHA1
=> 0x0203,
112 SIG_ALG_DSA_SHA1
=> 0x0202,
113 SIG_ALG_DSA_SHA256
=> 0x0402,
114 SIG_ALG_DSA_SHA384
=> 0x0502,
115 SIG_ALG_DSA_SHA512
=> 0x0602,
116 OSSL_SIG_ALG_RSA_PKCS1_SHA224
=> 0x0301,
117 OSSL_SIG_ALG_DSA_SHA224
=> 0x0302,
118 OSSL_SIG_ALG_ECDSA_SHA224
=> 0x0303
122 CIPHER_DHE_RSA_AES_128_SHA
=> 0x0033,
123 CIPHER_ADH_AES_128_SHA
=> 0x0034,
124 CIPHER_TLS13_AES_128_GCM_SHA256
=> 0x1301,
125 CIPHER_TLS13_AES_256_GCM_SHA384
=> 0x1302
131 my $startoffset = -1;
135 my @message_rec_list = ();
136 my @message_frag_lens = ();
138 my $successondata = 0;
149 @message_rec_list = ();
150 @message_frag_lens = ();
153 #Class method to extract messages from a record
157 my $serverin = shift;
162 @message_frag_lens = ();
164 if ($serverin != $server && length($payload) != 0) {
165 die "Changed peer, but we still have fragment data\n";
169 if ($record->content_type == TLSProxy
::Record
::RT_CCS
) {
170 if ($payload ne "") {
171 #We can't handle this yet
172 die "CCS received before message data complete\n";
175 TLSProxy
::Record
->server_encrypting(1);
177 TLSProxy
::Record
->client_encrypting(1);
179 } elsif ($record->content_type == TLSProxy
::Record
::RT_HANDSHAKE
) {
180 if ($record->len == 0 || $record->len_real == 0) {
181 print " Message truncated\n";
185 if (length $payload > 0) {
186 #We are continuing processing a message started in a previous
187 #record. Add this record to the list associated with this
189 push @message_rec_list, $record;
191 if ($messlen <= length($payload)) {
193 die "Internal error: invalid messlen: ".$messlen
194 ." payload length:".length($payload)."\n";
196 if (length($payload) + $record->decrypt_len >= $messlen) {
197 #We can complete the message with this record
198 $recoffset = $messlen - length($payload);
199 $payload .= substr($record->decrypt_data, 0, $recoffset);
200 push @message_frag_lens, $recoffset;
201 $message = create_message
($server, $mt, $payload,
203 push @messages, $message;
207 #This is just part of the total message
208 $payload .= $record->decrypt_data;
209 $recoffset = $record->decrypt_len;
210 push @message_frag_lens, $record->decrypt_len;
212 print " Partial message data read: ".$recoffset." bytes\n";
215 while ($record->decrypt_len > $recoffset) {
216 #We are at the start of a new message
217 if ($record->decrypt_len - $recoffset < 4) {
218 #Whilst technically probably valid we can't cope with this
219 die "End of record in the middle of a message header\n";
221 @message_rec_list = ($record);
224 ($mt, $lenhi, $lenlo) = unpack('CnC',
225 substr($record->decrypt_data,
227 $messlen = ($lenhi << 8) | $lenlo;
228 print " Message type: $message_type{$mt}\n";
229 print " Message Length: $messlen\n";
230 $startoffset = $recoffset;
234 if ($recoffset <= $record->decrypt_len) {
235 #Some payload data is present in this record
236 if ($record->decrypt_len - $recoffset >= $messlen) {
237 #We can complete the message with this record
238 $payload .= substr($record->decrypt_data, $recoffset,
240 $recoffset += $messlen;
241 push @message_frag_lens, $messlen;
242 $message = create_message
($server, $mt, $payload,
244 push @messages, $message;
248 #This is just part of the total message
249 $payload .= substr($record->decrypt_data, $recoffset,
250 $record->decrypt_len - $recoffset);
251 $recoffset = $record->decrypt_len;
252 push @message_frag_lens, $recoffset;
257 } elsif ($record->content_type == TLSProxy
::Record
::RT_APPLICATION_DATA
) {
258 print " [ENCRYPTED APPLICATION DATA]\n";
259 print " [".$record->decrypt_data."]\n";
261 if ($successondata) {
265 } elsif ($record->content_type == TLSProxy
::Record
::RT_ALERT
) {
266 my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
267 #A CloseNotify from the client indicates we have finished successfully
269 if (!$end && !$server && $alertlev == AL_LEVEL_WARN
270 && $alertdesc == AL_DESC_CLOSE_NOTIFY
) {
273 #All alerts end the test
280 #Function to work out which sub-class we need to create and then
284 my ($server, $mt, $data, $startoffset) = @_;
287 #We only support ClientHello in this version...needs to be extended for
289 if ($mt == MT_CLIENT_HELLO
) {
290 $message = TLSProxy
::ClientHello
->new(
298 } elsif ($mt == MT_HELLO_RETRY_REQUEST
) {
299 $message = TLSProxy
::HelloRetryRequest
->new(
307 } elsif ($mt == MT_SERVER_HELLO
) {
308 $message = TLSProxy
::ServerHello
->new(
316 } elsif ($mt == MT_ENCRYPTED_EXTENSIONS
) {
317 $message = TLSProxy
::EncryptedExtensions
->new(
325 } elsif ($mt == MT_CERTIFICATE
) {
326 $message = TLSProxy
::Certificate
->new(
334 } elsif ($mt == MT_CERTIFICATE_VERIFY
) {
335 $message = TLSProxy
::CertificateVerify
->new(
343 } elsif ($mt == MT_SERVER_KEY_EXCHANGE
) {
344 $message = TLSProxy
::ServerKeyExchange
->new(
352 } elsif ($mt == MT_NEW_SESSION_TICKET
) {
353 $message = TLSProxy
::NewSessionTicket
->new(
362 #Unknown message type
363 $message = TLSProxy
::Message
->new(
389 return !$success && $end;
399 $message_frag_lens) = @_;
406 startoffset
=> $startoffset,
407 message_frag_lens
=> $message_frag_lens
410 return bless $self, $class;
417 $ciphersuite = shift;
422 #Update all the underlying records with the modified data from this message
423 #Note: Only supports re-encrypting for TLSv1.3
429 my $numrecs = $#{$self->records};
431 $self->set_message_contents();
436 $lenlo = length($self->data) & 0xff;
437 $lenhi = length($self->data) >> 8;
438 $msgdata = pack('CnC', $self->mt, $lenhi, $lenlo).$self->data;
441 #The message is fully contained within one record
442 my ($rec) = @
{$self->records};
443 my $recdata = $rec->decrypt_data;
447 # We use empty message_frag_lens to indicates that pre-repacking,
448 # the message wasn't present. The first fragment length doesn't include
449 # the TLS header, so we need to check and compute the right length.
450 if (@
{$self->message_frag_lens}) {
451 $old_length = ${$self->message_frag_lens}[0] +
452 TLS_MESSAGE_HEADER_LENGTH
;
457 my $prefix = substr($recdata, 0, $self->startoffset);
458 my $suffix = substr($recdata, $self->startoffset + $old_length);
460 $rec->decrypt_data($prefix.($msgdata).($suffix));
461 # TODO(openssl-team): don't keep explicit lengths.
462 # (If a length override is ever needed to construct invalid packets,
463 # use an explicit override field instead.)
464 $rec->decrypt_len(length($rec->decrypt_data));
465 $rec->len($rec->len + length($msgdata) - $old_length);
466 # Only support re-encryption for TLSv1.3.
467 if (TLSProxy
::Proxy
->is_tls13() && $rec->encrypted()) {
468 #Add content type (1 byte) and 16 tag bytes
469 $rec->data($rec->decrypt_data
470 .pack("C", TLSProxy
::Record
::RT_HANDSHAKE
).("\0"x16
));
472 $rec->data($rec->decrypt_data);
475 #Update the fragment len in case we changed it above
476 ${$self->message_frag_lens}[0] = length($msgdata)
477 - TLS_MESSAGE_HEADER_LENGTH
;
481 #Note we don't currently support changing a fragmented message length
484 foreach my $rec (@
{$self->records}) {
485 my $recdata = $rec->decrypt_data;
487 #This is the first record
488 my $remainlen = length($recdata) - $self->startoffset;
489 $rec->data(substr($recdata, 0, $self->startoffset)
490 .substr(($msgdata), 0, $remainlen));
491 $datadone += $remainlen;
492 } elsif ($recctr + 1 == $numrecs) {
493 #This is the last record
494 $rec->data(substr($msgdata, $datadone));
496 #This is a middle record
497 $rec->data(substr($msgdata, $datadone, length($rec->data)));
498 $datadone += length($rec->data);
504 #To be overridden by sub-classes
505 sub set_message_contents
513 return $self->{server
};
516 #Read/write accessors
529 $self->{data
} = shift;
531 return $self->{data
};
537 $self->{records
} = shift;
539 return $self->{records
};
545 $self->{startoffset
} = shift;
547 return $self->{startoffset
};
549 sub message_frag_lens
553 $self->{message_frag_lens
} = shift;
555 return $self->{message_frag_lens
};
560 return TLS_MESSAGE_HEADER_LENGTH
+ length($self->data);
566 $successondata = shift;
568 return $successondata;