node.js - how to check/get ssl certificate expiry date
Asked Answered
E

3

15

I have the Let's encrypt certificate bundle. It includes the private key and certificate.crt

Using node.js and node-forge (not openssl), how can I get the expiry date of the certificate.crt?

Expertise answered 26/6, 2019 at 10:46 Comment(0)
S
12

You can use built-in crypto module:

const { X509Certificate } = require('crypto');

const { validTo } = new X509Certificate(certificate);

This is available in v15.6 onwards.

Synonym answered 22/11, 2021 at 15:4 Comment(1)
Should be the accepted answer. Better to avoid importing 3rd party modules and use built-in functionality for checking certificate validity.Ehtelehud
P
9

You can use x509 module

var crt_pem = "<certificate in pem format which is content of your certificate.crt>";
const x509 = require('x509');
var crt_obj = x509.parseCert(crt_pem);
console.log(crt_obj.notBefore);
console.log(crt_obj.notAfter);
Patel answered 17/7, 2019 at 5:14 Comment(2)
This is a great answer if you're trying to check the validity of a cert file, rather than a websiteOctaviaoctavian
@DominusVilicus which is exactly what the question is asking....the validity of a cert file.Feller
W
8

You can use Node SSL Checker

$ npm install ssl-checker --save # npm i -s ssh-checker

In your code:

var sslChecker = require("ssl-checker")
sslChecker('example.com', 'GET', 443).then(result => console.info(result));

the response will look like this:

{
"valid": true,
"days_remaining" : 90,
"valid_from" : "issue date",
"valid_to" : "expiry date"
}
Whereinto answered 26/6, 2019 at 11:0 Comment(3)
Nope. Please read the OP question, its not about checking a domain to get expiration date. Its about reading the certificate file directly to get the meta data.Expertise
@AwSnap have you considered that perhaps the question is being asked because they have (for whatever reason that isn't revealed), a specific requirement that they need to be reading in a certificate file in a nodejs app? It is not up to us to judge what the reason is. The requirement is spelled out and looking for a specific answer.Feller
How to check the root and intermediate certificate expiration date?Trixie

© 2022 - 2024 — McMap. All rights reserved.