使用OpenSSL制作HTTPS/SSL服务器端单向证书

首先,无论是在Linux下还是在Windows下的Cygwin中,进行下面的操作前都须确认已安装OpenSSL软件包。

1. 创建根证书密钥文件(自己做CA)root.key:
openssl genrsa -des3 -out root.key
输出内容为:
Administrator@lanbin ~
$ openssl genrsa -des3 -out root.key
Generating RSA private key, 1024 bit long modulus
....++++++
.........++++++
e is 65537 (0x10001)
Enter pass phrase for root.key: ← 输入一个新密码
Verifying - Enter pass phrase for root.key: ← 重新输入一遍密码

2. 创建根证书的申请文件root.csr:
openssl req -new -key root.key -out root.csr
输出内容为:
Administrator@lanbin ~
$ openssl req -new -key root.key -out root.csr
Enter pass phrase for root.key: ← 输入前面创建的密码
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 ← 国家代号,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (e.g. server FQDN or YOUR name) []: ← 此时不输入
Email Address []:[email protected] ← 电子邮箱,可随意填

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入

3. 创建一个自当前日期起为期十年的根证书root.crt:
openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt
输出内容为:
Administrator@lanbin ~
$ openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./[email protected]
Getting Private key
Enter pass phrase for root.key: ← 输入前面创建的密码

4. 创建服务器证书密钥server.key:
openssl genrsa -out server.key 2048
输出内容为:
Administrator@lanbin ~
$ openssl genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...........+++
...........+++
e is 65537 (0x10001)

5.创建服务器证书的申请文件server.csr:
openssl req -new -key server.key -out server.csr
输出内容为:
Administrator@lanbin ~
$ openssl req -new -key server.key -out server.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 ← 国家名称,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (e.g. server FQDN or YOUR name) []:www.mycompany.com ← 服务器主机名
Email Address []:[email protected] ← 电子邮箱,可随便填

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入

6. 创建自当前日期起有效期为期两年的服务器证书server.crt:
openssl x509 -req -days 3650 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in server.csr -out server.crt
输出内容为:
Administrator@lanbin ~
$ openssl x509 -req -days 3650 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in server.csr -out server.crt
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/[email protected]
Getting CA Private Key
Enter pass phrase for root.key: ← 输入前面创建的密码

7. 生成服务端p12格式根证书
openssl pkcs12 -export -clcerts -in server.crt -inkey server.key -out server.p12
输出内容为:
Administrator@lanbin ~
$ openssl pkcs12 -export -clcerts -in server.crt -inkey server.key -out server.p12
Enter Export Password: ← 输入一个新的密码,用作保护密码,在安装证书时需要输入此密码
Verifying - Enter Export Password: ← 确认密码

8. 将root.crt安装到系统中的“收信人的根证书颁发机构”即可。

你可能感兴趣的:(OpenSSL)