openssl生成证书及吊销列表
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
openssl生成证书及吊销列表
一,先来讲讲基本概念。
证书分类:
按类型可以分为CA证书和用户用户证书,我们我说的root也是特殊的CA证书。
用户证书又可以根据用途分类,放在服务器端的称为服务器证书,放在客户端一般称为客户端证书(这种说法不是很准确,只是一种理解。
实际应该是在对客户端认证时才会用到客户端证书且root、ca证书都可以放在客户端),记住,这两种证书都应为用户证书。
一般可以理解为证书由key和证书(没有key的文件也称为证书)组成,谁拥有这两个东西才真正拥有这个证书。
好比锁是有钥匙和锁头组成的,你得两都有。
你只有锁头锁住东西,却没有钥匙打开,也没什么用。
如果你对证书不了解,那一定要知道证书这三点作用(纯个人认为比较重要三点):
1,签名:通过签名技术可以保证证书拥有者的唯一性,而且所有信息没有被篡改。
想了解数字签名的自己百度一下。
2,提供公钥:通过签名技术知道证书拥有者是A,且所有信息都是A,就可以拿到A的公钥算法及公钥。
所以有些人理解为证书就是一个公钥(个人认为这种理解与实际偏差较大)。
3,颁发者:找到颁发者很重要,每个证书都有一个颁发者。
这个在证书认证时用得到。
对于用户(人)来说还是通过证书名称来区分证书,下面讲解几种常见的证书。
a).cert或.crt文件:这种证书倒是可以理解为公钥证书,因为它最主要的作用就是来提供公钥的,只是一种理解,签名认证这些作用一样不会少。
这种文件不含有key,就好像一个打开的锁头,可以发给任何人。
所以拥有.cer或.crt文件的不是真正的拥有者,因为你可以用证书里的公钥加密,但并没有私钥解密。
b).pfx文件:这种证书文件可以理解为.cer文件与key结合体,即拥有.pfx证书相当同时拥有公钥与私钥。
即可以加密也可以解密。
c).key文件:就是钥匙啦。
锁头可以给任何人,但是钥匙只能自己保留,所以这玩意一定要保存好。
d).p7b文件:为证书链文件,也不含私钥。
证书链即证书的拥有者、颁发者、颁发者的颁发者、依次类推的证书合成一个文件。
以上对证书概念基础的了解,有基础的可以不用看。
实际证书分类根据编码方式来区分的,只是为了方便理解才这么分的。
当我们需要向对方发送加密数据时,我们只需要对方的cer或crt文件就可以了。
而需要对方向自己发送加密数据时,自己得拥有key,并且对方要有自己公钥(从cer文件获得)。
二、环境搭建
安装openssl这个就不讲了,现在很多linux版本都默认安装了。
先来看下配置文件中一段。
默认配置文件/usr/local/openssl/ssl/f或者/etc/pki/tls/f。
dir = ./demoCA # 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 allowcreation 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
# mustbe 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
根据上面配置文件就知道,我们得搭建相同的目录结构环境。
先创建一个目录test,并且将1,配置文件cp到test目录下。
[iyunv@localhost ~]# mkdir test
[iyunv@localhost ~]# cd test
[iyunv@localhost test]# cp/usr/local/openssl/ssl/f ./
[iyunv@localhost test]# ls
f
2,根据配置文件中目录结构可知有个demoCA目录,目录下有各种文件。
[iyunv@localhost test]# mkdir ./demoCA./demoCA/newcerts ./demoCA/private
[iyunv@localhost test]# chmod 777./demoCA/private
[iyunv@localhost test]# echo'01'>./demoCA/serial
[iyunv@localhost test]# touch./demoCA/index.txt
生成证书暂时用到这么多,其他可以先不创建,用到时再作修改。
环境目录结构如下:
[iyunv@localhost test]# tree
.
├──demoCA
│├──certs
│├──index.txt
│├──private
│└──newcerts
└──f
3,环境搭建很重要的东西就修改配置文件了。
这里只是实现生成证书,不讲解配置文件。
所以只需要修改如下一句就可以了。
dir = ./demoCA # Where everything is kept
将路径改为实际绝对路径。
我这里就是/root/test/demoCA,所以修改后就是:
dir = /root/test/demoCA # Whereeverything is kept
三、整体步骤
openssl genrsa -des3 –out server.key 1024//生成key
openssl req -new –key server.key -outserver.csr -config f//生成csr文件openssl req -new -x509 -keyoutca.key -outca.crt -config f//自生成CA(root)openssl ca -in server.csr –out server.crt-cert ca.crt -keyfile ca.key -config f//签名penssl pkcs12 -export -inkeyserver.key -inserver.crt -out server.pfx//合成pfx格式
四、具体生成证书步骤
将以生成服务器端证书为例讲解
1,生成私钥文件,保存为server.key。
使用的3des算法,密钥长度为2048。
[iyunv@localhost test]# openssl genrsa -des3-out server.key 2048
Generating RSA private key, 2048 bit longmodulus ............................................................................+++
.....+++
e is 65537 (0x10001)
Enter pass phrase for server.key: #输入秘密
Verifying - Enter pass phrase forserver.key: #输入秘密
[iyunv@localhost test]# ls
demoCA f server.key
2,生成证书签名申请文件(csr)。
保存为server.csr
[iyunv@localhost test]# openssl req -new-key server.key -out server.csr -config f Enter pass phrase for server.key: #输入第1步输入的密码
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what iscalled a Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter '.', the field will be leftblank.
-----
Country Name (2 letter code) [AU]:cn
State or Province Name (full name)[Some-State]:bj
Locality Name (eg, city) []:hd
Organization Name (eg, company) [InternetWidgits Pty Ltd]: Organizational Unit Name (eg, section)[]:test
Common Name (e.g. server FQDN or YOUR name)[]:
Email Address []:test@
Please enter the following 'extra'attributes
to be sent with your certificate request
A challenge password []: #这里可以不输入
An optional company name []: #这里可以不输入
[iyunv@localhost test]# ls
demoCA f server.csr server.key
3,有申请文件,要有个机构来签名,实际是将server.csr文件提供给第三方可信任机构签名就可以。
这里为了演示,将自生成CA(root)。
证书保存为root.crt,key保存为root.key。
[iyunv@localhost test]# openssl req -new-x509 -keyout root.key -out root.crt -config f
Generating a 1024 bit RSA private key ...............................................++++++
..++++++
writing new private key to 'root.key'
Enter PEM pass phrase: #输入密码
Verifying - Enter PEM pass phrase: #确认输入
-----
You are about to be asked to enterinformation that will be incorporated
into your certificate request.
What you are about to enter is what iscalled a Distinguished Name or a DN.
There are quite a few fields but you canleave some blank
For some fields there will be a defaultvalue,
If you enter '.', the field will be leftblank.
-----
Country Name (2 letter code) [AU]:cn #保持与csr文件信息一致,与配置文件有关。
State or Province Name (full name)[Some-State]:bj #保持与csr文件信息一致
Locality Name (eg, city) []:hd #保持与csr文件信息一致
Organization Name (eg, company) [InternetWidgits Pty Ltd]: #保持与csr文件信息一致Organizational Unit Name (eg, section)[]:test # 保持与csr文件信息一致
Common Name (e.g. server FQDN or YOUR name)[]:
Email Address []:test@
[iyunv@localhost test]# ls
demoCA f root.crt root.key server.csr server.key
4,有了CA(root)我们就可以用它来为第2步生的csr文件进行签名了。
[iyunv@localhost test]# openssl ca -inserver.csr -out server.crt -cert root.crt -keyfile root.key -config f Using configuration from f
Enter pass phrase for root.key:
Check that the request matches thesignature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Oct 8 23:26:13 2015GMT
Not After : Oct 7 23:26:13 2016GMT
Subject:
countryName = cn
stateOrProvinceName = bj
organizationName =
organizationalUnitName = test
commonName =
emailAddress =test@
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
35:2B:69:AE:42:6F:D7:93:CC:AC:C5:88:89:DE:F0:CC:4E:D9:EF:FD X509v3 Authority Key Identifier:
keyid:0C:DA:95:AC:B9:BC:8E:F1:66:EC:DE:28:E3:01:66:D0:A4:82:1F:17
Certificate is to be certified untilOct 7 23:26:13 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
5,至此所有文件都生成好了,查看下整个过程中需要的文件及生成的文件。
[iyunv@localhost test]# tree
.
├──demoCA
│├──index.txt
│├──index.txt.attr
│├──index.txt.old
│├──newcerts
││└──01.pem
│├──private
│├──serial
│└──serial.old
├──f
├──root.crt
├──root.key
├──server.crt #我们要生成的服务器端证书
├──server.csr
└──server.key #我们要生成的服务器端证书的key
五、经常也会碰到证书吊销列表CRL,现在来生成server.crl文件,即将刚才的证书吊销。
同样先根据配置文件配置环境,如下:
[iyunv@localhost test]# cp root.key ./demoCA/private/cakey.pem
[iyunv@localhost test]# cp root.crt./demoCA/cacert.pem
[iyunv@localhost test]# echo '00' >./demoCA/crlnumber
吊销证书
[iyunv@localhost test]# openssl ca -revokeserver.crt -config f
Using configuration from f
Enter pass phrase for/root/test/demoCA/private/cakey.pem:
Revoking Certificate 01.
Data Base Updated
生成吊销列表
[iyunv@localhost test]# openssl ca-gencrl -out server.crl -config f Using configuration from f
Enter pass phrase for/root/test/demoCA/private/cakey.pem:
[iyunv@localhost test]# ls
demoCA f root.crt root.key server.crl server.crt server.csr server.key 查看所有生成的文件
[iyunv@localhost test]# tree
.
├──demoCA
│├──cacert.pem
│├──crlnumber
│├──crlnumber.old
│├──index.txt
│├──index.txt.attr
│├──index.txt.attr.old
│├──index.txt.old
│├──newcerts
││└──01.pem #生成的证书
│├──private
││└──cakey.pem
│├──serial
│└──serial.old
├──f
├──root.crt #根证书
├──root.key #根key文件
├──server.crl #吊销列表
├──server.crt #服务端证书
├──server.csr #服务证书签名请求文件
└──server.key #服务器端证书key文件
五、总结
以上只是简单讲解生成用户证书的过程,对于证书相关配置全部使用f默认配置。
如:证书有效期、ca与用户证书信息验证等等。
一开始说了,证书有很多格式,用的比较多的是pfx格式转换,转换命令如下:
从pfx读取crt和key
opensslpkcs12 -in server.pfx -nodes -out server.pem # 生成明文所有内容
opensslrsa -in server.pem -out server.key # 取key 文件
opensslx509 -in server.pem -out server.crt # 取证书
把crt和key合成pfx
1
openssl pkcs12 -export -out server.pfx-inkey server.key -in server.crt。