TLS/SSL 协议

TLS/SSL 协议

August 30, 2018
tls, security
  • TLS/SSL 是什么,他们的区别是什么
  • TLS 握手协议 - handshake protocol
  • TLS 如何提供认证、授权、保密以及完整性
  • 实例:用openssl 生成证书链, client/server 如何使用证书链

TLS/SSL 概念 #

tls-ssl

TLS vs SSL The ‘S’ in “HTTPS” is the TLS protocol. When folks refer to the “TLS” they are referring to the most common of modern protocols of encrypting data across the internet. “SSL”, when used by experts, refers to the older versions of these protocols. In general, people use “SSL” and “TLS” interchangeably, but that’s changing towards everyone saying “TLS”. “TLS” is what everyone will call it in the future, while “SSL” is the phrase everyone knows right now.

  • SSL:(Secure Socket Layer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协议层。SSL通过互相认证、使用数字签名确保完整性、使用加密确保私密性,以实现客户端和服务器之间的安全通讯。该协议由两层组成:SSL记录协议和SSL握手协议。

  • TLS:(Transport Layer Security,传输层安全协议),用于两个应用程序之间提供保密性和数据完整性。该协议由两层组成:TLS记录协议和TLS握手协议。

  • SSL是Netscape开发的专门用户保护Web通讯的,目前版本为3.0。最新版本的TLS 1.2是IETF(工程任务组)制定的一种新的协议,它建立在SSL 3.0协议规范之上,是SSL 3.0的后续版本。TLS更安全,它是写入了RFC的。 Google于2014年正式宣布在所有产品线停止支持SSL3.0, 转而使用更加完全的TLS, 目前chrome使用的TLS版本为1.2。

  • 查看openssl 支持的 tls/ssl 版本

openssl ciphers -v
  • 查看浏览器 tls/ssl 版本
F12 --> security tab

TLS/SSL握手协议 #

TLS的主要目标是使SSL更安全,并使协议的规范更精确和完善, TLS 与 SSL 在握手协议流程上基本相同。 TLS handshake 使得客户端和服务器能够建立以secrect keys加密的通信通道。 TLS handshake的目的:

  • 协商客户端、服务器使用的TLS版本号
  • 协商使用的加解密算法
  • 通过exchanging and velidating数字签名进行认证
  • 使用非对称加密算法生成握手完成后使用的对称加解密密钥, 以解决密钥分发的问题

TLS handshake 主要流程 #

tls-one-way-handshake

  1. TCP三握手完成之后, client发送"client hello" message到server, “client hello” message包含加解密相关的信息, 比如:TLS版本;client支持的加密算法,按优先级排序;client成的随机数random1, 用于后续的计算;client支持的压缩方式
  2. server收到client的"client hello"message后,回复"server hello" message, “server “hello” message 包含server根据client上送的client算法支持列表选择的加密算法; server端生成的random2; session ID;server自己的digital certificate。 如果server需要认证client(可选),那么server会发送 “client certificate request"消息, “client certificate request” message包含server自身支持的证书类型,以及信任的CA机构Certification Authorities(CAs)。
  3. client校验server的ditital certificate, 详见第三节.
  4. client发送由server的public key加密的第三个随机数random3,client和server使用random1+random2+random3这三个随机数各自计算对称加密的密钥.
  5. 如果server在第2步,发送了"client certificate request”,触发了双向认证, client发送用client自己的private key加密由client生成的一个随机数, 以及client的digital certificate或者"no digital certificate alert”。这个alert仅仅是一个warning, 但是有些握手协议的实现如果要求必须使用双向认证,则会把它当作握手失败处理。
  6. server检验客户端的digital certificate,详见第三节.
  7. client 发送使用对称密钥加密的"finished" message, 表明client端的握手完成
  8. server 发送用对称密钥加密的"finished" message, 表明server端握手完成, TLS session建立
  9. 在TLS session期间,client 和 server之前的消息使用对称密钥加解密

证书(Certificate) #

什么是证书(Certificate) #

关于TLS的认证机制里会经常听到或者遇到证书(Certificate)这个概念涉及其中,那么什么是证书呢?

Wikipedia对数字证书的定义是:

In cryptography, a public key certificate, also known as a digital certificate or identity certificate, is an electronic document used to prove the validity of a public key.[1] The certificate includes information about the key, information about the identity of its owner (called the subject), and the digital signature of an entity that has verified the certificate’s contents (called the issuer). If the signature is valid, and the software examining the certificate trusts the issuer, then it can use that key to communicate securely with the certificate’s subject. In email encryption, code signing, and e-signature systems, a certificate’s subject is typically a person or organization. However, in Transport Layer Security (TLS) a certificate’s subject is typically a computer or other device, though TLS certificates may identify organizations or individuals in addition to their core role in identifying devices. TLS, sometimes called by its older name Secure Sockets Layer (SSL), is notable for being a part of HTTPS, a protocol for securely browsing the web.

用一句概述就是: 数字证书又称为公钥证书或者身份证书是一种用来证明公钥的有效性的电子文档.

证书通常包含

  1. 关于公钥的信息
  2. 证书拥有者(Owner/Subject)的身份标识
  3. 由证书签发者(Issuer)对证书自身内容的生成数字签名(digital signature)
  4. 签发者(Issuer)的身份标识

其中Owner(也可以称为Subject)必须是可以标识服务主机名的Common Name(CN), 一个证书也可以对多个主机名有效(比如域名(domin)以及它的子域名(subdomains)), 对于有包含多个主机名的证书被称为Subject Alternative Name (SAN) certificates 或者 Unified Communications Certificates (UCC), 对于域名中包含通配符(*)的证书也称为wildcard certificate

PEM(Privacy Enhanced Mail) #

我们通常所说证书基本都是X.509的证书或者密钥, 关于X.509通常有两种编码格式PEM(Base64 ASCII)和DER(binary)的文件

PEM格式的证书文件是基于Base64 ASCII的, 它是最常见的X.509证书(Certificates)或者私钥(private keys)或者CSR编码格式,PEM文件内容包含一个或者多个项目, 每一个项目都有固定的开头和结尾(比如对证书而言会以-----BEGIN CERTIFICATE-----开头, -----END CERTIFICATE-----结尾),一个PEM文件可以包含一个终端(End-entity)证书,一个私钥(private key)或者由多个证书组成的一级证书链。常见的PEM文件后缀名有.crt, .pem, .cer, and .key(for private keys)

DER(Distinguished Encoding Rules) #

DER格式的证书文件是二进制格式的X.509证书(Certificates)或者私钥(private keys),不像PEM文件DER文件是被编码成为了非明文字符,没有类似-----BEGIN CERTIFICATE-----之类的开头以及结尾,常见的DER文件后缀名有der.cer

PKCS#12 & PKCS#7 #

PKCS#7 (also known as P7B) is a container format for digital certificates that is most often found in Windows and Java server contexts, and usually has the extension .p7b. PKCS#7 files are not used to store private keys. In the example below, -certfile MORE.pem represents a file with chained intermediate and root certificates.

# Convert PEM certificate with chain of trust to PKCS#7
$ openssl crl2pkcs7 -nocrl -certfile CERTIFICATE.pem -certfile MORE.pem -out CERTIFICATE.p7b

PKCS#12 (also known as PKCS12 or PFX) is a common binary format for storing a certificate chain and private key in a single, encryptable file, and usually have the filename extensions .p12 or .pfx. In the example below, -certfile MORE.pem adds a file with chained intermediate and root certificates, and -inkey PRIVATEKEY.key adds the private key for CERTIFICATE.crt(the end-entity certificate).

# Convert PEM certificate with chain of trust and private key to PKCS#12
# openssl pkcs12 -export -out CERTIFICATE.pfx -inkey PRIVATEKEY.key -in CERTIFICATE.crt -certfile MORE.crt

TLS如何提供认证 #

  1. 对于服务器身份验证,客户端使用服务器的公钥加密用于计算密钥的数据。只有当服务器能够用正确的私钥解密数据时,服务器才能生成密钥。
  2. 对于客户端身份验证,服务器使用客户端证书中的公钥来解密客户端在握手过程中发送的数据。用秘钥加密的已完成消息(步骤7和8)确认身份验证已经完成。
  3. 如果任何身份验证步骤失败,握手失败,会话终止。

涉及到的证书 #

在TLS握手期间交换数字证书是身份验证过程的一部分。证书交换期间,假设:CA X将证书颁发给TLS客户端,CA Y将证书颁发给TLS服务器

单向认证 #

handshake第2步时server未向客户端发送"client certificate request"

  • TLS server:
    • 由CA Y签发的server证书(server certificate)
    • server的private key
  • TLS client:
    • CA Y的证书

双向认证 #

handshake第2步时server向客户端发送"cleint certificate request"请求验证client certificate

  • TLS server:
    • 由CA Y签发的server证书(server certificate)
    • server的private key
    • CA X的证书
  • TLS client:
    • 由 CA X签发的client证书(client certificate)
    • client的private key
    • CA Y的证书

证书检验流程 #

在handshake第3步和第6步, TLS client校验服务证书 和 TLS server校验客户端的证书时涉及到以下几个方面

  1. 证书签名是否有效

Chain_Of_Trust

如果证书是有效并且Issuer是受信任的,那么证书里的公钥就是有效的,可用于TLS握手阶段的密钥交换流程.

  1. 证书有效期校验
  2. 证书是否被吊销
  3. 请求的主体是否与证书里的Subject匹配

密钥重置 #

在TLS握手期间,会生成一个秘钥,以便在TLS client和server之间加密数据。这个秘密密钥被用于一个对称加密算法中,该对称加密算法,将明文转换为不可读的密文,并将密文转换为明文。密钥是由handshake阶段生成的random1+random2+random3能过算法生成的,用于将明文加密为密文。密匙也用于MAC(消息身份验证码)算法,用于确定消息是否被更改。更多信息,请参见消息摘要和数字签名。如果密钥被截取,则可以从密文中对消息的明文进行解密,或者可以计算消息摘要,从而允许在不检测消息的情况下对消息进行修改。即使对于一个复杂的算法,也可以通过将所有可能的数学转换应用到密文中,最终可以发现明文。为了将可以被破译或修改的数据的数量减少到最小,如果密钥被破坏,密钥可以定期重新协商。当秘密密钥被重新协商时,以前的密钥不能再用于解密用新密钥加密的数据。

TLS如何提供保密性 #

TLS使用对称和非对称加密的组合来确保消息的私密性。在TLS握手期间,TLS client和server协商得到一种加密算法和一个对称密钥,只用于一个会话。在TLS client和server之间传输的所有消息都使用该算法和密钥进行加密,以确保消息在被拦截时仍然是私有的。SSL支持广泛的加密算法。因为SSL和TLS在传输对称密钥时使用非对称加密,所以没有关键的分发问题。有关加密技术的更多信息,请参阅密码学

TLS如何提供完整性 #

SSL and TLS provide data integrity by calculating a message digest. For more information, see Data integrity of messages. Use of SSL or TLS does ensure data integrity, provided that the CipherSpec in your channel definition uses a hash algorithm as described in the table in Specifying CipherSpecs. In particular, if data integrity is a concern, you should avoid choosing a CipherSpec whose hash algorithm is listed as “None”. Use of MD5 is also strongly discouraged as this is now very old and no longer secure for most practical purposes.

实例 #

Examples #

  1. Create a root certificate and private key to sign the certificates for your services:
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=example Inc./CN=example.com' -keyout example.com.key -out example.com.crt
  1. Create a certificate and a private key for httpbin.example.com:
openssl req -out httpbin.example.com.csr -newkey rsa:2048 -nodes -keyout httpbin.example.com.key -subj "/CN=httpbin.example.com/O=httpbin organization"
openssl x509 -req -sha256 -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in httpbin.example.com.csr -out httpbin.example.com.crt
  1. Format conversion
# OpenSSL: Convert CER to PEM
$ openssl x509 -in cert.cer -out cert.pem
  1. Check cert
openssl s_client -servername httpbin.example.com -connect httpbin.example.com:443

Demo #

用openssl 生成证书链, client/server 如何使用证书链

参考资料 #