mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2025-11-24 14:35:51 +00:00
Use JS fallback code for RSA decryption on Node if PKCS#1 is not supported
Necessary as Node v18.19.1, 20.11.1 and 21.6.2 have disabled support for PKCS#1 decryption.
This commit is contained in:
parent
7003d19c71
commit
3d664e7b26
@ -140,8 +140,15 @@ export async function encrypt(data, n, e) {
|
|||||||
* @async
|
* @async
|
||||||
*/
|
*/
|
||||||
export async function decrypt(data, n, e, d, p, q, u, randomPayload) {
|
export async function decrypt(data, n, e, d, p, q, u, randomPayload) {
|
||||||
if (util.getNodeCrypto()) {
|
// Node v18.19.1, 20.11.1 and 21.6.2 have disabled support for PKCS#1 decryption,
|
||||||
return nodeDecrypt(data, n, e, d, p, q, u, randomPayload);
|
// and we want to avoid checking the error type to decide if the random payload
|
||||||
|
// should indeed be returned.
|
||||||
|
if (util.getNodeCrypto() && !randomPayload) {
|
||||||
|
try {
|
||||||
|
return await nodeDecrypt(data, n, e, d, p, q, u);
|
||||||
|
} catch (err) {
|
||||||
|
util.printDebugError(err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return bnDecrypt(data, n, e, d, p, q, u, randomPayload);
|
return bnDecrypt(data, n, e, d, p, q, u, randomPayload);
|
||||||
}
|
}
|
||||||
@ -443,7 +450,7 @@ async function bnEncrypt(data, n, e) {
|
|||||||
return data.modExp(e, n).toUint8Array('be', n.byteLength());
|
return data.modExp(e, n).toUint8Array('be', n.byteLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
async function nodeDecrypt(data, n, e, d, p, q, u, randomPayload) {
|
async function nodeDecrypt(data, n, e, d, p, q, u) {
|
||||||
const { default: BN } = await import('bn.js');
|
const { default: BN } = await import('bn.js');
|
||||||
|
|
||||||
const pBNum = new BN(p);
|
const pBNum = new BN(p);
|
||||||
@ -477,9 +484,6 @@ async function nodeDecrypt(data, n, e, d, p, q, u, randomPayload) {
|
|||||||
try {
|
try {
|
||||||
return new Uint8Array(nodeCrypto.privateDecrypt(key, data));
|
return new Uint8Array(nodeCrypto.privateDecrypt(key, data));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (randomPayload) {
|
|
||||||
return randomPayload;
|
|
||||||
}
|
|
||||||
throw new Error('Decryption error');
|
throw new Error('Decryption error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user