目录
1 CA证书体系
1.1 CA
CA(Certificate Authority,证书授权)是数字证书认证的权威机构,负责颁发数字证书。是数字签名的技术基础保障,也是网上实体身份的证明,能够证明某一实体的身份及其公钥的合法性,证明该实体与公钥二者之间的匹配关系。
简单的说,它是负责管理和签发证书的第三方机构,目的在于让通信双方安全的交换公钥。
1.2 数字证书
证书是数字签名的技术基础保障,也是网上实体身份的证明。它能够证明某一实体的身份及其公钥的合法性,并证明该实体与公钥二者之间的匹配关系。证书是公钥的载体,证书上的公钥与实体身份相绑定。在电子商务系统中,所有实体的证书都是由证书授权中心即CA中心颁发并签名的。有了CA证书,就能够明确对方的身份,用数字证书加密,通过数字签名的电子文件防篡改。
1.3 CA证书
顾名思义,CA证书就是由CA机构颁发的一种数字证书,它包含了证书拥有者的身份信息、公钥、私钥以及CA机构的签名,用于在互联网通讯中验证通信实体的身份和公钥和合法性。
1.4 自签名证书
1.4.1 自签名证书的基本概念
自签名证书是指由用户自己生成和签名的证书,而非有公认的证书颁发机构签名的证书。但自签名证书毕竟是免费且未经过权威机构签名,通常不受浏览器和客户端信任。
1.4.2 带或不带CA的自签名证书
另外在创建自签名证书的时候,用户可以自行选择是否带CA,这是他们的区别:
带CA的证书:
- 用户不仅生成了自己的证书,还创建了自己的CA,并使用该CA签名证书。也就是说用户建立了独立的CA环境。
- 由于创建了自己的CA环境,这类自签名证书的可信度相对高一点。
- 带CA的自签名证书具有较好的可扩展性,适用于需要多个证书,并且需要统一管理和验证的场景。
不带CA的证书:
- 用户仅仅创建了一个证书,并没有创建CA。也就是说这是一张独立的证书。
- 由于没有CA机构的认证,可信度相对较低。
- 不带CA的自签名证书适用于单一、简单的应用场景,如个人网站或测试环境。
1.5 SSL/TLS安全协议
1.5.1 SSL
SSL(Secure Socket Layer)安全套接层是Netscape公司率先采用的网络安全协议。它是在传输通信协议(TCP/IP)上实现的一种安全协议,采用公开密钥技术。SSL广泛支持各种类型的网络,同时提供三种基本的安全服务,它们都使用公开密钥技术。
目前,SSL拥有1.0,2.0,3.0三个版本,我们目前使用的基本为SSL 3.0
1.5.2 TLS
TLS(Transport Layer Security)安全传输层协议是IETF(Internet Engineering Task Force,Internet工程任务组)以SSL协议为基础制定的一种新的协议,它对SSL 3.0进行了标准化并添加了少量的机制。
目前TLS有1.0-1.3共四个版本,但许多浏览器对1.0和1.1两个版本似乎已经停止支持。
2 OpenSSL
2.1 OpenSSL简介
OpenSSL既是一个项目又是一个软件。作为软件,它是一个功能齐全的工具包,广泛应用于通用加密和安全通信。
OpenSSL包括了三个组件:
- libcrypto:用于实现加密和解密的库
- libssl:用于实现ssl通信协议的安全库
- openssl:多用途命令工具
2.2 OpenSSL命令
2.2.1 计算hash
计算文件hash
Command:
openssl <加密算法> <path>
Eg:
[root@linux1 ~]# openssl md5 /etc/passwd
MD5(/etc/passwd)= a6853410a57f452e867727b2f4bd2c1c
[root@linux1 ~]#
计算密码hash
Command:
openssl passwd [options] <password>
Options:
-in infile 从文件中读取密码
-salt val 使用指定的盐
-stdin 从标准输入读取密码
-6 基于SHA512的密码算法
-5 基于SHA256的密码算法
-1 基于MD5的密码算法
-crypt 标准Unix密码算法(默认选项)
-rand val Load the file(s) into the random number generator
-writerand outfile Write random data to the specified file
-rand val 将文件加载到随机数生成器中
-writer和 outfile 将随机数据写入指定文件
Eg:
[root@linux1 ~]# openssl passwd -6 123456
$6$ztBdEjF.bkuzswqI$jRXCp.L4TbcCLSkTtDDavjFrrHlco2xzOpNLFm51Ej7IWcL4d56m5wKtpFE1vj3mK97Ar6QJDKv85F6kZYRpo0
[root@linux1 ~]# echo 123456 | openssl passwd -6 -stdin
$6$M/I/9sEqeq0vidz8$3lUn6bwmJod4azF6z6GfgKLR5JvoTGhCRZbNxMLD9vbC2YiDmLE6naLx3OV76PcPJsga9sHdtKhw/nwta35Xx/
[root@linux1 ~]# openssl passwd -6 -salt M/I/9sEqeq0vidz8 123456
$6$M/I/9sEqeq0vidz8$3lUn6bwmJod4azF6z6GfgKLR5JvoTGhCRZbNxMLD9vbC2YiDmLE6naLx3OV76PcPJsga9sHdtKhw/nwta35Xx/
[root@linux1 ~]#
这里可以看到openssl passwd -6 123456和echo 123456 | openssl passwd -6 -stdin的返回值不一样,这是应为这条命令在计算hash的时候加入了一个叫做盐(salt)的参数,通过openssl passwd -6 -salt M/I/9sEqeq0vidz8 123456命令可以看出只要salt和passwd相同,计算出的hash是一样的。
2.2.2 生成随机数
Command:
openssl rand [-base64] [-hex] <num>
Eg:
[root@linux1 ~]# openssl rand -hex 3
af847d
[root@linux1 ~]# openssl rand -base64 4
AsTleg==
[root@linux1 ~]#
2.2.3 生成私钥和公钥
生成私钥
Command:
openssl genrsa -out <filename> [options] [私钥位数,默认2048]
Options:
-aes128, -aes192, -aes256, -des, -des3, -idea 使用符合密码复杂性的密码对私钥进行指定的加密
Eg:
[root@linux1 ~]# openssl genrsa -out /etc/ssl/test.key
Generating RSA private key, 2048 bit long modulus (2 primes)
..............................................................................................................................+++++
..........................................+++++
e is 65537 (0x010001)
[root@linux1 ~]# ls /etc/ssl
bak test.key
[root@linux1 ~]#
对私钥解密
如果在生成私钥的时候对私钥进行了加密,可以用下面的命令进行解密
Command:
openssl genrsa -in <filename1> -out <filename2>
Eg:
[root@linux1 ~]# openssl genrsa -aes128 -out test.key
Generating RSA private key, 2048 bit long modulus (2 primes)
.........................+++++
.....................................................................+++++
e is 65537 (0x010001)
Enter pass phrase for test.key:
Verifying - Enter pass phrase for test.key:
[root@linux1 ~]# openssl rsa -in test.key -out test1.key
Enter pass phrase for test.key:
writing RSA key
[root@linux1 ~]#
当然,不解密 也是可以的,就是有时候会麻烦一点,比如:
从私钥中提取公钥
Command:
openssl rsa -in <filename1> -pubout -out <filename2>
Eg:
[root@linux1 ~]# openssl rsa -in test.key -pubout -out test.key.pub
Enter pass phrase for test.key:
writing RSA key
[root@linux1 ~]# openssl rsa -in test1.key -pubout -out test1.key.pub
writing RSA key
[root@linux1 ~]#
2.3 建立私有CA实现证书申请颁发
2.3.1 建立私有CA
建目录和文件
由于没有默认生成CA证书根目录结构,所以我们需根据配置文件自行创建目录结构,然后再根据配置文件的要求,创建存放证书索引的index.txt和存放下一个证书的编号的serial这两个文件
[root@linux2 ~]# mkdir -p /etc/pki/CA/{certs,crl,newcerts,private}
# 如果懒的话 yum -y install openssl-* 或者 yum -y install openssl-perl 也可以
[root@linux2 ~]# cd /etc/pki/CA/
[root@linux2 CA]# touch index.txt
[root@linux2 CA]# echo 01 > serial
[root@linux2 CA]#
创建私钥
[root@linux2 CA]# openssl genrsa -out private/cakey.pem
Generating RSA private key, 2048 bit long modulus (2 primes)
.........................................................................................+++++
.....................+++++
e is 65537 (0x010001)
[root@linux2 CA]#
生成自签名根证书
[root@linux2 CA]# openssl req -new -x509 -key private/cakey.pem -days 3650 -out 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 # 国家,使用国家的ISO 2代码
State or Province Name (full name) []:Zhejiang # 省
Locality Name (eg, city) [Default City]:Hangzhou # 市
Organization Name (eg, company) [Default Company Ltd]:test # 组织(公司)名
Organizational Unit Name (eg, section) []:test # 组织单位(部门)名
Common Name (eg, your name or your server's hostname) []:test.com #使用该证书的域名
Email Address []: # 邮箱,可以不填
[root@linux2 CA]#
选项说明
req 生成证书请求
-new: 生成新的证书签署请求
-x509: 专用于CA生成自签名证书
-key: 生成请求时的私钥
-days: 证书的有效期
-out: 证书的路径及名称
查看证书信息
openssl x509 -in /etc/pki/CA/cacert.pem -noout -text
需要注意的是,由于这是自签名证书,通常不受浏览器和客户端的信任,所以在使用是记得将其添加到【受信任的根证书颁发机构】之类的地方去
2.3.2 为用户颁发证书
生成给用户的私钥
[root@linux2 CA]# openssl genrsa -out /etc/ssl/test.key
Generating RSA private key, 2048 bit long modulus (2 primes)
...................................+++++
...............................+++++
e is 65537 (0x010001)
[root@linux2 CA]#
生成证书请求文件
[root@linux2 CA]# openssl genrsa -out /etc/ssl/test.key
Generating RSA private key, 2048 bit long modulus (2 primes)
...................................+++++
.......+++++
e is 65537 (0x010001)
[root@linux2 CA]# openssl req -new -key /etc/ssl/test.key -out /etc/ssl/test.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) [XX]:CN
State or Province Name (full name) []:Zhejiang
Locality Name (eg, city) [Default City]:Hangzhou
Organization Name (eg, company) [Default Company Ltd]:test
Organizational Unit Name (eg, section) []:test
Common Name (eg, your name or your server's hostname) []:linux2.test.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@linux2 CA]#
签发证书
[root@linux2 CA]# openssl ca -in /etc/ssl/test.csr -out /etc/ssl/test.crt -days 1825Using 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: Aug 6 12:07:20 2024 GMT
Not After : Aug 5 12:07:20 2029 GMT
Subject:
countryName = CN
stateOrProvinceName = Zhejiang
organizationName = test
organizationalUnitName = test
commonName = linux2.test.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
C6:24:43:01:A1:DD:17:ED:1B:F9:A8:B7:2F:4E:4E:6C:26:7A:70:B0
X509v3 Authority Key Identifier:
keyid:75:06:3A:B5:19:6F:93:BE:90:6D:24:88:54:0E:53:27:7E:49:78:35
Certificate is to be certified until Aug 5 12:07:20 2029 GMT (1825 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
[root@linux2 CA]#
签发的证书会同时在/etc/pki/CA/newcerts目录下生成一个副本,编号就是serial这个文件中的数字
同时index.txt文件中也对签发的证书进行了记录
[root@linux2 CA]# diff /etc/pki/CA/newcerts/01.pem /etc/ssl/test.crt
[root@linux2 CA]# cat index.txt
V 290805120720Z 01 unknown /C=CN/ST=Zhejiang/O=test/OU=test/CN=linux2.test.com
[root@linux2 CA]#
2.3.3 吊销证书
查看证书的serial
Command:
openssl x509 -in <filename> -noout -serial -subject
Eg:
[root@linux2 CA]# openssl x509 -in /etc/ssl/test.crt -noout -serial -subject
serial=01
subject=C = CN, ST = Zhejiang, O = test, OU = test, CN = linux2.test.com
[root@linux2 CA]#
查看证书状态
Command:
openssl ca -status <serial>
Eg:
[root@linux2 CA]# openssl ca -status 01
Using configuration from /etc/pki/tls/openssl.cnf
01=Valid (V)
[root@linux2 CA]#
吊销证书
[root@linux2 CA]# openssl ca -revoke /etc/pki/CA/newcerts/01.pem
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 01.
Data Base Updated
[root@linux2 CA]# cat /etc/pki/CA/index.txt
R 290805120720Z 240806124812Z 01 unknown /C=CN/ST=Zhejiang/O=test/OU=test/CN=linux2.test.com
[root@linux2 CA]#
创建用于存放证书吊销列表编号的文件
[root@linux2 CA]# echo 01 > /etc/pki/CA/crlnumber
[root@linux2 CA]#
更新证书吊销列表
[root@linux2 CA]# openssl ca -gencrl -out /etc/pki/CA/crl.pem
Using configuration from /etc/pki/tls/openssl.cnf
[root@linux2 CA]#