一、PKI PKI(Public KeyInfrastructure)公钥基础设施,是提供公钥加密和数字签名服务的系统或平台,目的是为了管理密钥和证书。在PKI中,为了确保用户的身份及他所持有密钥的正确匹配,公开密钥系统需要一个值得信赖而且独立的第三方机构充当认证中心(CertificationAuthority,CA),来确认公钥拥有人的真正身份。就像公安局发放的***一样,认证中心发放一个叫"数字证书"的***明,这个数字证书包含了用户身份的部分信息及用户所持有的公钥,CA利用自己的私钥来为数字证书加上数字签名,操作系统内置世界各权威发证机构的证书,因此用户根据系统里内置的CA官方证书可以检测对方的数字证书的真伪,从而确定发送方是否可以信任。
公钥有两大类用途,一个是用于验证数字签名,一个是用于加密信息。PKI在公开密钥密码的基础上,主要解决密钥属于谁,即密钥认证的问题。通过数字证书,PKI很好地证明了公钥是谁的。PKI的核心技术就围绕着数字证书的申请、颁发、使用与撤销
等整个生命周期进行展开。
PKI组成部分: 签证机构:CA 注册机构:RA 证书吊销列表:CRL 证书存取库
数字证书: 证书格式:x509,pkcs家族 x509格式: 公钥和有效期限: 持有者的个人合法身份信息(通常为主机名) 证书的使用方式 CA的信息 CA的数字签名 自签证书:自己给自己签署
二、密码算法和协议 加密算法安全性不在于算法是否公开,而在于密钥
1、对称加密 概念:将原始数据分割成固定大小的块,逐个进行加密,加密和解密使用同一个密钥,加密速度 快,一般用于加密所要发送的数据,提供数据的保密性。 算法:DES,3DES: AES:Advanced(128bits,192bits, 258, 384, 512bits) Blowfish Twofish/EDEA/RC6/CAST5
2、非对称加密 概念:非对称加密通常为公钥加密,公钥加密密钥是成对儿出现,公钥是公开给所有人的,而私钥是自己保存的,并且要保证其私密性。用公钥加密的数据只能使用与之配对的私钥解密,由于加密速度慢,一般不用于加密较大的数据,常用于数字签名以及密钥交换等过程的加密。 算法:RSA,DSA,ELGamal
数字签名:身份认证,主要在于让接收方确认发送方身份,私钥加密,公钥解密 算法:RSA,ELGamal,DSA(也称DSS,只可以数字签名,不可以公钥加密)
密钥交换:发送方用对方的公钥加密一个对称密钥,并发送给对方 算法:公钥加密和DH
数据传输的加密过程: 数据在传输前先在本地用单向加密算法计算特征码,然后对特征码进行数字签名,即用自 己的私钥加密特征码, 并将加密后的特征码附在数据上。接着用对称加密方法将整一个数据(包 括加密后的特征码)进行加密,而对称加密的密钥用对方的公钥加密后附在数据上发送给对方; 对方收到数据后,先用自己的私钥解密得到对称密钥,再用对称密钥解密数据,得到数据和加密 的特征码,然后进行数字签名认证,即使用对方的公钥解密得到特征码,并与用相同单向加密的 算法对数据部分进行计算得到的特征码进行比较,确定数据的来源和完整性。
3、单向加密 概念:只能加密,不能解密,提取数据指纹(特征码),提供完整性校验,一般用于计算数据的特 征码,提供数据的完整性校验 特性:定长输出:相同的数据加密后产生固定长度的输出 雪崩效应:数据的微小改变导致加密结果的巨大改变 算法: md5:128bits sha1:160bits sha224/sha256/sha384/sha512
4、MAC:消息验证码 概念:MessageAuthentication Code,单向加密的一种延伸应用,用于实现在网络通信中保证 所传输的数据的完整性机制。在发送数据之前,发送方首先使用通信双方协商好的散列函数计算其摘要值。在双方共享的会话密钥作用下,由摘要值获得消息验证码。之后,它和数据一起被发送。接收方收到报文后,首先利用会话密钥还原摘要值,同时利用散列函数在本地计算所收到数据的摘要值,并将这两个数据进行比对。若两者相等,则报文通过认证。
算法:CBC-MAC HMAC:使用md5和sha1算法
(图片来自网络)
5、密钥交换: 公钥加密:发送方用对方的公钥加密对称密钥,接收方用自己的私钥解密,反过来一样。 DH(Deffie-Hellman):前提发送方和接受方协商使用同一个大素数P和生成数g,各自产生的随机数X和Y。发送方将g的X次方modP产生的数值发送给接收方,接受方将g的Y次方mod P产生的数值发送给发送方,发送方再对接收的结果做X次方运算,接受方对接收的结果做Y次方运算,最终密码形成,密钥交换完成。
三、OpenSSL SSL(Secure Socket Layer),安全套接层,目的是为跨网络的两个应用间通信提供加密安全可靠的传输信道。SSL协议建立在TCP传输层,应用层各种协议(HTTP,SSH等)可以透明地建立在SSL协议之上,从而能够安全地在互联网中通信,OpenSSL是SSL的一种实现。 OpenSSL是一个强大的安全套接字层密码库,包括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,提供BIO高层IP接口,封装了大多数类型的IO接口,如内存访问,文件访问,以及Socket等,还提供了随机数生成和管理,口令生成密钥,证书签发和管理等功能。
OpenSSL三个主要的功能部分: 1)libssl,SSL/TLS协议库 2)openssl,应用程序及命令行工具 3)libcryto,密码算法库。
OpenSSL命令及用法: 1、对称加密 命令:openssl enc-ciphername [-in filename] [-out filename] [-e] [-d] [-a/-base64] [-k password][-kfile filename][-S salt] [-salt] [-nosalt] 选项:-ciphername:所支持的加密算法选项,主要有3des,aes,blowfish,rc4等 enc: symmetriccipher routines,对称加密 -e :加密 -d:解密 -a:编码类型为base64 -k:指定加密密码,安全起见,一般不建议在bash命令行上输入 例子: 1
| [iyunv@localhost ~]# openssl enc -e-des3 -a -salt -in fstab -out fstab.ciphertext # 加密
|
1
| [iyunv@localhost ~]# openssl enc -d -des3 -a -salt -in fstab.ciphertext -out fstab # 解密
|
2、单向加密 命令:openssl dgst[-md5|-md4|-md2|-sha1|-sha|-dss1] [-out filename] [file...] 1
| 例子:[iyunv@localhost ~]# openssl dgst -md5 -out fstab.dgst fstab.cfg
|
3、公钥加密 需要手动进行公钥加密场景很少,但平常会有生成密钥对的需求。 算法:RSA,ELGamal 工具:gpg ,openssl rsautl 生成密钥对: opensslgenrsa -out /path/to/file NUM_BITS 1
| [iyunv@localhost ~]# (umask 077;openssl genrsa -out keypri 2048) #括号内语句在子shell运行
|
1
| [iyunv@localhost ~]# openssl rsa -in /path/from/privatekey.file -pubout
|
4、生成用户密码 命令:openssl passwd [options] [passwords] options: -1 MD5-based password algorithm -apr1 MD5-based password algorithm,Apache variant -saltstring use provided salt -stdin read passwords from stdin -infile read passwords from file
5、生成随机数: 命令:openssl rand -base64|-hex NUM 选项:NUM表示字节数(-hex时,出现字符数为NUM*2) 例子:[iyunv@localhost ~]# openssl rand -hex10|cut -b 3-10
6、创建私有CA 由于CA证书价格昂贵,不适合一般的公司。如果想在公司内部创建私有CA,可以使用OpenCA或者 OpenSSL的CA功能创建。 证书申请及签署步骤: 1、生成申请请求; 2、RA核验; 3、CA签署; 4、获取证书;
1)配置文件:/etc/pki/tls/openssl.cnf,若按默认安装,则不需要修改 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| [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
x509_extensions
= usr_cert # The extentions to add
to the cert
|
2)生成密钥对儿
1
2
3
4
5
6
7
8
9
10
11
12
| [iyunv@Centos6_6-2~]# cd /etc/pki/CA
[iyunv@Centos6_6-2 CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating
RSA private key, 2048 bit long modulus
............................+++
...+++
e is
65537 (0x10001)
[iyunv@Centos6_6-2
CA]# ll private/cakey.pem
-rw-------.
1 root root 1679 Sep 16 22:38 private/cakey.pem
|
3)生成自签证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| [iyunv@Centos6_6-2 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
(前面提示内容省略)
-----
Country
Name (2 letter code) [XX]:CN
State
or Province Name (full name) []:Beijing
Locality
Name (eg, city) [Default City]:Beijing
Organization
Name (eg, company) [Default Company Ltd]:student
Organizational
Unit Name (eg, section) []:OPS
Common
Name (eg, your name or your server's hostname) []:ca.student.com
Email
Address []:caadmin@student.com
[iyunv@Centos6_6-2
CA]# ll
total
20
-rw-r--r--.
1 root root 1428 Sep 16 22:43 cacert.pem
|
4)创建需要的文件
1
2
3
| [iyunv@Centos6_6-2 CA]# touch index.txt serial crlnumber # 分别创建数据库索引文件、当前
证书编号、当前吊销证书编号
[iyunv@Centos6_6-2 CA]# echo 01 > serial # 设置当前证书起始编号为01
|
5)客户端申请证书
在主机上生成密钥,保存至应用此证书的服务的配置文件目录下,例如:
1
2
3
4
5
6
7
8
9
| [iyunv@Centos6_6-1 ~]# mkdir /etc/httpd/ssl
[iyunv@Centos6_6-1 ~]# cd /etc/httpd/ssl
[iyunv@Centos6_6-1 ssl]# (umask 077;openssl genrsa -out httpd.key 1024)
Generating
RSA private key, 1024 bit long modulus
...........++++++
.......++++++
e is
65537 (0x10001)
|
6)生成证书签署请求,并将请求文件发往CA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| [iyunv@Centos6_6-1 ssl]# openssl req -new -key httpd.key -out httpd.csr
(前面提示省略)
-----
Country
Name (2 letter code) [XX]:CN
State
or Province Name (full name) []:Beijing
Locality
Name (eg, city) [Default City]:Beijing
Organization
Name (eg, company) [Default Company Ltd]:student
Organizational
Unit Name (eg, section) []:OPS
Common
Name (eg, your name or your server's hostname) []:www.web.com
Email
Address []:admin@web.com
Please
enter the following 'extra' attributes
to be
sent with your certificate request
A
challenge password []:
An
optional company name []:
|
7)CA签署证书,将证书传回请求者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| [iyunv@Centos6_6-2 CA]# openssl ca -in httpd.csr -out httpd.crt -days 365 # 签署
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: Sep 16 17:03:57 2015
GMT
Not After : Sep 15 17:03:57 2016
GMT
Subject:
countryName = CN
stateOrProvinceName = Beijing
organizationName = student
organizationalUnitName = OPS
commonName = www.web.com
emailAddress = admin@web.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
0E:B9:84:A8:6F:48:70:27:D4:53:6E:D0:8F:23:F4:02:73:EF:99:3A
X509v3 Authority Key Identifier:
keyid:2D:38:BC:4C:B8:17:12:0C:11:2F:39:0B:49:C7:68:90:0B:3B:84:E9
Certificate
is to be certified until Sep 15 17:03:57 2016 GMT (365 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
|
1
| [iyunv@Centos6_6-2 CA]# scp httpd.crt 192.168.52.10:/etc/httpd/ssl #将证书传回申请者
|
8)吊销证书
命令格式:opensslca -revoke /path/to/somefile.crt
|