|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | + |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +const fixtures = require('../common/fixtures'); |
| 8 | +const assert = require('assert'); |
| 9 | +const crypto = require('crypto'); |
| 10 | + |
| 11 | +const { subtle } = globalThis.crypto; |
| 12 | + |
| 13 | +// Regression test for https://github.com/nodejs/node/issues/57553. |
| 14 | +{ |
| 15 | + const privateKey = crypto.createPrivateKey(fixtures.readKey('rsa_private.pem', 'ascii')); |
| 16 | + const publicKey = crypto.createPublicKey(fixtures.readKey('rsa_public.pem', 'ascii')); |
| 17 | + |
| 18 | + const data = Buffer.alloc(0); |
| 19 | + { |
| 20 | + |
| 21 | + const ciphertext = crypto.publicEncrypt({ |
| 22 | + padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, |
| 23 | + key: publicKey, |
| 24 | + }, data); |
| 25 | + |
| 26 | + const plaintext = crypto.privateDecrypt({ |
| 27 | + padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, |
| 28 | + key: privateKey |
| 29 | + }, ciphertext); |
| 30 | + |
| 31 | + assert.deepStrictEqual(plaintext, data); |
| 32 | + } |
| 33 | + |
| 34 | + { |
| 35 | + const ciphertext = crypto.publicEncrypt(publicKey, data); |
| 36 | + const plaintext = crypto.privateDecrypt(privateKey, ciphertext); |
| 37 | + |
| 38 | + assert.deepStrictEqual(plaintext, data); |
| 39 | + } |
| 40 | + |
| 41 | + { |
| 42 | + (async () => { |
| 43 | + const pkcs8 = privateKey.export({ format: 'der', type: 'pkcs8' }); |
| 44 | + const spki = publicKey.export({ format: 'der', type: 'spki' }); |
| 45 | + |
| 46 | + const kp = { |
| 47 | + privateKey: await subtle.importKey('pkcs8', pkcs8, { name: 'RSA-OAEP', hash: 'SHA-1' }, false, ['decrypt']), |
| 48 | + publicKey: await subtle.importKey('spki', spki, { name: 'RSA-OAEP', hash: 'SHA-1' }, false, ['encrypt']), |
| 49 | + }; |
| 50 | + |
| 51 | + const ciphertext = await subtle.encrypt('RSA-OAEP', kp.publicKey, data); |
| 52 | + const plaintext = await subtle.decrypt('RSA-OAEP', kp.privateKey, ciphertext); |
| 53 | + assert.deepStrictEqual(plaintext, data.buffer); |
| 54 | + })().then(common.mustCall()); |
| 55 | + } |
| 56 | +} |
0 commit comments