Ubuntu使用OpenSSL生成数字证书详解
在安全通信编程中有时我们会用到数字证书进行通信加密,那么如何生成自己的数字证书进行测试呢?下面是使用OpenSSL在Ubuntu环境生成数字证书的步骤:
一、安装openssl
a)    略
二、生成ca证书
a)    创建一个证书目录,mkdir /home/liuzhigong/SSL
b)    将CA.sh拷贝到/home/liuzhigong/SSL目录,cp /usr/lib/ssl/misc/CA.sh /home/liuzhigong/SSL
c)    ./CA.sh -newca
d)    根据提示填写信息完成后,在demoCA下会生成证书文件,其中demoCA/private/cakey.pem为ca证书私钥,demoCA/cacert.pem为ca根证书。
三、生成服务器证书
a)    生成私钥:        openssl genrsa -des3 -out server.key 1024
b)    生成csr文件:   openssl req -new -key server.key -out server.csr
c)    生成证书&签名:    openssl ca -in server.csr -
四、生成客户端证书
a)    生成私钥:        openssl genrsa -des3 -out client.key 1024
b)    生成csr文件:   openssl req -new -key client.key -out client.csr
c)    生成证书&签名:        openssl ca -in client.csr -
五、生成浏览器支持的.pfx(.p12)证书
安装过支付宝、财付通证书的用户都知道,证书文件格式为pfx,那如何把我们的数字证书转换为浏览器识别的格式呢?
a)    openssl pkcs12 -export -clcerts - -inkey client.key -out client.pfx
六、生成不带密码验证的client/server.key.unsecure
如果你想要把数字证书用于Nginx、Apache等Web服务器,你会发现启动nginx服务器时会要求你输入数字证书密码,这是因为在设置私钥key时将密码写入了key文件,导致Nginx/Apache等系列服务器在启动时要求Enter PEM pass phrase。我们需要做的是剥离这个密码,利用如下OpenSSL命令生成server.key.unsecure文件
openssl rsa -in server.key -out server.key.unsecure
参考资料:Can I skip the PEM pass phrase question when I restart the webserver?
七、常见错误
a)    openssl TXT_DB error number 2 failed to update database.
This thing happens when certificates share common data. You cannot have two certificates that look otherwise the same.
这种情况一般发生在你之前生成过一样的证书,又想重新生成一模一样的证书。
免费ssl证书永久生成解决方法有2种:
1)    修改demoCA下 attr,将unique_subject = yes改为unique_subject = no
2)    删除demoCA下的,并新建一个空的