1、生成x509格式的CA自签名证书
openssl req -new -x509 -keyout ca.key -out ca.crt
2、生成服务端的私钥(key文件)及申请证书文件csr文件
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
3、生成客户端的私钥(key文件)及申请证书csr文件
openssl genrsa -des3 -out client.key 1024
openssl req -new -key client.key -out client.csr
4、用生成的CA的证书为刚才生成的server.csr,client.csr文件签名
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key
openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key
5、生成p12格式证书
openssl pkcs12 -export -inkey client.key -in client.crt -out client.pfx
openssl pkcs12 -export -inkey server.key -in server.crt -out server.pfx
6、生成pem格式证书
有时需要用到pem格式的证书,可以用以下方式合并证书文件(crt)和私钥文件(key)来生成。
cat client.crt client.key> client.pem
cat server.crt server.key > server.pem
7、pfx文件转换为X509证书文件和RSA密钥文件
openssl pkcs12 -in server.pfx -nodes -out server.pem
openssl x509 -in server.pem -out server2.crt
openssl rsa -in server.pem -out server2.key
注意:CA在签证时会出现如下错误,解决方法:
[[email protected] test]# openssl ca -in my.csr -out ldap.crt
Using configuration from /etc/pki/tls/openssl.cnf
I am unable to access the /etc/pki/CA/newcerts directory
/etc/pki/CA/newcerts: No such file or directory
[[email protected] test]# mkdir /etc/pki/CA/newcerts
[[email protected] test]# openssl ca -in my.csr -out ldap.crt
Using configuration from /etc/pki/tls/openssl.cnf
/etc/pki/CA/index.txt: No such file or directory
unable to open ‘/etc/pki/CA/index.txt‘
23016:error:02001002:system library:fopen:No such file or directory:bss_file.c:352:fopen(‘/etc/pki/CA/index.txt‘,‘r‘)
23016:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:354:
[[email protected] test]# touch /etc/pki/CA/index.txt
[[email protected] test]# openssl ca -in my.csr -out ldap.crt
Using configuration from /etc/pki/tls/openssl.cnf
/etc/pki/CA/serial: No such file or directory
error while loading serial number
23031:error:02001002:system library:fopen:No such file or directory:bss_file.c:352:fopen(‘/etc/pki/CA/serial‘,‘r‘)
23031:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:354:
[[email protected] test]# touch /etc/pki/CA/serial
[[email protected] test]# echo 00 > /etc/pki/CA/serial