1) 生成RSA密钥
heidong@HEIDONG:~/tmp/cert$ openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
........................+++
....................................................................................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
这样生成的密钥是有密码保护的,可去掉密码
heidong@HEIDONG:~/tmp/cert$ openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:
writing RSA key
2) 生成证书请求文件
heidong@HEIDONG:~/tmp/cert$ 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
State or Province Name (full name) [Some-State]:Guangdong
Locality Name (eg, city) []:Guangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:PAIDU, Ltd
Organizational Unit Name (eg, section) []:Hello
Common Name (e.g. server FQDN or YOUR name) []:www.paidu.com
Email Address []:paidu@paidu.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
在证书请求文件生成的过程中Common Name比较重要,必须和域名匹配
有了证书请求文件后,就可以直接用该文件到CA认证中心请求证书文件,当然是要收费的。
也可以生成自己测试用的证书
3) 生成证书
heidong@HEIDONG:~/tmp/cert$ openssl req -x509 -days 1024 -key server.key -in server.csr > server.crt
如果使用CA签名的方法生成证书,检查以下目录是否存在,如果步存在先新建
1) mkdir -p ./demoCA/newcerts
2) touch demoCA/index.txt
3) touch demoCA/serial
4) echo 01 > demoCA/serial
3.1) 生成CA证书
heidong@HEIDONG:~/tmp/cert$ openssl req -new -x509 -keyout ca.key -out ca.crt
Generating a 2048 bit RSA private key
............................................................+++
........................................+++
writing new private key to 'ca.key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
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]:Guangdong
Locality Name (eg, city) []:Guangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:PAIDU, Ltd
Organizational Unit Name (eg, section) []:Hello
Common Name (e.g. server FQDN or YOUR name) []:www.paidu.com
Email Address []:paidu@paidu.com
3.2) 用CA证书签名生成证书
heidong@HEIDONG:~/tmp/cert$ openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key
Using configuration from /usr/lib/ssl/openssl.cnf
Enter pass phrase for ca.key:
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Nov 16 09:00:43 2016 GMT
Not After : Nov 16 09:00:43 2017 GMT
Subject:
countryName = CN
stateOrProvinceName = Guangdong
organizationName = PAIDU, Ltd
organizationalUnitName = Hello
commonName = www.paidu.com
emailAddress = paidu@paidu.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
C0:2B:60:1A:2F:B2:38:B7:26:0A:F4:CA:F0:7B:BD:AE:BE:B7:56:C9
X509v3 Authority Key Identifier:
keyid:E9:9C:19:5C:66:D7:6C:F5:61:8C:72:19:39:6D:73:E1:B8:A3:C5:9B
Certificate is to be certified until Nov 16 09:00:43 2017 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
客户端证书生成
客户端证书生成过程和服务端一致,但是需增加一个步骤让转换为浏览器可识别的格式
heidong@HEIDONG:~/tmp/cert$ openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.pfx
Enter pass phrase for client.key:
Enter Export Password:
Verifying - Enter Export Password: