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 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". See alsotls.rootCertificates.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,但为实现互操作性可能需要这样设置。默认值: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(如果未提供object.passphrase)进行解密。secureOptions<number> 可选择性地影响 OpenSSL 协议行为,通常并不必要。如果使用,应谨慎!值是来自 OpenSSL 选项 的SSL_OP_*选项的数字位掩码。secureProtocol<string> 选择要使用的 TLS 协议版本的旧机制,它不支持对最小版本和最大版本的独立控制,也不支持将协议限制为 TLSv1.3。请改用minVersion和maxVersion。可能的值列在 SSL_方法 中,使用函数名称的字符串。例如,使用'TLSv1_1_method'强制使用 TLS 版本 1.1,或使用'TLS_method'允许使用最高到 TLSv1.3 的任何 TLS 协议版本。不推荐使用低于 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.】