| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # client::ssl Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Client::Ssl; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable tls) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | uri => [ qw(uri) ], | ||
| 20 | }, | ||
| 21 | commands => { | ||
| 22 | install => [ ], # Inherited | ||
| 23 | verify_server => [ qw(uri|OPTIONAL) ], | ||
| 24 | getcertificate => [ qw(uri|OPTIONAL) ], | ||
| 25 | getcertificate2 => [ qw(host port) ], | ||
| 26 | }, | ||
| 27 | require_modules => { | ||
| 28 | 'Data::Dumper' => [ ], | ||
| 29 | 'IO::Socket::SSL' => [ ], | ||
| 30 | 'LWP::UserAgent' => [ ], | ||
| 31 | 'LWP::ConnCache' => [ ], | ||
| 32 | 'URI' => [ ], | ||
| 33 | 'Net::SSLeay' => [ ], | ||
| 34 | 'Metabrik::String::Uri' => [ ], | ||
| 35 | }, | ||
| 36 | need_packages => { | ||
| 37 | ubuntu => [ qw(libssl-dev) ], | ||
| 38 | debian => [ qw(libssl-dev) ], | ||
| 39 | kali => [ qw(libssl-dev) ], | ||
| 40 | centos => [ qw(openssl-devel) ], | ||
| 41 | redhat => [ qw(openssl-devel) ], | ||
| 42 | }, | ||
| 43 | }; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub verify_server { | ||
| 47 | my $self = shift; | ||
| 48 | my ($uri) = @_; | ||
| 49 | |||
| 50 | $uri ||= $self->uri; | ||
| 51 | $self->brik_help_run_undef_arg('verify_server', $uri) or return; | ||
| 52 | |||
| 53 | my $su = Metabrik::String::Uri->new_from_brik_init($self) or return; | ||
| 54 | my $parsed = $su->parse($uri) or return; | ||
| 55 | |||
| 56 | my $host = $parsed->{host}; | ||
| 57 | my $port = $parsed->{port}; | ||
| 58 | |||
| 59 | $self->log->debug("verify_server: trying host [".$parsed->{host}."] ". | ||
| 60 | "with port [".$parsed->{port}."]"); | ||
| 61 | |||
| 62 | my $client = IO::Socket::SSL->new( | ||
| 63 | PeerHost => $parsed->{host}, | ||
| 64 | PeerPort => $parsed->{port}, | ||
| 65 | SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(), | ||
| 66 | SSL_verifycn_name => $parsed->{host}, | ||
| 67 | SSL_verifycn_scheme => 'http', | ||
| 68 | ); | ||
| 69 | if (! defined($client) && ! length($!)) { | ||
| 70 | $self->log->verbose("verify_server: not verified: [". | ||
| 71 | $IO::Socket::SSL::SSL_ERROR."]"); | ||
| 72 | return 0; | ||
| 73 | } | ||
| 74 | elsif (! defined($client)) { | ||
| 75 | return $self->log->error("verify_server: connection failed with ". | ||
| 76 | "error: [$!]"); | ||
| 77 | } | ||
| 78 | |||
| 79 | $self->log->verbose("verify_server: verified"); | ||
| 80 | |||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | # | ||
| 85 | # Note: works only with IO::Socket::SSL, not with Net::SSL (using Crypt::SSLeay) | ||
| 86 | # | ||
| 87 | sub getcertificate { | ||
| 88 | my $self = shift; | ||
| 89 | my ($uri) = @_; | ||
| 90 | |||
| 91 | $uri ||= $self->uri; | ||
| 92 | $self->brik_help_run_undef_arg('getcertificate', $uri) or return; | ||
| 93 | |||
| 94 | if ($uri !~ /^https:\/\//) { | ||
| 95 | return $self->log->error("must use https to get a certificate"); | ||
| 96 | } | ||
| 97 | |||
| 98 | my $ua = LWP::UserAgent->new( | ||
| 99 | #ssl_opts => { verify_hostname => 0 }, # will do manual check | ||
| 100 | ssl_opts => { SSL_verify_mode => 'SSL_VERIFY_NONE'}, | ||
| 101 | ); | ||
| 102 | $ua->timeout(defined($self->global) && $self->global->rtimeout || 3); | ||
| 103 | $ua->max_redirect(0); | ||
| 104 | $ua->env_proxy; | ||
| 105 | |||
| 106 | my $cache = LWP::ConnCache->new; | ||
| 107 | $ua->conn_cache($cache); | ||
| 108 | |||
| 109 | my $response = $ua->get($uri); | ||
| 110 | # XXX: we ignore response? | ||
| 111 | |||
| 112 | my $cc = $ua->conn_cache->{cc_conns}; | ||
| 113 | if (! defined($cc)) { | ||
| 114 | return $self->log->error("unable to retrieve connection cache"); | ||
| 115 | } | ||
| 116 | |||
| 117 | if (scalar(@$cc) == 0) { | ||
| 118 | return $self->log->error("getcertificate: no connection cached"); | ||
| 119 | } | ||
| 120 | |||
| 121 | my $sock = $cc->[0][0]; | ||
| 122 | |||
| 123 | my %info = (); | ||
| 124 | # peer_certificate from IO::Socket::SSL/Crypt::SSLeay | ||
| 125 | if ($sock->can('peer_certificate')) { | ||
| 126 | my $authority = $sock->peer_certificate('authority'); # issuer | ||
| 127 | my $owner = $sock->peer_certificate('owner'); # subject | ||
| 128 | my $commonName = $sock->peer_certificate('commonName'); # cn | ||
| 129 | my $subjectAltNames = $sock->peer_certificate('subjectAltNames'); | ||
| 130 | my $sslversion = $sock->get_sslversion; | ||
| 131 | my $cipher = $sock->get_cipher; | ||
| 132 | my $servername = $sock->get_servername; # Only when SNI is used | ||
| 133 | #my $verify_hostname = $sock->verify_hostname('hostname', 'http'); | ||
| 134 | |||
| 135 | $info{authority} = $authority; | ||
| 136 | $info{owner} = $owner; | ||
| 137 | $info{commonName} = $commonName; | ||
| 138 | $info{subjectAltNames} = $subjectAltNames; | ||
| 139 | $info{sslversion} = $sslversion; | ||
| 140 | $info{cipher} = $cipher; | ||
| 141 | $info{servername} = $servername; | ||
| 142 | #$info{verify_hostname} = $verify_hostname; | ||
| 143 | |||
| 144 | print Data::Dumper::Dumper(\%info)."\n"; | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | return $self->log->error("socket [$sock] cannot do 'peer_certificate'"); | ||
| 148 | } | ||
| 149 | |||
| 150 | #$sock->stop_SSL; | ||
| 151 | |||
| 152 | return $sock; | ||
| 153 | } | ||
| 154 | |||
| 155 | eval("use Net::SSLeay qw/XN_FLAG_RFC2253 ASN1_STRFLGS_ESC_MSB/;"); | ||
| 156 | |||
| 157 | # Taken from http://cpansearch.perl.org/src/MIKEM/Net-SSLeay-1.57/examples/x509_cert_details.pl | ||
| 158 | sub get_cert_details { | ||
| 159 | my $x509 = shift; | ||
| 160 | my $rv = {}; | ||
| 161 | my $flag_rfc22536_utf8 = (Net::SSLeay::XN_FLAG_RFC2253()) & (~ Net::SSLeay::ASN1_STRFLGS_ESC_MSB()); | ||
| 162 | |||
| 163 | die 'ERROR: $x509 is NULL, gonna quit' unless $x509; | ||
| 164 | |||
| 165 | #warn "Info: dumping subject\n"; | ||
| 166 | my $subj_name = Net::SSLeay::X509_get_subject_name($x509); | ||
| 167 | my $subj_count = Net::SSLeay::X509_NAME_entry_count($subj_name); | ||
| 168 | $rv->{subject}->{count} = $subj_count; | ||
| 169 | $rv->{subject}->{oneline} = Net::SSLeay::X509_NAME_oneline($subj_name); | ||
| 170 | $rv->{subject}->{print_rfc2253} = Net::SSLeay::X509_NAME_print_ex($subj_name); | ||
| 171 | $rv->{subject}->{print_rfc2253_utf8} = Net::SSLeay::X509_NAME_print_ex($subj_name, $flag_rfc22536_utf8); | ||
| 172 | $rv->{subject}->{print_rfc2253_utf8_decoded} = Net::SSLeay::X509_NAME_print_ex($subj_name, $flag_rfc22536_utf8, 1); | ||
| 173 | for my $i (0..$subj_count-1) { | ||
| 174 | my $entry = Net::SSLeay::X509_NAME_get_entry($subj_name, $i); | ||
| 175 | my $asn1_string = Net::SSLeay::X509_NAME_ENTRY_get_data($entry); | ||
| 176 | my $asn1_object = Net::SSLeay::X509_NAME_ENTRY_get_object($entry); | ||
| 177 | my $nid = Net::SSLeay::OBJ_obj2nid($asn1_object); | ||
| 178 | $rv->{subject}->{entries}->[$i] = { | ||
| 179 | oid => Net::SSLeay::OBJ_obj2txt($asn1_object,1), | ||
| 180 | data => Net::SSLeay::P_ASN1_STRING_get($asn1_string), | ||
| 181 | data_utf8_decoded => Net::SSLeay::P_ASN1_STRING_get($asn1_string, 1), | ||
| 182 | nid => ($nid>0) ? $nid : undef, | ||
| 183 | ln => ($nid>0) ? Net::SSLeay::OBJ_nid2ln($nid) : undef, | ||
| 184 | sn => ($nid>0) ? Net::SSLeay::OBJ_nid2sn($nid) : undef, | ||
| 185 | }; | ||
| 186 | } | ||
| 187 | |||
| 188 | #warn "Info: dumping issuer\n"; | ||
| 189 | my $issuer_name = Net::SSLeay::X509_get_issuer_name($x509); | ||
| 190 | my $issuer_count = Net::SSLeay::X509_NAME_entry_count($issuer_name); | ||
| 191 | $rv->{issuer}->{count} = $issuer_count; | ||
| 192 | $rv->{issuer}->{oneline} = Net::SSLeay::X509_NAME_oneline($issuer_name); | ||
| 193 | $rv->{issuer}->{print_rfc2253} = Net::SSLeay::X509_NAME_print_ex($issuer_name); | ||
| 194 | $rv->{issuer}->{print_rfc2253_utf8} = Net::SSLeay::X509_NAME_print_ex($issuer_name, $flag_rfc22536_utf8); | ||
| 195 | $rv->{issuer}->{print_rfc2253_utf8_decoded} = Net::SSLeay::X509_NAME_print_ex($issuer_name, $flag_rfc22536_utf8, 1); | ||
| 196 | for my $i (0..$issuer_count-1) { | ||
| 197 | my $entry = Net::SSLeay::X509_NAME_get_entry($issuer_name, $i); | ||
| 198 | my $asn1_string = Net::SSLeay::X509_NAME_ENTRY_get_data($entry); | ||
| 199 | my $asn1_object = Net::SSLeay::X509_NAME_ENTRY_get_object($entry); | ||
| 200 | my $nid = Net::SSLeay::OBJ_obj2nid($asn1_object); | ||
| 201 | $rv->{issuer}->{entries}->[$i] = { | ||
| 202 | oid => Net::SSLeay::OBJ_obj2txt($asn1_object,1), | ||
| 203 | data => Net::SSLeay::P_ASN1_STRING_get($asn1_string), | ||
| 204 | data_utf8_decoded => Net::SSLeay::P_ASN1_STRING_get($asn1_string, 1), | ||
| 205 | nid => ($nid>0) ? $nid : undef, | ||
| 206 | ln => ($nid>0) ? Net::SSLeay::OBJ_nid2ln($nid) : undef, | ||
| 207 | sn => ($nid>0) ? Net::SSLeay::OBJ_nid2sn($nid) : undef, | ||
| 208 | }; | ||
| 209 | } | ||
| 210 | |||
| 211 | #warn "Info: dumping alternative names\n"; | ||
| 212 | $rv->{subject}->{altnames} = [ Net::SSLeay::X509_get_subjectAltNames($x509) ]; | ||
| 213 | #XXX-TODO maybe add a function for dumping issuerAltNames | ||
| 214 | #$rv->{issuer}->{altnames} = [ Net::SSLeay::X509_get_issuerAltNames($x509) ]; | ||
| 215 | |||
| 216 | #warn "Info: dumping hashes/fingerprints\n"; | ||
| 217 | $rv->{hash}->{subject} = { dec=>Net::SSLeay::X509_subject_name_hash($x509), hex=>sprintf("%X",Net::SSLeay::X509_subject_name_hash($x509)) }; | ||
| 218 | $rv->{hash}->{issuer} = { dec=>Net::SSLeay::X509_issuer_name_hash($x509), hex=>sprintf("%X",Net::SSLeay::X509_issuer_name_hash($x509)) }; | ||
| 219 | $rv->{hash}->{issuer_and_serial} = { dec=>Net::SSLeay::X509_issuer_and_serial_hash($x509), hex=>sprintf("%X",Net::SSLeay::X509_issuer_and_serial_hash($x509)) }; | ||
| 220 | $rv->{fingerprint}->{md5} = Net::SSLeay::X509_get_fingerprint($x509, "md5"); | ||
| 221 | $rv->{fingerprint}->{sha1} = Net::SSLeay::X509_get_fingerprint($x509, "sha1"); | ||
| 222 | my $sha1_digest = Net::SSLeay::EVP_get_digestbyname("sha1"); | ||
| 223 | $rv->{digest_sha1}->{pubkey} = Net::SSLeay::X509_pubkey_digest($x509, $sha1_digest); | ||
| 224 | $rv->{digest_sha1}->{x509} = Net::SSLeay::X509_digest($x509, $sha1_digest); | ||
| 225 | |||
| 226 | #warn "Info: dumping expiration\n"; | ||
| 227 | $rv->{not_before} = Net::SSLeay::P_ASN1_TIME_get_isotime(Net::SSLeay::X509_get_notBefore($x509)); | ||
| 228 | $rv->{not_after} = Net::SSLeay::P_ASN1_TIME_get_isotime(Net::SSLeay::X509_get_notAfter($x509)); | ||
| 229 | |||
| 230 | #warn "Info: dumping serial number\n"; | ||
| 231 | my $ai = Net::SSLeay::X509_get_serialNumber($x509); | ||
| 232 | $rv->{serial} = { | ||
| 233 | hex => Net::SSLeay::P_ASN1_INTEGER_get_hex($ai), | ||
| 234 | dec => Net::SSLeay::P_ASN1_INTEGER_get_dec($ai), | ||
| 235 | long => Net::SSLeay::ASN1_INTEGER_get($ai), | ||
| 236 | }; | ||
| 237 | $rv->{version} = Net::SSLeay::X509_get_version($x509); | ||
| 238 | |||
| 239 | #warn "Info: dumping extensions\n"; | ||
| 240 | my $ext_count = Net::SSLeay::X509_get_ext_count($x509); | ||
| 241 | $rv->{extensions}->{count} = $ext_count; | ||
| 242 | for my $i (0..$ext_count-1) { | ||
| 243 | my $ext = Net::SSLeay::X509_get_ext($x509,$i); | ||
| 244 | my $asn1_string = Net::SSLeay::X509_EXTENSION_get_data($ext); | ||
| 245 | my $asn1_object = Net::SSLeay::X509_EXTENSION_get_object($ext); | ||
| 246 | my $nid = Net::SSLeay::OBJ_obj2nid($asn1_object); | ||
| 247 | $rv->{extensions}->{entries}->[$i] = { | ||
| 248 | critical => Net::SSLeay::X509_EXTENSION_get_critical($ext), | ||
| 249 | oid => Net::SSLeay::OBJ_obj2txt($asn1_object,1), | ||
| 250 | nid => ($nid>0) ? $nid : undef, | ||
| 251 | ln => ($nid>0) ? Net::SSLeay::OBJ_nid2ln($nid) : undef, | ||
| 252 | sn => ($nid>0) ? Net::SSLeay::OBJ_nid2sn($nid) : undef, | ||
| 253 | data => Net::SSLeay::X509V3_EXT_print($ext), | ||
| 254 | }; | ||
| 255 | } | ||
| 256 | |||
| 257 | #warn "Info: dumping CDP\n"; | ||
| 258 | $rv->{cdp} = [ Net::SSLeay::P_X509_get_crl_distribution_points($x509) ]; | ||
| 259 | #warn "Info: dumping extended key usage\n"; | ||
| 260 | $rv->{extkeyusage} = { | ||
| 261 | oid => [ Net::SSLeay::P_X509_get_ext_key_usage($x509,0) ], | ||
| 262 | nid => [ Net::SSLeay::P_X509_get_ext_key_usage($x509,1) ], | ||
| 263 | sn => [ Net::SSLeay::P_X509_get_ext_key_usage($x509,2) ], | ||
| 264 | ln => [ Net::SSLeay::P_X509_get_ext_key_usage($x509,3) ], | ||
| 265 | }; | ||
| 266 | #warn "Info: dumping key usage\n"; | ||
| 267 | $rv->{keyusage} = [ Net::SSLeay::P_X509_get_key_usage($x509) ]; | ||
| 268 | #warn "Info: dumping netscape cert type\n"; | ||
| 269 | $rv->{ns_cert_type} = [ Net::SSLeay::P_X509_get_netscape_cert_type($x509) ]; | ||
| 270 | |||
| 271 | #warn "Info: dumping other info\n"; | ||
| 272 | $rv->{certificate_type} = Net::SSLeay::X509_certificate_type($x509); | ||
| 273 | $rv->{signature_alg} = Net::SSLeay::OBJ_obj2txt(Net::SSLeay::P_X509_get_signature_alg($x509)); | ||
| 274 | $rv->{pubkey_alg} = Net::SSLeay::OBJ_obj2txt(Net::SSLeay::P_X509_get_pubkey_alg($x509)); | ||
| 275 | $rv->{pubkey_size} = Net::SSLeay::EVP_PKEY_size(Net::SSLeay::X509_get_pubkey($x509)); | ||
| 276 | $rv->{pubkey_bits} = Net::SSLeay::EVP_PKEY_bits(Net::SSLeay::X509_get_pubkey($x509)); | ||
| 277 | $rv->{pubkey_id} = Net::SSLeay::EVP_PKEY_id(Net::SSLeay::X509_get_pubkey($x509)); | ||
| 278 | |||
| 279 | return $rv; | ||
| 280 | } | ||
| 281 | |||
| 282 | # This routine will only check the certificate chain, not the actual contact | ||
| 283 | # of the certificate. You still have to check for CN validity and expiration date. | ||
| 284 | sub verify { | ||
| 285 | my ($ok, $x509_store_ctx) = @_; | ||
| 286 | |||
| 287 | print "**** Verify called ($ok)\n"; | ||
| 288 | |||
| 289 | my $x = Net::SSLeay::X509_STORE_CTX_get_current_cert($x509_store_ctx); | ||
| 290 | if ($x) { | ||
| 291 | print "Certificate:\n"; | ||
| 292 | print " Subject Name: " | ||
| 293 | . Net::SSLeay::X509_NAME_oneline( | ||
| 294 | Net::SSLeay::X509_get_subject_name($x)) | ||
| 295 | . "\n"; | ||
| 296 | print " Issuer Name: " | ||
| 297 | . Net::SSLeay::X509_NAME_oneline( | ||
| 298 | Net::SSLeay::X509_get_issuer_name($x)) | ||
| 299 | . "\n"; | ||
| 300 | } | ||
| 301 | |||
| 302 | return $ok; | ||
| 303 | } | ||
| 304 | |||
| 305 | sub getcertificate2 { | ||
| 306 | my $self = shift; | ||
| 307 | my ($host, $port) = @_; | ||
| 308 | |||
| 309 | $self->brik_help_run_undef_arg('getcertificate2', $host) or return; | ||
| 310 | $self->brik_help_run_undef_arg('getcertificate2', $port) or return; | ||
| 311 | |||
| 312 | eval("use Net::SSLeay qw(print_errs set_fd);"); | ||
| 313 | |||
| 314 | if (defined($ENV{HTTPS_PROXY})) { | ||
| 315 | my $proxy = URI->new($ENV{HTTPS_PROXY}); | ||
| 316 | my $user = ''; | ||
| 317 | my $pass = ''; | ||
| 318 | my $userinfo = $proxy->userinfo; | ||
| 319 | if (defined($userinfo)) { | ||
| 320 | ($user, $pass) = split(':', $userinfo); | ||
| 321 | } | ||
| 322 | my $host = $proxy->host; | ||
| 323 | my $port = $proxy->port; | ||
| 324 | Net::SSLeay::set_proxy($host, $port, $user, $pass); | ||
| 325 | } | ||
| 326 | |||
| 327 | # Taken from Net::SSLeay source code: sslcat() | ||
| 328 | my ($got, $errs) = Net::SSLeay::open_proxy_tcp_connection($host, $port); | ||
| 329 | if (! $got) { | ||
| 330 | return $self->log->error("Net::SSLeay::open_proxy_tcp_connection: $errs"); | ||
| 331 | } | ||
| 332 | |||
| 333 | Net::SSLeay::initialize(); | ||
| 334 | |||
| 335 | my $ctx = Net::SSLeay::new_x_ctx(); | ||
| 336 | if ($errs = print_errs('Net::SSLeay::new_x_ctx') || ! $ctx) { | ||
| 337 | return $self->log->error($errs); | ||
| 338 | } | ||
| 339 | |||
| 340 | Net::SSLeay::CTX_set_options($ctx, &Net::SSLeay::OP_ALL); | ||
| 341 | if ($errs = print_errs('Net::SSLeay::CTX_set_options')) { | ||
| 342 | return $self->log->error($errs); | ||
| 343 | } | ||
| 344 | |||
| 345 | # Certificate chain verification routines | ||
| 346 | Net::SSLeay::CTX_set_default_verify_paths($ctx); | ||
| 347 | my $cert_dir = '/etc/ssl/certs'; | ||
| 348 | Net::SSLeay::CTX_load_verify_locations($ctx, '', $cert_dir) | ||
| 349 | or return $self->log->error("CTX load verify loc=`$cert_dir' $!"); | ||
| 350 | Net::SSLeay::CTX_set_verify($ctx, 0, \&verify); | ||
| 351 | #die_if_ssl_error('callback: ctx set verify'); | ||
| 352 | |||
| 353 | # XXX: skipped client certs part from sslcat() | ||
| 354 | |||
| 355 | my $ssl = Net::SSLeay::new($ctx); | ||
| 356 | if ($errs = print_errs('Net::SSLeay::new')) { | ||
| 357 | return $self->log->error($errs); | ||
| 358 | } | ||
| 359 | |||
| 360 | set_fd($ssl, fileno(Net::SSLeay::SSLCAT_S())); | ||
| 361 | if ($errs = print_errs('fileno')) { | ||
| 362 | return $self->log->error($errs); | ||
| 363 | } | ||
| 364 | |||
| 365 | # Gather cipher list | ||
| 366 | my $i = 0; | ||
| 367 | my @cipher_list = (); | ||
| 368 | my $cont = 1; | ||
| 369 | while ($cont) { | ||
| 370 | my $cipher = Net::SSLeay::get_cipher_list($ssl, $i); | ||
| 371 | if (! $cipher) { | ||
| 372 | #print "DEBUG last cipher\n"; | ||
| 373 | $cont = 0; | ||
| 374 | last; | ||
| 375 | } | ||
| 376 | #print "cipher [$cipher]\n"; | ||
| 377 | push @cipher_list, $cipher; | ||
| 378 | $i++; | ||
| 379 | } | ||
| 380 | |||
| 381 | $got = Net::SSLeay::connect($ssl); | ||
| 382 | if (! $got) { | ||
| 383 | $errs = print_errs('Net::SSLeay::connect'); | ||
| 384 | return $self->log->error($errs); | ||
| 385 | } | ||
| 386 | |||
| 387 | my $cipher = Net::SSLeay::get_cipher($ssl); | ||
| 388 | print "Using cipher [$cipher]\n"; | ||
| 389 | |||
| 390 | print Net::SSLeay::dump_peer_certificate($ssl); | ||
| 391 | |||
| 392 | my $server_cert = Net::SSLeay::get_peer_certificate($ssl); | ||
| 393 | #print "get_peer_certificate: ".Data::Dumper::Dumper($server_cert)."\n"; | ||
| 394 | |||
| 395 | my $cert_details = get_cert_details($server_cert); | ||
| 396 | #print Data::Dumper::Dumper($cert_details)."\n"; | ||
| 397 | |||
| 398 | my @rv = Net::SSLeay::get_peer_cert_chain($ssl); | ||
| 399 | #print "get_peer_cert_chain: ".Data::Dumper::Dumper(\@rv)."\n"; | ||
| 400 | |||
| 401 | my $rv = Net::SSLeay::get_verify_result($ssl); | ||
| 402 | print "get_verify_result: ".Data::Dumper::Dumper($rv)."\n"; | ||
| 403 | |||
| 404 | #print 'Subject Name: '.Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_subject_name($server_cert)). | ||
| 405 | #"\n".'Issuer Name: '.Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_issuer_name($server_cert))."\n"; | ||
| 406 | |||
| 407 | my $subj_name = Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_subject_name($server_cert)); | ||
| 408 | print "$subj_name\n"; | ||
| 409 | |||
| 410 | #my $pem = Net::SSLeay::PEM_get_string_X509_CRL($server_cert); | ||
| 411 | |||
| 412 | |||
| 413 | |||
| 414 | # | ||
| 415 | # X509 certificate details | ||
| 416 | # | ||
| 417 | |||
| 418 | # # X509 version | ||
| 419 | # my $version = Net::SSLeay::X509_get_version($server_cert); | ||
| 420 | # print "version: $version\n"; | ||
| 421 | # | ||
| 422 | # # Number of extension used | ||
| 423 | # my $ext_count = Net::SSLeay::X509_get_ext_count($server_cert); | ||
| 424 | # print "ext_count: $ext_count\n"; | ||
| 425 | # | ||
| 426 | # # Extensions | ||
| 427 | # # X509_get_ext | ||
| 428 | # for my $index (0..$ext_count-1) { | ||
| 429 | # my $ext = Net::SSLeay::X509_get_ext($server_cert, $index); | ||
| 430 | # #my $data = Net::SSLeay::X509_EXTENSION_get_data($ext); | ||
| 431 | # #my $string = Net::SSLeay::P_ASN1_STRING_get($data); | ||
| 432 | # #print Data::Dumper::Dumper($string)."\n"; | ||
| 433 | # | ||
| 434 | # print "EXT: ".Net::SSLeay::X509V3_EXT_print($ext)."\n"; | ||
| 435 | # } | ||
| 436 | # | ||
| 437 | # # Fingerprint | ||
| 438 | # my $fingerprint = Net::SSLeay::X509_get_fingerprint($server_cert, "md5"); | ||
| 439 | # print "MD5 fingerprint: $fingerprint\n"; | ||
| 440 | # $fingerprint = Net::SSLeay::X509_get_fingerprint($server_cert, "sha1"); | ||
| 441 | # print "SHA-1 fingerprint: $fingerprint\n"; | ||
| 442 | # $fingerprint = Net::SSLeay::X509_get_fingerprint($server_cert, "sha256"); | ||
| 443 | # print "SHA-256 fingerprint: $fingerprint\n"; | ||
| 444 | # $fingerprint = Net::SSLeay::X509_get_fingerprint($server_cert, "ripemd160"); | ||
| 445 | # print "RIPEMD160 fingerprint: $fingerprint\n"; | ||
| 446 | # | ||
| 447 | # # Issuer name | ||
| 448 | # my $issuer = Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_issuer_name($server_cert)); | ||
| 449 | # print "issuer: $issuer\n"; | ||
| 450 | # | ||
| 451 | # # Not after | ||
| 452 | # my $time = Net::SSLeay::X509_get_notAfter($server_cert); | ||
| 453 | # my $not_after = Net::SSLeay::P_ASN1_TIME_get_isotime($time); | ||
| 454 | # print "not after: $not_after\n"; | ||
| 455 | # | ||
| 456 | # # Not before | ||
| 457 | # $time = Net::SSLeay::X509_get_notBefore($server_cert); | ||
| 458 | # my $not_before = Net::SSLeay::P_ASN1_TIME_get_isotime($time); | ||
| 459 | # print "not before: $not_before\n"; | ||
| 460 | # | ||
| 461 | # # What kind of encryption is using the public key | ||
| 462 | # my $pubkey = Net::SSLeay::X509_get_pubkey($server_cert); | ||
| 463 | # my $type = Net::SSLeay::EVP_PKEY_id($pubkey); | ||
| 464 | # my $encryption_type = Net::SSLeay::OBJ_nid2sn($type); | ||
| 465 | # print "pubkey: $encryption_type\n"; | ||
| 466 | # | ||
| 467 | # # | ||
| 468 | # @rv = Net::SSLeay::X509_get_subjectAltNames($server_cert); | ||
| 469 | # print Data::Dumper::Dumper(\@rv)."\n"; | ||
| 470 | # | ||
| 471 | # # Serial number | ||
| 472 | # my $serial_number = Net::SSLeay::X509_get_serialNumber($server_cert); | ||
| 473 | # print "serial_number: $serial_number\n"; | ||
| 474 | |||
| 475 | return $server_cert; | ||
| 476 | } | ||
| 477 | |||
| 478 | 1; | ||
| 479 | |||
| 480 | __END__ | ||