jwk-to-pem
Advanced tools
Comparing version 1.2.6 to 2.0.0
{ | ||
"name": "jwk-to-pem", | ||
"version": "1.2.6", | ||
"version": "2.0.0", | ||
"description": "Convert a JSON Web Key to a PEM", | ||
"main": "src/jwk-to-pem.js", | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"src" | ||
], | ||
"scripts": { | ||
@@ -7,0 +12,0 @@ "check-style": "eslint .", |
120
src/ec.js
@@ -9,12 +9,47 @@ 'use strict'; | ||
var PublicKeyInfo = require('./asn1/public-key-info'), | ||
PrivateKeyInfo = require('./asn1/private-key-info'), | ||
Version = require('./asn1/version'); | ||
var ECParameters = asn1.define('ECParameters', /* @this */ function() { | ||
this.choice({ | ||
namedCurve: this.objid() | ||
}); | ||
}); | ||
var ecPrivkeyVer1 = 1; | ||
var ECPrivateKey = asn1.define('ECPrivateKey', /* @this */ function() { | ||
this.seq().obj( | ||
this.key('version').use(Version), | ||
this.key('privateKey').octstr(), | ||
this.key('parameters').explicit(0).optional().any(), | ||
this.key('publicKey').explicit(1).optional().bitstr() | ||
); | ||
}); | ||
var curves = { | ||
'P-256': 'p256', | ||
'P-384': 'p384', | ||
'P-521': 'p521' | ||
}, | ||
oids = { | ||
'P-256': [1, 2, 840, 10045, 3, 1, 7], | ||
'P-384': [1, 3, 132, 0, 34], | ||
'P-521': [1, 3, 132, 0, 35] | ||
'P-256': 'p256', | ||
'P-384': 'p384', | ||
'P-521': 'p521' | ||
}; | ||
var oids = { | ||
'P-256': [1, 2, 840, 10045, 3, 1, 7], | ||
'P-384': [1, 3, 132, 0, 34], | ||
'P-521': [1, 3, 132, 0, 35] | ||
}; | ||
var parameters = {}; | ||
var algorithms = {}; | ||
Object.keys(oids).forEach(function(crv) { | ||
parameters[crv] = ECParameters.encode({ | ||
type: 'namedCurve', | ||
value: oids[crv] | ||
}, 'der'); | ||
algorithms[crv] = { | ||
algorithm: [1, 2, 840, 10045, 2, 1], | ||
parameters: parameters[crv] | ||
}; | ||
}); | ||
oids = null; | ||
@@ -78,14 +113,9 @@ function ecJwkToBuffer(jwk, opts) { | ||
var compact = false; | ||
var subjectPublicKey = key.getPublic(compact, 'hex'); | ||
subjectPublicKey = Buffer.from(subjectPublicKey, 'hex'); | ||
subjectPublicKey = { | ||
var publicKey = key.getPublic(compact, 'hex'); | ||
publicKey = Buffer.from(publicKey, 'hex'); | ||
publicKey = { | ||
unused: 0, | ||
data: subjectPublicKey | ||
data: publicKey | ||
}; | ||
var parameters = ECParameters.encode({ | ||
type: 'namedCurve', | ||
value: oids[crv] | ||
}, 'der'); | ||
var result; | ||
@@ -96,9 +126,13 @@ if (opts.private) { | ||
result = ECPrivateKey.encode({ | ||
version: ecPrivkeyVer1, | ||
privateKey: privateKey, | ||
parameters: parameters, | ||
publicKey: subjectPublicKey | ||
result = PrivateKeyInfo.encode({ | ||
version: 0, | ||
privateKeyAlgorithm: algorithms[crv], | ||
privateKey: ECPrivateKey.encode({ | ||
version: ecPrivkeyVer1, | ||
privateKey: privateKey, | ||
parameters: parameters[crv], | ||
publicKey: publicKey | ||
}, 'der') | ||
}, 'pem', { | ||
label: 'EC PRIVATE KEY' | ||
label: 'PRIVATE KEY' | ||
}); | ||
@@ -108,8 +142,5 @@ | ||
} else { | ||
result = SubjectPublicKeyInfo.encode({ | ||
algorithm: { | ||
algorithm: [1, 2, 840, 10045, 2, 1], | ||
parameters: parameters | ||
}, | ||
subjectPublicKey: subjectPublicKey | ||
result = PublicKeyInfo.encode({ | ||
algorithm: algorithms[crv], | ||
PublicKey: publicKey | ||
}, 'pem', { | ||
@@ -129,33 +160,2 @@ label: 'PUBLIC KEY' | ||
var ECParameters = asn1.define('ECParameters', /* @this */ function() { | ||
this.choice({ | ||
namedCurve: this.objid() | ||
}); | ||
}); | ||
var ecPrivkeyVer1 = 1; | ||
var ECPrivateKey = asn1.define('ECPrivateKey', /* @this */ function() { | ||
this.seq().obj( | ||
this.key('version').int(), | ||
this.key('privateKey').octstr(), | ||
this.key('parameters').explicit(0).optional().any(), | ||
this.key('publicKey').explicit(1).optional().bitstr() | ||
); | ||
}); | ||
var AlgorithmIdentifier = asn1.define('AlgorithmIdentifier', /* @this */ function() { | ||
this.seq().obj( | ||
this.key('algorithm').objid(), | ||
this.key('parameters').optional().any() | ||
); | ||
}); | ||
var SubjectPublicKeyInfo = asn1.define('SubjectPublicKeyInfo', /* @this */ function() { | ||
this.seq().obj( | ||
this.key('algorithm').use(AlgorithmIdentifier), | ||
this.key('subjectPublicKey').bitstr() | ||
); | ||
}); | ||
module.exports = ecJwkToBuffer; |
@@ -7,5 +7,5 @@ 'use strict'; | ||
var Version = asn1.define('Version', /* @this */ function() { | ||
this.int(); | ||
}); | ||
var PublicKeyInfo = require('./asn1/public-key-info'), | ||
PrivateKeyInfo = require('./asn1/private-key-info'), | ||
Version = require('./asn1/version'); | ||
@@ -33,2 +33,7 @@ var RSAPrivateKey = asn1.define('RSAPrivateKey', /* @this */ function() { | ||
var algorithm = { | ||
algorithm: [1, 2, 840, 113549, 1, 1, 1], | ||
parameters: [5, 0] | ||
}; | ||
function rsaJwkToBuffer(jwk, opts) { | ||
@@ -71,21 +76,31 @@ if ('string' !== typeof jwk.e) { | ||
if (opts.private) { | ||
pem = RSAPrivateKey.encode({ | ||
pem = PrivateKeyInfo.encode({ | ||
version: 0, | ||
modulus: b64ToBn(jwk.n, false), | ||
publicExponent: b64ToBn(jwk.e, false), | ||
privateExponent: b64ToBn(jwk.d, true), | ||
prime1: b64ToBn(jwk.p, true), | ||
prime2: b64ToBn(jwk.q, true), | ||
exponent1: b64ToBn(jwk.dp, true), | ||
exponent2: b64ToBn(jwk.dq, true), | ||
coefficient: b64ToBn(jwk.qi, true) | ||
privateKeyAlgorithm: algorithm, | ||
privateKey: RSAPrivateKey.encode({ | ||
version: 0, | ||
modulus: b64ToBn(jwk.n, false), | ||
publicExponent: b64ToBn(jwk.e, false), | ||
privateExponent: b64ToBn(jwk.d, true), | ||
prime1: b64ToBn(jwk.p, true), | ||
prime2: b64ToBn(jwk.q, true), | ||
exponent1: b64ToBn(jwk.dp, true), | ||
exponent2: b64ToBn(jwk.dq, true), | ||
coefficient: b64ToBn(jwk.qi, true) | ||
}, 'der') | ||
}, 'pem', { | ||
label: 'RSA PRIVATE KEY' | ||
label: 'PRIVATE KEY' | ||
}); | ||
} else { | ||
pem = RSAPublicKey.encode({ | ||
modulus: b64ToBn(jwk.n, false), | ||
publicExponent: b64ToBn(jwk.e, false) | ||
pem = PublicKeyInfo.encode({ | ||
algorithm: algorithm, | ||
PublicKey: { | ||
unused: 0, | ||
data: RSAPublicKey.encode({ | ||
modulus: b64ToBn(jwk.n, false), | ||
publicExponent: b64ToBn(jwk.e, false) | ||
}, 'der') | ||
} | ||
}, 'pem', { | ||
label: 'RSA PUBLIC KEY' | ||
label: 'PUBLIC KEY' | ||
}); | ||
@@ -92,0 +107,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22342
11
291