11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciif (!common.hasCrypto) 251cb0ef41Sopenharmony_ci common.skip('missing crypto'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciif (!common.opensslCli) 281cb0ef41Sopenharmony_ci common.skip('missing openssl-cli'); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciconst assert = require('assert'); 311cb0ef41Sopenharmony_ciconst tls = require('tls'); 321cb0ef41Sopenharmony_ciconst net = require('net'); 331cb0ef41Sopenharmony_ciconst spawn = require('child_process').spawn; 341cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciconst key = fixtures.readKey('rsa_private.pem'); 371cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('rsa_cert.crt'); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cifunction log(a) { 401cb0ef41Sopenharmony_ci console.error('***server***', a); 411cb0ef41Sopenharmony_ci} 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ciconst server = net.createServer(common.mustCall(function(socket) { 441cb0ef41Sopenharmony_ci log(`connection fd=${socket.fd}`); 451cb0ef41Sopenharmony_ci const sslcontext = tls.createSecureContext({ key, cert }); 461cb0ef41Sopenharmony_ci sslcontext.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA'); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci const pair = tls.createSecurePair(sslcontext, true); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci assert.ok(pair.encrypted.writable); 511cb0ef41Sopenharmony_ci assert.ok(pair.cleartext.writable); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci pair.encrypted.pipe(socket); 541cb0ef41Sopenharmony_ci socket.pipe(pair.encrypted); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci log('i set it secure'); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci pair.on('secure', function() { 591cb0ef41Sopenharmony_ci log('connected+secure!'); 601cb0ef41Sopenharmony_ci pair.cleartext.write('hello\r\n'); 611cb0ef41Sopenharmony_ci log(pair.cleartext.getPeerCertificate()); 621cb0ef41Sopenharmony_ci log(pair.cleartext.getCipher()); 631cb0ef41Sopenharmony_ci }); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci pair.cleartext.on('data', function(data) { 661cb0ef41Sopenharmony_ci log(`read bytes ${data.length}`); 671cb0ef41Sopenharmony_ci pair.cleartext.write(data); 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci socket.on('end', function() { 711cb0ef41Sopenharmony_ci log('socket end'); 721cb0ef41Sopenharmony_ci }); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci pair.cleartext.on('error', function(err) { 751cb0ef41Sopenharmony_ci log('got error: '); 761cb0ef41Sopenharmony_ci log(err); 771cb0ef41Sopenharmony_ci socket.destroy(); 781cb0ef41Sopenharmony_ci }); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci pair.encrypted.on('error', function(err) { 811cb0ef41Sopenharmony_ci log('encrypted error: '); 821cb0ef41Sopenharmony_ci log(err); 831cb0ef41Sopenharmony_ci socket.destroy(); 841cb0ef41Sopenharmony_ci }); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci socket.on('error', function(err) { 871cb0ef41Sopenharmony_ci log('socket error: '); 881cb0ef41Sopenharmony_ci log(err); 891cb0ef41Sopenharmony_ci socket.destroy(); 901cb0ef41Sopenharmony_ci }); 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci socket.on('close', function(err) { 931cb0ef41Sopenharmony_ci log('socket closed'); 941cb0ef41Sopenharmony_ci }); 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci pair.on('error', function(err) { 971cb0ef41Sopenharmony_ci log('secure error: '); 981cb0ef41Sopenharmony_ci log(err); 991cb0ef41Sopenharmony_ci socket.destroy(); 1001cb0ef41Sopenharmony_ci }); 1011cb0ef41Sopenharmony_ci})); 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_cilet gotHello = false; 1041cb0ef41Sopenharmony_cilet sentWorld = false; 1051cb0ef41Sopenharmony_cilet gotWorld = false; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() { 1081cb0ef41Sopenharmony_ci // To test use: openssl s_client -connect localhost:8000 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci const args = ['s_client', '-connect', `127.0.0.1:${this.address().port}`]; 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci const client = spawn(common.opensslCli, args); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci let out = ''; 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci client.stdout.setEncoding('utf8'); 1181cb0ef41Sopenharmony_ci client.stdout.on('data', function(d) { 1191cb0ef41Sopenharmony_ci out += d; 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci if (!gotHello && /hello/.test(out)) { 1221cb0ef41Sopenharmony_ci gotHello = true; 1231cb0ef41Sopenharmony_ci client.stdin.write('world\r\n'); 1241cb0ef41Sopenharmony_ci sentWorld = true; 1251cb0ef41Sopenharmony_ci } 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci if (!gotWorld && /world/.test(out)) { 1281cb0ef41Sopenharmony_ci gotWorld = true; 1291cb0ef41Sopenharmony_ci client.stdin.end(); 1301cb0ef41Sopenharmony_ci } 1311cb0ef41Sopenharmony_ci }); 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci client.stdout.pipe(process.stdout, { end: false }); 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci client.on('exit', common.mustCall(function(code) { 1361cb0ef41Sopenharmony_ci assert.strictEqual(code, 0); 1371cb0ef41Sopenharmony_ci server.close(); 1381cb0ef41Sopenharmony_ci })); 1391cb0ef41Sopenharmony_ci})); 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ciprocess.on('exit', function() { 1421cb0ef41Sopenharmony_ci assert.ok(gotHello); 1431cb0ef41Sopenharmony_ci assert.ok(sentWorld); 1441cb0ef41Sopenharmony_ci assert.ok(gotWorld); 1451cb0ef41Sopenharmony_ci}); 146