tls.createSecureContext([options])


  • options <Object>
    • ca <string> | <string[]> | <Buffer> | <Buffer[]> 可选择覆盖受信任的 CA 证书。 默认是信任 Mozilla 策划的知名 CA。 当使用此选项明确指定 CA 时,Mozilla 的 CA 将被完全替换。 该值可以是字符串、或 Buffer、或 Array 的字符串和/或 Buffer。 任何字符串或 Buffer 都可以包含多个连接在一起的 PEM CA。 对等方的证书必须可链接到服务器信任的 CA 才能对连接进行身份验证。 当使用不可链接到知名 CA 的证书时,必须明确指定证书的 CA 为受信任的 CA,否则连接将无法通过身份验证。 如果对等方使用的证书不匹配或链接到默认 CA 之一,则使用 ca 选项提供对等方证书可以匹配或链接到的 CA 证书。 对于自签名证书,证书是自己的CA,必须提供。 对于 PEM 编码的证书,支持的类型是 "TRUSTED CERTIFICATE"、"X509 CERTIFICATE"、以及 "CERTIFICATE"。 另见 tls.rootCertificates
    • cert <string> | <string[]> | <Buffer> | <Buffer[]> PEM 格式的证书链。 每个私钥应提供证书链。 每个证书链都应包含提供的私有 key 的 PEM 格式证书,然后是 PEM 格式的中间证书(如果有),按顺序排列,并且不包括根 CA(根 CA 必须是对等方预先知道的,参见 ca)。 在提供多个证书链时,它们不必与 key 中的私钥顺序相同。 如果不提供中间证书,则对端将无法验证证书,握手将失败。
    • sigalgs <string> 支持的签名算法的冒号分隔列表。 该列表可以包含摘要算法(SHA256MD5 等)、公钥算法(RSA-PSSECDSA 等)、两者的组合(例如 'RSA+SHA384')或 TLS v1.3 方案名称(例如 rsa_pss_pss_sha512)。 请参阅 OpenSSL 手册页 了解更多信息。
    • ciphers <string> 密码套件规范,替换默认值。 有关详细信息,请参阅修改默认的 TLS 密码套件。 可以通过 tls.getCiphers() 获得允许的密码。 密码名称必须大写,OpenSSL 才能接受它们。
    • clientCertEngine <string> 可以提供客户端证书的 OpenSSL 引擎的名称。
    • crl <string> | <string[]> | <Buffer> | <Buffer[]> PEM 格式的 CRL(证书吊销列表)。
    • dhparam <string> | <Buffer> Diffie-Hellman 参数,完美前向保密所需。 使用 openssl dhparam 创建参数。 密钥长度必须大于等于 1024 位,否则会报错。 虽然 1024 位是允许的,但为了更强的安全性,请使用 2048 位或更大的位。 如果省略或无效,参数将被静默丢弃,DHE 密码将不可用。
    • ecdhCurve <string> 描述命名曲线或以冒号分隔的曲线 NID 或名称列表的字符串,例如 P-521:P-384:P-256,用于 ECDH 密钥协议。 设置为 auto 自动选择曲线。 使用 crypto.getCurves() 获取可用曲线名称的列表。 在最近的版本中,openssl ecparam -list_curves 还将显示每个可用椭圆曲线的名称和描述。 默认值: tls.DEFAULT_ECDH_CURVE.
    • honorCipherOrder <boolean> 尝试使用服务器的密码套件首选项而不是客户端的。 当为 true 时,导致 SSL_OP_CIPHER_SERVER_PREFERENCEsecureOptions 中被设置,请参阅 OpenSSL 选项了解更多信息。
    • key <string> | <string[]> | <Buffer> | <Buffer[]> | <Object[]> PEM 格式的私钥。 PEM 允许选择加密私钥。 加密的密钥将用 options.passphrase 解密。 使用不同算法的多个密钥可以作为未加密密钥字符串或缓冲区的数组提供,也可以作为 {pem: <string|buffer>[, passphrase: <string>]} 形式的对象数组提供。 对象形式只能出现在数组中。 object.passphrase 是可选的。 如果提供了加密的密钥,则将使用 object.passphrase 解密,否则使用 options.passphrase 解密。
    • privateKeyEngine <string> 从中获取私钥的 OpenSSL 引擎的名称。 应与 privateKeyIdentifier 一起使用。
    • privateKeyIdentifier <string> 由 OpenSSL 引擎管理的私钥的标识符。 应与 privateKeyEngine 一起使用。 不应与 key 一起设置,因为这两个选项定义的私钥的方式不同。
    • maxVersion <string> 可选择设置允许的最大 TLS 版本。 'TLSv1.3''TLSv1.2''TLSv1.1''TLSv1' 之一。 不能与 secureProtocol 选项一起指定;使用其中之一。 默认值: tls.DEFAULT_MAX_VERSION.
    • minVersion <string> 可选择设置允许的最低 TLS 版本。 'TLSv1.3''TLSv1.2''TLSv1.1''TLSv1' 之一。 不能与 secureProtocol 选项一起指定;使用其中之一。 避免设置为低于 TLSv1.2,但可能需要互操作性。 默认值: tls.DEFAULT_MIN_VERSION.
    • passphrase <string> 用于单个私钥和/或 PFX 的共享密码。
    • pfx <string> | <string[]> | <Buffer> | <Buffer[]> | <Object[]> PFX 或 PKCS12 编码的私钥和证书链。 pfx 是单独提供 keycert 的替代方案。 PFX 通常是加密的,如果是的话,会用 passphrase 来解密。 多个 PFX 可以作为未加密的 PFX 缓冲区数组或 {buf: <string|buffer>[, passphrase: <string>]} 形式的对象数组提供。 对象形式只能出现在数组中。 object.passphrase 是可选的。 如果提供加密的 PFX 将使用 object.passphrase 解密,否则将使用 options.passphrase 解密。
    • secureOptions <number> 可选地影响 OpenSSL 协议行为,这通常不是必需的。 如果有的话应该小心使用! 值是 OpenSSL 选项SSL_OP_* 选项的数字位掩码。
    • secureProtocol <string> 旧的机制选择使用的 TLS 协议版本,不支持独立控制最小和最大版本,也不支持将协议限制为 TLSv1.3。改用 minVersionmaxVersion。 可能的值被列为 SSL_METHODS,使用函数名称作为字符串。 例如,使用 'TLSv1_1_method' 强制使用 TLS 版本 1.1,或使用 'TLS_method' 允许任何 TLS 协议版本最高为 TLSv1.3。不建议使用低于 1.2 的 TLS 版本,但可能需要互操作性。 **默认:**无,见 minVersion
    • sessionIdContext <string> 服务器使用不透明标识符来确保应用程序之间不共享会话状态。 客户端未使用。
    • ticketKeys: <Buffer> 48 字节的加密强伪随机数据。 请参阅会话恢复了解更多信息。
    • sessionTimeout <number> 服务器创建的 TLS 会话将无法恢复之前的秒数。 请参阅会话恢复了解更多信息。 默认值: 300

