11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
61cb0ef41Sopenharmony_ciconst SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;
71cb0ef41Sopenharmony_ciconst tls = require('tls');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Check that TLS1.2 session resumption callbacks don't explode when made after
101cb0ef41Sopenharmony_ci// the tls socket is destroyed. Disable TLS ticket support to force the legacy
111cb0ef41Sopenharmony_ci// session resumption mechanism to be used.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// TLS1.2 is the last protocol version to support TLS sessions, after that the
141cb0ef41Sopenharmony_ci// new and resume session events will never be emitted on the server.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst options = {
171cb0ef41Sopenharmony_ci  secureOptions: SSL_OP_NO_TICKET,
181cb0ef41Sopenharmony_ci  key: fixtures.readKey('rsa_private.pem'),
191cb0ef41Sopenharmony_ci  cert: fixtures.readKey('rsa_cert.crt')
201cb0ef41Sopenharmony_ci};
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst server = tls.createServer(options, common.mustCall());
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cilet sessionCb = null;
251cb0ef41Sopenharmony_cilet client = null;
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciserver.on('newSession', common.mustCall((key, session, done) => {
281cb0ef41Sopenharmony_ci  done();
291cb0ef41Sopenharmony_ci}));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciserver.on('resumeSession', common.mustCall((id, cb) => {
321cb0ef41Sopenharmony_ci  sessionCb = cb;
331cb0ef41Sopenharmony_ci  // Destroy the client and then call the session cb, to check that the cb
341cb0ef41Sopenharmony_ci  // doesn't explode when called after the handle has been destroyed.
351cb0ef41Sopenharmony_ci  next();
361cb0ef41Sopenharmony_ci}));
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
391cb0ef41Sopenharmony_ci  const clientOpts = {
401cb0ef41Sopenharmony_ci    // Don't send a TLS1.3/1.2 ClientHello, they contain a fake session_id,
411cb0ef41Sopenharmony_ci    // which triggers a 'resumeSession' event for client1. TLS1.2 ClientHello
421cb0ef41Sopenharmony_ci    // won't have a session_id until client2, which will have a valid session.
431cb0ef41Sopenharmony_ci    maxVersion: 'TLSv1.2',
441cb0ef41Sopenharmony_ci    port: server.address().port,
451cb0ef41Sopenharmony_ci    rejectUnauthorized: false,
461cb0ef41Sopenharmony_ci    session: false
471cb0ef41Sopenharmony_ci  };
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  const s1 = tls.connect(clientOpts, common.mustCall(() => {
501cb0ef41Sopenharmony_ci    clientOpts.session = s1.getSession();
511cb0ef41Sopenharmony_ci    console.log('1st secure');
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    s1.destroy();
541cb0ef41Sopenharmony_ci    const s2 = tls.connect(clientOpts, (s) => {
551cb0ef41Sopenharmony_ci      console.log('2nd secure');
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci      s2.destroy();
581cb0ef41Sopenharmony_ci    }).on('connect', common.mustCall(() => {
591cb0ef41Sopenharmony_ci      console.log('2nd connected');
601cb0ef41Sopenharmony_ci      client = s2;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci      next();
631cb0ef41Sopenharmony_ci    }));
641cb0ef41Sopenharmony_ci  }));
651cb0ef41Sopenharmony_ci}));
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_cifunction next() {
681cb0ef41Sopenharmony_ci  if (!client || !sessionCb)
691cb0ef41Sopenharmony_ci    return;
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  client.destroy();
721cb0ef41Sopenharmony_ci  setTimeout(common.mustCall(() => {
731cb0ef41Sopenharmony_ci    sessionCb();
741cb0ef41Sopenharmony_ci    server.close();
751cb0ef41Sopenharmony_ci  }), 100);
761cb0ef41Sopenharmony_ci}
77