如何使用 javascripts forge 从 p12 证书获取证书信息?

How do I get certificate information from p12 certificate using javascripts forge?

我得到 p12

var p12Der = ...
var p12Asn1 = forge.asn1.fromDer(p12Der);
var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, password);

如何从证书中获取信息,例如主题名称、发行者名称、序列号...

p12 结构包含 "bags" 不同类型。一种这样的类型是证书袋。您可以像这样从 forge 中的 p12 结构请求所有证书包:


// get bags by type
var bags = p12.getBags({bagType: forge.pki.oids.certBag});
// bags are key'd by bagType and each bagType key's value
// is an array of matches (in this case, certificate objects)
var cert = bags[forge.pki.oids.certBag][0];

获得证书对象后,您可以console.log查看有哪些信息可用。以下是一些证书属性的快速示例:


// version
cert.version;

// serial number
cert.serialNumber;

// validity
cert.validity;

// issuer
cert.issuer;
// all issuer attributes
cert.issuer.attributes;
// get, for example, common name via its short name "CN"
cert.issuer.getField('CN');

// subject
cert.subject;
// all subject attributes
cert.subject.attributes;
// get, for example, common name via its short name "CN"
cert.subject.getField('CN');