Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@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.
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);
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 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 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.
npm i --save @fidm/asn1
const fs = require('fs')
const { PEM, ASN1, Class, Tag } = require('@fidm/asn1')
// ASN.1 Template https://tools.ietf.org/html/rfc5208
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)
// { privateKeyInfo:
// <ASN1 { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'INTEGER', value: 0 },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '1.2.840.113549.1.1.1' },
// { class: 'UNIVERSAL', tag: 'NULL', value: null } ] },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 30 82 04 a5 02 01 00 02 82 01 01 00 bf 9a 15 d6 cd cd ba ce d2 20 d8 3b a2 6b b9 03 1b 9e 12 02 bd ee 68 79 3d 4d e1 81 9a 65 89 21 5a 11 29 8b da a2 ... > } ] }>,
// privateKeyVersion: <ASN1 { class: 'UNIVERSAL', tag: 'INTEGER', value: 0 }>,
// privateKeyOID:
// <ASN1 { class: 'UNIVERSAL', tag: 'OID', value: '1.2.840.113549.1.1.1' }>,
// privateKey:
// <ASN1 { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 30 82 04 a5 02 01 00 02 82 01 01 00 bf 9a 15 d6 cd cd ba ce d2 20 d8 3b a2 6b b9 03 1b 9e 12 02 bd ee 68 79 3d 4d e1 81 9a 65 89 21 5a 11 29 8b da a2 ... > }> }
const { ASN1, Class, Tag } = require('@fidm/asn1')
const rsaPrivateKeyASN1 = getSomeRSAPrivateKeyASN1()
const privateKeyASN1 = ASN1.Seq([
// Version (INTEGER)
rsaPrivateKeyASN1.value[0],
// AlgorithmIdentifier
ASN1.Seq([
// algorithm
ASN1.OID('1.2.840.113549.1.1.1'),
// optional parameters
ASN1.Null(),
]),
// PrivateKey
new ASN1(Class.UNIVERSAL, Tag.OCTETSTRING, rsaPrivateKeyASN1.DER),
])
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)
// <ASN1 { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'CONTEXT_SPECIFIC',
// tag: 0,
// value: [ { class: 'UNIVERSAL', tag: 'INTEGER', value: 2 } ] },
// { class: 'UNIVERSAL',
// tag: 'INTEGER',
// value: '0a0630427f5bbced6957396593b6451f' },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'OID',
// value: '1.2.840.113549.1.1.11' },
// { class: 'UNIVERSAL', tag: 'NULL', value: null } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.6' },
// { class: 'UNIVERSAL', tag: 'PRINTABLESTRING', value: 'US' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.10' },
// { class: 'UNIVERSAL',
// tag: 'PRINTABLESTRING',
// value: 'DigiCert Inc' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.11' },
// { class: 'UNIVERSAL',
// tag: 'PRINTABLESTRING',
// value: 'www.digicert.com' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.3' },
// { class: 'UNIVERSAL',
// tag: 'PRINTABLESTRING',
// value: 'DigiCert SHA2 Extended Validation Server CA' } ] } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'UTCTIME',
// value: 2018-05-08T00:00:00.000Z },
// { class: 'UNIVERSAL',
// tag: 'UTCTIME',
// value: 2020-06-03T12:00:00.000Z } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.15' },
// { class: 'UNIVERSAL',
// tag: 'UTF8',
// value: 'Private Organization' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'OID',
// value: '1.3.6.1.4.1.311.60.2.1.3' },
// { class: 'UNIVERSAL', tag: 'PRINTABLESTRING', value: 'US' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'OID',
// value: '1.3.6.1.4.1.311.60.2.1.2' },
// { class: 'UNIVERSAL', tag: 'PRINTABLESTRING', value: 'Delaware' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.5' },
// { class: 'UNIVERSAL', tag: 'PRINTABLESTRING', value: '5157550' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.6' },
// { class: 'UNIVERSAL', tag: 'PRINTABLESTRING', value: 'US' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.8' },
// { class: 'UNIVERSAL',
// tag: 'PRINTABLESTRING',
// value: 'California' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.7' },
// { class: 'UNIVERSAL',
// tag: 'PRINTABLESTRING',
// value: 'San Francisco' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.10' },
// { class: 'UNIVERSAL',
// tag: 'PRINTABLESTRING',
// value: 'GitHub, Inc.' } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SET',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.4.3' },
// { class: 'UNIVERSAL',
// tag: 'PRINTABLESTRING',
// value: 'github.com' } ] } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '1.2.840.113549.1.1.1' },
// { class: 'UNIVERSAL', tag: 'NULL', value: null } ] },
// { class: 'UNIVERSAL',
// tag: 'BITSTRING',
// value:
// BitString {
// buf:
// <Buffer 30 82 01 0a 02 82 01 01 00 c6 3c aa f2 3c 97 0c 3a c1 4f 28 ad 72 70 7d d3 ce b9 b5 60 73 a4 74 9b 8a 77 46 fd 7a 98 42 4c c5 30 19 57 9a a9 33 0b e1 ... >,
// bitLen: 2160 } } ] },
// { class: 'CONTEXT_SPECIFIC',
// tag: 3,
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.35' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 30 16 80 14 3d d3 50 a5 d6 a0 ad ee f3 4a 60 0a 65 d3 21 d4 f8 f8 d6 0f> } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.14' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 04 14 c9 c2 53 61 66 9d 5f ab 25 f4 26 cd 0f 38 9a a8 49 ea 48 a9> } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.17' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 30 1c 82 0a 67 69 74 68 75 62 2e 63 6f 6d 82 0e 77 77 77 2e 67 69 74 68 75 62 2e 63 6f 6d> } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.15' },
// { class: 'UNIVERSAL', tag: 'BOOLEAN', value: true },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value: <Buffer 03 02 05 a0> } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.37' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 30 14 06 08 2b 06 01 05 05 07 03 01 06 08 2b 06 01 05 05 07 03 02> } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.31' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 30 6c 30 34 a0 32 a0 30 86 2e 68 74 74 70 3a 2f 2f 63 72 6c 33 2e 64 69 67 69 63 65 72 74 2e 63 6f 6d 2f 73 68 61 32 2d 65 76 2d 73 65 72 76 65 72 2d ... > } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.32' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 30 42 30 37 06 09 60 86 48 01 86 fd 6c 02 01 30 2a 30 28 06 08 2b 06 01 05 05 07 02 01 16 1c 68 74 74 70 73 3a 2f 2f 77 77 77 2e 64 69 67 69 63 65 72 ... > } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '1.3.6.1.5.5.7.1.1' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 30 7a 30 24 06 08 2b 06 01 05 05 07 30 01 86 18 68 74 74 70 3a 2f 2f 6f 63 73 70 2e 64 69 67 69 63 65 72 74 2e 63 6f 6d 30 52 06 08 2b 06 01 05 05 07 ... > } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.19' },
// { class: 'UNIVERSAL', tag: 'BOOLEAN', value: true },
// { class: 'UNIVERSAL', tag: 'OCTETSTRING', value: <Buffer 30 00> } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'OID',
// value: '1.3.6.1.4.1.11129.2.4.2' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// <Buffer 04 82 01 6a 01 68 00 76 00 a4 b9 09 90 b4 18 58 14 87 bb 13 a2 cc 67 70 0a 3c 35 98 04 f9 1b df b8 e3 77 cd 0e c8 0d dc 10 00 00 01 63 41 62 6d 0a 00 ... > } ] } ] } ] } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL',
// tag: 'OID',
// value: '1.2.840.113549.1.1.11' },
// { class: 'UNIVERSAL', tag: 'NULL', value: null } ] },
// { class: 'UNIVERSAL',
// tag: 'BITSTRING',
// value:
// BitString {
// buf:
// <Buffer 70 0f 5a 96 a7 58 e5 bf 8a 9d a8 27 98 2b 00 7f 26 a9 07 da ba 7b 82 54 4f af 69 cf bc f2 59 03 2b f2 d5 74 58 25 d8 1e a4 20 76 62 60 29 73 2a d7 dc ... >,
// bitLen: 2048 } } ] }>
@fidm/asn1 is licensed under the MIT license. Copyright © 2018-2019 FIdM.
FAQs
ASN.1/DER, PEM for Node.js
We found that @fidm/asn1 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.