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 createPrivateKey, 101cb0ef41Sopenharmony_ci generateKeyPair, 111cb0ef41Sopenharmony_ci} = require('crypto'); 121cb0ef41Sopenharmony_ciconst { 131cb0ef41Sopenharmony_ci testSignVerify, 141cb0ef41Sopenharmony_ci} = require('../common/crypto'); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci// Passing an empty passphrase string should not cause OpenSSL's default 171cb0ef41Sopenharmony_ci// passphrase prompt in the terminal. 181cb0ef41Sopenharmony_ci// See https://github.com/nodejs/node/issues/35898. 191cb0ef41Sopenharmony_cifor (const type of ['pkcs1', 'pkcs8']) { 201cb0ef41Sopenharmony_ci generateKeyPair('rsa', { 211cb0ef41Sopenharmony_ci modulusLength: 1024, 221cb0ef41Sopenharmony_ci privateKeyEncoding: { 231cb0ef41Sopenharmony_ci type, 241cb0ef41Sopenharmony_ci format: 'pem', 251cb0ef41Sopenharmony_ci cipher: 'aes-256-cbc', 261cb0ef41Sopenharmony_ci passphrase: '' 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci }, common.mustSucceed((publicKey, privateKey) => { 291cb0ef41Sopenharmony_ci assert.strictEqual(publicKey.type, 'public'); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci for (const passphrase of ['', Buffer.alloc(0)]) { 321cb0ef41Sopenharmony_ci const privateKeyObject = createPrivateKey({ 331cb0ef41Sopenharmony_ci passphrase, 341cb0ef41Sopenharmony_ci key: privateKey 351cb0ef41Sopenharmony_ci }); 361cb0ef41Sopenharmony_ci assert.strictEqual(privateKeyObject.asymmetricKeyType, 'rsa'); 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci // Encrypting with an empty passphrase is not the same as not encrypting 401cb0ef41Sopenharmony_ci // the key, and not specifying a passphrase should fail when decoding it. 411cb0ef41Sopenharmony_ci assert.throws(() => { 421cb0ef41Sopenharmony_ci return testSignVerify(publicKey, privateKey); 431cb0ef41Sopenharmony_ci }, common.hasOpenSSL3 ? { 441cb0ef41Sopenharmony_ci name: 'Error', 451cb0ef41Sopenharmony_ci code: 'ERR_OSSL_CRYPTO_INTERRUPTED_OR_CANCELLED', 461cb0ef41Sopenharmony_ci message: 'error:07880109:common libcrypto routines::interrupted or cancelled' 471cb0ef41Sopenharmony_ci } : { 481cb0ef41Sopenharmony_ci name: 'TypeError', 491cb0ef41Sopenharmony_ci code: 'ERR_MISSING_PASSPHRASE', 501cb0ef41Sopenharmony_ci message: 'Passphrase required for encrypted key' 511cb0ef41Sopenharmony_ci }); 521cb0ef41Sopenharmony_ci })); 531cb0ef41Sopenharmony_ci} 54