node/test/parallel/test-tls-client-getephemeralkeyinfo.js
Shigeki Ohtsu c51b7b296e
tls: fix getEphemeralKeyInfo to support X25519
`EVP_PKEY_EC` only covers ANSI X9.62 curves not IETF ones(curve25519
and curve448). This fixes to add support of X25519 in
`tlsSocket.getEphemeralKeyInfo()`.
X448 should be added in the future upgrade to OpenSSL-1.1.1.

PR-URL: https://github.com/nodejs/node/pull/20273
Fixes: https://github.com/nodejs/node/issues/20262
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
2018-04-28 17:17:53 +02:00

100 lines
2.1 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const assert = require('assert');
const tls = require('tls');
const key = fixtures.readKey('agent2-key.pem');
const cert = fixtures.readKey('agent2-cert.pem');
let ntests = 0;
let nsuccess = 0;
function loadDHParam(n) {
return fixtures.readKey(`dh${n}.pem`);
}
const cipherlist = {
'NOT_PFS': 'AES128-SHA256',
'DH': 'DHE-RSA-AES128-GCM-SHA256',
'ECDH': 'ECDHE-RSA-AES128-GCM-SHA256'
};
function test(size, type, name, next) {
const cipher = type ? cipherlist[type] : cipherlist.NOT_PFS;
if (name) tls.DEFAULT_ECDH_CURVE = name;
const options = {
key: key,
cert: cert,
ciphers: cipher
};
if (type === 'DH') options.dhparam = loadDHParam(size);
const server = tls.createServer(options, function(conn) {
assert.strictEqual(conn.getEphemeralKeyInfo(), null);
conn.end();
});
server.on('close', common.mustCall(function(err) {
assert.ifError(err);
if (next) next();
}));
server.listen(0, '127.0.0.1', common.mustCall(function() {
const client = tls.connect({
port: this.address().port,
rejectUnauthorized: false
}, function() {
const ekeyinfo = client.getEphemeralKeyInfo();
assert.strictEqual(ekeyinfo.type, type);
assert.strictEqual(ekeyinfo.size, size);
assert.strictEqual(ekeyinfo.name, name);
nsuccess++;
server.close();
});
}));
}
function testNOT_PFS() {
test(undefined, undefined, undefined, testDHE1024);
ntests++;
}
function testDHE1024() {
test(1024, 'DH', undefined, testDHE2048);
ntests++;
}
function testDHE2048() {
test(2048, 'DH', undefined, testECDHE256);
ntests++;
}
function testECDHE256() {
test(256, 'ECDH', 'prime256v1', testECDHE512);
ntests++;
}
function testECDHE512() {
test(521, 'ECDH', 'secp521r1', testX25519);
ntests++;
}
function testX25519() {
test(253, 'ECDH', 'X25519', null);
ntests++;
}
testNOT_PFS();
process.on('exit', function() {
assert.strictEqual(ntests, nsuccess);
assert.strictEqual(ntests, 6);
});