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