|
| 1 | +'use strict'; |
| 2 | +var common = require('../common'); |
| 3 | +var assert = require('assert'); |
| 4 | + |
| 5 | +if (!common.hasCrypto) { |
| 6 | + console.log('1..0 # Skipped: missing crypto'); |
| 7 | + process.exit(); |
| 8 | +} |
| 9 | +var tls = require('tls'); |
| 10 | + |
| 11 | +var fs = require('fs'); |
| 12 | +var key = fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'); |
| 13 | +var cert = fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem'); |
| 14 | + |
| 15 | +var ntests = 0; |
| 16 | +var nsuccess = 0; |
| 17 | + |
| 18 | +function loadDHParam(n) { |
| 19 | + var path = common.fixturesDir; |
| 20 | + if (n !== 'error') path += '/keys'; |
| 21 | + return fs.readFileSync(path + '/dh' + n + '.pem'); |
| 22 | +} |
| 23 | + |
| 24 | +var cipherlist = { |
| 25 | + 'NOT_PFS': 'AES128-SHA256', |
| 26 | + 'DH': 'DHE-RSA-AES128-GCM-SHA256', |
| 27 | + 'ECDH': 'ECDHE-RSA-AES128-GCM-SHA256' |
| 28 | +}; |
| 29 | + |
| 30 | +function test(size, type, name, next) { |
| 31 | + var cipher = type ? cipherlist[type] : cipherlist['NOT_PFS']; |
| 32 | + |
| 33 | + if (name) tls.DEFAULT_ECDH_CURVE = name; |
| 34 | + |
| 35 | + var options = { |
| 36 | + key: key, |
| 37 | + cert: cert, |
| 38 | + ciphers: cipher |
| 39 | + }; |
| 40 | + |
| 41 | + if (type === 'DH') options.dhparam = loadDHParam(size); |
| 42 | + |
| 43 | + var server = tls.createServer(options, function(conn) { |
| 44 | + assert.strictEqual(conn.getEphemeralKeyInfo(), null); |
| 45 | + conn.end(); |
| 46 | + }); |
| 47 | + |
| 48 | + server.on('close', function(err) { |
| 49 | + assert(!err); |
| 50 | + if (next) next(); |
| 51 | + }); |
| 52 | + |
| 53 | + server.listen(common.PORT, '127.0.0.1', function() { |
| 54 | + var client = tls.connect({ |
| 55 | + port: common.PORT, |
| 56 | + rejectUnauthorized: false |
| 57 | + }, function() { |
| 58 | + var ekeyinfo = client.getEphemeralKeyInfo(); |
| 59 | + assert.strictEqual(ekeyinfo.type, type); |
| 60 | + assert.strictEqual(ekeyinfo.size, size); |
| 61 | + assert.strictEqual(ekeyinfo.name, name); |
| 62 | + nsuccess++; |
| 63 | + server.close(); |
| 64 | + }); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +function testNOT_PFS() { |
| 69 | + test(undefined, undefined, undefined, testDHE1024); |
| 70 | + ntests++; |
| 71 | +} |
| 72 | + |
| 73 | +function testDHE1024() { |
| 74 | + test(1024, 'DH', undefined, testDHE2048); |
| 75 | + ntests++; |
| 76 | +} |
| 77 | + |
| 78 | +function testDHE2048() { |
| 79 | + test(2048, 'DH', undefined, testECDHE256); |
| 80 | + ntests++; |
| 81 | +} |
| 82 | + |
| 83 | +function testECDHE256() { |
| 84 | + test(256, 'ECDH', tls.DEFAULT_ECDH_CURVE, testECDHE512); |
| 85 | + ntests++; |
| 86 | +} |
| 87 | + |
| 88 | +function testECDHE512() { |
| 89 | + test(521, 'ECDH', 'secp521r1', null); |
| 90 | + ntests++; |
| 91 | +} |
| 92 | + |
| 93 | +testNOT_PFS(); |
| 94 | + |
| 95 | +process.on('exit', function() { |
| 96 | + assert.equal(ntests, nsuccess); |
| 97 | + assert.equal(ntests, 5); |
| 98 | +}); |
0 commit comments