openssl是当前非常流行的SSL密码库工具,如果服务器或者网站需要使用https协议,就需要使用openssl工具生成证书。

之前在Windows上有用Perl编译过OpenSSL,这次项目上要搭一个https server需要用它来生成自签名证书,其中我的配置文件在openssl/apps/openssl.cnf,编译后openssl.exe在openssl/out32dll文件夹中。

准备工作:

需要在系统系统配置文件中配置openssl.cnf的路径,后面生成证书时会用到。

openssl生成前端后端证书pem格式 如何用openssl生成证书_ci

 

下面将罗列生成CA、server、client三方证书的步骤,但对于只做单向鉴定的情况下client证书是不必要的。

 

A、生成根证书及自签名证书

1.创建根证私钥 

    openssl genrsa -out ca-key.pem 1024

该命含义如下:

genrsa——使用RSA算法产生私钥

-out——输出文件的路径

1024——指定私钥长度

2.创建根证书请求文件 

    openssl req -new -out ca-req.csr -key ca-key.pem

该命令含义如下:

req——执行证书签发命令

-new——新证书签发请求

-key——指定私钥路径

-out——输出的csr文件的路径

3.自签根证书 
    openssl x509 -req   -extfile ../apps/openssl.cnf -extensions v3_req  -in ca-req.csr -out ca-cert.pem -signkey ca-key.pem -CAcreateserial -days 3650

该命令的含义如下:

x509——生成x509格式证书

-req——输入csr文件

-days——证书的有效期(天)

-sha1——证书摘要采用sha1算法

-extensions——按照openssl.cnf文件中配置的v3_ca项添加扩展

-signkey——签发证书的私钥

-in——要输入的csr文件

-out——输出的cer证书文件

    重要说明: -extfile ../apps/openssl.cnf -extensions v3_req  参数是生成 X509 V3 版本的证书的必要条件,如果不带此参数,则生成v1版本证书。 ../apps/openssl.cnf  是系统自带的OpenSSL配置文件,该配置文件默认开启 X509 V3 格式。下同。

4.导出p12格式根证书 
    openssl pkcs12 -export -clcerts -in ca-cert.pem -inkey ca-key.pem -out ca.p12 

参数含义如下:

pkcs12——用来处理pkcs#12格式的证书

-export——执行的是导出操作

-clcerts——导出的是客户端证书,-cacerts则表示导出的是ca证书

-inkey——证书的私钥路径

-in——要导出的证书的路径

-out——输出的密钥库文件的路径

B、生成服务端证书,使用根证书签发

1.生成服务端key 

    openssl genrsa -out server-key.pem 1024

2.生成服务端请求文件 

    openssl req -new -out server-req.csr -key server-key.pem

3.生成服务端证书,使用根证书签名 

    openssl x509 -req -extfile ../apps/openssl.cnf -extensions v3_req -in server-req.csr -out server-cert.pem -signkey server-key.pem -CA ca-cert.pem  -CAkey ca-key.pem -CAcreateserial -days 3650

4.生成服务端p12格式根证书 
    openssl pkcs12 -export -clcerts -in server-cert.pem -inkey server-key.pem -out server.p12 

 

C、生成客户证书,使用根证书签发

1.生成客户端key 

    openssl genrsa -out client-key.pem 1024

2.生成客户端请求文件 

    openssl req -new -out client-req.csr -key client-key.pem

3.生成客户端证书,使用根证书签名 

    openssl x509 -req -extfile ../apps/openssl.cnf -extensions v3_req -in client-req.csr -out client-cert.pem -signkey client-key.pem -CA ca-cert.pem  -CAkey ca-key.pem -CAcreateserial -days 3650

4.生成客户端p12格式根证书 
    openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12 

 

D、查看证书

openssl x509  -in ca-cert.pem -text -noout 

Certificate: 
    Data: 
        Version: 3 (0x2) 
        Serial Number: 13373217044989835800 (0xb997360c4ed17a18) 
    Signature Algorithm: sha1WithRSAEncryption 
        Issuer: C=CN, ST=bj, L=Default City, O=Default Company Ltd 
        Validity 
            Not Before: May 16 02:25:21 2018 GMT 
            Not After : May 13 02:25:21 2028 GMT 
        Subject: C=CN, ST=bj, L=bj, O=bj, OU=bj, CN=bj/emailAddress=bj 
        Subject Public Key Info: 
            Public Key Algorithm: rsaEncryption 
                Public-Key: (1024 bit) 
                Modulus: 
                    00:b1:3d:63:35:52:a6:75:c1:9c:2e:5f:88:df:7e: 
                    fc:29:a9:d4:bb:91:e5:27:b8:92:cc:63:7d:d8:7a: 
                    b0:3f:7c:43:f8:e7:f9:ed:b7:f6:26:00:d1:ee:68: 
                    20:6a:80:bc:0f:0d:3f:94:3f:b2:4d:ab:49:3f:f6: 
                    88:db:5a:0c:f4:41:5d:d5:d3:34:27:b6:87:c0:65: 
                    c6:f6:0c:e3:b1:ea:59:24:ff:14:48:6a:d2:51:2a: 
                    61:a9:c9:24:cc:e5:6a:ba:d7:83:76:1a:54:6d:a6: 
                    01:f6:75:98:4c:45:6d:a1:ad:9c:88:1b:d7:ae:c6: 
                    a4:1e:99:ba:44:ea:52:1b:37 
                Exponent: 65537 (0x10001) 
        X509v3 extensions: 
            X509v3 Basic Constraints:  
                CA:FALSE 
            X509v3 Key Usage:  
                Digital Signature, Non Repudiation, Key Encipherment 
            X509v3 Basic Constraints:  
                CA:FALSE 
            X509v3 Key Usage:  
                Digital Signature, Non Repudiation, Key Encipherment 
    Signature Algorithm: sha1WithRSAEncryption 
         3f:e5:fd:ab:08:2e:37:6c:5f:12:aa:0c:b4:28:da:2e:7a:c7: 
         0a:43:89:81:1a:33:c2:d7:dd:95:c5:d6:a9:4c:12:d2:54:ee: 
         ec:9a:15:93:ab:a6:59:40:2e:a8:ad:02:19:69:d3:49:17:08: 
         f5:61:e1:68:0d:1b:ac:0f:9e:eb:a7:03:fa:9d:64:1f:42:cd: 
         24:58:ce:ad:6c:14:e2:78:77:42:37:1f:be:a9:a3:e1:bb:43: 
         20:05:a3:9c:94:98:49:c0:f3:09:ce:11:f6:17:cf:3f:07:da: 
         a0:fc:cd:0c:6f:09:d1:3c:5f:5d:c6:81:c8:d5:62:59:3a:9e: 
         39:49

 

