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::CertificateRequest;
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_CERTIFICATE_REQUEST,
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    my $ptr = 1;
49
50    if (TLSProxy::Proxy->is_tls13()) {
51        my $request_ctx_len = unpack('C', $self->data);
52        my $request_ctx = substr($self->data, $ptr, $request_ctx_len);
53        $ptr += $request_ctx_len;
54
55        my $extensions_len = unpack('n', substr($self->data, $ptr));
56        $ptr += 2;
57        my $extension_data = substr($self->data, $ptr);
58        if (length($extension_data) != $extensions_len) {
59            die "Invalid extension length\n";
60        }
61        my %extensions = ();
62        while (length($extension_data) >= 4) {
63            my ($type, $size) = unpack("nn", $extension_data);
64            my $extdata = substr($extension_data, 4, $size);
65            $extension_data = substr($extension_data, 4 + $size);
66            $extensions{$type} = $extdata;
67        }
68        $self->extension_data(\%extensions);
69
70        print "    Extensions Len:".$extensions_len."\n";
71    }
72    # else parse TLSv1.2 version - we don't support that at the moment
73}
74
75#Reconstruct the on-the-wire message data following changes
76sub set_message_contents
77{
78    my $self = shift;
79    my $data;
80    my $extensions = "";
81
82    foreach my $key (keys %{$self->extension_data}) {
83        my $extdata = ${$self->extension_data}{$key};
84        $extensions .= pack("n", $key);
85        $extensions .= pack("n", length($extdata));
86        $extensions .= $extdata;
87    }
88
89    $data = pack('n', length($extensions));
90    $data .= $extensions;
91    $self->data($data);
92}
93
94#Read/write accessors
95sub extension_data
96{
97    my $self = shift;
98    if (@_) {
99        $self->{extension_data} = shift;
100    }
101    return $self->{extension_data};
102}
103sub set_extension
104{
105    my ($self, $ext_type, $ext_data) = @_;
106    $self->{extension_data}{$ext_type} = $ext_data;
107}
108sub delete_extension
109{
110    my ($self, $ext_type) = @_;
111    delete $self->{extension_data}{$ext_type};
112}
1131;
114