繁体   English   中英

配置 https 代理以仅允许 TLS1.2 用于传出请求

[英]Configure https agent to allow only TLS1.2 for outgoing requests

我正在使用客户端证书从节点应用程序建立 HTTPS 连接:

var options = { 
    hostname: 'https://my-server.com', 
    port: 443, 
    path: '/', 
    method: 'GET', 
    key: fs.readFileSync('client1-key.pem'), 
    cert: fs.readFileSync('client1-crt.pem'), 
    ca: fs.readFileSync('ca-crt.pem') }; 

var req = https.request(options, res => { 
    [...]
}); 

一切正常,但我想添加代码以确保只允许 TLS 1.2 连接。 我在https.agent选项或其他地方找不到任何配置它的方法 是否可以配置它,或者我是否必须建立连接然后查询协议版本,例如:

res.socket.getProtocol() === 'TLSv1.2'

如果协议不满意就中止连接?

首先,我找到了有关发出HTTPS 请求的文档。 它提到您可以将其他选项传递给tls.connect() ,其中包括称为secureProtocol 深入研究tls.connect() ,我发现了secureContext选项,其中提到了tls.createSecureContext() 在那里它最后提到了secureProtocol ,它可以用来自OpenSSL 页面的字符串指定。 我选择了一个看起来合理的字符串( TLSv1_2_method )并将secureProtocol选项直接传递给https.request

这将打印SSL Version: TLS 1.2 with the given secureProtocolSSL Version: TLS 1.1 with secureProtocol: "TLSv1_1_method" 如果无法使用给定的 TLS 版本建立连接,则会调用最后的错误处理程序。

var https = require('https')

var options = {
    hostname: 'www.howsmyssl.com',
    port: 443,
    path: '/a/check',
    method: 'GET',
    secureProtocol: "TLSv1_2_method"
}

https.request(options, res => {
  let body = ''
  res.on('data', d => body += d)
  res.on('end', () => {
    data = JSON.parse(body)
    console.log('SSL Version: ' + data.tls_version)
  })
}).on('error', err => {
  // This gets called if a connection cannot be established.
  console.warn(err)
}).end()

只是关于此解决方案的更新,几年过去了,有些事情发生了变化。

Node 文档现在建议使用minVersionmaxVersion代替secureProtocol因为最后一个选项已成为选择 TLS 协议版本的遗留机制,因此您可以使用minVersion: "TLSv1.2"获得相同的结果:

var https = require('https')

var options = {
    hostname: 'www.howsmyssl.com',
    port: 443,
    path: '/a/check',
    method: 'GET',
    minVersion: "TLSv1.2",
    maxVersion: "TLSv1.2"
}
...

参考: 节点文档:tls_tls_createsecurecontext_options

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM