What If Netscape Did SSL the HTTP Way?
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
What If Netscape Did SSL the HTTP Way?
When you visit a secure website today, your browser connects to port 443 and immediately starts speaking TLS. Only once the encryption is up and running does it get around to speaking HTTP. That’s two protocols stacked on top of each other, and the one at the bottom doesn’t know or care about anything the one on top has learned over the decades.
如今,当你访问一个安全网站时,浏览器会连接到 443 端口并立即开始使用 TLS 协议。只有在加密建立并运行后,它才会开始进行 HTTP 通信。这实际上是两个协议堆叠在一起,底层的协议既不知道也不关心顶层协议在过去几十年里学到了什么。
This is why so many good ideas from HTTP had to be reinvented, years later, inside TLS. The most notorious is SNI, which is a Host header with extra steps. It didn’t have to be this way. Let’s take a trip into a parallel universe where Netscape didn’t take the easy way out.
这就是为什么 HTTP 中许多好的想法在多年后不得不被重新发明并塞进 TLS 里。最臭名昭著的例子就是 SNI,它本质上就是一个带有额外步骤的 Host 请求头。其实本不必如此。让我们进入一个平行宇宙,看看如果网景公司(Netscape)当初没有选择走捷径会怎样。
🕰️ The Fork in the Road When Netscape released SSL in 1995, it came with a new URL scheme (https://) and a new port number (443). Plenty of people grumbled. The web already had a protocol and a port, so why invent new ones? Why not secure the conversation you’re already having?
🕰️ 分岔路口 当网景公司在 1995 年发布 SSL 时,它带来了一个新的 URL 方案(https://)和一个新的端口号(443)。许多人对此抱怨不已。当时网络已经有了协议和端口,为什么要发明新的呢?为什么不直接保护现有的通信过程呢?
To be fair to Netscape, in 1995 there wasn’t really a “proper” way to do that. HTTP/1.0 had no mechanism for handing a connection over to another protocol. The Upgrade header didn’t arrive until HTTP/1.1 in 1997, and the Host header, which made it possible to run many websites on one IP address, came with it. Netscape couldn’t have used either of them because neither existed yet.
平心而论,在 1995 年确实没有一种“妥当”的方法来实现这一点。HTTP/1.0 没有将连接移交给其他协议的机制。Upgrade 请求头直到 1997 年的 HTTP/1.1 才出现,而使在单个 IP 地址上运行多个网站成为可能的 Host 请求头也是随之而来的。网景公司当时无法使用它们,因为它们尚未诞生。
But this is my parallel universe, so let’s say HTTP grew those headers a couple of years early and Netscape decided to wait for them.
但这是我的平行宇宙,所以假设 HTTP 提前几年就拥有了这些请求头,而网景公司决定等待它们的出现。
🌐 Hello, Parallel Universe In our universe, a TLS connection begins with a ClientHello and the server replies with its certificate. In the parallel universe, that first exchange is just plain HTTP in the clear. The browser wants to buy something from shop.example.com. It connects to port 80, the same as always, and asks for the server’s certificate.
🌐 你好,平行宇宙 在我们的宇宙中,TLS 连接以 ClientHello 开始,服务器随后回复其证书。而在平行宇宙中,第一次交换只是明文的普通 HTTP。浏览器想要从 shop.example.com 购买商品,它像往常一样连接到 80 端口,并请求服务器的证书。
GET /.well-known/tls-cert HTTP/1.1 Host: shop.example.com Accept: application/pkix-cert (Yes, /.well-known/ is from 2010. It’s a parallel universe. Let me have this one.)
GET /.well-known/tls-cert HTTP/1.1 Host: shop.example.com Accept: application/pkix-cert (是的,/.well-known/ 是 2010 年才有的。这是平行宇宙,就让我任性一次吧。)
The server responds with its certificate, and the certificates of any intermediate CAs, as an ordinary HTTP response. The browser checks the chain against its list of root CAs, exactly as it would in our universe. Is it signed by someone we trust? Is it for shop.example.com? Has it expired?
服务器以普通的 HTTP 响应返回其证书以及任何中间证书颁发机构(CA)的证书。浏览器会像在我们的宇宙中一样,根据其根证书列表检查证书链。它是由我们信任的人签名的吗?它是发给 shop.example.com 的吗?它过期了吗?
If you’re worried about sending the certificate in the clear, don’t be. Every version of SSL and TLS up to 1.2 sent the certificate unencrypted too. A certificate is public by design. An attacker can swap it for a different one, but they can’t make one that’s valid for shop.example.com without a CA’s help, and if they can get one of those, you have bigger problems.
如果你担心以明文形式发送证书,大可不必。直到 1.2 版本为止,所有版本的 SSL 和 TLS 协议也都是以未加密方式发送证书的。证书的设计初衷就是公开的。攻击者可以将其替换为另一个证书,但如果没有 CA 的帮助,他们无法伪造一个对 shop.example.com 有效的证书;如果他们真能做到这一点,那你面临的问题就严重多了。
Once the browser is happy, it knows the server’s public key. It invents a random secret, encrypts it with that key and asks to upgrade.
一旦浏览器确认无误,它就获得了服务器的公钥。它会生成一个随机密钥,用该公钥加密,并请求升级连接。
GET /checkout HTTP/1.1 Host: shop.example.com Connection: Upgrade Upgrade: TLS/1.0 TLS-Key: (the random secret, encrypted with the server’s public key, in base-64)
GET /checkout HTTP/1.1 Host: shop.example.com Connection: Upgrade Upgrade: TLS/1.0 TLS-Key: (用服务器公钥加密的随机密钥,Base-64 编码)
Only the real server, holding the private key, can decrypt that secret. It replies with a random value of its own and switches protocols.
只有持有私钥的真实服务器才能解密该密钥。它会回复一个自己的随机值,并切换协议。
HTTP/1.1 101 Switching Protocols Connection: Upgrade Upgrade: TLS/1.0 TLS-Nonce: (the server’s random value, in base-64)
HTTP/1.1 101 Switching Protocols Connection: Upgrade Upgrade: TLS/1.0 TLS-Nonce: (服务器的随机值,Base-64 编码)
Both sides now derive their encryption keys from the browser’s secret, the server’s random value and a hash of everything that was said in the clear up to this point. Mixing in that hash means that if anyone tampered with the plaintext part, the two sides end up with different keys and the connection fails. From this point on everything on the connection is encrypted, starting with the response to GET /checkout.
双方现在根据浏览器的密钥、服务器的随机值以及到目前为止所有明文通信的哈希值来推导加密密钥。混入该哈希值意味着,如果有人篡改了明文部分,双方最终得到的密钥就会不同,从而导致连接失败。从这一刻起,连接上的所有内容都将被加密,从对 GET /checkout 的响应开始。
I’m describing the 1995 approach, where the browser encrypted a secret with the server’s RSA key. When Diffie-Hellman key exchange came along, it would have been just a different pair of headers: the browser sends its half of the exchange, and the server replies with its half, signed by the key in its certificate. Changing how keys are exchanged becomes header negotiation, which HTTP is already very good at.
我描述的是 1995 年的方法,即浏览器使用服务器的 RSA 公钥加密一个密钥。当 Diffie-Hellman 密钥交换出现时,它只需要一对不同的请求头:浏览器发送其交换部分,服务器回复其交换部分,并由证书中的密钥签名。改变密钥交换方式变成了请求头协商,而这正是 HTTP 非常擅长的。
Or, to put it another way: Browser: Hello, shop.example.com. Who are you? Server: Here’s my certificate. Browser: That checks out. Here’s a secret only you can read. Shall we go somewhere private? Server: 101. Eavesdropper: Aw. “So why don’t we go somewhere only we know?”
换句话说: 浏览器:你好,shop.example.com。你是谁? 服务器:这是我的证书。 浏览器:验证通过。这是一个只有你能读取的密钥。我们去个私密的地方谈谈? 服务器:101(切换协议)。 窃听者:唉。“为什么我们不去个只有我们知道的地方呢?”
🏠 Virtual Hosting From Day One The first thing we get for free is the one that took our universe the longest to sort out. That very first request for the certificate has a Host header. A server hosting a thousand websites on one IP address knows exactly which certificate to send back, because the browser told it, using a header the server already understands. There’s nothing to retrofit.
🏠 从第一天起就支持虚拟主机 我们免费获得的第一项优势,正是我们宇宙中花费最长时间才解决的问题。最初请求证书的那个请求中包含 Host 请求头。一台在单个 IP 地址上托管一千个网站的服务器,能够准确知道该返回哪个证书,因为浏览器已经通过服务器能够理解的请求头告知了它。无需进行任何改造。
In our universe, the server had to pick a certificate before it knew which website you wanted. For years, every secure website needed its own IP address, which was one of the reasons HTTPS stayed expensive and rare for so long. The fix was SNI, standardized in 2003: an extension that lets the browser mention the website’s name at the very start of the TLS handshake. In other words, a Host header, reinvented inside TLS. The parallel universe never needs it.
在我们的宇宙中,服务器必须在知道你想要访问哪个网站之前就选好证书。多年来,每个安全网站都需要独立的 IP 地址,这也是 HTTPS 长期以来昂贵且罕见的原因之一。解决方案是 2003 年标准化的 SNI:一个允许浏览器在 TLS 握手开始时就提及网站名称的扩展。换句话说,这就是在 TLS 内部重新发明的 Host 请求头。而在平行宇宙中,根本不需要它。
And it’s all on one port. Firewalls, proxies and load balancers deal with port 80 and nothing else. A URL is a URL.
而且这一切都在同一个端口上完成。防火墙、代理和负载均衡器只需要处理 80 端口。URL 就是 URL。
📦 A Smaller Black Box So if SNI was standardized in 2003, why couldn’t a website rely on it until the mid-2010s? This is the part I find most interesting.
📦 更小的黑盒 那么,如果 SNI 在 2003 年就标准化了,为什么网站直到 2010 年代中期才敢依赖它呢?这是我觉得最有趣的部分。
In our universe, TLS is a black box. Your application hands a socket to a TLS library, and the library takes care of everything: negotiating versions, swapping certificates, exchanging keys and the encryption itself. The box comes with a large “do not touch” sign, which is good advice. People who write their own cryptography tend to get it wrong.
在我们的宇宙中,TLS 是一个黑盒。你的应用程序将套接字交给 TLS 库,库会处理一切:协商版本、交换证书、交换密钥以及加密本身。这个盒子上贴着一个大大的“请勿触碰”标签,这确实是个好建议。自己编写加密代码的人往往会出错。
The trouble is that a black box that does everything can only be improved by whoever owns it. Browsers couldn’t use SNI until their TLS library supported it, and on many platforms that library was part of the operating system. Internet Explorer on Windows XP never got SNI at all. A website cou
问题在于,一个包办一切的黑盒只能由其所有者来改进。浏览器必须等到其 TLS 库支持 SNI 才能使用它,而在许多平台上,该库是操作系统的一部分。Windows XP 上的 Internet Explorer 从未支持过 SNI。一个网站……