11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Verify that privateDecrypt() does not leave an error on the
41cb0ef41Sopenharmony_ci// openssl error stack that is visible to subsequent operations.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ciif (!common.hasCrypto)
81cb0ef41Sopenharmony_ci  common.skip('missing crypto');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  generateKeyPairSync,
131cb0ef41Sopenharmony_ci  publicEncrypt,
141cb0ef41Sopenharmony_ci  privateDecrypt,
151cb0ef41Sopenharmony_ci} = require('crypto');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst pair = generateKeyPairSync('rsa', { modulusLength: 512 });
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst expected = Buffer.from('shibboleth');
201cb0ef41Sopenharmony_ciconst encrypted = publicEncrypt(pair.publicKey, expected);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst pkey = pair.privateKey.export({ type: 'pkcs1', format: 'pem' });
231cb0ef41Sopenharmony_ciconst pkeyEncrypted =
241cb0ef41Sopenharmony_ci  pair.privateKey.export({
251cb0ef41Sopenharmony_ci    type: 'pkcs1',
261cb0ef41Sopenharmony_ci    format: 'pem',
271cb0ef41Sopenharmony_ci    cipher: 'aes128',
281cb0ef41Sopenharmony_ci    passphrase: 'secret',
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cifunction decrypt(key) {
321cb0ef41Sopenharmony_ci  const decrypted = privateDecrypt(key, encrypted);
331cb0ef41Sopenharmony_ci  assert.deepStrictEqual(decrypted, expected);
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cidecrypt(pkey);
371cb0ef41Sopenharmony_ciassert.throws(() => decrypt(pkeyEncrypted), common.hasOpenSSL3 ?
381cb0ef41Sopenharmony_ci  { message: 'error:07880109:common libcrypto routines::interrupted or ' +
391cb0ef41Sopenharmony_ci             'cancelled' } :
401cb0ef41Sopenharmony_ci  { code: 'ERR_MISSING_PASSPHRASE' });
411cb0ef41Sopenharmony_cidecrypt(pkey);  // Should not throw.
42