PKI-密鑰對/jks/證書的相關操作(keytool、openSSL)


title: 密鑰對的相關操作(keytool、openSSL)
date: 2018-10-25 10:01:32
tags:
密碼學


cacerts

概念

cacerts:trusted certificate authority (CA) certificates

​ The cacerts file is a collection of trusted certificate authority (CA) certificates. Sun Microsystems™ includes a cacerts file with its SSL support in the Java™ Secure Socket Extension (JSSE) tool kit and JDK 1.4.x. It contains certificate references for well-known Certificate authorities, such as VeriSign™. Its format is the “keystore” format defined by Sun. An administrator can edit the cacerts file with a command line tool (also provided by Sun) called keytool. For more information about keytool, see the Sun Web site.

Note: The default password for the cacerts file supplied by Sun is changeit. You must use this password to view the contents or to import a new certificate. For security reasons, change the default password.

The essential requirement is that the certificate authority that signed the HPE Service Manager server’s certificate must be in the list of certificate authorities named in this file. To use a self-issued server certificate created with OpenSSL or a tool such as Microsoft Certificate Server™, you must import the certificate for this private certificate authority into the cacerts file that the client uses for SSL. If you do not import the certificate, SSL connections fail because the Java SSL implementation does not recognize the certificate authority.

​ 簡單的來說,cacerts是java用於接受受信任的證書已經證書鏈,cacerts默認包含有一些衆所周知的權威證書,以及證書鏈。當你需要使用https協議,爲你的服務添加ssl的時候,需要將你的證書和證書鏈導入cacerts(如果已經導入過了,就不需要了)。

keytool

查看java自帶的cacerts

# Linux
cd $JAVA_HOME/jre/lib/security
keytool -list -storepass changeit -keystore "./cacerts"		# 查詢所有的證書
keytool -list -storepass changeit -keystore "./cacerts" |grep server 
# 查詢alias帶有server的證書

# Windows
cd %JAVA_HOME%/jre/lib/security
keytool -list -storepass changeit -keystore "./cacerts"		# 查詢所有的證書
keytool -list -storepass changeit -keystore "./cacerts" |findstr /i server
# 查詢alias帶有server的證書

產生jks密鑰對

# 產生一個簽名算法爲SHA256withRSA的,密鑰長度爲2048,的RSA密鑰對,即軟證書server.jks
keytool -keystore server.jks -genkey -alias xxx_server -keyalg RSA -sigalg SHA256withRSA -keysize 2048

導出jks的證書

# 證書,即公鑰
keytool -export -alias server -keystore server.jks -rfc -file sign.cer 

導入證書到cacerts

cd $JAVA_HOME/jre/lib/security
# 將證書導入cacerts,並別名爲server
keytool -import -keystore "./cacerts" -alias server -trustcacerts -storepass changeit -file /var/tmp/certs/Server.cer

在cacerts上刪除證書

cd $JAVA_HOME/jre/lib/security
# 刪除命名爲server的證書
keytool -delete -alias server -keystore "./cacerts" -storepass changeit

參數分析

-genkey 在用戶主目錄中創建一個默認文件".keystore" 的軟證書,產生一個mykey的別名,其中包含用戶的公鑰、私鑰和證書
(在沒有指定生成位置的情況下,keystore會存在用戶系統默認目錄,如:對於window xp系統,會生成在系統的C:/Documents and Settings/UserName/文件名爲“.keystore”)
-alias 產生別名,keytool將通過這個別名進行查找
-keystore 指定密鑰庫的名稱(產生的各類信息將不在.keystore文件中)
-keyalg 指定密鑰的算法 (如 RSA DSA(如果不指定默認採用DSA),推薦採用RSA)
-validity 指定創建的證書有效期多少天
-keysize 指定密鑰長度
-storepass 指定密鑰庫的密碼(獲取keystore信息所需的密碼)
-keypass 指定別名條目的密碼(私鑰的密碼)
-dname 指定證書擁有者信息
-list 顯示密鑰庫中的證書信息
-v 顯示密鑰庫中的證書詳細信息
-export 將別名指定的證書導出到文件
-file 參數指定導出到文件的文件名
-delete 刪除密鑰庫中某條目
-printcert 查看導出的證書信息
-keypasswd 修改密鑰庫中指定條目口令
-storepasswd 修改keystore口令
-import 將已簽名數字證書導入密鑰庫

證書擁有者信息

例如: “CN=名字與姓氏,OU=組織單位名稱,O=組織名稱,L=城市或區域名稱,ST=州或省份名稱,C=單位的兩字母國家代碼”

常用命令:

keytool -export -alias 需要導出的別名 -keystore 指定keystore -file 指定導出的證書路徑位置,及證書名稱 -storepass 密碼

keytool -printcert -file yushan.crt

keytool -delete -alias 指定需刪除的別 -keystore 指定keystore -storepass 密碼

keytool -list -v -keystore 指定keystore -storepass 密碼

keytool -import -alias 指定導入條目的別名 -keystore 指定keystore -file 需導入的證書

keytool -storepasswd -keystore e:/yushan.keystore(需修改口令的keystore) -storepass 123456(原始密碼) -new yushan(新密碼)

keytool -keypasswd -alias 需修改的別名 -keypass 舊密碼 -new 新密碼 -storepass keystore密碼 -keystore sage

下面是各選項的缺省值。
-alias “mykey”
-keyalg “DSA”
-keysize 1024
-validity 90
-keystore 用戶宿主目錄中名爲 .keystore 的文件
-file 讀時爲標準輸入,寫時爲標準輸出

參考

  1. 什麼是cacerts: https://docs.microfocus.com/SM/9.50/Hybrid/Content/security/concepts/what_is_a_cacerts_file.htm
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章