过程都是相同的,先生成1024位的RSA私钥,然后生成证书请求文件(.csr),csr文件经CA私钥签名后生成公钥(即X.509证书),如果需要的话还可以再把它导出为其他格式比如PKCS#12证书(.p12)。

参考代码如下:

D:\Code\openssl\apps>openssl genrsa -out ies/ca-key.pem 1024
Generating RSA private key, 1024 bit long modulus
.............++++++
.....................++++++
e is 65537 (0x10001)

D:\Code\openssl\apps>openssl req -new -out ies/ca-req.csr -key ies/ca-key.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) [AU]:CN
State or Province Name (full name) [Some-State]:Shan-Dong
Locality Name (eg, city) []:jinan
Organization Name (eg, company) [Internet Widgits Pty Ltd]:iESLab
Organizational Unit Name (eg, section) []:SK
Common Name (e.g. server FQDN or YOUR name) []:www.iessk.com
Email Address []:sorata_kasugano@qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.

D:\Code\openssl\apps>openssl x509 -req -in ies/ca-req.csr -out ies/ca-cert.pem -signkey ies/ca-key.pem -days 3650
Signature ok
subject=/C=CN/ST=Shan-Dong/L=jinan/O=iESLab/OU=SK/CN=www.iessk.com/emailAddress=sorata_kasugano@qq.com
Getting Private key

D:\Code\openssl\apps>openssl pkcs12 -export -clcerts -in ies/ca-cert.pem -inkey ies/ca-key.pem -out ies/ca.p12
Enter Export Password:
Verifying - Enter Export Password:

 

D:\Code\openssl\apps>openssl genrsa -out ies/server-key.pem 1024
Generating RSA private key, 1024 bit long modulus
......++++++
.++++++
e is 65537 (0x10001)

D:\Code\openssl\apps>openssl req -new -out ies/server-req.csr -key ies/server-key.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) [AU]:CN
State or Province Name (full name) [Some-State]:Shan-Dong
Locality Name (eg, city) []:jinan
Organization Name (eg, company) [Internet Widgits Pty Ltd]:iESLab
Organizational Unit Name (eg, section) []:SK
Common Name (e.g. server FQDN or YOUR name) []:www.iessk.com
Email Address []:sorata_kasugano@qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.

D:\Code\openssl\apps>openssl x509 -req -in ies/server-req.csr -out ies/server-cert.pem -signkey ies/server-key.pem -CA ies/ca-cert.pem -CAkey ies/ca-key.pem -CAcreateserial -days 3650
Signature ok
subject=/C=CN/ST=Shan-Dong/L=jinan/O=iESLab/OU=SK/CN=www.iessk.com/emailAddress=sorata_kasugano@qq.com
Getting Private key
Getting CA Private Key

D:\Code\openssl\apps>openssl pkcs12 -export -clcerts -in ies/server-cert.pem -inkey ies/server-key.pem -out ies/server.p12
Enter Export Password:
Verifying - Enter Export Password:

 

D:\Code\openssl\apps>openssl genrsa -out ies/client-key.pem 1024
Generating RSA private key, 1024 bit long modulus
......................++++++
...............++++++
e is 65537 (0x10001)

D:\Code\openssl\apps>openssl req -new -out ies/client-req.csr -key ies/client-key.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) [AU]:CN
State or Province Name (full name) [Some-State]:Shan-Dong
Locality Name (eg, city) []:jinan
Organization Name (eg, company) [Internet Widgits Pty Ltd]:iESLab
Organizational Unit Name (eg, section) []:SK
Common Name (e.g. server FQDN or YOUR name) []:www.iessk.com
Email Address []:sorata_kasugano@qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.

D:\Code\openssl\apps>openssl x509 -req -in ies/client-req.csr -out ies/client-cert.pem -signkey ies/client-key.pem -CA ies/ca-cert.pem -CAkey ies/ca-key.pem -CAcreateserial -days 3650
Signature ok
subject=/C=CN/ST=Shan-Dong/L=jinan/O=iESLab/OU=SK/CN=www.iessk.com/emailAddress=sorata_kasugano@qq.com
Getting Private key
Getting CA Private Key

D:\Code\openssl\apps>openssl pkcs12 -export -clcerts -in ies/client-cert.pem -inkey ies/client-key.pem -out ies/client.p12
Enter Export Password:
Verifying - Enter Export Password: