使用ssl-validator识别证书信息
前言
使用 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('证书、秘钥、域名格式错误或不匹配');
}
};
})();
总结
- 如果在windowns下执行,需要先安装OpenSSL
原博客链接:https://www.cnblogs.com/xpengp/