11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst { TestTLSSocket, ccs } = require('../common/tls');
71cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
81cb0ef41Sopenharmony_ciconst https = require('https');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Regression test for an use-after-free bug in the TLS implementation that
111cb0ef41Sopenharmony_ci// would occur when `SSL_write()` failed.
121cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs-private/security/issues/189
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst server_key = fixtures.readKey('agent1-key.pem');
151cb0ef41Sopenharmony_ciconst server_cert = fixtures.readKey('agent1-cert.pem');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst opts = {
181cb0ef41Sopenharmony_ci  key: server_key,
191cb0ef41Sopenharmony_ci  cert: server_cert,
201cb0ef41Sopenharmony_ci  ciphers: 'ALL@SECLEVEL=0'
211cb0ef41Sopenharmony_ci};
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst server = https.createServer(opts, (req, res) => {
241cb0ef41Sopenharmony_ci  res.write('hello');
251cb0ef41Sopenharmony_ci}).listen(0, common.mustCall(() => {
261cb0ef41Sopenharmony_ci  const client = new TestTLSSocket(server_cert);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  client.connect({
291cb0ef41Sopenharmony_ci    host: 'localhost',
301cb0ef41Sopenharmony_ci    port: server.address().port
311cb0ef41Sopenharmony_ci  }, common.mustCall(() => {
321cb0ef41Sopenharmony_ci    const ch = client.createClientHello();
331cb0ef41Sopenharmony_ci    client.write(ch);
341cb0ef41Sopenharmony_ci  }));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  client.once('data', common.mustCall((buf) => {
371cb0ef41Sopenharmony_ci    let remaining = buf;
381cb0ef41Sopenharmony_ci    do {
391cb0ef41Sopenharmony_ci      remaining = client.parseTLSFrame(remaining);
401cb0ef41Sopenharmony_ci    } while (remaining.length > 0);
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    const cke = client.createClientKeyExchange();
431cb0ef41Sopenharmony_ci    const finished = client.createFinished();
441cb0ef41Sopenharmony_ci    const ill = client.createIllegalHandshake();
451cb0ef41Sopenharmony_ci    const frames = Buffer.concat([
461cb0ef41Sopenharmony_ci      cke,
471cb0ef41Sopenharmony_ci      ccs,
481cb0ef41Sopenharmony_ci      client.encrypt(finished),
491cb0ef41Sopenharmony_ci      client.encrypt(ill),
501cb0ef41Sopenharmony_ci    ]);
511cb0ef41Sopenharmony_ci    client.write(frames, common.mustCall(() => {
521cb0ef41Sopenharmony_ci      client.end();
531cb0ef41Sopenharmony_ci      server.close();
541cb0ef41Sopenharmony_ci    }));
551cb0ef41Sopenharmony_ci  }));
561cb0ef41Sopenharmony_ci}));
57