错误信息
java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
也可能输出为
- javax.net.ssl.SSLException: (internal_error) Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
- the trustAnchors parameter must be non-empty
JDK 25 报告为“javax.net.ssl.SSLException: (internal_error) Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty”,异常原因链中还有“java.lang.RuntimeException: Unexpected error: …”。如果信任库路径根本不存在,报出的则是“PKIX path building failed”。
Java (JSSE / PKIX) — the trust store the JVM loaded contains no trusted certificates
含义
JVM 加载的信任库中没有任何受信任证书条目,因此 PKIX 验证器连一个信任锚都没有,拒绝启动。这发生在检查任何证书之前:文件存在且能打开,但就 TLS 信任而言它是空的。该进程的每个 HTTPS 连接都会以同样的方式失败。
原因
把密钥库配置成了信任库
常见javax.net.ssl.trustStore 指向的文件只包含私钥条目(客户端或服务器自己的密钥),没有任何受信任证书条目。
JDK 的 cacerts 为空或已被替换
常见有些发行版的软件包会在安装时根据系统证书包生成 cacerts;安装后步骤失败、精简的容器镜像或损坏的符号链接,都会留下一个空的信任库。
为其他用途创建的密钥库文件
偶见新创建的密钥库文件(先 keytool -genkeypair 再 -delete,或者脚本生成的空 PKCS12),从未导入过 CA 证书。
修复方法
- 1.
统计受信任条目的数量
keytool -list 会显示其中的条目;信任库中必须有 trustedCertEntry 行。与 JDK 的默认信任库对比一下。
bash # How many trusted certificates does the store hold? (0 reproduces this error) keytool -list -keystore "$TRUSTSTORE" -storepass "$TRUSTSTORE_PASSWORD" | grep -c trustedCertEntry keytool -list -cacerts -storepass changeit | grep -c trustedCertEntry # the JDK's default store - 2.
恢复 JDK 的默认信任库
在 Debian 和 Ubuntu 上,重新安装 ca-certificates-java 会重新生成 /etc/ssl/certs/java/cacerts;在 Alpine 上,它由 java-cacerts 软件包提供。
bash # Debian/Ubuntu OpenJDK packages build cacerts from the system CA bundle sudo apt-get install --reinstall ca-certificates-java sudo update-ca-certificates -f # Alpine apk add --no-cache java-cacerts - 3.
把密钥库和信任库分开
通过 javax.net.ssl.keyStore 传入你自己的证书和密钥,通过 javax.net.ssl.trustStore 传入 CA 证书。
bash # Key store (your key + certificate) and trust store (CAs you trust) are different files java -Djavax.net.ssl.keyStore=client.p12 -Djavax.net.ssl.keyStorePassword=changeit \ -Djavax.net.ssl.trustStore=truststore.p12 -Djavax.net.ssl.trustStorePassword=changeit -jar app.jar