在安全通訊編程中有時咱們會用到數字證書進行通訊加密,那麼如何生成本身的數字證書進行測試呢?下面是使用OpenSSL在Ubuntu環境生成數字證書的步驟:nginx
1、安裝opensslweb
a) 略編程
2、生成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 -newcaapp
d) 根據提示填寫信息完成後,在demoCA下會生成證書文件,其中demoCA/private/cakey.pem爲ca證書私鑰,demoCA/cacert.pem爲ca根證書。測試
3、生成服務器證書加密
a) 生成私鑰: openssl genrsa -des3 -out server.key 1024rest
b) 生成csr文件: openssl req -new -key server.key -out server.csr
c) 生成證書&簽名: openssl ca -in server.csr -out server.crt
4、生成客戶端證書
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 -out client.crt
5、生成瀏覽器支持的.pfx(.p12)證書
安裝過支付寶、財付通證書的用戶都知道,證書文件格式爲pfx,那如何把咱們的數字證書轉換爲瀏覽器識別的格式呢?
a) openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.pfx
6、生成不帶密碼驗證的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?
7、常見錯誤
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.
這種狀況通常發生在你以前生成過同樣的證書,又想從新生成如出一轍的證書。
解決方法有2種:
1) 修改demoCA下 index.txt.attr,將unique_subject = yes改成unique_subject = no
2) 刪除demoCA下的index.txt,並新建一個空的index.txt