SSL工作原理

关键词:SSL,PKI,MAC

摘    要:SSL利用数据加密、身份验证和消息完整性验证机制,为基于TCP等可靠连接的应用层协议提供安全性保证。本文介绍了SSL的产生背景、安全机制、工作过程及典型组网应用。

缩略语:

缩略语

英文全名

中文解释

AES

Advanced Encryption Standard

高级加密标准

CA

Certificate Authority

证书机构

DES

Data Encryption Standard

数据加密标准

HTTPS

Hypertext Transfer Protocol Secure

安全超文本传输协议

MAC

Message Authentication Code

消息验证码

MD5

Message Digest 5

消息摘要算法5

PKI

Public Key Infrastructure

公钥基础设施

RSA

Rivest Shamir and Adleman

非对称密钥算法的一种

SHA

Secure Hash Algorithm

安全散列算法

SSL

Secure Sockets Layer

安全套接层

VPN

Virtual Private Network

虚拟专有网络

 

 


 


 

1  概述

1.1  产生背景

基于万维网的电子商务和网上银行等新兴应用,极大地方便了人们的日常生活,受到人们的青睐。由于这些应用都需要在网络上进行在线交易,它们对网络通 信的安全性提出了更高的要求。传统的万维网协议HTTP不具备安全机制——采用明文的形式传输数据、不能验证通信双方的身份、无法防止传输的数据被篡改 等,导致HTTP无法满足电子商务和网上银行等应用的安全性要求。

Netscape公司提出的安全协议SSL,利用数据加密、身份验证和消息完整性验证机制,为网络上数据的传输提供安全性保证。SSL可以为HTTP提供安全连接,从而很大程度上改善了万维网的安全性问题。

1.2  技术优点

SSL具有如下优点:

l              提供较高的安全性保证。SSL利用数据加密、身份验证和消息完整性验证机制,保证网络上数据传输的安全性。

l              支持各种应用层协议。虽然SSL设计的初衷是为了解决万维网安全性问题,但是由于SSL位于应用层和传输层之间,它可以为任何基于TCP等可靠连接的应用层协议提供安全性保证。

l              部署简单。目前SSL已经成为网络中用来鉴别网站和网页浏览者身份,在浏览器使用者及Web服务器之间进行加密通信的全球化标准。SSL协议已被集成到大 部分的浏览器中,如IE、Netscape、Firefox等。这就意味着几乎任意一台装有浏览器的计算机都支持SSL连接,不需要安装额外的客户端软 件。

2  协议安全机制

SSL协议实现的安全机制包括:

l              数据传输的机密性:利用对称密钥算法对传输的数据进行加密。

l              身份验证机制:基于证书利用数字签名方法对服务器和客户端进行身份验证,其中客户端的身份验证是可选的。

l              消息完整性验证:消息传输过程中使用MAC算法来检验消息的完整性。

2.1  数据传输的机密性

网络上传输的数据很容易被非法用户窃取,SSL采用在通信双方之间建立加密通道的方法保证数据传输的机密性。

所谓加密通道,是指发送方在发送数据前,使用加密算法和加密密钥对数据进行加密,然后将数据发送给对方;接收方接收到数据后,利用解密算法和解密密钥从密文中获取明文。没有解密密钥的第三方,无法将密文恢复为明文,从而保证数据传输的机密性。

加解密算法分为两类:

l              对称密钥算法:数据加密和解密时使用相同的密钥。

l              非对称密钥算法:数据加密和解密时使用不同的密钥,一个是公开的公钥,一个是由用户秘密保存的私钥。利用公钥(或私钥)加密的数据只能用相应的私钥(或公钥)才能解密。

与非对称密钥算法相比,对称密钥算法具有计算速度快的优点,通常用于对大量信息进行加密(如对所有报文加密);而非对称密钥算法,一般用于数字签名和对较少的信息进行加密。

SSL加密通道上的数据加解密使用对称密钥算法,目前主要支持的算法有DES、3DES、AES等,这些算法都可以有效地防止交互数据被窃听。

对称密钥算法要求解密密钥和加密密钥完全一致。因此,利用对称密钥算法加密传输数据之前,需要在通信两端部署相同的密钥。对称密钥的部署方法请参见“2.4  利用非对称密钥算法保证密钥本身的安全”。

2.2  身份验证机制

电子商务和网上银行等应用中必须保证要登录的Web服务器是真实的,以免重要信息被非法窃取。SSL利用数字签名来验证通信对端的身份。

