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_ENCRYPTED_EXTENSIONS => 8,
22 MT_SERVER_KEY_EXCHANGE => 12,
23 MT_CERTIFICATE_REQUEST => 13,
24 MT_SERVER_HELLO_DONE => 14,
25 MT_CERTIFICATE_VERIFY => 15,
26 MT_CLIENT_KEY_EXCHANGE => 16,
28 MT_CERTIFICATE_STATUS => 22,
40 AL_DESC_CLOSE_NOTIFY => 0,
41 AL_DESC_UNEXPECTED_MESSAGE => 10,
42 AL_DESC_NO_RENEGOTIATION => 100
46 MT_HELLO_REQUEST, "HelloRequest",
47 MT_CLIENT_HELLO, "ClientHello",
48 MT_SERVER_HELLO, "ServerHello",
49 MT_NEW_SESSION_TICKET, "NewSessionTicket",
50 MT_ENCRYPTED_EXTENSIONS, "EncryptedExtensions",
51 MT_CERTIFICATE, "Certificate",
52 MT_SERVER_KEY_EXCHANGE, "ServerKeyExchange",
53 MT_CERTIFICATE_REQUEST, "CertificateRequest",
54 MT_SERVER_HELLO_DONE, "ServerHelloDone",
55 MT_CERTIFICATE_VERIFY, "CertificateVerify",
56 MT_CLIENT_KEY_EXCHANGE, "ClientKeyExchange",
57 MT_FINISHED, "Finished",
58 MT_CERTIFICATE_STATUS, "CertificateStatus",
59 MT_NEXT_PROTO, "NextProto"
64 EXT_MAX_FRAGMENT_LENGTH => 1,
65 EXT_STATUS_REQUEST => 5,
66 EXT_SUPPORTED_GROUPS => 10,
67 EXT_EC_POINT_FORMATS => 11,
74 EXT_ENCRYPT_THEN_MAC => 22,
75 EXT_EXTENDED_MASTER_SECRET => 23,
76 EXT_SESSION_TICKET => 35,
79 EXT_SUPPORTED_VERSIONS => 43,
81 EXT_PSK_KEX_MODES => 45,
82 EXT_RENEGOTIATE => 65281,
84 # This extension is an unofficial extension only ever written by OpenSSL
85 # (i.e. not read), and even then only when enabled. We use it to test
86 # handling of duplicate extensions.
87 EXT_DUPLICATE_EXTENSION => 0xfde8,
88 EXT_UNKNOWN => 0xfffe,
89 #Unknown extension that should appear last
90 EXT_FORCE_LAST => 0xffff
93 # SignatureScheme of TLS 1.3, from
94 # https://tools.ietf.org/html/draft-ietf-tls-tls13-20#appendix-B.3.1.3
95 # TODO(TLS1.3) update link to IANA registry after publication
96 # We have to manually grab the SHA224 equivalents from the old registry
98 SIG_ALG_RSA_PKCS1_SHA256 => 0x0401,
99 SIG_ALG_RSA_PKCS1_SHA384 => 0x0501,
100 SIG_ALG_RSA_PKCS1_SHA512 => 0x0601,
101 SIG_ALG_ECDSA_SECP256R1_SHA256 => 0x0403,
102 SIG_ALG_ECDSA_SECP384R1_SHA384 => 0x0503,
103 SIG_ALG_ECDSA_SECP521R1_SHA512 => 0x0603,
104 SIG_ALG_RSA_PSS_SHA256 => 0x0804,
105 SIG_ALG_RSA_PSS_SHA384 => 0x0805,
106 SIG_ALG_RSA_PSS_SHA512 => 0x0806,
107 SIG_ALG_ED25519 => 0x0807,
108 SIG_ALG_ED448 => 0x0808,
109 SIG_ALG_RSA_PKCS1_SHA1 => 0x0201,
110 SIG_ALG_ECDSA_SHA1 => 0x0203,
111 SIG_ALG_DSA_SHA1 => 0x0202,
112 SIG_ALG_DSA_SHA256 => 0x0402,
113 SIG_ALG_DSA_SHA384 => 0x0502,
114 SIG_ALG_DSA_SHA512 => 0x0602,
115 OSSL_SIG_ALG_RSA_PKCS1_SHA224 => 0x0301,
116 OSSL_SIG_ALG_DSA_SHA224 => 0x0302,
117 OSSL_SIG_ALG_ECDSA_SHA224 => 0x0303
121 CIPHER_DHE_RSA_AES_128_SHA => 0x0033,
122 CIPHER_ADH_AES_128_SHA => 0x0034,
123 CIPHER_TLS13_AES_128_GCM_SHA256 => 0x1301,
124 CIPHER_TLS13_AES_256_GCM_SHA384 => 0x1302
130 my $startoffset = -1;
134 my @message_rec_list = ();
135 my @message_frag_lens = ();
137 my $successondata = 0;
148 @message_rec_list = ();
149 @message_frag_lens = ();
152 #Class method to extract messages from a record
156 my $serverin = shift;
161 @message_frag_lens = ();
163 if ($serverin != $server && length($payload) != 0) {
164 die "Changed peer, but we still have fragment data\n";
168 if ($record->content_type == TLSProxy::Record::RT_CCS) {
169 if ($payload ne "") {
170 #We can't handle this yet
171 die "CCS received before message data complete\n";
173 if (!TLSProxy::Proxy->is_tls13()) {
175 TLSProxy::Record->server_encrypting(1);
177 TLSProxy::Record->client_encrypting(1);
180 } elsif ($record->content_type == TLSProxy::Record::RT_HANDSHAKE) {
181 if ($record->len == 0 || $record->len_real == 0) {
182 print " Message truncated\n";
186 if (length $payload > 0) {
187 #We are continuing processing a message started in a previous
188 #record. Add this record to the list associated with this
190 push @message_rec_list, $record;
192 if ($messlen <= length($payload)) {
194 die "Internal error: invalid messlen: ".$messlen
195 ." payload length:".length($payload)."\n";
197 if (length($payload) + $record->decrypt_len >= $messlen) {
198 #We can complete the message with this record
199 $recoffset = $messlen - length($payload);
200 $payload .= substr($record->decrypt_data, 0, $recoffset);
201 push @message_frag_lens, $recoffset;
202 $message = create_message($server, $mt, $payload,
204 push @messages, $message;
208 #This is just part of the total message
209 $payload .= $record->decrypt_data;
210 $recoffset = $record->decrypt_len;
211 push @message_frag_lens, $record->decrypt_len;
213 print " Partial message data read: ".$recoffset." bytes\n";
216 while ($record->decrypt_len > $recoffset) {
217 #We are at the start of a new message
218 if ($record->decrypt_len - $recoffset < 4) {
219 #Whilst technically probably valid we can't cope with this
220 die "End of record in the middle of a message header\n";
222 @message_rec_list = ($record);
225 ($mt, $lenhi, $lenlo) = unpack('CnC',
226 substr($record->decrypt_data,
228 $messlen = ($lenhi << 8) | $lenlo;
229 print " Message type: $message_type{$mt}\n";
230 print " Message Length: $messlen\n";
231 $startoffset = $recoffset;
235 if ($recoffset <= $record->decrypt_len) {
236 #Some payload data is present in this record
237 if ($record->decrypt_len - $recoffset >= $messlen) {
238 #We can complete the message with this record
239 $payload .= substr($record->decrypt_data, $recoffset,
241 $recoffset += $messlen;
242 push @message_frag_lens, $messlen;
243 $message = create_message($server, $mt, $payload,
245 push @messages, $message;
249 #This is just part of the total message
250 $payload .= substr($record->decrypt_data, $recoffset,
251 $record->decrypt_len - $recoffset);
252 $recoffset = $record->decrypt_len;
253 push @message_frag_lens, $recoffset;
258 } elsif ($record->content_type == TLSProxy::Record::RT_APPLICATION_DATA) {
259 print " [ENCRYPTED APPLICATION DATA]\n";
260 print " [".$record->decrypt_data."]\n";
262 if ($successondata) {
266 } elsif ($record->content_type == TLSProxy::Record::RT_ALERT) {
267 my ($alertlev, $alertdesc) = unpack('CC', $record->decrypt_data);
268 #A CloseNotify from the client indicates we have finished successfully
270 if (!$end && !$server && $alertlev == AL_LEVEL_WARN
271 && $alertdesc == AL_DESC_CLOSE_NOTIFY) {
274 #All alerts end the test
281 #Function to work out which sub-class we need to create and then
285 my ($server, $mt, $data, $startoffset) = @_;
288 #We only support ClientHello in this version...needs to be extended for
290 if ($mt == MT_CLIENT_HELLO) {
291 $message = TLSProxy::ClientHello->new(
299 } elsif ($mt == MT_SERVER_HELLO) {
300 $message = TLSProxy::ServerHello->new(
308 } elsif ($mt == MT_ENCRYPTED_EXTENSIONS) {
309 $message = TLSProxy::EncryptedExtensions->new(
317 } elsif ($mt == MT_CERTIFICATE) {
318 $message = TLSProxy::Certificate->new(
326 } elsif ($mt == MT_CERTIFICATE_VERIFY) {
327 $message = TLSProxy::CertificateVerify->new(
335 } elsif ($mt == MT_SERVER_KEY_EXCHANGE) {
336 $message = TLSProxy::ServerKeyExchange->new(
344 } elsif ($mt == MT_NEW_SESSION_TICKET) {
345 $message = TLSProxy::NewSessionTicket->new(
354 #Unknown message type
355 $message = TLSProxy::Message->new(
381 return !$success && $end;
391 $message_frag_lens) = @_;
398 startoffset => $startoffset,
399 message_frag_lens => $message_frag_lens
402 return bless $self, $class;
409 $ciphersuite = shift;
414 #Update all the underlying records with the modified data from this message
415 #Note: Only supports re-encrypting for TLSv1.3
421 my $numrecs = $#{$self->records};
423 $self->set_message_contents();
428 $lenlo = length($self->data) & 0xff;
429 $lenhi = length($self->data) >> 8;
430 $msgdata = pack('CnC', $self->mt, $lenhi, $lenlo).$self->data;
433 #The message is fully contained within one record
434 my ($rec) = @{$self->records};
435 my $recdata = $rec->decrypt_data;
439 # We use empty message_frag_lens to indicates that pre-repacking,
440 # the message wasn't present. The first fragment length doesn't include
441 # the TLS header, so we need to check and compute the right length.
442 if (@{$self->message_frag_lens}) {
443 $old_length = ${$self->message_frag_lens}[0] +
444 TLS_MESSAGE_HEADER_LENGTH;
449 my $prefix = substr($recdata, 0, $self->startoffset);
450 my $suffix = substr($recdata, $self->startoffset + $old_length);
452 $rec->decrypt_data($prefix.($msgdata).($suffix));
453 # TODO(openssl-team): don't keep explicit lengths.
454 # (If a length override is ever needed to construct invalid packets,
455 # use an explicit override field instead.)
456 $rec->decrypt_len(length($rec->decrypt_data));
457 $rec->len($rec->len + length($msgdata) - $old_length);
458 # Only support re-encryption for TLSv1.3.
459 if (TLSProxy::Proxy->is_tls13() && $rec->encrypted()) {
460 #Add content type (1 byte) and 16 tag bytes
461 $rec->data($rec->decrypt_data
462 .pack("C", TLSProxy::Record::RT_HANDSHAKE).("\0"x16));
464 $rec->data($rec->decrypt_data);
467 #Update the fragment len in case we changed it above
468 ${$self->message_frag_lens}[0] = length($msgdata)
469 - TLS_MESSAGE_HEADER_LENGTH;
473 #Note we don't currently support changing a fragmented message length
476 foreach my $rec (@{$self->records}) {
477 my $recdata = $rec->decrypt_data;
479 #This is the first record
480 my $remainlen = length($recdata) - $self->startoffset;
481 $rec->data(substr($recdata, 0, $self->startoffset)
482 .substr(($msgdata), 0, $remainlen));
483 $datadone += $remainlen;
484 } elsif ($recctr + 1 == $numrecs) {
485 #This is the last record
486 $rec->data(substr($msgdata, $datadone));
488 #This is a middle record
489 $rec->data(substr($msgdata, $datadone, length($rec->data)));
490 $datadone += length($rec->data);
496 #To be overridden by sub-classes
497 sub set_message_contents
505 return $self->{server};
508 #Read/write accessors
521 $self->{data} = shift;
523 return $self->{data};
529 $self->{records} = shift;
531 return $self->{records};
537 $self->{startoffset} = shift;
539 return $self->{startoffset};
541 sub message_frag_lens
545 $self->{message_frag_lens} = shift;
547 return $self->{message_frag_lens};
552 return TLS_MESSAGE_HEADER_LENGTH + length($self->data);
558 $successondata = shift;
560 return $successondata;