tls.createSecureContext([options])


  • 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[]> 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 时,会在 secureOptions 中设置 SSL_OP_CIPHER_SERVER_PREFERENCE,有关更多信息,请参见 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_方法 中,使用函数名作为字符串。例如,使用 '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(),但它没有公开的方法。

🌐 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.

对于使用证书的密码来说,_需要_一个密钥。可以使用 keypfx 来提供它。

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

如果未提供 ca 选项,那么 Node.js 将默认使用 Mozilla 的公开信任 CA 列表

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