An open API service indexing awesome lists of open source software.

https://github.com/fidm/asn1

ASN.1/DER, PEM for Node.js
https://github.com/fidm/asn1

asn1 der pem

Last synced: 6 months ago
JSON representation

ASN.1/DER, PEM for Node.js

Awesome Lists containing this project

README

          

# [@fidm/asn1](https://github.com/fidm/asn1)
ASN.1/DER, PEM for Node.js.

[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Downloads][downloads-image]][downloads-url]

## Install

```
npm i --save @fidm/asn1
```

## Documentation

https://fidm.github.io/asn1/

## Dependents

[@fidm/x509](https://github.com/fidm/x509)

## Example

### Parse a private key from PEM file with ASN.1 Template
```js
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:
// } ] }>,
// privateKeyVersion: ,
// privateKeyOID:
// ,
// privateKey:
// }> }
```

### Build PKCS#8 private key ASN1 object from PKCS#1 private key ASN1 object
```js
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),
])
```

### Parse a certificate from PEM file
```js
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)
// ,
// 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:
// } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.14' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.17' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// } ] },
// { 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: } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.37' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.31' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '2.5.29.32' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// } ] },
// { class: 'UNIVERSAL',
// tag: 'SEQUENCE',
// value:
// [ { class: 'UNIVERSAL', tag: 'OID', value: '1.3.6.1.5.5.7.1.1' },
// { class: 'UNIVERSAL',
// tag: 'OCTETSTRING',
// value:
// } ] },
// { 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: } ] },
// { 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:
// } ] } ] } ] } ] },
// { 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:
// ,
// bitLen: 2048 } } ] }>
```

### License
@fidm/asn1 is licensed under the [MIT](https://github.com/fidm/asn1/blob/master/LICENSE) license.
Copyright © 2018-2019 FIdM.

[npm-url]: https://www.npmjs.com/package/@fidm/asn1
[npm-image]: https://img.shields.io/npm/v/@fidm/asn1.svg

[travis-url]: https://travis-ci.org/fidm/asn1
[travis-image]: http://img.shields.io/travis/fidm/asn1.svg

[downloads-url]: https://npmjs.org/package/@fidm/asn1
[downloads-image]: https://img.shields.io/npm/dm/@fidm/asn1.svg?style=flat-square