有没有什么方法可以直接加载证书&以编程方式传输 SSLServerSocket (Java) 的私钥?

发布于 2024-11-16 01:10:52 字数 254 浏览 3 评论 0原文

我来自“C”背景。我正在尝试分析如何编写Java SSL服务器。

到目前为止,我发现的示例都讨论了将证书和私有密钥加载到密钥库以及以编程方式将密钥库关联到 SSLContext,然后从中创建 SSLServerSocket。

但是,是否有任何方法可以直接提及 SSLServerSocket 应使用哪个证书?另外,即使我使用密钥库,并且如果有多个证书和密钥,我如何指定 SSLServerSocket 应该使用哪一个?

任何示例链接都会有所帮助。

I am from a 'C' background. I am trying to analyze how to write a Java SSL Server.

So, far the examples I found talk about loading certificate and private to a Key Store and the programmatically associating the KeyStore to SSLContext and then create a SSLServerSocket from the same.

But, are there any methods for directly mentioning which certificate the SSLServerSocket should use? Also, even if I use keystore and if there are multiple certificates and keys how can I specify to SSLServerSocket which one it should use?

Any sample link will be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2024-11-23 01:10:52

正如我在评论中所说,在 回答之前的一个问题,您需要使用 KeyManager 来执行此操作。

有多种方法可以做到这一点。 jSSLutils 是一个可以使其更方便的库(但您也可以更手动地完成)。有一个使用 FixedServerAliasKeyManager 的示例。最短的方法之一是使用这样的东西:

X509SSLContextFactory sslContextFactory = new X509SSLContextFactory();
// By default, this would use the keystore passed with the usual system properties.

sslContextFactory.setKeyManagerWrapper(
    new FixedServerAliasKeyManager.Wrapper("the-alias-you-want"));
// You could read the alias name from a custom system property, for example.

SSLContext sslContext = sslContextFactory.buildSSLContext("TLS");
SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory();

As I was saying in a comment, in an answer to one of your questions earlier, you'll need to use a KeyManager to do this.

There are various ways of doing this. jSSLutils is a library that can make it a bit more convenient (but you could also do it more manually). There's an example with the FixedServerAliasKeyManager. One of the shortest ways to do it would be to use something like this:

X509SSLContextFactory sslContextFactory = new X509SSLContextFactory();
// By default, this would use the keystore passed with the usual system properties.

sslContextFactory.setKeyManagerWrapper(
    new FixedServerAliasKeyManager.Wrapper("the-alias-you-want"));
// You could read the alias name from a custom system property, for example.

SSLContext sslContext = sslContextFactory.buildSSLContext("TLS");
SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文