这里将证书文件的生成分成了两部分:第一部分是生成自签名证书文件,自签名证书文件一般是作为根证书文件(也就是认证机构的证书文件);第二部分是生成个人服务器的证书文件,个人服务器的证书文件是使用根证书文件及其私钥生成而来。
生成自签名证书文件
生成一个私钥文件
D:\Cert>openssl genrsa -out ceveryc.key 1024
Generating RSA private key, 1024 bit long modulus (2 primes)
........................................+++++
........+++++
e is 65537 (0x010001)
由私钥文件生成证书请求文件
D:\Cert>openssl req -new -key ceveryc.key -out ceveryc.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) [AU]:CN
State or Province Name (full name) [Some-State]:Si Chuan
Locality Name (eg, city) []:Cheng Du
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Cloud
Organizational Unit Name (eg, section) []:Technology
Common Name (e.g. server FQDN or YOUR name) []:*******.com
Email Address []:***@gmail.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:***********
An optional company name []:Cloud
生成自签名文件
可以用刚刚创建的私钥和证书申请文件生成自签名的证书,也就是自签名证书。
D:\Cert>openssl x509 -req -in ceveryc.csr -signkey ceveryc.key -out ceveryc.crt
Signature ok
subject=C = CN, ST = Si Chuan, L = Cheng Du, O = Cloud, OU = Technology, CN = *******.com, emailAddress = ***@gmail.com
Getting Private key
D:\Cert>
生成个人服务器证书
可以把上面生成的自签名证书ceveryc.crt当作CA证书(根证书),ceveryc.key当作CA证书的私钥,下面演示CA机构是如何生成
用户的服务器证书。
为用户创建一个私钥文件
这一步和生成自签名文件的第一步是完全一样的,这里为用户生成了一个私钥文件user.key
D:\Cert>openssl genrsa -out user.key 1024
为用户创建一个证书申请文件
使用刚刚为用户生成的私钥文件user.key来生成用户的证书申请文件user.csr,这一步和生成自签名文件的第二步也是相同的
D:\Cert>openssl req -new -key user.key -out user.csr
使用根证书和根证书的密钥来生成用户的证书文件
其实,最关键的步骤就是这一步,通过参数 -CA 指定根证书为ceveryc.cert, 通过参数 -CAkey 指定根证书的私钥为 ceveryc.key, 通过参数 -in 指定用户的证书申请文件为 user.csr, 通过参数 -out 指定生成的用户证书文件为user.crt:
D:\Cert>openssl x509 -req -CA ceveryc.crt -CAkey ceveryc.key -CAcreateserial -in user.csr -out user.crt