script/ssltest.pl untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 239 CC 8 SCAR 32.6
CC
8
Cov
0%
check_certpinning
56.0
show_usage
6.0
BEGIN
2.0
CRAP 26.1
1 #!/usr/local/bin/perl
2 use strict;
3 use warnings;
4 use URI;
5 use Getopt::Long;
6 use FindBin;
7 use lib "$FindBin::Bin/../lib";
8 use LightTCP::SSLclient qw(ECONNECT ETIMEOUT ESSL);
9  
10 my %opts = (
11 verbose => 0,
12 insecure => 0,
13 json => 0,
14 save => 0,
15 timeout => 15,
16 type => 'get',
17 cert => undef,
18 dir => '',
19 file => '',
20 proxy => '',
21 proxyauth => '',
22 header => [],
23 keep_alive => 0,
24 follow_redirects => 1,
25 max_redirects => 5,
26 );
27  
28 GetOptions(
29 'v+' => \$opts{verbose},
30 'insecure' => \$opts{insecure},
31 'json' => \$opts{json},
32 'save' => \$opts{save},
33 'timeout=i' => \$opts{timeout},
34 'type=s' => \$opts{type},
35 'cert=s' => \$opts{cert},
36 'dir=s' => \$opts{dir},
37 'file=s' => \$opts{file},
38 'proxy=s' => \$opts{proxy},
39 'proxyauth=s' => \$opts{proxyauth},
40 'header=s' => \@{$opts{header}},
41 'keep-alive' => \$opts{keep_alive},
42 'no-redirect' => sub { $opts{follow_redirects} = 0 },
43 ) or show_usage();
44  
45 my $url = $ARGV[0] // '';
46 if (substr($url, 0, 8) ne 'https://') {
47 show_usage("ERROR: You must always provide a full URL as the last argument");
48 }
49  
50 my $uri = URI->new($url);
51 die "ERROR: Invalid URL: $url\n" unless ($uri->scheme && $uri->host);
52 my $host = $uri->host;
53 my $port = $uri->port || 443;
54 my $path = $uri->path_query || '/';
55  
56 $opts{type} = 'post' if ($opts{file} && $opts{type} eq 'get');
57 unless ($opts{type} =~ /^(get|post|put|delete)$/) {
58 show_usage("ERROR: Invalid -type. Must be get, post, put, or delete");
59 }
60 my $method = uc $opts{type};
61  
62 my $client = LightTCP::SSLclient->new(
63 timeout => $opts{timeout},
64 insecure => $opts{insecure},
65 cert => $opts{cert},
66 verbose => $opts{verbose},
67 keep_alive => $opts{keep_alive},
68 follow_redirects => $opts{follow_redirects},
69 max_redirects => $opts{max_redirects},
70 );
71  
72 my ($ok, $errors, $debug, $error_code) = $client->connect($host, $port, $opts{proxy}, $opts{proxyauth});
73 print join('', @$debug) if $opts{verbose};
74 print join('', @$errors) if @$errors;
75 if (!$ok) {
76 my $err_type = $error_code == ECONNECT ? 'Connection' :
77 $error_code == ETIMEOUT ? 'Timeout' :
78 $error_code == ESSL ? 'SSL' : 'Connection';
79 die "- ERROR: $err_type error. Failed to establish SSL connection.\n";
80 }
81  
82 check_certpinning($client, $host, $port, $opts{dir}, $opts{save}) if $opts{dir} ne '';
83  
84 my %hdr;
85 for my $h (@{$opts{header}}) {
86 if ($h =~ /^\s*([^:]+)\s*:\s*(.+)\s*$/) {
87 $hdr{$1} = $2;
88 } elsif ($opts{verbose}) {
89 print "# WARNING: Invalid format (skipping): $h\n";
90 }
91 }
92  
93 my $payload = undef;
94 if ($opts{file} && -r $opts{file}) {
95 print "- adding file: $opts{file}\n" if $opts{verbose} > 1;
96 open my $fh, '<:raw', $opts{file} or die "Cannot open '$opts{file}': $!\n";
97 $payload = do { local $/; <$fh> };
98 close $fh;
99 }
100  
101 if ($method eq 'PUT') {
102 my ($filename) = $opts{file} =~ m{([^/\\]+)$} or die "Cannot extract filename from $opts{file}";
103 $filename //= 'uploaded.file';
104 $hdr{'Content-Type'} = 'application/octet-stream';
105 $hdr{'Content-Disposition'} = "attachment; filename=\"$filename\"";
106 } elsif ($opts{json}) {
107 $hdr{'Content-Type'} ||= 'application/json';
108 $hdr{'Accept'} ||= 'application/json';
109 }
110 $hdr{'Content-Type'} = 'application/json' if $opts{json};
111  
112 ($ok, $errors, $debug, $error_code) = $client->request($method, $path,
113 host => $host,
114 payload => $payload,
115 headers => \%hdr,
116 );
117 print join('', @$debug) if $opts{verbose};
118 print join('', @$errors) if @$errors;
119 if (!$ok) {
120 my $err_type = $error_code == ETIMEOUT ? 'Timeout' : 'Request';
121 die "- ERROR: $err_type error. Unable to do the request\n";
122 }
123  
124 my ($code, $state, $headers, $body, $resp_errors, $resp_debug, $resp_code) = $client->response();
125 print join('', @$resp_debug) if $opts{verbose};
126 print join('', @$resp_errors) if @$resp_errors;
127 $client->close();
128  
129 if ($code) {
130 if ($opts{verbose} > 1) {
131 print "# === Headers: ===\n";
132 for my $k (sort keys %$headers) {
133 printf "- %-30s: %s\n", $k, $headers->{$k};
134 }
135 }
136 print "# === Body: ===\n" if $opts{verbose};
137 print $body;
138 print "\n" unless $body =~ /\n\z/;
139 }
140 exit 0;
141  
142 sub check_certpinning {
143 my ($client, $host, $port, $fingerprint_dir, $save) = @_;
144  
145 my $socket = $client->socket();
146 my $actual_fp = $socket->get_fingerprint('sha256');
147 print "- Certificate fingerprint: $actual_fp\n" if $opts{verbose} > 1;
148  
149 my $expected_fp = $client->fingerprint_read($fingerprint_dir, $host, $port);
150 if ($expected_fp eq $actual_fp) {
151 print "- Same as the expected fingerprint\n" if $opts{verbose} > 1;
152 } else {
153 my ($err, $pferrors, $pfdebug, $pfcode) = $client->fingerprint_save($fingerprint_dir, $host, $port, $actual_fp, $save);
154 print join('', @$pfdebug) if $opts{verbose};
155 print join('', @$pferrors) if @$pferrors;
156 if ($save && $expected_fp) {
157 print "- ERROR: FINGERPRINT MISMATCH!\n Expected: $expected_fp\n Actual: $actual_fp\n";
158 $client->close();
159 exit 1;
160 }
161 }
162 }
163  
164 sub show_usage {
165 my $msg = shift // '';
166 print "$msg\n\n" if $msg;
167  
168 my @progparts = split(/\//, $0);
169 my $prognm = pop @progparts;
170 print <<"EOF";
171 Usage : $prognm [options] <https://host/path>
172 Options:
173 -v Verbose mode (repeat for more verbosity)
174 --timeout default 15 seconds timeout for the request
175 --json add Content-Type: application/json
176 --type <get|post|put|delete> HTTP method (default: get)
177 --file <filename> Upload file -> forces method to POST
178 --header "Name: Value" Custom header (can be repeated)
179 --proxy <host:port> HTTP CONNECT proxy
180 --proxyauth <user:pass> Proxy Basic authentication
181 --cert <client base filename> Client certificate without extension for mTLS
182 --insecure self-signed certificate: use SSL_VERIFY_NONE
183 --save save certificate fingerprint as expected
184 --dir <path> Fingerprint directory (optional)
185 If omitted: no certificate pinning
186 --keep-alive Use HTTP keep-alive connection
187 --no-redirect Don't follow 3xx redirects
188 Example:
189 $prognm -v -v https://reqbin.com/echo/get/json
190 $prognm --cert ./client https://reqbin.com/echo/post/json
191 $prognm --file data.json https://reqbin.com/echo/post/json
192 $prognm -v --dir cert.d --save https://reqbin.com/echo/get/json
193 $prognm --type put --file doc.pdf https://example.com/upload
194 $prognm --header "Authorization: Bearer abc123" --header "Accept: application/json" https://api.example.com/data
195  
196 EOF
197 exit 1;
198 }