简体   繁体   English

如何生成 Java PKCS12 存储以连接到常规 SSL 网站?

[英]How to generate Java PKCS12 store to connect to regular SSL website?

we run a standard web API over https with a regular purchased SSL certificate.我们在 https 上运行标准 web API 并定期购买 ZEA52C36203C5F99C3CE2442D523 证书。 Our clients just access it via https, the certificate is trusted via default system RootCA.我们的客户只需通过 https 访问它,该证书是通过默认系统 RootCA 信任的。

A new client is using a Java communication server that requires the certificate in a PKCS12 keystore.新客户端正在使用 Java 通信服务器,该服务器需要 PKCS12 密钥库中的证书。 How can we generate the PKS12 keystore from our key/csr/crt/pem files?我们如何从我们的 key/csr/crt/pem 文件生成 PKS12 密钥库?

I did some research, most examples are requiring a private key.我做了一些研究,大多数例子都需要私钥。 Of course I do not want to share our private key with the client.当然,我不想与客户共享我们的私钥。

Can a PKCS12 keystore be created without private key, similar to standard RootCA in browsers?是否可以在没有私钥的情况下创建 PKCS12 密钥库,类似于浏览器中的标准 RootCA?

Thanks, bluepuma谢谢,蓝豹

YES-ish.是的。

Although PFX-now-PKCS12 was designed primarily to store or transfer a privatekey and cert and chain cert(s) as a clump, and most commonly is used for that, it is capable of storing one or more 'lone' cert(s) not matched to any privatekey.尽管 PFX-now-PKCS12 主要设计用于将私钥证书以及链证书作为一个块存储或传输,并且最常用于此目的,但它能够存储一个或多个“单独”证书不匹配任何私钥。 And you are correct the client wanting to connect to you should have in their truststore ideally the root cert for your server cert's chain, or alternatively your server cert itself, but decidedly not your privatekey.你是正确的,想要连接到你的客户端应该在他们的信任库中拥有你的服务器证书链的根证书,或者你的服务器证书本身,但绝对不是你的私钥。

openssl can actually create such a PKCS12: openssl实际上可以创建这样一个PKCS12:

 openssl pkcs12 -export -in certfile.pem [-name $name] -nokeys -out blah.p12 
 # if you don't specify -name it defaults to 1 (the digit one) which can be mildly confusing
 # instead of being prompted for the password you can use -passout with several forms
 # described on the openssl man page, but consider the warning below

But the result won't work in Java if they use the standard (Sun/Oracle/OpenJDK) cryptoproviders, which (in 8+) support lone cert(s) in a PKCS12 as 'trustedCert' entries in Java only if the(each) certbag has a special Sun-defined attribute which the Java providers write when they create such a file, but OpenSSL doesn't.但是,如果他们使用标准(Sun/Oracle/OpenJDK)加密提供程序,则结果在 Java 中不起作用,该加密提供程序(在 8+ 中)支持 PKCS12 中的单独证书作为 Java 中的“trustedCert”条目,仅当) certbag 有一个特殊的 Sun 定义属性,Java 提供者在创建此类文件时会写入该属性,但 OpenSSL 没有。

Instead use Java's keytool in 8:而是在 8 中使用 Java 的 keytool:

jre8/bin/keytool -importcert -keystore blah.p12 -storetype pkcs12 -file $certfile [-alias $name]

or in 9+ where pkcs12 is now the default:或者在 9+ 中,pkcs12 现在是默认值:

jre9+/bin/keytool -importcert -keystore blah.p12 -file $certfile [-alias $name]

If you don't specify the alias it defaults to mykey .如果您不指定别名,则默认为mykey In both cases you can add -storepass $pw to avoid being prompted for it, but as a result the password will be visible on your screen, in the command history of your shell or other command processor it is has one, and in most cases to other processes run on your system concurrently, (any of) which may be a security issue or not.在这两种情况下,您都可以添加-storepass $pw以避免被提示,但因此密码将显示在屏幕上,在 shell 或其他命令处理器的命令历史记录中,它有一个,并且在大多数情况下到同时在您的系统上运行的其他进程,(任何)这可能是一个安全问题或不是。 You can also add -noprompt to avoid the confirmation prompt.您还可以添加-noprompt以避免确认提示。

But user207421 is (roughly) correct that using such a truststore can break other SSL/TLS connection(s) made from their system, at least from the same JVM, unless the individual calls specify individual, separate truststores, and if they had coded that they would know how to handle (and ask for) a simpler certificate format, such as PEM.但是 user207421 是(大致)正确的,即使用这样的信任库可以破坏从他们的系统建立的其他SSL/TLS 连接,至少来自同一个 JVM,除非个别调用指定单独的、单独的信任库,并且如果他们已经编码他们会知道如何处理(并要求)更简单的证书格式,例如 PEM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM