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_ci
251cb0ef41Sopenharmony_ciif (!common.opensslCli)
261cb0ef41Sopenharmony_ci  common.skip('node compiled without OpenSSL CLI.');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciif (!common.hasCrypto)
291cb0ef41Sopenharmony_ci  common.skip('missing crypto');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst tls = require('tls');
321cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst assert = require('assert');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst SSL_OP_NO_TICKET = require('crypto').constants.SSL_OP_NO_TICKET;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciconst pfx = fixtures.readKey('agent1.pfx');
391cb0ef41Sopenharmony_ciconst key = fixtures.readKey('agent1-key.pem');
401cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('agent1-cert.pem');
411cb0ef41Sopenharmony_ciconst ca = fixtures.readKey('ca1-cert.pem');
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cifunction test(testOptions, cb) {
441cb0ef41Sopenharmony_ci  const options = {
451cb0ef41Sopenharmony_ci    key,
461cb0ef41Sopenharmony_ci    cert,
471cb0ef41Sopenharmony_ci    ca: [ca]
481cb0ef41Sopenharmony_ci  };
491cb0ef41Sopenharmony_ci  const requestCount = testOptions.response ? 0 : 1;
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  if (!testOptions.ocsp)
521cb0ef41Sopenharmony_ci    assert.strictEqual(testOptions.response, undefined);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  if (testOptions.pfx) {
551cb0ef41Sopenharmony_ci    delete options.key;
561cb0ef41Sopenharmony_ci    delete options.cert;
571cb0ef41Sopenharmony_ci    options.pfx = testOptions.pfx;
581cb0ef41Sopenharmony_ci    options.passphrase = testOptions.passphrase;
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  const server = tls.createServer(options, common.mustCall((cleartext) => {
621cb0ef41Sopenharmony_ci    cleartext.on('error', function(er) {
631cb0ef41Sopenharmony_ci      // We're ok with getting ECONNRESET in this test, but it's
641cb0ef41Sopenharmony_ci      // timing-dependent, and thus unreliable. Any other errors
651cb0ef41Sopenharmony_ci      // are just failures, though.
661cb0ef41Sopenharmony_ci      if (er.code !== 'ECONNRESET')
671cb0ef41Sopenharmony_ci        throw er;
681cb0ef41Sopenharmony_ci    });
691cb0ef41Sopenharmony_ci    cleartext.end();
701cb0ef41Sopenharmony_ci  }, requestCount));
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  if (!testOptions.ocsp)
731cb0ef41Sopenharmony_ci    server.on('OCSPRequest', common.mustNotCall());
741cb0ef41Sopenharmony_ci  else
751cb0ef41Sopenharmony_ci    server.on('OCSPRequest', common.mustCall((cert, issuer, callback) => {
761cb0ef41Sopenharmony_ci      assert.ok(Buffer.isBuffer(cert));
771cb0ef41Sopenharmony_ci      assert.ok(Buffer.isBuffer(issuer));
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci      // Callback a little later to ensure that async really works.
801cb0ef41Sopenharmony_ci      return setTimeout(callback, 100, null, testOptions.response ?
811cb0ef41Sopenharmony_ci        Buffer.from(testOptions.response) : null);
821cb0ef41Sopenharmony_ci    }));
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  server.listen(0, function() {
851cb0ef41Sopenharmony_ci    const client = tls.connect({
861cb0ef41Sopenharmony_ci      port: this.address().port,
871cb0ef41Sopenharmony_ci      requestOCSP: testOptions.ocsp,
881cb0ef41Sopenharmony_ci      secureOptions: testOptions.ocsp ? 0 : SSL_OP_NO_TICKET,
891cb0ef41Sopenharmony_ci      rejectUnauthorized: false
901cb0ef41Sopenharmony_ci    }, common.mustCall(requestCount));
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci    client.on('OCSPResponse', common.mustCall((resp) => {
931cb0ef41Sopenharmony_ci      if (testOptions.response) {
941cb0ef41Sopenharmony_ci        assert.strictEqual(resp.toString(), testOptions.response);
951cb0ef41Sopenharmony_ci        client.destroy();
961cb0ef41Sopenharmony_ci      } else {
971cb0ef41Sopenharmony_ci        assert.strictEqual(resp, null);
981cb0ef41Sopenharmony_ci      }
991cb0ef41Sopenharmony_ci    }, testOptions.ocsp === false ? 0 : 1));
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci    client.on('close', common.mustCall(() => {
1021cb0ef41Sopenharmony_ci      server.close(cb);
1031cb0ef41Sopenharmony_ci    }));
1041cb0ef41Sopenharmony_ci  });
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_citest({ ocsp: true, response: false });
1081cb0ef41Sopenharmony_citest({ ocsp: true, response: 'hello world' });
1091cb0ef41Sopenharmony_citest({ ocsp: false });
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ciif (!common.hasFipsCrypto) {
1121cb0ef41Sopenharmony_ci  test({ ocsp: true, response: 'hello pfx', pfx: pfx, passphrase: 'sample' });
1131cb0ef41Sopenharmony_ci}
114