How to get ssl/tls certificate from https response in node.js?

Member

by orpha , in category: Third Party Scripts , 14 days ago

How to get ssl/tls certificate from https response in node.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 13 days ago

@orpha 

You can use the tls module in Node.js to obtain a SSL/TLS certificate from a HTTPS response. Here is an example of how you can do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
const https = require('https');

const options = {
  hostname: 'example.com',
  port: 443,
  path: '/',
  method: 'GET'
};

const req = https.request(options, (res) => {
  const { socket } = res;
  const certificate = socket.getPeerCertificate();

  console.log('SSL/TLS certificate:', certificate);
});

req.on('error', (error) => {
  console.error(error);
});

req.end();


In this example, we make a HTTPS request to example.com and retrieve the SSL/TLS certificate from the response using the getPeerCertificate() method on the socket object of the response. The certificate object will contain information about the certificate, such as the subject, issuer, validity dates, and public key.


Please note that this code should be used for educational or debugging purposes only, as extracting the certificate from a HTTPS response may be subject to security and privacy concerns.