PKI:Public Key Infrastructure 公共密钥加密体系
X.509:定义了证书的结构以及认证协议标准
证书类型:
获取证书两种方法:
SSL:Secure Socket Layer,TLS: Transport Layer Security
1995:SSL 2.0 Netscape 开发
1996:SSL 3.0
1999:TLS 1.0
2006:TLS 1.1 IETF(Internet工程任务组) RFC 4346
2008:TLS 1.2 当前主要使用
2015:TLS 1.3
功能:
机密性
认证
完整性
重放保护
实现分为握手阶段和应用阶段
目前密钥交换 + 签名有三种主流选择
HTTPS 协议:就是“HTTP 协议”和“SSL/TLS 协议”的组合。HTTP over SSL”或“HTTP over TLS”,对http协议的文本数据进行加密处理后,成为二进制形式传输
两种运行模式:
交互模式
批处理模式
三种子命令:
标准命令
消息摘要命令
加密命令
[root@centos8 ~]#openssl version
OpenSSL 1.1.1 FIPS 11 Sep 2018
[root@centos8 ~]#openssl
OpenSSL> help
Standard commands
asn1parse ca ciphers cms
crl crl2pkcs7 dgst dhparam
......
OpenSSL> ca --help
Usage: ca [options]
Valid options are:
-help Display this summary
-verbose Verbose output during processing
-config val A config file
......
OpenSSL>q
工具:openssl enc, gpg
算法:3des, aes, blowfish, twofish
enc命令:帮助:man enc #对称加密
[root@centos7|~]#cp /etc/fstab .
[root@centos7|~]#ls
2.sh fstab 公共 视频 文档 音乐
anaconda-ks.cfg initial-setup-ks.cfg 模板 图片 下载 桌面
#加密
[root@centos7|~]#openssl enc -e -des3 -a -salt -in fstab -out fstab.cipher
enter des-ede3-cbc encryption password:
Verifying - enter des-ede3-cbc encryption password:
[root@centos7|~]#ls
2.sh fstab initial-setup-ks.cfg 模板 图片 下载 桌面
anaconda-ks.cfg fstab.cipher 公共 视频 文档 音乐
[root@centos7|~]#cat fstab.cipher
U2FsdGVkX1+rUiNa6ZVGRdNyQzcIY+F72pwOq3iXqYFmEKpJiNPENQUyj7X2DvDy
nQT5MZhF3IKC4TkXp91OdhBIBHtiRn+M4qp2WWQcg1f/ThF5TeOOMDMaAab/3FCO
OAiQ1H4X5hnQP0nn+YUZyDmI+oW57Et9U31MJs5EE4tYoiTB3zlNBeeYUen2i4DM
iZxiCKBC+fAq0NXWWFlL9mc7u
#解密:
[root@centos7|~]#openssl enc -d -des3 -a -salt –in fstab.cipher -out fstab2
工具:openssl dgst 摘要,实现各种哈希算法
算法:md5sum, sha1sum, sha224sum,sha256sum…
dgst命令:帮助:man dgst
[root@centos7|~]#openssl dgst -md5 fstab
MD5(fstab)= 00724aa3987a2a0c7afb6f79dbaa2655
[root@centos7|~]#openssl dgst --sha512 fstab
passwd命令:帮助:man sslpasswd
[root@Centos8|13|~]#openssl passwd -6
Password:
Verifying - Password:
$6$bBWPPgoJtrl0ITPM$I5vQ9av3R0aLsJwf.3ACKCrQtB4mez5CirVfRob2BSdnOMxfWoUGleXUvMCrbg4VIlHzVDRdmLSfC7COHKjUn1
bBWPPgoJtrl0ITPM 盐
[root@Centos8|14|~]#openssl passwd -6 -salt "centos"
[root@Centos8|14|~]#getent shadow lin
lin:$6$a72OU1Cubg6Y6Xy7$pL4LfPkxmbASRs7boWmWwUrHEPnnZAmr9yP9D4rWaqEJVLc/U0JzC64gAtDkawaDF.YSN7E9TI/.KQIfqkkTN.'
[root@Centos8|16|~]#useradd -p '$6$a72OU1Cubg6Y6Xy7$pL4LfPkxmbASRs7boWmWwUrHEPnnZAmr9yP9D4rWaqEJVLc/U0JzC64gAtDkawaDF.YSN7E9TI/.KQIfqkkTN.' li
生成的li用户和lin密码相同
随机数生成器:伪随机数字,利用键盘和鼠标,块设备中断生成随机数
/dev/random:仅从熵池返回随机数;随机数用尽,阻塞
/dev/urandom:从熵池返回随机数;随机数用尽,会利用软件生成伪随机数,非阻塞
帮助:man sslrand
openssl rand -base64|-hex NUM
NUM: 表示字节数,使用-hex,每个字符为十六进制,相当于4位二进制,出现的字符数为NUM*2
[root@Centos8|19|~]#openssl rand -base64 3
WIIl
[root@Centos8|20|~]#openssl rand -base64 3
GbqR
[root@Centos8|21|~]#openssl rand -base64 3
x/6N
[root@Centos8|22|~]#openssl rand -base64 3
gxkA
方法一
[root@Centos8|51|~]#cat /dev/urandom | tr -dc '[[:alpha:]]' | head -c 12
qnoOIePgVBQo[root@Centos8|52|~]#cat /dev/urandom | tr -dc '[[:alpha:]]' | head -c 12
MRVDrTJMIZBi[root@Centos8|53|~]#cat /dev/urandom | tr -dc '[[:alpha:]]' | head -c 12
方法二
[root@Centos8|58|~]#openssl rand -base64 9
+eipeS6/TuH7
[root@Centos8|59|~]#openssl rand -base64 9
HHrFuxpe2xqN
[root@Centos8|60|~]#openssl rand -base64 9
uyfpvt93eiTH
公钥加密:
算法:RSA, ELGamal
工具:gpg, openssl rsautl(man rsautl)
数字签名:
算法:RSA, DSA, ELGamal
密钥交换:
算法:dh
DSA:Digital Signature Algorithm
DSS:Digital Signature Standard
RSA:
openssl命令生成密钥对儿:man genrsa
[root@centos7 ~]#(umask 066;openssl genrsa -out /data/app.key)
Generating RSA private key, 2048 bit long modulus
........................+++
.+++
e is 65537 (0x10001)
[root@Centos8|69|~]#openssl genrsa -out /data/app.key 1024
Generating RSA private key, 1024 bit long modulus (2 primes)
..........+++++
................................................+++++
e is 65537 (0x010001)
[root@VM_0_3_centos|24|data]#openssl rsa -in app.key -pubout -out app.key.pub
writing RSA key
[root@VM_0_3_centos|26|data]#cat app.key.pub
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9Tr8cbZn1nOVExxdD3PKUqFP9
uyYPddZ/5+G9/rn6D11yO8ePMNiwlq60svxwvh+oq8/J302NPX1BjI54HisnCidP
7m2DjGdFVWKeFaxO5VVEuGoqyYLRfwrIwZ4SeOflZc2aTlanxXaEoUXv7TK7yR7d
B6FTF+YtNVyZHTb6sQIDAQAB
-----END PUBLIC KEY-----
范例:
#生成对称秘钥加密的私钥
(umask 077; openssl genrsa –out test.key –des 2048)
#将加密对称秘钥key解密
openssl rsa -in test.key –out test2.key
范例:生成加密的私钥,并解密
[root@centos8 ~]#openssl genrsa -out /data/app.key -des3 1024
Generating RSA private key, 1024 bit long modulus (2 primes)
......+++++
...........+++++
e is 65537 (0x010001)
Enter pass phrase for /data/app.key:
Verifying - Enter pass phrase for /data/app.key:
[root@centos8 ~]#ls -l /data
total 4
-rw------- 1 root root 963 Feb 3 15:27 app.key
[root@centos8 ~]#cat /data/app.key
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,577C3B861BAD86B6
VM8P7vx1UUcSJyXCB0pDO9xgmdNgsMOcl6NitdUvBA9Jx2oLyxsT6TYbbvZvlF55
aQB0bq43atECDBz2+v1ghacPp78S2wuGuTR1hdWwfFKJNr6d/5yXO4y1ZOt3RLvR
E4K6TCeSwZTIUNeQyuh+vstarQmaLQmdObb3lsMG+WipQj3hb0oGdZcWjuQ0gi1B
RKN1duhsWFQbdXZamBqWQqCbvigmqRwjk7S6GE3YwVhys1T4N0BFX/edNCMnzb79 6/mR+LJ2Wz/ecJXB5250rVby3h88ZNsgARg7jUM9zI6jf7G4t1etRlCJ8A9TvDe8
J/5lkDUSWEh1dnB+xw5uamDY7f3GanuKTEe54DxuBwmbBpphV1QTTefSJ01Q6l9K
wS0zV6WE+vCt99dE9J8+GXGD77twRcbmjDWfaoibvwMu00crB9K5dbxdSX50jlD9
Mj+bVr9tcwQW/WzA+V05Ndb74e8OE97pEFjTX8DeIxcZomDUcpNGpQ0eWvyE+A2x
Srux9nN8z9dUF963V4NjQGUg1owQPAlfO6zBGObXnynOqKDmBj+8FfWrnHnZUVt5
3HTV+uSkLuA+8lGoNoxH4/6ZLfvY0Y5+WSg3st2EvwGT74SNNrsNYD0qGt1LujQx
IiwfCI0uv8rqgtLtsYmJmYI0t7hWUVmb6QgX1Qh0Kvzc0A34IMDjY6dhXTKnxeF3
LGkrFAgl3+6tKXxMuQDLB6Jy9m3SOwW/JoXMVVcYHrSPzTgAl2sgAkgEq8nf4yfm
ZP9WHrDe10yXY+5K2h8UiFhvrnQ+YnH4BcTrKuEa9T7pxToo0cTdqg==
-----END RSA PRIVATE KEY-----
[root@centos8 ~]#openssl rsa -in /data/app.key -out /data/app.key
Enter pass phrase for /data/app.key:
writing RSA key
[root@centos8 ~]#ls -l /data
total 4
-rw------- 1 root root 887 Feb 3 15:28 app.key
[root@centos8 ~]#cat /data/app.key
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCvkS+Z4NWAMoXEwNUyn58J0oI+ZjXotZUJLfbVHvGd3Ug6Rk52
imHp1J629edUn0Cw7KoPfQLegmWsldG4v931HCdlELT2vj+QE7KJhc1tGFomzCnX
8Q41tRrVVbHPxQYvNmMRXRqIdqXGxFpR758EngxFzAGcnLTrDz/I2GocrQIDAQAB
AoGAQ/uDJCGkanSlya8lnumiKqqB1mm7nDWb1ScgOhw2UPubeT06Krqg+WtkXdJQ
VjsoUJoDq+WrU7/IYRDOWayp5Be3EXCdyldSrWu1+wqJ1Vnpk2oUAEyr+lzcHhW1
FNQ/5rb8kIUjR7DZpwnsYJxDygnaKaNKiUiF2FsMX8JcS8ECQQDoZt3zSsXYeR4t
Y9kPPA19npQXx9K4Wv2wsCR904pznzoaJ9Kj+6E/3AdxtXcTD0GiZe8vW+H6WCmW
gB1NpGiRAkEAwWTwO9ZncQnA+X2PYTkizBp/JdEdRjcL/D2g+g3rpL2nLChI56C5
zA4NsJFmblE2uY1OLIJBGExiZP/XS74gXQJBAISTOgYyH48P+OEX1plUPrXsorq2
KUU10wbaVNbauF6g9Lo7AXS+dQxC7pQ1Wsoqp9yGnd28Yrs3U/Ig/5ZtNaECQG+/
kKUy3bDOjwhbCjeGmVnQ0bmbXMwO0MkfH15+HrShtfBpEr9s+w8y66wkSEjkere7
M/m6Bj0xHgX4Y4JryS0CQQChBua8JXCCUGLle7+IEEcgQZSF4PdLrmnhrRG7Qrrg
yd6pPuvd2jAGv5fMhjROmf9MWc4DFiRK0B6dz7OyF9j/
-----END RSA PRIVATE KEY-----
建立私有CA:
证书申请及签署步骤:
1、生成申请请求
2、RA核验
3、CA签署
4、获取证书
openssl的配置文件:
/etc/pki/tls/openssl.cnf
三种策略:match匹配、optional可选、supplied提供
[ CA_default ]
dir = /etc/pki/CA #数据存放的主目录
certs = $dir/certs #颁发的证书
crl_dir = $dir/crl #吊销的证书
database = $dir/index.txt #证书相关信息
new_certs_dir = $dir/newcerts #新颁发的证书
certificate = $dir/cacert.pem #CA自己的证书 :自签名的证书
serial = $dir/serial #下一个即将颁发的证书的证书编号
crlnumber = $dir/crlnumber #吊销证书文件
crl = $dir/crl.pem #吊销列表
private_key = $dir/private/cakey.pem #CA私钥的文件名
-------------------------------------------------------------
default_days = 365 #有效期
default_crl_days= 30
default_md = sha256 #加密算法
preserve = no
-------------------------------------------------------------
# A few difference way of specifying how similar the request should look
# For type CA, the listed attributes must be the same, and the optional
# and supplied fields are just that :-)
policy = policy_match 哪些设置一致
# For the CA policy
#match 强制要求
[ policy_match ]
countryName = match #国家
stateOrProvinceName = match #省
organizationName = match #组织名称
organizationalUnitName = optional #组织单位
commonName = supplied #通用名
emailAddress = optional #邮箱
-------------------------------------------------------------
#oplicy可选项
[ policy_anything ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional
[root@centos7|~]#ll /etc/pki/CA
总用量 0
drwxr-xr-x. 2 root root 6 8月 9 09:38 certs
drwxr-xr-x. 2 root root 6 8月 9 09:38 crl
drwxr-xr-x. 2 root root 6 8月 9 09:38 newcerts
drwx------. 2 root root 6 8月 9 09:38 private
[root@Centos8|105|~]#mkdir -p /etc/pki/CA
[root@Centos8|103|~]#for dir in certs crl newcerts private ;do mkdir /etc/pki/CA/$dir;done
[root@Centos8|104|~]#tree /etc/pki/CA
/etc/pki/CA
|-- crl
|-- incerts
|-- newcerts
`-- private
[root@Centos8|105|~]#chmod 700 /etc/pki/CA/private/
1.创建CA所需要的文件
#生成证书索引数据库文件
[root@Centos8|106|~]#touch /etc/pki/CA/index.txt
#指定第一个颁发证书的序列号
[root@Centos8|107|~]#echo 01 > /etc/pki/CA/serial
2.生成CA私钥
[root@Centos8|108|~]#cd /etc/pki/CA/
#注意文件名 private/cakey.pem
[root@Centos8|110|CA]#tree
.
|-- crl
|-- incerts
|-- index.txt
|-- newcerts
|-- private
`-- serial
[root@Centos8|115|CA]#(umask 066; openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
......................................................................+++++
..................+++++
e is 65537 (0x010001)
[root@Centos8|116|CA]#tree
.
|-- crl
|-- incerts
|-- index.txt
|-- newcerts
|-- private
| `-- cakey.pem
`-- serial
[root@Centos8|118|CA]#openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 3650 -out /etc/pki/CA/cacert.pem
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) [XX]:CN #国家
State or Province Name (full name) []:beijing #省
Locality Name (eg, city) [Default City]:beijing #市区
Organization Name (eg, company) [Default Company Ltd]:magedu #组织(公司名)
Organizational Unit Name (eg, section) []:devops #组织单位(部门)
Common Name (eg, your name or your server's hostname) []:ca.magedu.org #主机名称
Email Address []:magedu.com #邮箱
选项说明:
-new:生成新证书签署请求
-x509:专用于CA生成自签证书
-key:生成请求时用到的私钥文件
-days n:证书的有效期限
-out /PATH/TO/SOMECERTFILE: 证书的保存路径
查看方法
方法一
[root@Centos8|178|CA]#openssl x509 -in cacert.pem -noout -text
方法二
[root@Centos8|121|CA]#sz cacert.pem
#上传到windows后改为.crt查看
国家代码:https://country-code.cl/
为需要使用证书的主机生成生成私钥
[root@Centos8|126|CA]#(umask 066; openssl genrsa -out /data/app.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
......+++++
...........................................................................................
e is 65537 (0x010001)
为需要使用证书的主机生成证书申请文件
[root@Centos8|127|CA]#openssl req -new -key /data/app.key -out /data/mysql.csr
#根据规则看哪项应该一样(1,2,4)
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) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:devops
Common Name (eg, your name or your server hostname) []:www.magedu.com #一定是对外发布的网站名
Email Address []:magedu.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:#空
An optional company name []:#空
[root@Centos8|185|CA]#cd /etc/pki/CA
[root@Centos8|185|CA]#tree
.
|-- app.key
|-- cacert.pem
|-- certs #其中颁发的证书存放的路径(由你指定)
|-- crl
|-- incerts
|-- index.txt # 颁发的证书信息
|-- newcerts #自动生成的新证书存放的路径
|-- private
| `-- cakey.pem
`-- serial #下一个证书的编号
5 directories, 5 files
[root@Centos8|188|CA]#openssl ca -in /data/mysql.csr -out /etc/pki/CA/certs/mysql.crt -days 100
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
The organizationName field is different between
CA certificate (devops) and the request (dev)
[root@Centos8|200|CA]#vim /etc/pki/tls/openssl.cnf
#报错修改为optional
[ policy_match ]
103 countryName = match
104 stateOrProvinceName = match
105 organizationName = optional
106 organizationalUnitName = optional
107 commonName = supplied
108 emailAddress = optional
#查看证书中的信息
openssl x509 -in certs/app.crt -noout -text
#查看指定编号的证书状态
[root@Centos8|205|CA]#cat index.txt
V 200514012741Z 0F unknown /C=CN/ST=beijing/O=dev/OU=dev/CN=magedu/emailAddress=magedu.com
[root@Centos8|206|CA]#openssl ca -status 0F
Using configuration from /etc/pki/tls/openssl.cnf
0F=Valid (V)
回到windows安装先根证书(选择安装在根证书)再安装颁发的证书
[root@Centos8|208|CA]#cat index.txt.attr #yes 代表必须唯一
unique_subject = yes
在客户端获取要吊销的证书的serial
[root@Centos8|209|CA]#openssl x509 -in /etc/pki/CA/certs/mysql.crt -noout -subject
subject=C = CN, ST = beijing, O = dev, OU = dev, CN = magedu, emailAddress = magedu.com
[root@Centos8|210|CA]#openssl x509 -in /etc/pki/CA/certs/mysql.crt -noout -serial
serial=0F
[root@Centos8|211|CA]#cat /etc/pki/CA/index.txt
V 200514012741Z 0F unknown /C=CN/ST=beijing/O=dev/OU=dev/CN=magedu/emailAddress=magedu.com
在CA上,根据客户提交的serial与subject信息,对比检验是否与index.txt文件中的信息一致,吊销证书对应的文件:
[root@Centos8|213|CA]#openssl ca -revoke /etc/pki/CA/newcerts/0F.pem
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 0F.
Data Base Updated
[root@Centos8|214|CA]#cat /etc/pki/CA/index.txt
R 200514012741Z 200204020855Z 0F unknown /C=CN/ST=beijing/O=dev/OU=dev/CN=magedu/emailAddress=magedu.com
指定第一个吊销证书的编号,注意:第一次更新证书吊销列表前,才需要执行
生成证书吊销列表文件
[root@Centos8|217|CA]#openssl ca -gencrl -out /etc/pki/CA/crl.pem
Using configuration from /etc/pki/tls/openssl.cnf
/etc/pki/CA/crlnumber: No such file or directory
error while loading CRL number
140034962831168:error:02001002:system library:fopen:No such file or directory:crypto/bio/bss_file.c:72:fopen('/etc/pki/CA/crlnumber','r')
140034962831168:error:2006D080:BIO routines:BIO_new_file:no such file:crypto/bio/bss_file.c:79:
[root@Centos8|218|CA]#echo 11 > /etc/pki/CA/crlnumber
[root@Centos8|219|CA]#openssl ca -gencrl -out /etc/pki/CA/crl.pem
Using configuration from /etc/pki/tls/openssl.cnf
[root@Centos8|220|CA]#cat /etc/pki/CA/crlnumber
12
[root@Centos8|221|CA]#tree
.
|-- app.key
|-- cacert.pem
|-- certs
| |-- incerts
| `-- mysql.crt
|-- crl
|-- crl.pem
|-- crlnumber
|-- crlnumber.old
|-- index.txt
|-- index.txt.attr
|-- index.txt.attr.old
|-- index.txt.old
|-- newcerts
| `-- 0F.pem
|-- private
| `-- cakey.pem
|-- serial
`-- serial.old
5 directories, 14 files
[root@Centos8|222|CA]#sz /etc/pki/CA/crl.pem #改为crl后缀
openssl ca -status 0F 查看证书是否有效
[root@VM_0_3_centos|88|~]#cd /etc/pki/tls/certs/
[root@VM_0_3_centos|89|certs]#make 查看格式
[root@VM_0_3_centos|94|certs]#make app.crt 生成
[root@VM_0_3_centos|95|certs]#openssl x509 -in app.crt -noout -text 查看
[root@VM_0_3_centos|96|certs]#sz app.crt
[root@VM_0_3_centos|99|certs]#cat app.crt app.key > app.pem 私钥和证书重定向到一起