@elastic.io/x509

中文文档
1.1.3发布于 2023/09/26MITCommonJS近 30 天下载 4355

Simple X509 certificate parser

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. Methods
  4. x509.getAltNames( cert )
  5. x509.getIssuer( cert )
  6. x509.getSubject( cert )
  7. x509.parseCert( cert )
  8. x509.verify( cert , CABundlePath , function(err, result){ /…
  9. Examples
  10. License

node-x509

Simple X509 certificate parser. This fork provides a compatibility with Node.js versions later than 10. The original library seems like stopped being supported.

Installation

From NPM (recommended):

npm install @elastic.io/x509

Building and testing from source:

npm install
npm test

Usage

Reading from a file:

const x509 = require('@elastic.io/x509');
const issuer = x509.getIssuer(__dirname + '/certs/your.crt');

Reading from a string:

const fs = require('fs');
const x509 = require('@elastic.io/x509');
const issuer = x509.getIssuer(fs.readFileSync('./certs/your.crt').toString());

Methods

Notes:

  • cert may be a filename or a raw base64 encoded PEM string in any of these methods.

x509.getAltNames(cert)

Parse certificate with x509.parseCert and return the alternate names.

const x509 = require('@elastic.io/x509');
const altNames = x509.getAltNames(__dirname + '/certs/nodejitsu.com.crt');
// altNames = [ '*.nodejitsu.com', 'nodejitsu.com' ]

x509.getIssuer(cert)

Parse certificate with x509.parseCert and return the issuer.

const x509 = require('@elastic.io/x509');
const issuer = x509.getIssuer(__dirname + '/certs/nodejitsu.com.crt');
// issuer = {
//   countryName: 'GB',
//   stateOrProvinceName: 'Greater Manchester',
//   localityName: 'Salford',
//   organizationName: 'COMODO CA Limited',
//   commonName: 'COMODO High-Assurance Secure Server CA'
// }

x509.getSubject(cert)

Parse certificate with x509.parseCert and return the subject.

const x509 = require('@elastic.io/x509');
const subject = x509.getSubject(__dirname + '/certs/nodejitsu.com.crt');
// subject = {
//   countryName: 'US',
//   postalCode: '10010',
//   stateOrProvinceName: 'NY',
//   localityName: 'New York',
//   streetAddress: '902 Broadway, 4th Floor',
//   organizationName: 'Nodejitsu',
//   organizationalUnitName: 'PremiumSSL Wildcard',
//   commonName: '*.nodejitsu.com'
// }

x509.parseCert(cert)

Parse subject, issuer, valid before and after date, and alternate names from certificate.

const x509 = require('@elastic.io/x509');
const cert = x509.parseCert(__dirname + '/certs/nodejitsu.com.crt');
// cert = {
//   subject: {
//     countryName: 'US',
//     postalCode: '10010',
//     stateOrProvinceName: 'NY',
//     localityName: 'New York',
//     streetAddress: '902 Broadway, 4th Floor',
//     organizationName: 'Nodejitsu',
//     organizationalUnitName: 'PremiumSSL Wildcard',
//     commonName: '*.nodejitsu.com'
//   },
//   issuer: {
//     countryName: 'GB',
//     stateOrProvinceName: 'Greater Manchester',
//     localityName: 'Salford',
//     organizationName: 'COMODO CA Limited',
//     commonName: 'COMODO High-Assurance Secure Server CA'
//   },
//   notBefore: Sun Oct 28 2012 20:00:00 GMT-0400 (EDT),
//   notAfter: Wed Nov 26 2014 18:59:59 GMT-0500 (EST),
//   altNames: [ '*.nodejitsu.com', 'nodejitsu.com' ],
//   signatureAlgorithm: 'sha1WithRSAEncryption',
//   fingerPrint: 'E4:7E:24:8E:86:D2:BE:55:C0:4D:41:A1:C2:0E:06:96:56:B9:8E:EC',
//   publicKey: {
//     algorithm: 'rsaEncryption',
//     e: '65537',
//     n: '.......'
//   }
// }

x509.verify(cert, CABundlePath, function(err, result){ /.../})

Performs basic certificate validation against a bundle of ca certificates.

It accepts an error-first callback as first argument. If the error is null, then the certificate is valid.

The error messages are the same returned by openssl: x509_verify_cert_error_string

Note: As now, this function only accepts absolute paths to existing files as arguments.

const x509 = require('@elastic.io/x509');

x509.verify(
  __dirname + '/certs/user.com.crt',
  __dirname + 'end-user-example.com.chain',
  function(err, result) { /*...*/ }
);

Examples

Checking the date to make sure the certificate is active:

const x509 = require('@elastic.io/x509');
const cert = x509.parseCert('your_cert.crt');
const date = new Date();

if (cert.notBefore > date) {
  // Certificate isn't active yet.
}

if (cert.notAfter < date) {
  // Certificate has expired.
}

License

MIT

版本与发布记录

@elastic.io/x509 共发布 3 个版本,首次发布于 2023/07/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.3latest2023/09/263 年前
1.1.22023/07/173 年前
1.1.12023/07/173 年前

关于 @elastic.io/x509 的常见问题

@elastic.io/x509 是什么?

Simple X509 certificate parser

如何安装 @elastic.io/x509?

在项目目录运行 npm install @elastic.io/x509,或使用 pnpm add / yarn add / bun add。

@elastic.io/x509 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/elastic.io__x509 是否存在。

@elastic.io/x509 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@elastic.io/x509 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@elastic.io/x509 可以商用吗?

@elastic.io/x509 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elastic.io/x509 的下载量有多少?

根据 npm 官方下载统计,@elastic.io/x509 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,355 次,日均约 145 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elastic.io/x509 还在维护吗?

最新版本 1.1.3 发布于 2023/09/26(3 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/07/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。