1# Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
2#
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
7
8use strict;
9
10package TLSProxy::EncryptedExtensions;
11
12use vars '@ISA';
13push @ISA, 'TLSProxy::Message';
14
15sub new
16{
17    my $class = shift;
18    my ($isdtls,
19        $server,
20        $msgseq,
21        $msgfrag,
22        $msgfragoffs,
23        $data,
24        $records,
25        $startoffset,
26        $message_frag_lens) = @_;
27
28    my $self = $class->SUPER::new(
29        $isdtls,
30        $server,
31        TLSProxy::Message::MT_ENCRYPTED_EXTENSIONS,
32        $msgseq,
33        $msgfrag,
34        $msgfragoffs,
35        $data,
36        $records,
37        $startoffset,
38        $message_frag_lens);
39
40    $self->{extension_data} = "";
41
42    return $self;
43}
44
45sub parse
46{
47    my $self = shift;
48
49    my $extensions_len = unpack('n', $self->data);
50    if (!defined $extensions_len) {
51        $extensions_len = 0;
52    }
53
54    my $extension_data;
55    if ($extensions_len != 0) {
56        $extension_data = substr($self->data, 2);
57
58        if (length($extension_data) != $extensions_len) {
59            die "Invalid extension length\n";
60        }
61    } else {
62        if (length($self->data) != 2) {
63            die "Invalid extension length\n";
64        }
65        $extension_data = "";
66    }
67    my %extensions = ();
68    while (length($extension_data) >= 4) {
69        my ($type, $size) = unpack("nn", $extension_data);
70        my $extdata = substr($extension_data, 4, $size);
71        $extension_data = substr($extension_data, 4 + $size);
72        $extensions{$type} = $extdata;
73    }
74
75    $self->extension_data(\%extensions);
76
77    print "    Extensions Len:".$extensions_len."\n";
78}
79
80#Reconstruct the on-the-wire message data following changes
81sub set_message_contents
82{
83    my $self = shift;
84    my $data;
85    my $extensions = "";
86
87    foreach my $key (keys %{$self->extension_data}) {
88        my $extdata = ${$self->extension_data}{$key};
89        $extensions .= pack("n", $key);
90        $extensions .= pack("n", length($extdata));
91        $extensions .= $extdata;
92    }
93
94    $data = pack('n', length($extensions));
95    $data .= $extensions;
96    $self->data($data);
97}
98
99#Read/write accessors
100sub extension_data
101{
102    my $self = shift;
103    if (@_) {
104        $self->{extension_data} = shift;
105    }
106    return $self->{extension_data};
107}
108sub set_extension
109{
110    my ($self, $ext_type, $ext_data) = @_;
111    $self->{extension_data}{$ext_type} = $ext_data;
112}
113sub delete_extension
114{
115    my ($self, $ext_type) = @_;
116    delete $self->{extension_data}{$ext_type};
117}
1181;
119