我需要一些帮助将我的 .P12 证书文件转换为 JKS 密钥库。我使用 Java 的 keytool 实用程序遵循标准命令。但是,当我尝试使用生成的 JKS 文件通过 SOAPUI 访问 WS 端点时,收到 403.7 错误 - 禁止:需要 SSL 证书。针对同一端点使用带有 SOAPUI 的 P12 文件会产生成功的响应。 以下是将 P12 密钥库导入 JKS 密钥库的标准命令 -
keytool -importkeystore -srckeystore src.p12 -srcstoretype PKCS12 -deststoretype JKS -destkeystore target.jks
我还尝试使用 openssl 转换 P12 -> PEM -> DER -> JKS:
openssl pkcs12 -in src.p12 -out src.pem -clcerts
(将 src.pem 编辑为其两个复合部分,称为 src.key 和 src.cer)
openssl pkcs8 -topk8 -nocrypt -in src.key -out key.der -inform PEM -outform DER
openssl x509 -in src.cer -inform PEM -out cert.der -outform DER
(我运行了一个实用程序将两个密钥组合到 keystore.ImportKey 中)
keytool -importkeystore -srckeystore keystore.ImportKey -destkeystore target.JKS
同样没有骰子。
我有什么遗漏的吗?
如果您有 Keytool 应用程序和 PKCS#12 文件,请启动一行命令:
keytool -importkeystore -srckeystore [MY_FILE.p12] -srcstoretype pkcs12
-srcalias [ALIAS_SRC] -destkeystore [MY_KEYSTORE.jks]
-deststoretype jks -deststorepass [PASSWORD_JKS] -destalias [ALIAS_DEST]
您需要修改这些参数:
MY_FILE.p12
:表示要转换的PKCS#12文件(.p12或.pfx扩展名)的路径。MY_KEYSTORE.jks
:要存储证书的密钥库的路径。如果不存在,则会自动创建。PASSWORD_JKS
:打开密钥库时需要的密码。ALIAS_SRC
:与 PKCS#12 文件中的证书条目匹配的名称,例如“tomcat”。 如果您要从生成
.PFX
文件的 Windows 服务器导出证书,则必须检索 Windows 创建的“别名”名称。为此,您可以执行以下命令:
keytool -v -list -storetype pkcs12 -keystore FILE_PFX
“别名”字段表示您需要在命令行中使用的证书的存储名称。
ALIAS_DEST
:与 JKS 密钥库中的证书条目匹配的名称,例如“tomcat”。但是他问如何将.p12转换为JKS,所以答案是:
keytool -importkeystore -srckeystore mystore.p12 -destkeystore myotherstore.jks -srcstoretype PKCS12 -deststoretype jks -srcstorepass mystorepass -deststorepass myotherstorepass -srcalias myserverkey -destalias myotherserverkey -srckeypass mykeypass -destkeypass myotherkeypass
只需使用这条线,对我有用。
我很惊讶为什么这么长时间没有人回答这个问题。无论如何,将 p12 转换为 jks 的最简单方法是使用 Keytool。以下是您可能需要使用的命令:
keytool -importkeystore -srckeystore mystore.jck -destkeystore myotherstore.jks -srcstoretype jceks
-deststoretype jks -srcstorepass mystorepass -deststorepass myotherstorepass -srcalias myserverkey
-destalias myotherserverkey -srckeypass mykeypass -destkeypass myotherkeypass
我相信您遇到的问题可能是因为您没有提供Keypass。请注意,最好保持 keypass 和 storepass 相同,因为有时服务器无法区分 keypass 和 storepass。
这里有一些很好的答案,但我发现,在某些情况下,如果转换为 jks,某些 p12 证书可能无法在某些情况下工作。例如,我们使用 Java 11 在 keytool 中生成的 jks,对于在 Java 11 上的 tomcat 9 中运行的应用程序来说是可以的,但是以相同方式创建的证书在使用 java 17 的 Tomcat 10 中不起作用与旧的 javax 库以兼容模式运行,因此不清楚哪些库实际上如何用于从 Java 应用程序访问 jks 密钥库。
密钥工具和密钥库是使用哪个 Java 版本生成的也很重要。
但是我得到了信息,如果 PKCS12 ckeystore 曾经“准备好完全兼容 java” - 请不要问我,它是什么意思 :-) - 那么它就可以工作 - 似乎并非所有 p12 证书都是相同的。实际上,这也是我们的情况 - 因此,如果您需要将客户端证书存储在 jks 中以作为客户端访问某些远程服务,那么您可能会发现这个脚本很漂亮 - 它首先通过 pem 重新转换 p12 证书,然后创建jks 密钥库,然后还将密钥库中导入的密钥重命名为使用请求的别名命名。
#!/bin/bash
############### start of the execution ################
#Perform initial cleanup:
echo -e 'Cleanup of possibly existing temporary files...'
rm -f "${PKCS12_FILENAME}.pem"
rm -f "${PKCS12_FILENAME}.der"
#First delete also the result keystore if existing - there can be only one client certificate in client keystore, so any previous keystore content will be so deleted:
rm -f "${KEYSTORE_FILENAME}"
rm -f "JAVA_FULLY_COMPATIBLE_${KEYSTORE_FILENAME}"
echo -e 'Starting...'
#For export of the p12 certificate into pem format use following statement:
openssl pkcs12 -in "${PKCS12_FILENAME}" -out "${PKCS12_FILENAME}.pem" -descert -passin pass:"${PKCS12_PASSWORD}" -passout pass:"${PKCS12_PASSWORD}"
# Create Java-fully-compatible P12 certificate file:
openssl pkcs12 -export -out "JAVA_FULLY_COMPATIBLE_${PKCS12_FILENAME}" -in "${PKCS12_FILENAME}.pem" -passin pass:"${PKCS12_PASSWORD}" -passout pass:"${PKCS12_PASSWORD}"
#Now create the JKS keystore from the fully compatible PKCS12 certificate
${KEYTOOL_LOCATION}keytool -importkeystore -srckeystore "JAVA_FULLY_COMPATIBLE_${PKCS12_FILENAME}" -srcstoretype PKCS12 -srcstorepass "${PKCS12_PASSWORD}" -deststorepass "${KEYSTORE_PASSWORD}" -destkeypass "${PKCS12_PASSWORD}" -deststoretype ${KEYSTORE_TYPE} -destkeystore "${KEYSTORE_FILENAME}"
#Now rename the key in the keystore to the wished alias, the default created key is named "1"
${KEYTOOL_LOCATION}keytool -changealias -alias "1" -destalias "${ALIAS}" -keypass "${PKCS12_PASSWORD}" -keystore "${KEYSTORE_FILENAME}" -storepass "${KEYSTORE_PASSWORD}"
echo -e 'Keystore with following content created:'
${KEYTOOL_LOCATION}keytool -list -keystore "${KEYSTORE_FILENAME}" -storepass "${KEYSTORE_PASSWORD}"
echo -e 'Finished.'
例如这样调用(上面的脚本在文件
keystore-gen-tomcat-compatibility-mode.sh
中):
#!/bin/bash
# Created with the help of following sources:
# https://stackoverflow.com/questions/16244182/need-help-converting-p12-certificate-into-jks
# Customize following properties to meet your needs
KEYTOOL_LOCATION="/usr/lib/jdk11.0/bin/"
PKCS12_FILENAME="my p12 certificate.p12"
PKCS12_PASSWORD="pkcspwd"
# possible values here are {jks|pkcs12}:
KEYSTORE_TYPE=jks
KEYSTORE_FILENAME="java_keystore.jks"
KEYSTORE_PASSWORD="jkspwd"
ALIAS=${PKCS12_FILENAME}
#performs the generation based on corresponding variables:
. ../../../common/certs/keystore-gen-tomcat-compatibility-mode.sh