非对称密钥算法可以用来实现数字签名。由于通过私钥加密后的数据只能利用对应的公钥进行解密,因此根据解密是否成功,就可以判断发送者的身份,如同 发送者对数据进行了“签名”。例如,Alice使用自己的私钥对一段固定的信息加密后发给Bob,Bob利用Alice的公钥解密,如果解密结果与固定信 息相同,那么就能够确认信息的发送者为Alice,这个过程就称为数字签名。

SSL客户端必须验证SSL服务器的身份,SSL服务器是否验证SSL客户端的身份,则由SSL服务器决定。SSL客户端和SSL服务器的身份验证过程,请参见“3.2  SSL握手过程”。

使用数字签名验证身份时,需要确保被验证者的公钥是真实的,否则,非法用户可能会冒充被验证者与验证者通信。如图1所示,Cindy冒充Bob,将 自己的公钥发给Alice,并利用自己的私钥计算出签名发送给Alice,Alice利用“Bob”的公钥(实际上为Cindy的公钥)成功验证该签名, 则Alice认为Bob的身份验证成功,而实际上与Alice通信的是冒充Bob的Cindy。SSL利用PKI提供的机制保证公钥的真实性,详细介绍请 参见“2.5  利用PKI保证公钥的真实性”。

图1 伪造公钥

2.3  消息完整性验证

为了避免网络中传输的数据被非法篡改,SSL利用基于MD5或SHA的MAC算法来保证消息的完整性。

MAC算法是在密钥参与下的数据摘要算法,能将密钥和任意长度的数据转换为固定长度的数据。利用MAC算法验证消息完整性的过程如图2所示。发送者 在密钥的参与下,利用MAC算法计算出消息的MAC值,并将其加在消息之后发送给接收者。接收者利用同样的密钥和MAC算法计算出消息的MAC值,并与接 收到的MAC值比较。如果二者相同,则报文没有改变;否则,报文在传输过程中被修改,接收者将丢弃该报文。

图2 MAC算法示意图

MAC算法具有如下特征,使其能够用来验证消息的完整性:

l              消息的任何改变,都会引起输出的固定长度数据产生变化。通过比较MAC值,可以保证接收者能够发现消息的改变。

l              MAC算法需要密钥的参与,因此没有密钥的非法用户在改变消息的内容后,无法添加正确的MAC值,从而保证非法用户无法随意修改消息内容。

MAC算法要求通信双方具有相同的密钥,否则MAC值验证将会失败。因此,利用MAC算法验证消息完整性之前,需要在通信两端部署相同的密钥。MAC密钥的部署方法请参见“2.4  利用非对称密钥算法保证密钥本身的安全”。

2.4  利用非对称密钥算法保证密钥本身的安全

对称密钥算法和MAC算法要求通信双方具有相同的密钥,否则解密或MAC值验证将失败。因此,要建立加密通道或验证消息完整性,必须先在通信双方部署一致的密钥。

SSL利用非对称密钥算法加密密钥的方法实现密钥交换,保证第三方无法获取该密钥。如图3所示,SSL客户端(如Web浏览器)利用SSL服务器 (如Web服务器)的公钥加密密钥,将加密后的密钥发送给SSL服务器,只有拥有对应私钥的SSL服务器才能从密文中获取原始的密钥。SSL通常采用 RSA算法加密传输密钥。

图3 密钥交换示意图

l      实际上,SSL客户端发送给SSL服务器的密钥不能直接用来加密数据或计算MAC值,该密钥是用来计算对称密钥和MAC密钥的信息,称为 premaster secret。SSL客户端和SSL服务器利用premaster secret计算出相同的主密钥(master secret),再利用master secret生成用于对称密钥算法、MAC算法等的密钥。premaster secret是计算对称密钥、MAC算法密钥的关键。

l      用来实现密钥交换的算法称为密钥交换算法。非对称密钥算法RSA用于密钥交换时,也可以称之为密钥交换算法。

 

利用非对称密钥算法加密密钥之前,发送者需要获取接收者的公钥,并保证该公钥确实属于接收者,否则,密钥可能会被非法用户窃取。如图1所 示,Cindy冒充Bob,将自己的公钥发给Alice,Alice利用Cindy的公钥加密发送给Bob的数据,Bob由于没有对应的私钥无法解密该数 据,而Cindy截取数据后,可以利用自己的私钥解密该数据。SSL利用PKI提供的机制保证公钥的真实性,详细介绍请参见“2.5  利用PKI保证公钥的真实性”。

2.5  利用PKI保证公钥的真实性

PKI通过数字证书来发布用户的公钥,并提供了验证公钥真实性的机制。数字证书(简称证书)是一个包含用户的公钥及其身份信息的文件,证明了用户与公钥的关联。数字证书由权威机构——CA签发,并由CA保证数字证书的真实性。

