tls.createSecureContext([options])
options<Object>allowPartialTrustChain<boolean> 将信任的 CA 证书列表中的中间(非自签名)证书视为受信任的。ca<string> | <string[]> | <Buffer> | <Buffer[]> Optionally override the trusted CA certificates. If not specified, the CA certificates trusted by default are the same as the ones returned bytls.getCACertificates()using thedefaulttype. If specified, the default list would be completely replaced (instead of being concatenated) by the certificates in thecaoption. Users need to concatenate manually if they wish to add additional certificates instead of completely overriding the default. The value can be a string orBuffer, or anArrayof strings and/orBuffers. Any string orBuffercan 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 thecaoption 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".cert<string> | <string[]> | <Buffer> | <Buffer[]> PEM 格式的证书链。每个私钥应提供一个证书链。每个证书链应包含为所提供的私有key生成的 PEM 格式证书,然后按顺序包含 PEM 格式的中级证书(如果有),但不包括根 CA(根 CA 必须为对端预先已知,见ca)。在提供多个证书链时,其顺序不必与key中的私钥顺序相同。如果未提供中级证书,对端将无法验证证书,握手将失败。sigalgs<string> 以冒号分隔的支持的签名算法列表。列表可以包含摘要算法(如SHA256、MD5等)、公钥算法(如RSA-PSS、ECDSA等)、两者的组合(例如 '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>'auto'或自定义 Diffie-Hellman 参数,适用于非 ECDHE 的 完美前向保密。如果省略或无效,参数将被静默丢弃,DHE 密码套件将不可用。基于 ECDHE 的 完美前向保密 仍然可用。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,但为了互操作性可能需要这样做。TLSv1.2 之前的版本可能需要降级 OpenSSL 安全级别。默认值:tls.DEFAULT_MIN_VERSION。passphrase<string> 用于单个私钥和/或 PFX 的共享密码短语。pfx<string> | <string[]> | <Buffer> | <Buffer[]> | <Object[]> PFX 或 PKCS12 编码的私钥和证书链。pfx是提供key和cert的替代方式。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。请改用minVersion和maxVersion。可选值列在 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 的参数使用,例如 server.addContext(),但没有公开的方法。tls.Server 构造函数和 tls.createServer() 方法不支持 secureContext 选项。
🌐 The tls.createSecureContext() method creates a SecureContext object. It is
usable as an argument to several tls APIs, such as server.addContext(),
but has no public methods. The tls.Server constructor and the
tls.createServer() method do not support the secureContext option.
对于使用证书的密码来说,_需要_一个密钥。可以使用 key 或 pfx 来提供它。
🌐 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.
不推荐使用自定义 DHE 参数,建议使用新的 dhparam: 'auto' 选项。设置为 'auto' 时,将会自动选择足够强度的知名 DHE 参数。否则,如果有必要,可以使用 openssl dhparam 来创建自定义参数。密钥长度必须大于或等于 1024 位,否则会抛出错误。虽然 1024 位是允许的,但为了更强的安全性,建议使用 2048 位或更长的密钥。
🌐 Custom DHE parameters are discouraged in favor of the new dhparam: 'auto'
option. When set to 'auto', well-known DHE parameters of sufficient strength
will be selected automatically. Otherwise, if necessary, openssl dhparam can
be used to create custom 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.