- 前言
- X509
- 1 X509证书格式
- SSLopenSSLTLS
- 1 简单介绍
- 2 openSSL常用命令
- 21 基础命令
- 22 文件加密解密
- 23 计算特征码
- 24 生成密码
- 25 生成伪随机数
- 26 生成秘钥
- 27 生成查看X509证书
- HTTPS
- 1 HTTP VS HTTPS
- 2 大致过程
- openSSL实现私有CA
- 1 准备工作
- 2 生成秘钥
- 3 生成自签署的证书
- 4 其他配置
- 5 为应用程序配置SSL
前言
上一篇文章 http://blog.csdn.net/hylexus/article/details/53048305、http://www.jianshu.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信息
[[email protected] ~]# rpm -q openssl
openssl-.el6_8.x86_64
# 测试当前机器对常用加密算法的运算性能
[[email protected]_15_242_centos ~]# openssl speed
Doing md2 for s on size blocks: md2's in s
………………………………………………
# 测试当前机器对指定算法的运算性能
[[email protected] ~]# openssl speed md5
Doing md5 for s on size blocks: md5's in s
Doing md5 for s on size blocks: md5's in s
Doing md5 for s on size blocks: md5's in s
Doing md5 for s on size blocks: md5's in s
Doing md5 for s on size blocks: md5's in s
OpenSSL e-fips Feb
…………………………………………………………………………………………
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/Q+vg==
[root@h1 ~]# openssl rand -hex 22
eda2a48cbc437578b41d5ec1ddc3e42fdf5a7bc9be
2.2.6 生成秘钥
# 生成1024位的rsa秘钥保存至文件server.pri.1024中
[[email protected] ~]# 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 )
# 可以用以下命令提取查看公钥
openssl rsa -in server.pri -pubout
2.2.7 生成/查看X509证书
# 新生成一个x509格式的证书保存至文件server.crt中,有效期365天
[[email protected] ~]# 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 ( 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 []:[email protected]
[[email protected] ~]#
查看证书信息
[[email protected] ~]# openssl x509 -text -in server.crt
Certificate:
Data:
Version: ()
Serial Number: ()
Signature Algorithm: sha1WithRSAEncryption
Issuer: C=CN, ST=ShangHai, L=ShangHai, O=KKBC, OU=develop, CN=h1.hylexus.tech/[email protected]
Validity
Not Before: Nov :: GMT
Not After : Nov :: GMT
Subject: C=CN, ST=ShangHai, L=ShangHai, O=KKBC, OU=develop, CN=h1.hylexus.tech/[email protected]
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: ( bit)
Modulus:
:c2:e:f4:e9:::a:a::c::f::cb:
b2:df:d3:f8::e:d:c1:ff::b7:ed:c3:a7:f:
b:ef:b6::da:df:d:a:b:b0::cb:e:a3::
d3:da:::a5:::ac:ec:cd:e8:c7:cc:aa:b9:
:d1:fe:f::e3:f7::fb:cd::a:ae:::
c0:a0::b9:e4:bd:e2::::b3:ef:e4:eb::
fc:a:ce:f:a8:d7:e:bd:ec:::b1:bd::ee:
dc:::b::a4:b9:fe:f:be:f3:de:c4::bc:
d1::d9:b:e5:a6::c:
Exponent: ()
X509v3 extensions:
X509v3 Subject Key Identifier:
::FC:F4:B::B1:CA:C3::B::E:B:BE::::CA:D
X509v3 Authority Key Identifier:
keyid:::FC:F4:B::B1:CA:C3::B::E:B:BE::::CA:D
X509v3 Basic Constraints:
CA:TRUE
Signature Algorithm: sha1WithRSAEncryption
b::c:a1:be:ec:a::fc:e2::a9:d3::::d:db:
::bc:c7:a9:a::a:e:f::f::b:a7:a2:a::
ce:bc:f5::a7::b:e:d5:ad::a7::a:a2:c9::eb:
b8:f:::ba:dd:f8:b7:d:::e9:::e0::df:fa:
fa:ab:e4::a::::ce:ac:b:b0:c:::d:::
:f9:ee:b1::a2:b:ec:b6::b5:d::a1::b::f:
:e4:cf:f:ab:d7:::e5:c7::a:b:f6::f:f9:fb:
da:e
-----BEGIN CERTIFICATE-----
MIIC7DCCAlWgAwIBAgIJAOwWo9W1KBynMA0GCSqGSIb3DQEBBQUAMIGOMQswCQYD
VQQGEwJDTjERMA8GA1UECAwIU2hhbmdIYWkxETAPBgNVBAcMCFNoYW5nSGFpMQ0w
CwYDVQQKDARLS0JDMRAwDgYDVQQLDAdkZXZlbG9wMRgwFgYDVQQDDA9oMS5oeWxl
eHVzLnRlY2gxHjAcBgkqhkiG9w0BCQEWD2h5bGV4dXNAMTYzLmNvbTAeFw0xNjEx
MDYxNDA0NDJaFw0xNzExMDYxNDA0NDJaMIGOMQswCQYDVQQGEwJDTjERMA8GA1UE
CAwIU2hhbmdIYWkxETAPBgNVBAcMCFNoYW5nSGFpMQ0wCwYDVQQKDARLS0JDMRAw
DgYDVQQLDAdkZXZlbG9wMRgwFgYDVQQDDA9oMS5oeWxleHVzLnRlY2gxHjAcBgkq
hkiG9w0BCQEWD2h5bGV4dXNAMTYzLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAw
gYkCgYEAwo706SEGSoojfBVvcMuy39P4IS4Nwf8Wt+Dp49L77Z12t8NSiuwJst+
oxbT2hVnpSF0rOzN6MfMqrl40f4vEeP3cvvNCIquV1PAoGG55L3iJUMDs+/k6zb8
es5PqNc+vew2ObG9Fe7ckgB7caS5/n++EQ7zRUtkb5aZ0DAcCAwEAAaNQME4w
HQYDVR0OBBYEFFc0/PQbUrHKw3A7eR5rvklTB8o9MB8GA1UdIwQYMBaAFFc0/PQb
UrHKw3A7eR5rvklTB8o9MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEA
O5Ysob7simj84mmp04MkAh3bFBm8x6kqU1p+b3YfaJunoppizrz1Eqc5K37VrTan
dkqiyTjruB9gcbrd+LcthgHpN3Tgh9/+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-方式二](“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 77;openssl genrsa -out private/cakey.pem )
Generating RSA private key, bit long modulus
................................+++
...........................................................................................................+++
e is (x10001)
[root@h1 CA]#
4.3 生成自签署的证书
注意此处的证书是CA自己的证书。
[[email protected] 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 ( 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 []:[email protected]
[[email protected] 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, bit long modulus
.......................++++++
................++++++
e is ()
生成证书颁发请求
# csr====Certificate Signature Request
[[email protected] 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 ( 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 []:[email protected]
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: ()
Validity
Not Before: Nov :: GMT
Not After : Nov :: GMT
Subject:
countryName = CN
stateOrProvinceName = ShangHai
organizationName = KKBC
organizationalUnitName = dev
commonName = h2.hylexus.tech
emailAddress = [email protected]
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
:E2::E3:::B:E4::0B::D:E2::F4:FC:CB:C3::
X509v3 Authority Key Identifier:
keyid:D8:E5:FB:::D:A6:ED:FB:D1:D6::B5::FF:D:E8::E0:
Certificate is to be certified until Nov :: GMT ( days)
Sign the certificate? [y/n]:y
out of certificate requests certified, commit? [y/n]y
Write out database with new entries
Data Base Updated