SSL客户端把密钥加密传递给SSL服务器之前,SSL服务器需要将从CA获取的证书发送给SSL客户端,SSL客户端通过PKI判断该证书的真实性。如果该证书确实属于SSL服务器,则利用该证书中的公钥加密密钥,发送给SSL服务器。

验证SSL服务器/SSL客户端的身份之前,SSL服务器/SSL客户端需要将从CA获取的证书发送给对端,对端通过PKI判断该证书的真实性。如果该证书确实属于SSL服务器/SSL客户端,则对端利用该证书中的公钥验证SSL服务器/SSL客户端的身份。

3  协议工作过程

3.1  SSL的分层结构

图4 SSL协议分层

如图4所示,SSL位于应用层和传输层之间,它可以为任何基于TCP等可靠连接的应用层协议提供安全性保证。SSL协议本身分为两层:

l              上层为SSL握手协议(SSL handshake protocol)、SSL密码变化协议(SSL change cipher spec protocol)和SSL警告协议(SSL alert protocol);

l              底层为SSL记录协议(SSL record protocol)。

其中:

l              SSL握手协议:是SSL协议非常重要的组成部分,用来协商通信过程中使用的加密套件(加密算法、密钥交换算法和MAC算法等)、在服务器和客户端之间安全地交换密钥、实现服务器和客户端的身份验证。

l              SSL密码变化协议:客户端和服务器端通过密码变化协议通知对端,随后的报文都将使用新协商的加密套件和密钥进行保护和传输。

l              SSL警告协议:用来向通信对端报告告警信息,消息中包含告警的严重级别和描述。

l              SSL记录协议:主要负责对上层的数据(SSL握手协议、SSL密码变化协议、SSL警告协议和应用层协议报文)进行分块、计算并添加MAC值、加密,并把处理后的记录块传输给对端。

3.2  SSL握手过程

SSL通过握手过程在客户端和服务器之间协商会话参数,并建立会话。会话包含的主要参数有会话ID、对方的证书、加密套件(密钥交换算法、数据加密 算法和MAC算法等)以及主密钥(master secret)。通过SSL会话传输的数据,都将采用该会话的主密钥和加密套件进行加密、计算MAC等处理。

不同情况下,SSL握手过程存在差异。下面将分别描述以下三种情况下的握手过程:

l              只验证服务器的SSL握手过程

l              验证服务器和客户端的SSL握手过程

l              恢复原有会话的SSL握手过程

3.2.1  只验证服务器的SSL握手过程

图5 只验证服务器的SSL握手过程

如图5所示,只需要验证SSL服务器身份,不需要验证SSL客户端身份时,SSL的握手过程为:

(1)        SSL客户端通过Client Hello消息将它支持的SSL版本、加密算法、密钥交换算法、MAC算法等信息发送给SSL服务器。

(2)        SSL服务器确定本次通信采用的SSL版本和加密套件,并通过Server Hello消息通知给SSL客户端。如果SSL服务器允许SSL客户端在以后的通信中重用本次会话,则SSL服务器会为本次会话分配会话ID,并通过 Server Hello消息发送给SSL客户端。

(3)        SSL服务器将携带自己公钥信息的数字证书通过Certificate消息发送给SSL客户端。

(4)        SSL服务器发送Server Hello Done消息,通知SSL客户端版本和加密套件协商结束,开始进行密钥交换。

(5)        SSL客户端验证SSL服务器的证书合法后,利用证书中的公钥加密SSL客户端随机生成的premaster secret,并通过Client Key Exchange消息发送给SSL服务器。

(6)        SSL客户端发送Change Cipher Spec消息,通知SSL服务器后续报文将采用协商好的密钥和加密套件进行加密和MAC计算。

(7)        SSL客户端计算已交互的握手消息(除Change Cipher Spec消息外所有已交互的消息)的Hash值,利用协商好的密钥和加密套件处理Hash值(计算并添加MAC值、加密等),并通过Finished消息 发送给SSL服务器。SSL服务器利用同样的方法计算已交互的握手消息的Hash值,并与Finished消息的解密结果比较,如果二者相同,且MAC值 验证成功,则证明密钥和加密套件协商成功。

(8)        同样地,SSL服务器发送Change Cipher Spec消息,通知SSL客户端后续报文将采用协商好的密钥和加密套件进行加密和MAC计算。

(9)        SSL服务器计算已交互的握手消息的Hash值,利用协商好的密钥和加密套件处理Hash值(计算并添加MAC值、加密等),并通过Finished消息 发送给SSL客户端。SSL客户端利用同样的方法计算已交互的握手消息的Hash值,并与Finished消息的解密结果比较,如果二者相同,且MAC值 验证成功,则证明密钥和加密套件协商成功。

SSL客户端接收到SSL服务器发送的Finished消息后,如果解密成功,则可以判断SSL服务器是数字证书的拥有者,即SSL服务器身份验证 成功,因为只有拥有私钥的SSL服务器才能从Client Key Exchange消息中解密得到premaster secret,从而间接地实现了SSL客户端对SSL服务器的身份验证。

&  说明:

l      Change Cipher Spec消息属于SSL密码变化协议,其他握手过程交互的消息均属于SSL握手协议,统称为SSL握手消息。

l      计算Hash值,指的是利用Hash算法(MD5或SHA)将任意长度的数据转换为固定长度的数据。

 

3.2.2  验证服务器和客户端的SSL握手过程

图6 验证服务器和客户端的SSL握手过程

SSL客户端的身份验证是可选的,由SSL服务器决定是否验证SSL客户端的身份。如图6中蓝色部分标识的内容所示,如果SSL服务器验证SSL客 户端身份,则SSL服务器和SSL客户端除了交互“3.2.1  只验证服务器的SSL握手过程”中的消息协商密钥和加密套件外,还需要进行以下操作:

(1)        SSL服务器发送Certificate Request消息,请求SSL客户端将其证书发送给SSL服务器。

(2)        SSL客户端通过Certificate消息将携带自己公钥的证书发送给SSL服务器。SSL服务器验证该证书的合法性。

(3)        SSL客户端计算已交互的握手消息、主密钥的Hash值,利用自己的私钥对其进行加密,并通过Certificate Verify消息发送给SSL服务器。

(4)        SSL服务器计算已交互的握手消息、主密钥的Hash值,利用SSL客户端证书中的公钥解密Certificate Verify消息,并将解密结果与计算出的Hash值比较。如果二者相同,则SSL客户端身份验证成功。

3.2.3  恢复原有会话的SSL握手过程

图7 恢复原有会话的SSL握手过程

协商会话参数、建立会话的过程中,需要使用非对称密钥算法来加密密钥、验证通信对端的身份,计算量较大,占用了大量的系统资源。为了简化SSL握手过程,SSL允许重用已经协商过的会话,具体过程为:

(1)        SSL客户端发送Client Hello消息,消息中的会话ID设置为计划重用的会话的ID。

(2)        SSL服务器如果允许重用该会话,则通过在Server Hello消息中设置相同的会话ID来应答。这样,SSL客户端和SSL服务器就可以利用原有会话的密钥和加密套件,不必重新协商。

(3)        SSL客户端发送Change Cipher Spec消息,通知SSL服务器后续报文将采用原有会话的密钥和加密套件进行加密和MAC计算。

(4)        SSL客户端计算已交互的握手消息的Hash值,利用原有会话的密钥和加密套件处理Hash值,并通过Finished消息发送给SSL服务器,以便SSL服务器判断密钥和加密套件是否正确。

(5)        同样地,SSL服务器发送Change Cipher Spec消息,通知SSL客户端后续报文将采用原有会话的密钥和加密套件进行加密和MAC计算。

(6)        SSL服务器计算已交互的握手消息的Hash值,利用原有会话的密钥和加密套件处理Hash值,并通过Finished消息发送给SSL客户端,以便SSL客户端判断密钥和加密套件是否正确。

4  典型组网应用

4.1  HTTPS

HTTPS是基于SSL安全连接的HTTP协议。HTTPS通过SSL提供的数据加密、身份验证和消息完整性验证等安全机制,为Web访问提供了安全性保证,广泛应用于网上银行、电子商务等领域。

图8为HTTPS在网上银行中的应用。某银行为了方便客户,提供了网上银行业务,客户可以通过访问银行的Web服务器进行帐户查询、转帐等。通过在客户和银行的Web服务器之间建立SSL连接,可以保证客户的信息不被非法窃取。

图8 HTTPS在网上银行中的应用

4.2  SSL VPN

SSL VPN是以SSL为基础的VPN技术,利用SSL提供的安全机制,为用户远程访问公司内部网络提供了安全保证。如图9所示,SSL VPN通过在远程接入用户和SSL VPN网关之间建立SSL安全连接,允许用户通过各种Web浏览器,各种网络接入方式,在任何地方远程访问企业网络资源,并能够保证企业网络的安全,保护 企业内部信息不被窃取。

图9 SSL VPN的典型组网环境 

 

 

SSL工作原理

SSL 是一个安全协议,它提供使用 TCP/IP 的通信应用程序间的隐私与完整性。因特网的 超文本传输协议 (HTTP)使用 SSL 来实现安全的通信。

在客户端与服务器间传输的数据是通过使用对称算法(如 DES 或 RC4)进行加密的。公用密钥算法(通常为 RSA)是用来获得加密密钥交换和数字签名的,此算法使用服务器的SSL数字证书中的公用密钥。有了服务器的SSL数字证书,客户端也可以验证服务器的身 份。SSL 协议的版本 1 和 2 只提供服务器认证。版本 3 添加了客户端认证,此认证同时需要客户端和服务器的数字证书。

SSL 握手/

SSL 连接总是由客户端启动的。在SSL 会话开始时执行 SSL 握手。此握手产生会话的密码参数。关于如何处理 SSL 握手的简单概述,如下图所示。此示例假设已在 Web 浏览器 和 Web 服务器间建立了 SSL 连接。

(1) 客户端发送列出客户端密码能力的客户端“您好”消息(以客户端首选项顺序排序),如 SSL 的版本、客户端支持的密码对和客户端支持的数据压缩方法。消息也包含 28 字节的随机数。

(2) 服务器以服务器“您好”消息响应,此消息包含密码方法(密码对)和由服务器选择的数据压缩方法,以及会话标识和另一个随机数。

注意:客户端和服务器至少必须支持一个公共密码对,否则握手失败。服务器一般选择最大的公共密码对。

(3) 服务器发送其SSL数字证书。(服务器使用带有 SSL 的 X.509 V3 数字证书。)

如果服务器使用 SSL V3,而服务器应用程序(如 Web 服务器)需要数字证书进行客户端认证,则客户端会发出“数字证书请求”消息。在 “数字证书请求”消息中,服务器发出支持的客户端数字证书类型的列表和可接受的CA的名称。

(4) 服务器发出服务器“您好完成”消息并等待客户端响应。

(5) 一接到服务器“您好完成”消息,客户端( Web 浏览器)将验证服务器的SSL数字证书的有效性并检查服务器的“你好”消息参数是否可以接受。

如果服务器请求客户端数字证书,客户端将发送其数字证书;或者,如果没有合适的数字证书是可用的,客户端将发送“没有数字证书”警告。此警告仅仅是警告而已,但如果客户端数字证书认证是强制性的话,服务器应用程序将会使会话失败。

(6) 客户端发送“客户端密钥交换”消息。

此消息包含 pre-master secret (一个用在对称加密密钥生成中的 46 字节的随机数字),和 消息认证代码 ( MAC )密钥(用服务器的公用密钥加密的)。

如果客户端发送客户端数字证书给服务器,客户端将发出签有客户端的专用密钥的“数字证书验证”消息。通过验证此消息的签名,服务器可以显示验证客户端数字证书的所有权。

注意: 如果服务器没有属于数字证书的专用密钥,它将无法解密 pre-master 密码,也无法创建对称加密算法的正确密钥,且握手将失败。

(7) 客户端使用一系列加密运算将 pre-master secret 转化为 master secret ,其中将派生出所有用于加密和消息认证的密钥。然后,客户端发出“更改密码规范” 消息将服务器转换为新协商的密码对。客户端发出的下一个消息(“未完成”的消息)为用此密码方法和密钥加密的第一条消息。

(8) 服务器以自己的“更改密码规范”和“已完成”消息响应。

(9) SSL 握手结束,且可以发送加密的应用程序数据。

 

 

http://security.stackexchange.com/questions/7421/how-do-the-processes-for-digital-certificates-signatures-and-ssl-work

 http://search.aol.com/aol/search?s_it=topsearchbox.search&s_chn=prt_ct12&v_t=comsearch&q=ssl+certificate+signature

Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

I have been trying to understand how ssl works. Instead of Alice and Bob, lets consider client and server communication. Server has a digital certificate acquired from a CA. It also has public and private keys. Server wants to send a message to Client. Server's public key is already available to client.

Assuming that ssl handshake is completed.

Server to Client:

  • Server attaches its public key to the message.
  • Runs hash function on (message+public key). Results is known as HMAC.
  • Encrypt HMAC using it's private key. Result is called digital signature.
  • Send it to Client along with the digital certificate.
  • Client checks the certificate and finds that it's from the expected Server.
  • Decrypts HMAC using Server's public key.
  • Runs the hash funciton on (message+public key) to obtain the original message.

Client to Server

  • Client runs hash funciton on (message+public key) and then encrypts using the same public key.
  • Server decrypts using private key, runs the hash funciton on the resultant data to obtain the message.

Please let me know if my understanding is correct.

shareimprove this question

migrated from stackoverflow.com Sep 22 '11 at 14:20

This question came from our site for professional and enthusiast programmers.

 
    
Please re-post the above steps with corrections or additions. Then, It will be easy to understand. –  John Sep 22 '11 at 16:07
add comment

4 Answers

up vote 10 down vote accepted

There are a few confusions in your post. First of all, HMAC is not a hash function. More about HMAC later on.

Hash Functions

A hash function is a completely public algorithm (no key in that) which mashes bit together in a way which is truly infeasible to untangle: anybody can run the hash function on any data, but finding the data back from the hash output appears to be much beyond our wit. The hash output has a fixed size, typically 256 bits (with SHA-256) or 512 bits (with SHA-512). The SHA-* function which outputs 160 bits is called SHA-1, not SHA-160, because cryptographers left to their own devices can remain reasonable for only that long, and certainly not beyond the fifth pint.

Signature Algorithms

A signature algorithm uses a pair of keys, which are mathematically linked together, the private key and the public key (recomputing the private key from the public key is theoretically feasible but too hard to do in practice, even with Really Big Computers, which is why the public key and be made public while the private key remains private). Using the mathematical structure of the keys, the signature algorithm allows:

  • to generate a signature on some input data, using the private key (the signature is a mathematical object which is reasonably compact, e.g. a few hundred bytes for a typical RSA signature);
  • to verify a signature on some input data, using the public key. Verification takes as parameters the signature, the input data, and the public key, and returns either "perfect, man !" or "dude, these don't match".

For a secure signature algorithm, it is supposedly unfeasible to produce a signature value and input data such that the verification algorithm with a given public key says "good", unless you know the corresponding private key, in which case it is easy and efficient. Note the fine print: without the private key, you cannot conjure some data and a signature value which work with the public key, even if you can choose the data and the signature as you wish.

"Supposedly unfeasible" means that all the smart cryptographers in the world worked on it for several years and yet did not find a way to do it, even after the fifth pint.

Most (actually, all) signature algorithms begin by processing the input data with a hash function, and then work on the hash value alone. This is because the signature algorithm needs mathematical objects in some given sets which are limited in size, so they need to work on values which are "not too big", such as the output of a hash function. Due to the nature of the hash function, things work out just well (signing the hash output is as good as signing the hash input).

Key Exchange and Asymmetric Encryption

A key exchange protocol is a protocol in which both parties throw mathematical objects at each other, each object being possibly linked with some secret values that they keep for them, in a way much similar to public/private key pairs. At the end of the key exchange, both parties can compute a common "value" (yet another mathematical object) which totally evades the grasp of whoever observed the bits which were exchanged on the wire. One common type of key exchange algorithm is asymmetric encryption. Asymmetric encryption uses a public/private key pair (not necessarily the same kind than for a signature algorithm):

  • With the public key you can encrypt a piece of data. That data is usually constrained in size (e.g. no more than 117 bytes for RSA with a 1024-bit public key). Encryption result is, guess what, a mathematical object which can be encoded into a sequence of bytes.
  • With the private key, you can decrypt, i.e. do the reverse operation and recover the initial input data. It is assumed that without the private key, tough luck.

Then the key exchange protocol runs thus: one party chooses a random value (a sequence of random bytes), encrypts that with the peer's public key, and sends him that. The peer uses his private key to decrypt, and recovers the random value, which is the shared secret.

An historical explanation of signatures is: "encryption with the private key, decryption with the public key". Forget that explanation. It is wrong. It may be true only for a specific algorithm (RSA), and, then again, only for a bastardized-down version of RSA which actually fails to have any decent security. So no, digital signatures are not asymmetric encryption "in reverse".

Symmetric Cryptography

Once two parties have a shared secret value, they can use symmetric cryptography to exchange further data in a confidential way. It is called symmetric because both parties have the same key, i.e. the same knowledge, i.e. the same power. No more private/public dichotomy. Two primitives are used:

  • Symmetric encryption: how to mangle data and unmangle it later on.
  • Message Authentication Codes: a "keyed checksum": only people knowing the secret key can compute the MAC on some data (it is like a signature algorithm in which the private and the public key are identical -- so the "public" key had better be not public !).

HMAC is a kind of MAC which is built over hash functions in a smart way, because there are many non-smart ways to do it, and which fail due to subtle details on what a hash function provides and does NOT provide.

Certificates

A certificate is a container for a public key. With the tools explained above, one can begin to envision that the server will have a public key, which the client will use to make a key exchange with the server. But how does the client make sure that he is using the right server's public key, and not that of a devious attacker, a villain who cunningly impersonates the server ? That's where certificates come into play. A certificate is signed by someone who is specialized in verifying physical identities; it is called a Certificate Authority. The CA meets the server "in real life" (e.g. in a bar), verifies the server identity, gets the server public key from the server himself, and signs the whole lot (server identity and public key). This results in a nifty bundle which is called a certificate. The certificate represents the guarantee by the CA that the name and public key match each other (hopefully, the CA is not too gullible, so the guarantee is reliable -- preferably, the CA does not sign certificates after its fifth pint).

The client, upon seeing the certificate, can verify the signature on the certificate relatively to the CA public key, and thus gain confidence in that the server public key really belongs to the intended server.

But, you would tell me, what have we gained ? We must still know a public key, namely the CA public key. How do we verify that one ? Well, we can use another CA. This just moves the issue around, but it can end up with the problem of knowing a priori a unique or a handful of public keys from über-CAs which are not signed by anybody else. Thoughtfully, Microsoft embedded about a hundred of such "root public keys" (also called "trust anchors") deep within Internet Explorer itself. This is where trust originates (precisely, you forfeited the basis of your trust to the Redmond firm -- now you understand how Bill Gates became the richest guy in the world ?).

SSL

Now let's put it all together, in the SSL protocol, which is now known as TLS ("SSL" was the protocol name when it was a property of Netscape Corporation).

The client wishes to talk to the server. It sends a message ("ClientHello") which contains a bunch of administrative data, such as the list of encryption algorithms that the client supports.

The server responds ("ServerHello") by telling which algorithms will be used; then the server sends his certificate ("Certificate"), possibly with a few CA certificates in case the client may need them (not root certificates, but intermediate, underling-CA certificates).

The client verifies the server certificate and extracts the server public key from it. The client generates a random value ("pre-master secret"), encrypts it with the server public key, and sends that to the server ("ClientKeyExchange").

The server decrypts the message, obtains the pre-master, and derive from it secret keys for symmetric encryption and MAC. The client performs the same computation.

Client sends a verification message ("Finished") which is encrypted and MACed with the derived keys. The server verifies that the Finished message is proper, and sends its own "Finished" message in response. At that point, both client and server have all the symmetric keys they need, and know that the "handshake" has succeeded. Application data (e.g. an HTTP request) is then exchanged, using the symmetric encryption and MAC.

There is no public key or certificate involved in the process beyond the handshake. Just symmetric encryption (e.g. 3DES, AES or RC4) and MAC (normally HMAC with SHA-1 or SHA-256).

shareimprove this answer
 
    
thanks. Could you check my answer to the question? I guess it is correct. –  John Nov 3 '11 at 14:30
    
@John: well, your description of asymmetric encryption and signatures is a bit confusing, since use the infamous "encrypt with private key" metaphor. Also, you seem to encrypt a signature, which is kind of weird. For the SSL part, you got it mostly right, except for the "CertificateVerify" message, which occurs after the "ClientKeyExchange", and only if the client sent a certificate (as a "Certificate" message before the ClientKeyExchange). See section 7.3 of RFC 5246, there is an explanatory diagram. –  Tom Leek Nov 3 '11 at 16:24
    
Thanks a ton... Tom. Could u edit my description of asymmetric encryption and signatures and make corrections. It would be very helpful. –  John Nov 3 '11 at 16:40
    
add comment

After much struggle. I have the below understand of the differences between SSL, Asymmetric key encryption, Digital Certificate (DC) and Digital Signature (DS).

What is Digital Certificate, also known as public key certificate?

DC is an electronic document which uses a digital signature to bind a public key with an identity information such as name, address, etc

Contents of certificate: Public key certificate

Most important being, Signature-Algorithm, Issuer and the Public Key.

What is a asymmetric key encryption and digital signature?

Explained by means of an example.

Both machines have a pair of cryptographic keys — a public encryption key and a private decryption key.

Machine-A has access to Machine-B's public key and certificate.
Machine-B has access to Machine-A's public key and certificate.

Machine-A to Machine-B

At Machine-A:

  • Hash_function(Data) = Hash
  • Encrypt(Hash) using Machine-A's private key = DS
  • Attach Data to DS and DC = Data+DS+DC
  • Encrypt(Data+DS+DC) using Machine-B's public key.
  • Send it to Machine-B.

