首页 > 其他 > 详细

使用ssl-validator识别证书信息

时间:2020-06-09 19:51:03      阅读:62      评论:0      收藏:0      [点我收藏+]

前言

使用 npm包 ssl-validator 来获取证书的域名、有效期等信息。

代码

const sslValidator = require(‘ssl-validator‘);
const SSLData = require(‘./data‘); //引入你的证书信息 csr key

(async () => {
    try {
        // 扫描所有有效信息是否配对
        const res = await sslValidator.validateSSL(SSLData.CSR, {
            key: SSLData.KEY
        });
        // 罗列几个主要的信息
        const certInfo = {
            domain: res.certInfo.commonName,
            validity: {
                start: res.certInfo.validity.start,
                startString: new Date(res.certInfo.validity.start).toLocaleString(),
                end: res.certInfo.validity.end,
                endString: new Date(res.certInfo.validity.end).toLocaleString()
            }
        };
        // console.log(‘res‘, res);
        console.log(‘certInfo‘, certInfo);
    } catch (e) {
        // console.error(e.message);
        // 不匹配的几种情况
        if(e.message.indexOf(‘unable to load certificate‘) !== -1 || e.message.indexOf(‘Certificate must start and end with proper formatting.‘) !== -1){
            console.error(‘无效的证书‘);
        }else if(e.message.indexOf(‘unable to load Private Key‘) !== -1 || e.message.indexOf(‘Key must start and end with proper formatting.‘) !== -1){
            console.error(‘无效的秘钥‘);
        }else if(e.message.indexOf(‘The certificate does not match the domain.‘) !== -1){
            console.error(‘证书与域名不匹配‘);
        }else if(e.message.indexOf(‘The provided certificate and key do not match.‘) !== -1){
            console.error(‘证书与秘钥不匹配‘);
        }else {
            console.error(‘证书、秘钥、域名格式错误或不匹配‘);
        }
    };
})();

总结

  1. 如果在windowns下执行,需要先安装OpenSSL

使用ssl-validator识别证书信息

原文:https://www.cnblogs.com/xpengp/p/12762603.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!