All files / lib _tls_common.js

98.12% Statements 157/160
97.43% Branches 38/39
100% Functions 4/4
98.12% Lines 157/160

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 1618x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 5009x 5009x 5009x 5009x 5009x 2215x 2x 5009x 8x 8x 8x 8x 8x 2533x 2533x       2533x 2533x 399x 399x 385x 399x 399x 2505x 2505x 2505x 2505x 2505x 2505x 2533x 922x 922x 922x 2533x 8x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 2533x 8x 8x 8x 8x 1260x 1260x 1260x 1251x 1260x 717x 717x 1258x 80x 80x 80x 80x 80x 80x 155x 8x 8x 8x 8x 8x 8x 8x 155x 155x 151x 151x 80x 80x 1251x 1260x 8x 8x 8x 8x 8x 8x  
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
'use strict';
 
const tls = require('tls');
 
const {
  ArrayPrototypePush,
  JSONParse,
  ObjectCreate,
  RegExpPrototypeSymbolReplace,
} = primordials;
 
const {
  codes: {
    ERR_TLS_INVALID_PROTOCOL_VERSION,
    ERR_TLS_PROTOCOL_VERSION_CONFLICT,
  },
} = require('internal/errors');
 
const {
  crypto: {
    SSL_OP_CIPHER_SERVER_PREFERENCE,
    TLS1_VERSION,
    TLS1_1_VERSION,
    TLS1_2_VERSION,
    TLS1_3_VERSION,
  },
} = internalBinding('constants');
 
const {
  kEmptyObject,
} = require('internal/util');
 
const {
  validateInteger,
} = require('internal/validators');
 
const {
  configSecureContext,
} = require('internal/tls/secure-context');
 
function toV(which, v, def) {
  if (v == null) v = def;
  if (v === 'TLSv1') return TLS1_VERSION;
  if (v === 'TLSv1.1') return TLS1_1_VERSION;
  if (v === 'TLSv1.2') return TLS1_2_VERSION;
  if (v === 'TLSv1.3') return TLS1_3_VERSION;
  throw new ERR_TLS_INVALID_PROTOCOL_VERSION(v, which);
}
 
const {
  SecureContext: NativeSecureContext,
} = internalBinding('crypto');
 
function SecureContext(secureProtocol, secureOptions, minVersion, maxVersion) {
  if (!(this instanceof SecureContext)) {
    return new SecureContext(secureProtocol, secureOptions, minVersion,
                             maxVersion);
  }
 
  if (secureProtocol) {
    if (minVersion != null)
      throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT(minVersion, secureProtocol);
    if (maxVersion != null)
      throw new ERR_TLS_PROTOCOL_VERSION_CONFLICT(maxVersion, secureProtocol);
  }
 
  this.context = new NativeSecureContext();
  this.context.init(secureProtocol,
                    toV('minimum', minVersion, tls.DEFAULT_MIN_VERSION),
                    toV('maximum', maxVersion, tls.DEFAULT_MAX_VERSION));
 
  if (secureOptions) {
    validateInteger(secureOptions, 'secureOptions');
    this.context.setOptions(secureOptions);
  }
}
 
function createSecureContext(options) {
  if (!options) options = kEmptyObject;
 
  const {
    honorCipherOrder,
    minVersion,
    maxVersion,
    secureProtocol,
  } = options;
 
  let { secureOptions } = options;
 
  if (honorCipherOrder)
    secureOptions |= SSL_OP_CIPHER_SERVER_PREFERENCE;
 
  const c = new SecureContext(secureProtocol, secureOptions,
                              minVersion, maxVersion);
 
  configSecureContext(c.context, options);
 
  return c;
}
 
// Translate some fields from the handle's C-friendly format into more idiomatic
// javascript object representations before passing them back to the user.  Can
// be used on any cert object, but changing the name would be semver-major.
function translatePeerCertificate(c) {
  if (!c)
    return null;
 
  if (c.issuerCertificate != null && c.issuerCertificate !== c) {
    c.issuerCertificate = translatePeerCertificate(c.issuerCertificate);
  }
  if (c.infoAccess != null) {
    const info = c.infoAccess;
    c.infoAccess = ObjectCreate(null);
 
    // XXX: More key validation?
    RegExpPrototypeSymbolReplace(/([^\n:]*):([^\n]*)(?:\n|$)/g, info,
                                 (all, key, val) => {
                                   if (val.charCodeAt(0) === 0x22) {
                                     // The translatePeerCertificate function is only
                                     // used on internally created legacy certificate
                                     // objects, and any value that contains a quote
                                     // will always be a valid JSON string literal,
                                     // so this should never throw.
                                     val = JSONParse(val);
                                   }
                                   if (key in c.infoAccess)
                                     ArrayPrototypePush(c.infoAccess[key], val);
                                   else
                                     c.infoAccess[key] = [val];
                                 });
  }
  return c;
}
 
module.exports = {
  SecureContext,
  createSecureContext,
  translatePeerCertificate,
};