PeculiarVentures / PKI.js

PKI.js is a pure JavaScript library implementing the formats that are used in PKI applications (signing, encryption, certificate requests, OCSP and TSP requests/responses). It is built on WebCrypto (Web Cryptography API) and requires no plug-ins.
http://pkijs.org
Other
1.25k stars 204 forks source link

Example of x509 v3 Certificate Extensions Decoding #320

Closed yaroslava-tkachuk closed 3 years ago

yaroslava-tkachuk commented 3 years ago

Hi,

I have been searching for any working example of x509 v3 certificate extensions decoding - unfortunately, with no luck. I am createing a Pkijs Certificate object from raw data and getting extensions array. extnValue is an OctetString object which I am struggling to decode to an integer (JS number type). Would highly appreciate any example on how to decode extensions values.

I am using only Pkijs and Asn1js libraries. Extending them with asn1js/org/pkijs/common and pkijs/org/pkijs/x509_schema:

`var merge = require("node.extend");

var common = require("asn1js/org/pkijs/common"); var _asn1js = require("asn1js"); var _pkijs = require("pkijs"); var _x509schema = require("pkijs/org/pkijs/x509_schema");

// #region Merging function/object declarations for ASN1js and PKIjs var asn1js = merge(true, _asn1js, common);

var x509schema = merge(true, _x509schema, asn1js);

var pkijs_1 = merge(true, _pkijs, asn1js); var pkijs = merge(true, pkijs_1, x509schema);`

causes an error - Error: EMFILE: Too many open files (on WIndows).

Will be really grateful for any advise / sample extensions decoding code snippet.

Kind regard, Yari

microshine commented 3 years ago

Try @peculiar/x509. It supports X509 extensions and allows extending it by registering your own

microshine commented 3 years ago

PKIjs implements X509 extensions too. See source code how it parses well-known extensions

YuryStrozhevsky commented 3 years ago

@yaroslava-tkachuk Example

yaroslava-tkachuk commented 3 years ago

Thanks a lot for the answers! Eventually, I have figured out that I was getting already decoded extensions, but there were some additional bitstring values at the beginnig (probably, some ASN1 related stuff). .extnValue.toJSON().valueBeforeDecode and skipping additional data worked for me to get a hex string representing my ASN1:INTEGER extension values. Again, thanks a lot for the fast replies!