【问题标题】:BouncyCastle is required to read a key of type ecdsa-sha2-nistp256BouncyCastle 需要读取 ecdsa-sha2-nistp256 类型的密钥
【发布时间】:2017-02-01 04:01:42
【问题描述】:

我正在使用 sshj (0.19.1) 开发具有 SSH2 连接的 scala 应用程序。我可以在 eclipse 中正常连接,但是从一个胖 jar 运行应用程序,我遇到了一些错误。我最初的错误是:

Line 3: TransportException: null
 at net.schmizz.sshj.transport.TransportException$1.chain(33)
 at net.schmizz.sshj.transport.TransportException$1.chain(27)
 at net.schmizz.concurrent.Promise.deliverError(96)
 at net.schmizz.concurrent.Event.deliverError(74)
 at net.schmizz.concurrent.ErrorDeliveryUtil.alertEvents(34)
 at net.schmizz.sshj.transport.KeyExchanger.notifyError(386)
 at net.schmizz.sshj.transport.TransportImpl.die(596)
 at net.schmizz.sshj.transport.Reader.run(68)

我已将 US_export_policy.jar 和 local_policy.jar 添加到 $JAVA_HOME/lib/security。我添加了以下内容以尝试将 bouncycastle 注册为提供者,

import org.bouncycastle.jce.provider.BouncyCastleProvider
val bouncyCastle = new BouncyCastleProvider()
java.security.Security.addProvider(bouncyCastle)
net.schmizz.sshj.common.SecurityUtils.registerSecurityProvider("org.bouncycastle.jce.provider.BouncyCastleProvider")

但我仍然收到以下错误:

INFO  n.schmizz.sshj.common.SecurityUtils - Registration of Security Provider 'org.bouncycastle.jce.provider.BouncyCastleProvider' unexpectedly failed
INFO  n.schmizz.sshj.common.SecurityUtils - BouncyCastle not registered, using the default JCE provider
INFO  n.s.sshj.transport.random.JCERandom - Creating new SecureRandom.
WARN  net.schmizz.sshj.DefaultConfig - Illegal key size
WARN  net.schmizz.sshj.DefaultConfig - Cannot find any provider supporting Twofish/CBC/NoPadding
...
WARN  net.schmizz.sshj.DefaultConfig - Illegal key size or default parameters
WARN  net.schmizz.sshj.DefaultConfig - Disabling high-strength ciphers: cipher strengths apparently limited by JCE policy
INFO  n.s.sshj.transport.TransportImpl - Client identity string: SSH-2.0-SSHJ_0.19.1
INFO  n.s.sshj.transport.TransportImpl - Server identity string: SSH-2.0-OpenSSH_6.6.1
ERROR n.s.sshj.transport.TransportImpl - Dying because - {}
net.schmizz.sshj.common.SSHRuntimeException: BouncyCastle is required to read a key of type ecdsa-sha2-nistp256
    at net.schmizz.sshj.common.Buffer.readPublicKey(Buffer.java:431)
    at net.schmizz.sshj.transport.kex.AbstractDHG.next(AbstractDHG.java:66)
    at net.schmizz.sshj.transport.KeyExchanger.handle(KeyExchanger.java:358)
    at net.schmizz.sshj.transport.TransportImpl.handle(TransportImpl.java:493)
    at net.schmizz.sshj.transport.Decoder.decode(Decoder.java:104)
    at net.schmizz.sshj.transport.Decoder.received(Decoder.java:172)
    at net.schmizz.sshj.transport.Reader.run(Reader.java:60)
Caused by: java.security.GeneralSecurityException: BouncyCastle is required to read a key of type ecdsa-sha2-nistp256
    at net.schmizz.sshj.common.KeyType$3.readPubKeyFromBuffer(KeyType.java:120)
    at net.schmizz.sshj.common.Buffer.readPublicKey(Buffer.java:429)
    ... 6 common frames omitted
INFO  n.s.sshj.transport.TransportImpl - Disconnected - UNKNOWN
ERROR net.schmizz.concurrent.Promise - <<kex done>> woke to: net.schmizz.sshj.transport.TransportException: BouncyCastle is required to read a key of type ecdsa-sha2-nistp256

我以java -cp ../lib/bcprov-jdk15on-1.51.jar -jar &lt;my jar&gt; 的身份运行 jar,因为据我了解,您不能将 bouncycastle 作为提供程序的胖 jar 的一部分。但我不确定我错过了什么,我无法将其注册为提供者。任何帮助将不胜感激。

【问题讨论】:

    标签: java scala jar bouncycastle sshj


    【解决方案1】:

    java -jar 忽略来自命令行或 envvar 的类路径,而是使用指定的 jar 以及其清单中的任何 class-path 项。要么

    • reference jar 清单中的 bcprov(但不要不将像 bcprov 这样的提供程序合并到您自己的 jar 中,您是正确的)或

    • 将 bcprov 放入您的 JRE/lib/ext 中,这样 JVM 就可以在不使用类路径的情况下找到它。

    或使用确实使用类路径的java -cp myjar:bcprov mainclassname(Windows 上为;)运行。

    我希望您的 new BouncyCastleProvider() 在到达您的代码中尝试使用提供程序的点之前抛出,但我不知道 scala,也许它在这里有所不同。

    FWIW,如果此项目升级到 5 年前(Java 7),您将不需要 Bouncy for EC,包括 ECDSA。对于 Twofish,您仍然需要它,但我不知道谁(其他)实现了 Twofish,如果有人 需要 它,我会感到惊讶;您的服务器在此处标识为 OpenSSH 而不是。

    【讨论】:

    • 谢谢,我会试试看。当您提到 java7 时,这很有趣,因为我运行的是 1.8.0_121。我没有使用过gradle,所以我不是100%熟悉它,但是我注意到在github上的sshj build.gradle中,它使用了sourceCompatibility和targetCompatibility = 1.6。如果这就是我什至不得不跳过这些障碍的原因,我应该重新考虑使用那个库。
    • 谢谢。将 bcprov 复制到 JRE/lib/ext 就可以了。我将研究是否是 sshj 中的 sourceCompatibility = 1.6 一开始就需要这样做。如果是这样,那么我可能会切换到不同的 ssh 库,以便更轻松地将其推广给客户,而无需将额外的文件复制到他们的 java_home 中。
    • 来自作者的评论 :) (me): SSHJ 使用 Java6,因为仍有许多用户需要它。例如:github.com/hierynomus/sshj/pull/269.
    • 我实际上正在努力使安全提供程序(BC、JCE、SC 等)更容易切换,并且不再对 BC 有任何硬性要求。
    猜你喜欢
    • 1970-01-01
    • 2022-10-01
    • 2017-12-02
    • 1970-01-01
    • 2014-05-20
    • 2022-12-03
    • 2016-11-11
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多