fidm / asn1 Goto Github PK
View Code? Open in Web Editor NEWASN.1/DER, PEM for Node.js
Home Page: https://fidm.github.io/asn1/
License: MIT License
ASN.1/DER, PEM for Node.js
Home Page: https://fidm.github.io/asn1/
License: MIT License
Hi, I'm working on p12 generation for ECC keytype pem and certs.
I'm getting error when I'm passing EC private key to below logic, can you guide me if you have any idea?
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('ecctestPK.pem'))[0];
console.log(rootkey)
const captures = ASN1.parseDERWithTemplate(rootkey.body, privateKeyValidator);
console.log("******************")
console.log(captures)```
ASN1.Spec doesn't seem to work correctly when converting to DER.
I'm using Node v12.16.1 on Linux.
Running ASN1.Spec(0, []).toDER()
results in an error
RangeError [ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= -128 and <= 127. Received 160
at writeU_Int8 (internal/buffer.js:728:11)
at Buffer.writeInt8 (internal/buffer.js:859:10)
at ASN1.toDER (/asn1-test/node_modules/@fidm/asn1/build/asn1.js:859:13)
at repl:1:18
at Script.runInThisContext (vm.js:120:20)
at REPLServer.defaultEval (repl.js:430:29)
at bound (domain.js:426:14)
at REPLServer.runBound [as eval] (domain.js:439:12)
at REPLServer.onLine (repl.js:758:10)
at REPLServer.emit (events.js:323:22) {
code: 'ERR_OUT_OF_RANGE'
Seems to be caused by first byte (b1
) in src/asn1.ts:959 being unsigned (which I believe is JS standard behaviour), and later on is expected to a signed by buf.writeInt8.
CONTEXT_SPECIFIC = 0x80
0x80 | 0x20 == 0xa0
0xa0 unsigned is 160, while signed is -96.
Expected results is a <Buffer <a0 00>
.
I resolved the issue by changing line 964 in src/asn1.ts to buf.writeUInt8(b1, 0)
instead of having buf.writeInt8(b1, 0)
, so that we're parsing the integer as unsigned.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.