11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst {
91cb0ef41Sopenharmony_ci  generateKeyPair,
101cb0ef41Sopenharmony_ci} = require('crypto');
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  testSignVerify,
131cb0ef41Sopenharmony_ci  spkiExp,
141cb0ef41Sopenharmony_ci  sec1EncExp,
151cb0ef41Sopenharmony_ci} = require('../common/crypto');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci{
181cb0ef41Sopenharmony_ci  // Test async explicit elliptic curve key generation with an encrypted
191cb0ef41Sopenharmony_ci  // private key.
201cb0ef41Sopenharmony_ci  generateKeyPair('ec', {
211cb0ef41Sopenharmony_ci    namedCurve: 'prime256v1',
221cb0ef41Sopenharmony_ci    paramEncoding: 'explicit',
231cb0ef41Sopenharmony_ci    publicKeyEncoding: {
241cb0ef41Sopenharmony_ci      type: 'spki',
251cb0ef41Sopenharmony_ci      format: 'pem'
261cb0ef41Sopenharmony_ci    },
271cb0ef41Sopenharmony_ci    privateKeyEncoding: {
281cb0ef41Sopenharmony_ci      type: 'sec1',
291cb0ef41Sopenharmony_ci      format: 'pem',
301cb0ef41Sopenharmony_ci      cipher: 'aes-128-cbc',
311cb0ef41Sopenharmony_ci      passphrase: 'secret'
321cb0ef41Sopenharmony_ci    }
331cb0ef41Sopenharmony_ci  }, common.mustSucceed((publicKey, privateKey) => {
341cb0ef41Sopenharmony_ci    assert.strictEqual(typeof publicKey, 'string');
351cb0ef41Sopenharmony_ci    assert.match(publicKey, spkiExp);
361cb0ef41Sopenharmony_ci    assert.strictEqual(typeof privateKey, 'string');
371cb0ef41Sopenharmony_ci    assert.match(privateKey, sec1EncExp('AES-128-CBC'));
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    // Since the private key is encrypted, signing shouldn't work anymore.
401cb0ef41Sopenharmony_ci    assert.throws(() => testSignVerify(publicKey, privateKey),
411cb0ef41Sopenharmony_ci                  common.hasOpenSSL3 ? {
421cb0ef41Sopenharmony_ci                    message: 'error:07880109:common libcrypto ' +
431cb0ef41Sopenharmony_ci                             'routines::interrupted or cancelled'
441cb0ef41Sopenharmony_ci                  } : {
451cb0ef41Sopenharmony_ci                    name: 'TypeError',
461cb0ef41Sopenharmony_ci                    code: 'ERR_MISSING_PASSPHRASE',
471cb0ef41Sopenharmony_ci                    message: 'Passphrase required for encrypted key'
481cb0ef41Sopenharmony_ci                  });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    testSignVerify(publicKey, { key: privateKey, passphrase: 'secret' });
511cb0ef41Sopenharmony_ci  }));
521cb0ef41Sopenharmony_ci}
53