Bad Certificate error while making rest api call in perl

I am making a simple rest API call and getting error code:

  500 Can't connect to www.abc.com:44

#!/usr/bin/perl -w
use LWP::UserAgent;

my $client = LWP::UserAgent->new;

# Load client certificate
$client->ssl_opts(
                  SSL_cert_file => 'ca-cert.pem',
                  SSL_key_file => 'key.pem'
                 );

# Verify server certificate
$client->ssl_opts( SSL_verify_mode => SSL_VERIFY_PEER );

# Set up HTTPS request
my $response = $client->get('https://exmaple.com/serviceNow/1.0.0?format=json');

if ($response->is_success) {
     print $response->decoded_content;
  }
else {
    print  $response->status_line;
  }

When I checked the logs on server side it seems the certificate was not getting attached. as I am getting

 javax.net.ssl.SSLHandshakeException: Empty server certificate chain at 
  sun.security.ssl.Alert.createSSLException(Alert.java:131)" } 

sharing the logs below –

    javax.net.ssl|FINE|1E|https-jsse-nio-8124-exec-3|2023-12-07 11:31:15.485 
    UTC|CertificateMessage.java:372|Consuming client Certificate handshake message (
    "Certificates": <empty list>
    )
    javax.net.ssl|SEVERE|1E|https-jsse-nio-8124-exec-3|2023-12-07 11:31:15.486 
    UTC|TransportContext.java:347|Fatal (BAD_CERTIFICATE): Empty server certificate chain 
               

  • Please add use IO::Socket::SSL; $IO::Socket::SSL::DEBUG=5; at the start of your code and include the SSL debug output in your question.

    – 

  • it seems you are using ssl_opts which not exist for the Useragent. see metacpan.org/pod/LWP::UserAgent#ssl_opts

    – 

  • @OliverGaida: to cite from the documentation you’ve linked: “Other options can be set and are processed directly by the SSL Socket implementation in use. See IO::Socket::SSL or Net::SSL for details”. I very much hope that the OP is using IO::Socket::SSL (Net::SSL is basically obsolete) and there these options exist.

    – 

Leave a Comment