tls.createServer()honorCipherOrder 选项的默认值设置为 true,创建安全上下文的其他 API 未设置。

tls.createServer() 使用从 process.argv 生成的 128 位截断 SHA1 哈希值作为 sessionIdContext 选项的默认值,其他创建安全上下文的 API 没有默认值。

tls.createSecureContext() 方法创建了 SecureContext 对象。 它可用作几个 tls API 的参数,例如 tls.createServer()server.addContext(),但没有公共方法。

使用证书的密码需要密钥。 keypfx 都可以提供。

如果没有给出 ca 选项,则 Node.js 将默认使用 Mozilla 的公开信任的 CA 列表

  • options <Object>
    • ca <string> | <string[]> | <Buffer> | <Buffer[]> Optionally override the trusted CA certificates. Default is to trust the well-known CAs curated by Mozilla. Mozilla's CAs are completely replaced when CAs are explicitly specified using this option. The value can be a string or Buffer, or an Array of strings and/or Buffers. Any string or Buffer can contain multiple PEM CAs concatenated together. The peer's certificate must be chainable to a CA trusted by the server for the connection to be authenticated. When using certificates that are not chainable to a well-known CA, the certificate's CA must be explicitly specified as a trusted or the connection will fail to authenticate. If the peer uses a certificate that doesn't match or chain to one of the default CAs, use the ca option to provide a CA certificate that the peer's certificate can match or chain to. For self-signed certificates, the certificate is its own CA, and must be provided. For PEM encoded certificates, supported types are "TRUSTED CERTIFICATE", "X509 CERTIFICATE", and "CERTIFICATE". See also tls.rootCertificates.
    • cert <string> | <string[]> | <Buffer> | <Buffer[]> Cert chains in PEM format. One cert chain should be provided per private key. Each cert chain should consist of the PEM formatted certificate for a provided private key, followed by the PEM formatted intermediate certificates (if any), in order, and not including the root CA (the root CA must be pre-known to the peer, see ca). When providing multiple cert chains, they do not have to be in the same order as their private keys in key. If the intermediate certificates are not provided, the peer will not be able to validate the certificate, and the handshake will fail.
    • sigalgs <string> Colon-separated list of supported signature algorithms. The list can contain digest algorithms (SHA256, MD5 etc.), public key algorithms (RSA-PSS, ECDSA etc.), combination of both (e.g 'RSA+SHA384') or TLS v1.3 scheme names (e.g. rsa_pss_pss_sha512). See OpenSSL man pages for more info.
    • ciphers <string> Cipher suite specification, replacing the default. For more information, see Modifying the default TLS cipher suite. Permitted ciphers can be obtained via tls.getCiphers(). Cipher names must be uppercased in order for OpenSSL to accept them.
    • clientCertEngine <string> Name of an OpenSSL engine which can provide the client certificate.
    • crl <string> | <string[]> | <Buffer> | <Buffer[]> PEM formatted CRLs (Certificate Revocation Lists).
    • dhparam <string> | <Buffer> Diffie-Hellman parameters, required for perfect forward secrecy. Use openssl dhparam to create the parameters. The key length must be greater than or equal to 1024 bits or else an error will be thrown. Although 1024 bits is permissible, use 2048 bits or larger for stronger security. If omitted or invalid, the parameters are silently discarded and DHE ciphers will not be available.
    • ecdhCurve <string> A string describing a named curve or a colon separated list of curve NIDs or names, for example P-521:P-384:P-256, to use for ECDH key agreement. Set to auto to select the curve automatically. Use crypto.getCurves() to obtain a list of available curve names. On recent releases, openssl ecparam -list_curves will also display the name and description of each available elliptic curve. Default: tls.DEFAULT_ECDH_CURVE.
    • honorCipherOrder <boolean> Attempt to use the server's cipher suite preferences instead of the client's. When true, causes SSL_OP_CIPHER_SERVER_PREFERENCE to be set in secureOptions, see OpenSSL Options for more information.
    • key <string> | <string[]> | <Buffer> | <Buffer[]> | <Object[]> Private keys in PEM format. PEM allows the option of private keys being encrypted. Encrypted keys will be decrypted with options.passphrase. Multiple keys using different algorithms can be provided either as an array of unencrypted key strings or buffers, or an array of objects in the form {pem: <string|buffer>[, passphrase: <string>]}. The object form can only occur in an array. object.passphrase is optional. Encrypted keys will be decrypted with object.passphrase if provided, or options.passphrase if it is not.
    • privateKeyEngine <string> Name of an OpenSSL engine to get private key from. Should be used together with privateKeyIdentifier.
    • privateKeyIdentifier <string> Identifier of a private key managed by an OpenSSL engine. Should be used together with privateKeyEngine. Should not be set together with key, because both options define a private key in different ways.
    • maxVersion <string> Optionally set the maximum TLS version to allow. One of 'TLSv1.3', 'TLSv1.2', 'TLSv1.1', or 'TLSv1'. Cannot be specified along with the secureProtocol option; use one or the other. Default: tls.DEFAULT_MAX_VERSION.
    • minVersion <string> Optionally set the minimum TLS version to allow. One of 'TLSv1.3', 'TLSv1.2', 'TLSv1.1', or 'TLSv1'. Cannot be specified along with the secureProtocol option; use one or the other. Avoid setting to less than TLSv1.2, but it may be required for interoperability. Default: tls.DEFAULT_MIN_VERSION.
    • passphrase <string> Shared passphrase used for a single private key and/or a PFX.
    • pfx <string> | <string[]> | <Buffer> | <Buffer[]> | <Object[]> PFX or PKCS12 encoded private key and certificate chain. pfx is an alternative to providing key and cert individually. PFX is usually encrypted, if it is, passphrase will be used to decrypt it. Multiple PFX can be provided either as an array of unencrypted PFX buffers, or an array of objects in the form {buf: <string|buffer>[, passphrase: <string>]}. The object form can only occur in an array. object.passphrase is optional. Encrypted PFX will be decrypted with object.passphrase if provided, or options.passphrase if it is not.
    • secureOptions <number> Optionally affect the OpenSSL protocol behavior, which is not usually necessary. This should be used carefully if at all! Value is a numeric bitmask of the SSL_OP_* options from OpenSSL Options.
    • secureProtocol <string> Legacy mechanism to select the TLS protocol version to use, it does not support independent control of the minimum and maximum version, and does not support limiting the protocol to TLSv1.3. Use minVersion and maxVersion instead. The possible values are listed as SSL_METHODS, use the function names as strings. For example, use 'TLSv1_1_method' to force TLS version 1.1, or 'TLS_method' to allow any TLS protocol version up to TLSv1.3. It is not recommended to use TLS versions less than 1.2, but it may be required for interoperability. Default: none, see minVersion.
    • sessionIdContext <string> Opaque identifier used by servers to ensure session state is not shared between applications. Unused by clients.
    • ticketKeys: <Buffer> 48-bytes of cryptographically strong pseudorandom data. See Session Resumption for more information.
    • sessionTimeout <number> The number of seconds after which a TLS session created by the server will no longer be resumable. See Session Resumption for more information. Default: 300.

tls.createServer() sets the default value of the honorCipherOrder option to true, other APIs that create secure contexts leave it unset.

tls.createServer() uses a 128 bit truncated SHA1 hash value generated from process.argv as the default value of the sessionIdContext option, other APIs that create secure contexts have no default value.

The tls.createSecureContext() method creates a SecureContext object. It is usable as an argument to several tls APIs, such as tls.createServer() and server.addContext(), but has no public methods.

A key is required for ciphers that use certificates. Either key or pfx can be used to provide it.

If the ca option is not given, then Node.js will default to using Mozilla's publicly trusted list of CAs.