What is @fidm/asn1?
@fidm/asn1 is a Node.js library for encoding and decoding ASN.1 (Abstract Syntax Notation One) data structures. ASN.1 is a standard interface description language for defining data structures that can be serialized and deserialized in a cross-platform way. This package is useful for working with various cryptographic standards and protocols that use ASN.1 encoding, such as X.509 certificates.
What are @fidm/asn1's main functionalities?
Encoding ASN.1 Data
This feature allows you to encode data into ASN.1 format. The code sample demonstrates how to create a BER (Basic Encoding Rules) writer, start a sequence, write an integer, and end the sequence.
const asn1 = require('@fidm/asn1');
const data = asn1.BerWriter();
data.startSequence();
data.writeInt(12345);
data.endSequence();
console.log(data.buffer);
Decoding ASN.1 Data
This feature allows you to decode ASN.1 encoded data. The code sample demonstrates how to create a BER reader, read a sequence, and extract an integer value from the encoded data.
const asn1 = require('@fidm/asn1');
const buffer = Buffer.from([0x30, 0x03, 0x02, 0x01, 0x01]);
const reader = new asn1.BerReader(buffer);
reader.readSequence();
const intValue = reader.readInt();
console.log(intValue);
Parsing X.509 Certificates
This feature allows you to parse X.509 certificates, which are commonly used in SSL/TLS. The code sample demonstrates how to read a certificate file and parse its ASN.1 structure.
const asn1 = require('@fidm/asn1');
const fs = require('fs');
const certBuffer = fs.readFileSync('path/to/certificate.crt');
const reader = new asn1.BerReader(certBuffer);
reader.readSequence();
const cert = reader.readSequence();
console.log(cert);
Other packages similar to @fidm/asn1
asn1js
asn1js is a JavaScript library for encoding and decoding ASN.1 data structures. It is designed to work in both Node.js and browser environments. Compared to @fidm/asn1, asn1js offers a more comprehensive set of features and better support for various ASN.1 encoding rules, including DER (Distinguished Encoding Rules) and CER (Canonical Encoding Rules).
node-forge
node-forge is a comprehensive cryptographic library for Node.js and browsers. It includes support for ASN.1 encoding and decoding, as well as a wide range of cryptographic operations such as encryption, decryption, and digital signatures. Compared to @fidm/asn1, node-forge provides a broader set of cryptographic functionalities beyond just ASN.1 handling.
asn1
asn1 is a Node.js library for encoding and decoding ASN.1 data structures. It is part of the node-browserify project and is designed to be lightweight and easy to use. Compared to @fidm/asn1, the asn1 package is more focused on simplicity and ease of integration with other Node.js modules.
ASN.1/DER, PEM for Node.js.
Install
npm i --save @fidm/asn1
Documentation
https://fidm.github.io/asn1/
Dependents
@fidm/x509
Example
Parse a private key from PEM file with ASN.1 Template
const fs = require('fs')
const { PEM, ASN1, Class, Tag } = require('@fidm/asn1')
const privateKeyValidator = {
name: 'PrivateKeyInfo',
class: Class.UNIVERSAL,
tag: Tag.SEQUENCE,
capture: 'privateKeyInfo',
value: [{
name: 'PrivateKeyInfo.Version',
class: Class.UNIVERSAL,
tag: Tag.INTEGER,
capture: 'privateKeyVersion'
}, {
name: 'PrivateKeyInfo.AlgorithmIdentifier',
class: Class.UNIVERSAL,
tag: Tag.SEQUENCE,
value: [{
name: 'PrivateKeyAlgorithmIdentifier.algorithm',
class: Class.UNIVERSAL,
tag: Tag.OID,
capture: 'privateKeyOID'
}]
}, {
name: 'PrivateKeyInfo.PrivateKey',
class: Class.UNIVERSAL,
tag: Tag.OCTETSTRING,
capture: 'privateKey'
}]
}
const rootkey = PEM.parse(fs.readFileSync('./test/cert/rootkey.pem'))[0]
const captures = ASN1.parseDERWithTemplate(rootkey.body, privateKeyValidator)
console.log(captures)
Build PKCS#8 private key ASN1 object from PKCS#1 private key ASN1 object
const { ASN1, Class, Tag } = require('@fidm/asn1')
const rsaPrivateKeyASN1 = getSomeRSAPrivateKeyASN1()
const privateKeyASN1 = ASN1.Seq([
rsaPrivateKeyASN1.value[0],
ASN1.Seq([
ASN1.OID('1.2.840.113549.1.1.1'),
ASN1.Null(),
]),
new ASN1(Class.UNIVERSAL, Tag.OCTETSTRING, rsaPrivateKeyASN1.DER),
])
Parse a certificate from PEM file
const fs = require('fs')
const { PEM, ASN1 } = require('@fidm/asn1')
const pems = PEM.parse(fs.readFileSync('./test/cert/github.crt'))
const asn1 = ASN1.fromDER(pems[0].body)
console.log(asn1)
License
@fidm/asn1 is licensed under the MIT license.
Copyright © 2018-2019 FIdM.