xref: /freebsd/crypto/openssl/util/perl/TLSProxy/CertificateRequest.pm (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1# Copyright 2016 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 ($server,
19        $data,
20        $records,
21        $startoffset,
22        $message_frag_lens) = @_;
23
24    my $self = $class->SUPER::new(
25        $server,
26        TLSProxy::Message::MT_CERTIFICATE_REQUEST,
27        $data,
28        $records,
29        $startoffset,
30        $message_frag_lens);
31
32    $self->{extension_data} = "";
33
34    return $self;
35}
36
37sub parse
38{
39    my $self = shift;
40    my $ptr = 1;
41
42    if (TLSProxy::Proxy->is_tls13()) {
43        my $request_ctx_len = unpack('C', $self->data);
44        my $request_ctx = substr($self->data, $ptr, $request_ctx_len);
45        $ptr += $request_ctx_len;
46
47        my $extensions_len = unpack('n', substr($self->data, $ptr));
48        $ptr += 2;
49        my $extension_data = substr($self->data, $ptr);
50        if (length($extension_data) != $extensions_len) {
51            die "Invalid extension length\n";
52        }
53        my %extensions = ();
54        while (length($extension_data) >= 4) {
55            my ($type, $size) = unpack("nn", $extension_data);
56            my $extdata = substr($extension_data, 4, $size);
57            $extension_data = substr($extension_data, 4 + $size);
58            $extensions{$type} = $extdata;
59        }
60        $self->extension_data(\%extensions);
61
62        print "    Extensions Len:".$extensions_len."\n";
63    }
64    # else parse TLSv1.2 version - we don't support that at the moment
65}
66
67#Reconstruct the on-the-wire message data following changes
68sub set_message_contents
69{
70    my $self = shift;
71    my $data;
72    my $extensions = "";
73
74    foreach my $key (keys %{$self->extension_data}) {
75        my $extdata = ${$self->extension_data}{$key};
76        $extensions .= pack("n", $key);
77        $extensions .= pack("n", length($extdata));
78        $extensions .= $extdata;
79    }
80
81    $data = pack('n', length($extensions));
82    $data .= $extensions;
83    $self->data($data);
84}
85
86#Read/write accessors
87sub extension_data
88{
89    my $self = shift;
90    if (@_) {
91        $self->{extension_data} = shift;
92    }
93    return $self->{extension_data};
94}
95sub set_extension
96{
97    my ($self, $ext_type, $ext_data) = @_;
98    $self->{extension_data}{$ext_type} = $ext_data;
99}
100sub delete_extension
101{
102    my ($self, $ext_type) = @_;
103    delete $self->{extension_data}{$ext_type};
104}
1051;
106