• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Verify that privateDecrypt() does not leave an error on the
4// openssl error stack that is visible to subsequent operations.
5
6const common = require('../common');
7if (!common.hasCrypto)
8  common.skip('missing crypto');
9
10const assert = require('assert');
11const {
12  generateKeyPairSync,
13  publicEncrypt,
14  privateDecrypt,
15} = require('crypto');
16
17const pair = generateKeyPairSync('rsa', { modulusLength: 512 });
18
19const expected = Buffer.from('shibboleth');
20const encrypted = publicEncrypt(pair.publicKey, expected);
21
22const pkey = pair.privateKey.export({ type: 'pkcs1', format: 'pem' });
23const pkeyEncrypted =
24  pair.privateKey.export({
25    type: 'pkcs1',
26    format: 'pem',
27    cipher: 'aes128',
28    passphrase: 'secret',
29  });
30
31function decrypt(key) {
32  const decrypted = privateDecrypt(key, encrypted);
33  assert.deepStrictEqual(decrypted, expected);
34}
35
36decrypt(pkey);
37assert.throws(() => decrypt(pkeyEncrypted), common.hasOpenSSL3 ?
38  { message: 'error:07880109:common libcrypto routines::interrupted or ' +
39             'cancelled' } :
40  { code: 'ERR_MISSING_PASSPHRASE' });
41decrypt(pkey);  // Should not throw.
42