At Machine-B:

  • Decrypt (Data+DS+DC) using Machine-B's private key.
  • Verify DC to authenticate the Machine-A.
  • Decrypt(DS) using Machine-A's public key = Hash#1
  • Hash_function(Data) = Hash#2
  • if(Hash#1 == Hash#2) Data and signature are valid.

Machine-B to Machine-A

The process is now exactly the reverse of the above.

What is SSL/TLS?

The TLS protocol allows client/server applications to communicate across a network in a way designed to prevent eavesdropping and tampering. In most of the Client-Server communication, only the Server needs to be authenticated. TLS streamlines Asymmetric key encryption to make effective use of this phenomenon. Secure Sockets Layer

Client and Server example.

Server has a digital certificate acquired from a CA. It also has public and private keys.

User clicks on a URL starting with

https://

A secure connection is needed for this session. The browser establishes a TCP connection on the HTTPS TCP Port 443.

  1. Client > Server : SYN
  2. Client < Server : SYN+ACK
  3. Client > Server : ACK

    SSL Handshake on new TCP connection:

  4. Client > Server : CLIENT_HELLO

    The client sends a CLIENT_HELLO command to the server, which includes:

    • The highest SSL and TLS version supported by the client.
    • Ciphers supported bythe client. The ciphers are listed in order of preference.
    • Data compression methods that are supported by the client.
    • The session ID. If the client is starting a new SSL session, the session ID is 0.
    • Random data that is generated by the client for use in the key generation process.
  5. Client < Server : SERVER_HELLO

    The server sends a SERVER_HELLO command to the client, which includes:

    • The SSL or TLS version that will be used for the SSL session.
    • The cipher that will be used for the SSL session.
    • Data compression method that will be used for the SSL session.
    • The session ID for the SSL session.
    • Random data that is generated by the server for use in the key generation process.
  6. Client < Server : CERTIFICATE (PUBLIC KEY)

    The server sends the CERTIFICATE command. It includes the servers certificate.

  7. Client < Server : SERVER_DONE

    The server sends the SERVER_DONE command. This command indicates that the server has completed this phase of the SSL handshake.

  8. Client > Server : CERTIFICATE_VERIFY

    Client informs the server that it has verified the server's certificate

  9. Client > Server :

    Using all data generated in the handshake thus far, the client (with the cooperation of the server, depending on the cipher being used) creates the pre-master secret for the session, encrypts it with the server's public key (obtained from the server's certificate), and then sends the encrypted pre-master secret to the server.

    The server uses its private key to decrypt the pre-master secret, and then performs a series of steps to generate the master secret.

    Client also performs the same series of steps on the pre-master secret to generate the same master secret.

    Note: In situations where client needs to be authenticated, the client also signs another piece of data that is unique to this handshake and known by both the client and server. In this case, the client sends both the signed data and the client's own certificate to the server along with the encrypted pre-master secret.

  10. Client <> Server :

    Both the client and the server use the master secret to generate the session keys, which are symmetric keys used to encrypt and decrypt information exchanged during the SSL session and to verify its integrity.

    Note: From now on it's symmetric key encryption.

  11. Client > Server :

    The client sends a message to the server informing it that future messages from the client will be encrypted with the session key.

  12. Client > Server : FINISHED

    Client then sends a separate (encrypted) message indicating that it's portion of the handshake is finished.

  13. Client < Server :

    The server sends a message to the client informing it that future messages from the server will be encrypted with the session key.

  14. Client < Server : FINISHED

    Server sends a separate (encrypted) message indicating that it's portion of the handshake is finished.

    The SSL handshake is now complete and the session begins. The client and the server use the session keys to encrypt and decrypt the data they send to each other and to validate its integrity.

shareimprove this answer
 
    
The above explanation is taken from various sources. It would be great if this is turned to a wiki. –  John Sep 23 '11 at 17:50
add comment

For a more detailed explanation "under the hood", I can also suggest the following article: The First Few Milliseconds of an HTTPS Connection by Jeff Moser. The article uses packet captures of an HTTPS communication session to illustrate how the protocol works. It's interesting to see the things we are talking about in action and it clears out many "dark" spots.

shareimprove this answer
 
    
Simply throwing out a link is generally considered bad form. Would you care to summarize the relevant portions of the article as well? –  Scott Pack Dec 27 '11 at 16:11
    
Apologies, I updated my answer above. –  Georgios Dec 28 '11 at 8:35
add comment

 

Not quite; the certificates only come into play during the initial SSL handshake or during an SSL renegotiation. After that, a symmetric cipher such as AES, (3)DES, or RC4 will be used. Public-key crypto is generally more expensive than symmetric crypto, so it is generally used to agree on a symmetric key at the beginning.

shareimprove this answer
posted @ 2014-06-23 17:03  alxe_yu  阅读(282)  评论(0)    收藏  举报