1e1051a39Sopenharmony_ci# Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
2e1051a39Sopenharmony_ci#
3e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
4e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
5e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
6e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
7e1051a39Sopenharmony_ci
8e1051a39Sopenharmony_ciuse strict;
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_cipackage TLSProxy::CertificateRequest;
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ciuse vars '@ISA';
13e1051a39Sopenharmony_cipush @ISA, 'TLSProxy::Message';
14e1051a39Sopenharmony_ci
15e1051a39Sopenharmony_cisub new
16e1051a39Sopenharmony_ci{
17e1051a39Sopenharmony_ci    my $class = shift;
18e1051a39Sopenharmony_ci    my ($server,
19e1051a39Sopenharmony_ci        $data,
20e1051a39Sopenharmony_ci        $records,
21e1051a39Sopenharmony_ci        $startoffset,
22e1051a39Sopenharmony_ci        $message_frag_lens) = @_;
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ci    my $self = $class->SUPER::new(
25e1051a39Sopenharmony_ci        $server,
26e1051a39Sopenharmony_ci        TLSProxy::Message::MT_CERTIFICATE_REQUEST,
27e1051a39Sopenharmony_ci        $data,
28e1051a39Sopenharmony_ci        $records,
29e1051a39Sopenharmony_ci        $startoffset,
30e1051a39Sopenharmony_ci        $message_frag_lens);
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ci    $self->{extension_data} = "";
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci    return $self;
35e1051a39Sopenharmony_ci}
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cisub parse
38e1051a39Sopenharmony_ci{
39e1051a39Sopenharmony_ci    my $self = shift;
40e1051a39Sopenharmony_ci    my $ptr = 1;
41e1051a39Sopenharmony_ci
42e1051a39Sopenharmony_ci    if (TLSProxy::Proxy->is_tls13()) {
43e1051a39Sopenharmony_ci        my $request_ctx_len = unpack('C', $self->data);
44e1051a39Sopenharmony_ci        my $request_ctx = substr($self->data, $ptr, $request_ctx_len);
45e1051a39Sopenharmony_ci        $ptr += $request_ctx_len;
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ci        my $extensions_len = unpack('n', substr($self->data, $ptr));
48e1051a39Sopenharmony_ci        $ptr += 2;
49e1051a39Sopenharmony_ci        my $extension_data = substr($self->data, $ptr);
50e1051a39Sopenharmony_ci        if (length($extension_data) != $extensions_len) {
51e1051a39Sopenharmony_ci            die "Invalid extension length\n";
52e1051a39Sopenharmony_ci        }
53e1051a39Sopenharmony_ci        my %extensions = ();
54e1051a39Sopenharmony_ci        while (length($extension_data) >= 4) {
55e1051a39Sopenharmony_ci            my ($type, $size) = unpack("nn", $extension_data);
56e1051a39Sopenharmony_ci            my $extdata = substr($extension_data, 4, $size);
57e1051a39Sopenharmony_ci            $extension_data = substr($extension_data, 4 + $size);
58e1051a39Sopenharmony_ci            $extensions{$type} = $extdata;
59e1051a39Sopenharmony_ci        }
60e1051a39Sopenharmony_ci        $self->extension_data(\%extensions);
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci        print "    Extensions Len:".$extensions_len."\n";
63e1051a39Sopenharmony_ci    }
64e1051a39Sopenharmony_ci    # else parse TLSv1.2 version - we don't support that at the moment
65e1051a39Sopenharmony_ci}
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci#Reconstruct the on-the-wire message data following changes
68e1051a39Sopenharmony_cisub set_message_contents
69e1051a39Sopenharmony_ci{
70e1051a39Sopenharmony_ci    my $self = shift;
71e1051a39Sopenharmony_ci    my $data;
72e1051a39Sopenharmony_ci    my $extensions = "";
73e1051a39Sopenharmony_ci
74e1051a39Sopenharmony_ci    foreach my $key (keys %{$self->extension_data}) {
75e1051a39Sopenharmony_ci        my $extdata = ${$self->extension_data}{$key};
76e1051a39Sopenharmony_ci        $extensions .= pack("n", $key);
77e1051a39Sopenharmony_ci        $extensions .= pack("n", length($extdata));
78e1051a39Sopenharmony_ci        $extensions .= $extdata;
79e1051a39Sopenharmony_ci    }
80e1051a39Sopenharmony_ci
81e1051a39Sopenharmony_ci    $data = pack('n', length($extensions));
82e1051a39Sopenharmony_ci    $data .= $extensions;
83e1051a39Sopenharmony_ci    $self->data($data);
84e1051a39Sopenharmony_ci}
85e1051a39Sopenharmony_ci
86e1051a39Sopenharmony_ci#Read/write accessors
87e1051a39Sopenharmony_cisub extension_data
88e1051a39Sopenharmony_ci{
89e1051a39Sopenharmony_ci    my $self = shift;
90e1051a39Sopenharmony_ci    if (@_) {
91e1051a39Sopenharmony_ci        $self->{extension_data} = shift;
92e1051a39Sopenharmony_ci    }
93e1051a39Sopenharmony_ci    return $self->{extension_data};
94e1051a39Sopenharmony_ci}
95e1051a39Sopenharmony_cisub set_extension
96e1051a39Sopenharmony_ci{
97e1051a39Sopenharmony_ci    my ($self, $ext_type, $ext_data) = @_;
98e1051a39Sopenharmony_ci    $self->{extension_data}{$ext_type} = $ext_data;
99e1051a39Sopenharmony_ci}
100e1051a39Sopenharmony_cisub delete_extension
101e1051a39Sopenharmony_ci{
102e1051a39Sopenharmony_ci    my ($self, $ext_type) = @_;
103e1051a39Sopenharmony_ci    delete $self->{extension_data}{$ext_type};
104e1051a39Sopenharmony_ci}
105e1051a39Sopenharmony_ci1;
106