This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: remove asn1.js and use node-forge (#166)
* perf: remove asn1.js from rsa * fix: tweaks * fix: it works, but I do not know 100% why * chore: remove asn1.js * fix: ensure jwk params encoded as uint * fix: util tests * fix: zero pad base64urlToBuffer * fix: more zero pad * test: add round trip test * test: base64url to Buffer with padding
- Loading branch information
Alan Shaw
authored
Feb 26, 2020
1 parent
0f4c533
commit 00477e3
Showing
6 changed files
with
114 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,68 @@ | ||
'use strict' | ||
|
||
const asn1 = require('asn1.js') | ||
|
||
const util = require('./../util') | ||
const toBase64 = util.toBase64 | ||
const toBn = util.toBn | ||
|
||
const RSAPrivateKey = asn1.define('RSAPrivateKey', function () { | ||
this.seq().obj( | ||
this.key('version').int(), | ||
this.key('modulus').int(), | ||
this.key('publicExponent').int(), | ||
this.key('privateExponent').int(), | ||
this.key('prime1').int(), | ||
this.key('prime2').int(), | ||
this.key('exponent1').int(), | ||
this.key('exponent2').int(), | ||
this.key('coefficient').int() | ||
) | ||
}) | ||
|
||
const AlgorithmIdentifier = asn1.define('AlgorithmIdentifier', function () { | ||
this.seq().obj( | ||
this.key('algorithm').objid({ | ||
'1.2.840.113549.1.1.1': 'rsa' | ||
}), | ||
this.key('none').optional().null_(), | ||
this.key('curve').optional().objid(), | ||
this.key('params').optional().seq().obj( | ||
this.key('p').int(), | ||
this.key('q').int(), | ||
this.key('g').int() | ||
) | ||
) | ||
}) | ||
|
||
const PublicKey = asn1.define('RSAPublicKey', function () { | ||
this.seq().obj( | ||
this.key('algorithm').use(AlgorithmIdentifier), | ||
this.key('subjectPublicKey').bitstr() | ||
) | ||
}) | ||
|
||
const RSAPublicKey = asn1.define('RSAPublicKey', function () { | ||
this.seq().obj( | ||
this.key('modulus').int(), | ||
this.key('publicExponent').int() | ||
) | ||
}) | ||
const { Buffer } = require('buffer') | ||
require('node-forge/lib/asn1') | ||
require('node-forge/lib/rsa') | ||
const forge = require('node-forge/lib/forge') | ||
const { bigIntegerToUintBase64url, base64urlToBigInteger } = require('./../util') | ||
|
||
// Convert a PKCS#1 in ASN1 DER format to a JWK key | ||
exports.pkcs1ToJwk = function (bytes) { | ||
const asn1 = RSAPrivateKey.decode(bytes, 'der') | ||
const asn1 = forge.asn1.fromDer(bytes.toString('binary')) | ||
const privateKey = forge.pki.privateKeyFromAsn1(asn1) | ||
|
||
// https://tools.ietf.org/html/rfc7518#section-6.3.1 | ||
return { | ||
kty: 'RSA', | ||
n: toBase64(asn1.modulus), | ||
e: toBase64(asn1.publicExponent), | ||
d: toBase64(asn1.privateExponent), | ||
p: toBase64(asn1.prime1), | ||
q: toBase64(asn1.prime2), | ||
dp: toBase64(asn1.exponent1), | ||
dq: toBase64(asn1.exponent2), | ||
qi: toBase64(asn1.coefficient), | ||
n: bigIntegerToUintBase64url(privateKey.n), | ||
e: bigIntegerToUintBase64url(privateKey.e), | ||
d: bigIntegerToUintBase64url(privateKey.d), | ||
p: bigIntegerToUintBase64url(privateKey.p), | ||
q: bigIntegerToUintBase64url(privateKey.q), | ||
dp: bigIntegerToUintBase64url(privateKey.dP), | ||
dq: bigIntegerToUintBase64url(privateKey.dQ), | ||
qi: bigIntegerToUintBase64url(privateKey.qInv), | ||
alg: 'RS256', | ||
kid: '2011-04-29' | ||
} | ||
} | ||
|
||
// Convert a JWK key into PKCS#1 in ASN1 DER format | ||
exports.jwkToPkcs1 = function (jwk) { | ||
return RSAPrivateKey.encode({ | ||
version: 0, | ||
modulus: toBn(jwk.n), | ||
publicExponent: toBn(jwk.e), | ||
privateExponent: toBn(jwk.d), | ||
prime1: toBn(jwk.p), | ||
prime2: toBn(jwk.q), | ||
exponent1: toBn(jwk.dp), | ||
exponent2: toBn(jwk.dq), | ||
coefficient: toBn(jwk.qi) | ||
}, 'der') | ||
const asn1 = forge.pki.privateKeyToAsn1({ | ||
n: base64urlToBigInteger(jwk.n), | ||
e: base64urlToBigInteger(jwk.e), | ||
d: base64urlToBigInteger(jwk.d), | ||
p: base64urlToBigInteger(jwk.p), | ||
q: base64urlToBigInteger(jwk.q), | ||
dP: base64urlToBigInteger(jwk.dp), | ||
dQ: base64urlToBigInteger(jwk.dq), | ||
qInv: base64urlToBigInteger(jwk.qi) | ||
}) | ||
|
||
return Buffer.from(forge.asn1.toDer(asn1).getBytes(), 'binary') | ||
} | ||
|
||
// Convert a PKCIX in ASN1 DER format to a JWK key | ||
exports.pkixToJwk = function (bytes) { | ||
const ndata = PublicKey.decode(bytes, 'der') | ||
const asn1 = RSAPublicKey.decode(ndata.subjectPublicKey.data, 'der') | ||
const asn1 = forge.asn1.fromDer(bytes.toString('binary')) | ||
const publicKey = forge.pki.publicKeyFromAsn1(asn1) | ||
|
||
return { | ||
kty: 'RSA', | ||
n: toBase64(asn1.modulus), | ||
e: toBase64(asn1.publicExponent), | ||
n: bigIntegerToUintBase64url(publicKey.n), | ||
e: bigIntegerToUintBase64url(publicKey.e), | ||
alg: 'RS256', | ||
kid: '2011-04-29' | ||
} | ||
} | ||
|
||
// Convert a JWK key to PKCIX in ASN1 DER format | ||
exports.jwkToPkix = function (jwk) { | ||
return PublicKey.encode({ | ||
algorithm: { | ||
algorithm: 'rsa', | ||
none: null | ||
}, | ||
subjectPublicKey: { | ||
data: RSAPublicKey.encode({ | ||
modulus: toBn(jwk.n), | ||
publicExponent: toBn(jwk.e) | ||
}, 'der') | ||
} | ||
}, 'der') | ||
const asn1 = forge.pki.publicKeyToAsn1({ | ||
n: base64urlToBigInteger(jwk.n), | ||
e: base64urlToBigInteger(jwk.e) | ||
}) | ||
|
||
return Buffer.from(forge.asn1.toDer(asn1).getBytes(), 'binary') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,55 @@ | ||
'use strict' | ||
|
||
const BN = require('asn1.js').bignum | ||
const { Buffer } = require('buffer') | ||
require('node-forge/lib/util') | ||
require('node-forge/lib/jsbn') | ||
const forge = require('node-forge/lib/forge') | ||
|
||
// Convert a BN.js instance to a base64 encoded string without padding | ||
// Adapted from https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#appendix-C | ||
exports.toBase64 = function toBase64 (bn, len) { | ||
// if len is defined then the bytes are leading-0 padded to the length | ||
const s = bn.toArrayLike(Buffer, 'be', len).toString('base64') | ||
exports.bigIntegerToUintBase64url = (num, len) => { | ||
// Call `.abs()` to convert to unsigned | ||
let buf = Buffer.from(num.abs().toByteArray()) // toByteArray converts to big endian | ||
|
||
// toByteArray() gives us back a signed array, which will include a leading 0 | ||
// byte if the most significant bit of the number is 1: | ||
// https://docs.microsoft.com/en-us/windows/win32/seccertenroll/about-integer | ||
// Our number will always be positive so we should remove the leading padding. | ||
buf = buf[0] === 0 ? buf.slice(1) : buf | ||
|
||
if (len != null) { | ||
if (buf.length > len) throw new Error('byte array longer than desired length') | ||
buf = Buffer.concat([Buffer.alloc(len - buf.length), buf]) | ||
} | ||
|
||
return s | ||
.replace(/(=*)$/, '') // Remove any trailing '='s | ||
return exports.bufferToBase64url(buf) | ||
} | ||
|
||
// Convert a Buffer to a base64 encoded string without padding | ||
// Adapted from https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#appendix-C | ||
exports.bufferToBase64url = buf => { | ||
return buf | ||
.toString('base64') | ||
.split('=')[0] // Remove any trailing '='s | ||
.replace(/\+/g, '-') // 62nd char of encoding | ||
.replace(/\//g, '_') // 63rd char of encoding | ||
} | ||
|
||
// Convert a base64 encoded string to a BN.js instance | ||
exports.toBn = function toBn (str) { | ||
return new BN(Buffer.from(str, 'base64')) | ||
// Convert a base64url encoded string to a BigInteger | ||
exports.base64urlToBigInteger = str => { | ||
const buf = exports.base64urlToBuffer(str) | ||
return new forge.jsbn.BigInteger(buf.toString('hex'), 16) | ||
} | ||
|
||
exports.base64urlToBuffer = (str, len) => { | ||
str = (str + '==='.slice((str.length + 3) % 4)) | ||
.replace(/-/g, '+') | ||
.replace(/_/g, '/') | ||
|
||
let buf = Buffer.from(str, 'base64') | ||
|
||
if (len != null) { | ||
if (buf.length > len) throw new Error('byte array longer than desired length') | ||
buf = Buffer.concat([Buffer.alloc(len - buf.length), buf]) | ||
} | ||
|
||
return buf | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters