[TOC]
前言
上一篇文章 http://blog.csdn.net/hylexus/article/details/53048305、//www.greatytc.com/p/c929ac2d9134 中,最终得到的安全通信的结论的前提都是基于CA及CA颁发的证书是可靠的基础上的,整个通信过程的安全性也都依赖于CA这个根源。本篇文章就来说说CA及与其相关的一些概念。
本文章中的诸多信息都是来自大牛 马哥 的linux视频教程。
1 X509
X509,简言之,也是个人理解:就是证书的元数据,也就是来约定证书格式的标准。
我们常见的证书的格式大都是基于X509的标准的。
1.1 X509证书格式
以下信息来源于百度百科:
所有的X.509证书包含以下数据:
- X.509版本号:指出该证书使用了哪种版本的X.509标准,版本号会影响证书中的一些特定信息。目前的版本是3。
- 证书持有人的公钥:包括证书持有人的公钥、算法(指明密钥属于哪种密码系统)的标识符和其他相关的密钥参数。
- 证书的序列号:由CA给予每一个证书分配的唯一的数字型编号,当证书被取消时,实际上是将此证书序列号放入由CA签发的CRL(Certificate Revocation List证书作废表,或证书黑名单表)中。这也是序列号唯一的原因。
- 主题信息:证书持有人唯一的标识符(或称DN-distinguished name)这个名字在 Internet上应该是唯一的。DN由许多部分组成,看起来象这样:
CN=Bob Allen, OU=Total Network Security Division
O=Network Associates, Inc.
C=US
这些信息指出该科目的通用名、组织单位、组织和国家或者证书持有人的姓名、服务处所等信息。 - 书的有效期:证书起始日期和时间以及终止日期和时间;指明证书在这两个时间内有效。
- 认证机构:证书发布者,是签发该证书的实体唯一的CA的X.509名字。使用该证书意味着信任签发证书的实体。(注意:在某些情况下,比如根或顶级CA证书,发布者自己签发证书)
- 发布者的数字签名:这是使用发布者私钥生成的签名,以确保这个证书在发放之后没有被撰改过。
- 签名算法标识符:用来指定CA签署证书时所使用的签名算法。算法标识符用来指定CA签发证书时所使用的公开密钥算法和HASH算法。
2 SSL/openSSL/TLS
2.1 简单介绍
先来看看这两张来自百度的OSI七层模型图和四层模型图:
我们常见的一些协议,比如 http、smtp、telnet、ftp本身默认是不支持数据传输加密的。
SSL(Secure Socket Layer)就是在应用层和TCP/IP层之间加的层,好像和这个快被历史遗忘了的牛逼的NetScape公司有关系。
有了SSL层,本来不支持加密传输的一些协议比如http就可以支持加密了即https,smtps,ftps等。
TLS(Transport Layer Security)安全传输层协议。TLS-v1相当于SSL-v3。
本文不加区别的使用SSL和TLS。
openSSL即是SSL的开源实现版本。
openSSL
- libcrypto:通用加密库
- libssl:SSL/TLS的实现
- openssl:命令行工具
2.2 openSSL常用命令
2.2.1 基础命令
# 查看当前机器上安装的openssl信息
[root@h1 ~]# rpm -q openssl
openssl-1.0.1e-48.el6_8.3.x86_64
# 测试当前机器对常用加密算法的运算性能
[root@VM_15_242_centos ~]# openssl speed
Doing md2 for 3s on 16 size blocks: 388053 md2's in 2.99s
………………………………………………
# 测试当前机器对指定算法的运算性能
[root@h1 ~]# openssl speed md5
Doing md5 for 3s on 16 size blocks: 9085406 md5's in 3.00s
Doing md5 for 3s on 64 size blocks: 6577474 md5's in 2.99s
Doing md5 for 3s on 256 size blocks: 3690426 md5's in 3.00s
Doing md5 for 3s on 1024 size blocks: 1305693 md5's in 2.99s
Doing md5 for 3s on 8192 size blocks: 186074 md5's in 3.00s
OpenSSL 1.0.1e-fips 11 Feb 2013
…………………………………………………………………………………………
2.2.2 文件加密/解密
# 加密文件
openssl enc -des3 -salt -a -in /etc/passwd -out /root/passwd.enc
-des3:des加密方式
-salt:加盐
-a:基于base64编码处理
-in:输入文件
-out:加密结果输出至何处
# 解密文件
openssl enc -des3 -d -salt -a -in /root/passwd.enc -out /root/passwd.plaintext
-des3:des加密方式
-d:解密
-salt:加盐
-a:基于base64编码处理
-in:输入文件
-out:加密结果输出至何处
2.2.3 计算特征码
[root@h1 ~]# openssl dgst -sha1 /etc/passwd
SHA1(/etc/passwd)= cda7fc123305e443155760afa8789b8e757d819a
[root@h1 ~]# openssl dgst -md5 /etc/passwd
MD5(/etc/passwd)= eaa520eb398cfedf2bdd7d785e5dcd78
# 和以下命令的计算结果一致
[root@h1 ~]# md5sum /etc/passwd
eaa520eb398cfedf2bdd7d785e5dcd78 /etc/passwd
[root@h1 ~]# sha1sum /etc/passwd
cda7fc123305e443155760afa8789b8e757d819a /etc/passwd
2.2.4 生成密码
# 和passwd命令类似
[root@h1 ~]# openssl passwd -1
Password:
Verifying - Password:
$1$THXDghVa$jF7Ds7zDQpaIDbUEFZZMF1
2.2.5 生成伪随机数
man sslrand 查看帮助
[root@h1 ~]# openssl rand -base64 22
UESrys2wxAQKBa2ofpcxC06/37Q+vg==
[root@h1 ~]# openssl rand -hex 22
87eda2a48cbc437578b41d5ec1ddc3e42fdf5a7bc9be
2.2.6 生成秘钥
# 生成1024位的rsa秘钥保存至文件server.pri.1024中
[root@h1 ~]# openssl genrsa 1024 > server.pri.1024
Generating RSA private key, 1024 bit long modulus
.................................................++++++
........++++++
e is 65537 (0x10001)
# 或者直接用以下命令在子shell中执行以便直接将mod设置为600
(umask 077;openssl genrsa -out server.pri.1024 1024)
# 可以用以下命令提取查看公钥
openssl rsa -in server.pri.1024 -pubout
2.2.7 生成/查看X509证书
# 新生成一个x509格式的证书保存至文件server.crt中,有效期365天
[root@h1 ~]# openssl req -new -x509 -key ./server.pri.1024 -out server.crt -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
# 国家代码:CN
Country Name (2 letter code) [XX]:CN
# 省份
State or Province Name (full name) []:ShangHai
# 城市
Locality Name (eg, city) [Default City]:ShangHai
# 组织机构名称
Organization Name (eg, company) [Default Company Ltd]:KKBC
# 部门
Organizational Unit Name (eg, section) []:develop
# 主机名
Common Name (eg, your name or your server's hostname) []:h1.hylexus.tech
# 电子邮件
Email Address []:hylexus@163.com
[root@h1 ~]#
查看证书信息
[root@h1 ~]# openssl x509 -text -in server.crt
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 17011964780701293735 (0xec16a3d5b5281ca7)
Signature Algorithm: sha1WithRSAEncryption
Issuer: C=CN, ST=ShangHai, L=ShangHai, O=KKBC, OU=develop, CN=h1.hylexus.tech/emailAddress=hylexus@163.com
Validity
Not Before: Nov 6 14:04:42 2016 GMT
Not After : Nov 6 14:04:42 2017 GMT
Subject: C=CN, ST=ShangHai, L=ShangHai, O=KKBC, OU=develop, CN=h1.hylexus.tech/emailAddress=hylexus@163.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (1024 bit)
Modulus:
00:c2:8e:f4:e9:21:06:4a:8a:23:7c:15:6f:70:cb:
b2:df:d3:f8:21:2e:0d:c1:ff:16:b7:ed:c3:a7:8f:
4b:ef:b6:75:da:df:0d:4a:2b:b0:26:cb:7e:a3:16:
d3:da:15:67:a5:21:74:ac:ec:cd:e8:c7:cc:aa:b9:
78:d1:fe:2f:11:e3:f7:72:fb:cd:08:8a:ae:57:53:
c0:a0:61:b9:e4:bd:e2:25:43:03:b3:ef:e4:eb:36:
fc:7a:ce:4f:a8:d7:3e:bd:ec:36:39:b1:bd:15:ee:
dc:92:00:7b:71:a4:b9:fe:7f:be:f3:de:c4:43:bc:
d1:52:d9:1b:e5:a6:74:0c:07
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Subject Key Identifier:
57:34:FC:F4:1B:52:B1:CA:C3:70:3B:79:1E:6B:BE:49:53:07:CA:3D
X509v3 Authority Key Identifier:
keyid:57:34:FC:F4:1B:52:B1:CA:C3:70:3B:79:1E:6B:BE:49:53:07:CA:3D
X509v3 Basic Constraints:
CA:TRUE
Signature Algorithm: sha1WithRSAEncryption
3b:96:2c:a1:be:ec:8a:68:fc:e2:69:a9:d3:83:24:02:1d:db:
14:19:bc:c7:a9:2a:53:5a:7e:6f:76:1f:68:9b:a7:a2:9a:62:
ce:bc:f5:12:a7:39:2b:7e:d5:ad:36:a7:76:4a:a2:c9:38:eb:
b8:1f:60:71:ba:dd:f8:b7:2d:86:01:e9:37:74:e0:87:df:fa:
fa:ab:e4:88:1a:58:85:08:ce:ac:2b:b0:0c:95:02:4d:66:42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3Tgh9/6+qvkiBpYhQjOrCuwDJUCTWZCAfnusYai
K+y2YrWdlKEZW5YPk+TPP6vXWYXlx0MKO/YgL/n72h4=
-----END CERTIFICATE-----
3 HTTPS
HTTPS(Hyper Text Transfer Protocol over Secure Socket Layer)即HTTP在SSL/TLS基础上的安全版本。
3.1 HTTP VS HTTPS
以下对比来自于百度百科:
- https协议需要到ca申请证书,一般免费证书很少,需要交费。
- http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议。
- http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。
- http的连接很简单,是无状态的;HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。
3.2 大致过程
- 三次握手当然是必不可少的了
既然是安全的,当然就得加密传输数据了。
怎么加密传输呢?
非对称加密代价太大,HTTPS使用的方式大致和上篇文章中所说的
第二种安全通信方式:http://blog.csdn.net/hylexus/article/details/53048305#72-方式二 类似。
客户端和服务端需要协商通信的对称加密的加密算法等信息。一般并不是基于IKE实现的。
4 openSSL实现私有CA
4.1 准备工作
先查看或按需修改/etc/pki/tls/openssl.cnf文件内容,其中有以下一些配置项:
####################################################################
[ CA_default ]
dir = /etc/pki/CA # Where everything is kept
certs = $dir/certs # Where the issued certs are kept
crl_dir = $dir/crl # Where the issued crl are kept
database = $dir/index.txt # database index file.
#unique_subject = no # Set to 'no' to allow creation of
# several ctificates with same subject.
new_certs_dir = $dir/newcerts # default place for new certs.
certificate = $dir/cacert.pem # The CA certificate
serial = $dir/serial # The current serial number
crlnumber = $dir/crlnumber # the current crl number
# must be commented out to leave a V1 CRL
crl = $dir/crl.pem # The current CRL
private_key = $dir/private/cakey.pem# The private key
RANDFILE = $dir/private/.rand # private random number file
为方便,此处直接cd至/etc/pki/CA目录进行后续操作
4.2 生成秘钥
[root@h1 CA]# pwd
/etc/pki/CA
# 注意此处的输出位置应该和/etc/pki/tls/openssl.cnf中的配置相对应
[root@h1 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
................................+++
...........................................................................................................+++
e is 65537 (0x10001)
[root@h1 CA]#
4.3 生成自签署的证书
注意此处的证书是CA自己的证书。
[root@h1 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [ShangHai]:
Locality Name (eg, city) [ShangHai]:
Organization Name (eg, company) [Default Company Ltd]:KKBC
Organizational Unit Name (eg, section) [dev]:
Common Name (eg, your name or your server's hostname) []:h1.hylexus.tech
Email Address []:hylexus@163.com
[root@h1 CA]#
4.4 其他配置
# 此时的目录大概是这个样子,具体应该和/etc/pki/tls/openssl.cnf中的配置相对应
[root@h1 CA]# tree
.
├── cacert.pem
├── certs
├── crl
├── newcerts
└── private
└── cakey.pem
# 新建 database index file.
[root@h1 CA]# touch index.txt
# The current serial number
[root@h1 CA]# echo 01 > serial
# 最终的目录结构大概是这个样子,具体应该和/etc/pki/tls/openssl.cnf中的配置相对应
[root@h1 CA]# tree
.
├── cacert.pem
├── certs
├── crl
├── index.txt
├── newcerts
├── private
│ └── cakey.pem
└── serial
4.5 为应用程序配置SSL
此处本人在/etc/nginx/ssl目录下操作,只是示例而已:
生成私钥(应用程序自己的私钥,不要和上面的CA的私钥混了)
[root@h1 ssl]# cd /etc/nginx/
[root@h1 nginx]# mkdir ssl ; cd ssl
[root@h1 ssl]# pwd
/etc/nginx/ssl
# 生成私钥
[root@h1 ssl]# (umask 077;openssl genrsa -out nginx.key)
Generating RSA private key, 1024 bit long modulus
.......................++++++
................++++++
e is 65537 (0x10001)
生成证书颁发请求
# csr====Certificate Signature Request
[root@h1 ssl]# openssl req -new -key nginx.key -out nginx.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [CN]:
State or Province Name (full name) [ShangHai]:
Locality Name (eg, city) [ShangHai]:
Organization Name (eg, company) [Default Company Ltd]:KKBC
Organizational Unit Name (eg, section) [dev]:
Common Name (eg, your name or your server's hostname) []:h2.hylexus.tech
Email Address []:hylexus@163.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
将证书颁发请求发送给CA,让CA签署(CA签名认证)
此处CA和应用都在同一台主机上,直接操作即可
[root@h1 ssl]# openssl ca -in nginx.csr -out nginx.crt -days 3650
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Nov 6 23:22:02 2016 GMT
Not After : Nov 4 23:22:02 2026 GMT
Subject:
countryName = CN
stateOrProvinceName = ShangHai
organizationName = KKBC
organizationalUnitName = dev
commonName = h2.hylexus.tech
emailAddress = hylexus@163.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
06:E2:32:E3:46:07:3B:E4:39:0B:44:8D:E2:60:F4:FC:CB:C3:17:81
X509v3 Authority Key Identifier:
keyid:D8:E5:FB:17:23:6D:A6:ED:FB:D1:D6:82:B5:97:FF:2D:E8:05:E0:67
Certificate is to be certified until Nov 4 23:22:02 2026 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated