赞
踩
一: SSL/TLS介绍 什么是SSL,什么是TLS呢?官话说SSL是安全套接层(secure sockets layer),TLS是SSL的继任者,叫传输层安全(transport layer security)。说白点,就是在明文的上层和TCP层之间加上一层加密,这样就保证上层信息传输的安全。如HTTP协议是明文传输,加上SSL层之后,就有了雅称HTTPS。它存在的唯一目的就是保证上层通讯安全的一套机制。它的发展依次经历了下面几个时期,像手机软件升级一样,每次更新都添加或去除功能,比如引进新的加密算法,修改握手方式等。 二: CA & SSL Server & SSL Client 介绍 如何保证安全呢?你说安全就安全吗,究竟是怎么实现的呢?绝对安全吗? 图一 SSL/TLS 工作流
CA 的证书 ca.crt 和 SSL Server的证书 server.crt 是什么关系呢? 何为SSL/TLS单向认证,双向认证? 图二 证书详细工作流
(1)client_hello 客户端发起请求,以明文传输请求信息,包含版本信息,加密套件候选列表,压缩算法候选列表,随机数,扩展字段等信息,相关信息如下:
(2).server_hello+server_certificate+sever_hello_done
(3).证书校验
(4).client_key_exchange+change_cipher_spec+encrypted_handshake_message
(5).change_cipher_spec+encrypted_handshake_message
(6).握手结束 客户端计算所有接收信息的 hash 值,并采用协商密钥解密 encrypted_handshake_message,验证服务器发送的数据和密钥,验证通过则握手完成; (7).加密通信 开始使用协商密钥与算法进行加密通信。
和单向认证几乎一样,只是在client认证完服务器证书后,client会将自己的证书client.crt传给服务器。服务器验证通过后,开始秘钥协商。 和单向认证一样: crt/key/req/csr/pem/der等拓展名都是什么东东? openssl x509 -in ca.crt -outform DER -out ca.der //pem -> der openssl x509 -inform der -in ca.der -out ca.pem // der -> pem pem格式:经过加密的文本文件,一般有下面几种开头结尾: -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- or: -----BEGIN CERTIFICATE REQUEST----- -----END CERTIFICATE REQUEST----- or: ----BEGIN CERTIFICATE----- -----END CERTIFICATE----- der格式: 经过加密的二进制文件。 2) 证书中含有 申请者公钥、申请者的组织信息和个人信息、签发机构 CA的信息、有效时间、证书序列号等信息的明文,同时包含一个签名。如查看百度证书详细信息。 b) 命令查看证书详细信息 openssl x509 -noout -text -in baiducom.crt 详细信息中,有一个字段: X509v3 Basic Constraints: CA: FALSE 3) 私钥加密,公钥解密! 2.8 SSL/TLS和 Openssl,mbedtls是什么关系? SSL/TLS是一种工作原理,openssl和mbedtls是SSL/TLS的具体实现,很类似于 TCP/IP协议和socket之间的关系。 我们自己本地使用 makefile.sh 脚本建立一个CA(ca.crt + ca.key),用这个CA给server和client分别颁发证书。 makefile.sh # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the axTLS project nor the names of its # contributors may be used to endorse or promote products derived # from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # # Generate the certificates and keys for testing. # PROJECT_NAME="TLS Project" # Generate the openssl configuration files. cat > ca_cert.conf << EOF [ req ] distinguished_name = req_distinguished_name prompt = no [ req_distinguished_name ] O = $PROJECT_NAME Dodgy Certificate Authority EOF cat > server_cert.conf << EOF [ req ] distinguished_name = req_distinguished_name prompt = no [ req_distinguished_name ] O = $PROJECT_NAME CN = 192.168.111.100 EOF cat > client_cert.conf << EOF [ req ] distinguished_name = req_distinguished_name prompt = no [ req_distinguished_name ] O = $PROJECT_NAME Device Certificate CN = 192.168.111.101 EOF mkdir ca mkdir server mkdir client mkdir certDER # private key generation openssl genrsa -out ca.key 1024 openssl genrsa -out server.key 1024 openssl genrsa -out client.key 1024 # cert requests openssl req -out ca.req -key ca.key -new -config ./ca_cert.conf openssl req -out server.req -key server.key -new -config ./server_cert.conf openssl req -out client.req -key client.key -new -config ./client_cert.conf # generate the actual certs. openssl x509 -req -in ca.req -out ca.crt -sha1 -days 5000 -signkey ca.key openssl x509 -req -in server.req -out server.crt -sha1 -CAcreateserial -days 5000 -CA ca.crt -CAkey ca.key openssl x509 -req -in client.req -out client.crt -sha1 -CAcreateserial -days 5000 -CA ca.crt -CAkey ca.key openssl x509 -in ca.crt -outform DER -out ca.der openssl x509 -in server.crt -outform DER -out server.der openssl x509 -in client.crt -outform DER -out client.der mv ca.crt ca.key ca/ mv server.crt server.key server/ mv client.crt client.key client/ mv ca.der server.der client.der certDER/ rm *.conf rm *.req rm *.srl 将上述代码保存为makefile.sh - 修改 CN 域中 IP 地址为你主机/设备的 IP 地址 将会看到: ca目录:保存ca的私钥ca.key和证书ca.crt $./makefile.sh 删除脚本rmfile.sh: rm ca/ -rf rm certDER/ -rf rm client/ -rf rm server/ -rf 将上述代码保存为rmfile.sh,终端执行,将会删除产生过的目录和文件: $./rmfile.sh 我们可在本地使用 CA证书来分别校验由自己颁发的服务器证书 server.crt 和客户端证书 client.crt . $openssl verify -CAfile ca/ca.crt server/server.crt $openssl verify -CAfile ca/ca.crt client/client.crt | |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。