openssl

创建证书 #

1 根证书 #

1.1 创建根证书密钥 root.key #

bash
openssl genrsa -des3 -out root.key

# Output
Generating RSA private key, 2048 bit long modulus
..................................+++
..+++
e is 65537 (0x10001)
Enter pass phrase for root.key:
Verifying - Enter pass phrase for root.key:

1.2 创建根证书的申请文件 root.csr #

bash
openssl req -new -key root.key -out root.csr

# Output
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) [XX]:CN
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

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

1.3 创建自己的根证书 root.crt #

bash
openssl x509 -req -days 36500 -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt

# Output
Signature ok
subject=/C=CN/L=Default City/O=Default Company Ltd
Getting Private key
Enter pass phrase for root.key:

2 创建服务器证书 #

2.1 创建服务器证书密钥 server.key #

bash
openssl genrsa -out server.key 2048

# Output
Generating RSA private key, 2048 bit long modulus
.............................................................+++
............................................................................................+++
e is 65537 (0x10001)

2.2 创建服务器证书的申请文件 server.csr #

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

# Output
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) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

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

2.3 创建服务器证书 server.crt #

bash
openssl x509 -req -days 3650 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in server.csr -out server.crt

# Output
Signature ok
subject=/C=CN/L=Default City/O=Default Company Ltd
Getting CA Private Key
Enter pass phrase for root.key:

3 创建客户证书 #

3.1 创建客户密钥 client.key #

bash
openssl genrsa -des3 -out client.key 2048

# Output
Generating RSA private key, 2048 bit long modulus
......+++
......................+++
e is 65537 (0x10001)
Enter pass phrase for client.key:
Verifying - Enter pass phrase for client.key:

3.2 创建客户证书的申请文件 client.csr #

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

# Output
Enter pass phrase for client.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) [XX]:CN
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

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

3.3 创建客户证书 client.crt #

bash
openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in client.csr -out client.crt

# Output
Signature ok
subject=/C=CN/L=Default City/O=Default Company Ltd
Getting CA Private Key
Enter pass phrase for root.key:
2023年4月2日