它看起来像一个标准问题,但我无法在任何地方找到明确的方向.
我有java代码尝试连接到可能具有自签名(或过期)证书的服务器.代码报告以下错误:
[HttpMethodDirector] I/O exception (javax.net.ssl.SSLHandshakeException) caught
when processing request: sun.security.validator.ValidatorException: PKIX path
building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
Run Code Online (Sandbox Code Playgroud)
据我了解,我必须使用keytool并告诉java允许这种连接是可以的.
解决此问题的所有说明都假设我完全熟练使用keytool,例如
为服务器生成私钥并将其导入密钥库
有没有人可以发布详细说明?
我正在运行unix,所以bash脚本最好.
不确定它是否重要,但代码是在jboss中执行的.
我正在用Java编写一个应用程序,它通过HTTPS连接到两个Web服务器.一个通过默认的信任链获得证书,另一个使用自签名证书.当然,连接到第一台服务器是开箱即用的,而在我使用该服务器的证书创建一个trustStore之前,使用自签名证书连接到服务器不起作用.但是,与默认可信服务器的连接不再起作用,因为显然我创建自己的默认trustStore会被忽略.
我找到的一个解决方案是将证书从默认的trustStore添加到我自己的.但是,我不喜欢这个解决方案,因为它需要我继续管理那个trustStore.(我不能假设这些证书在可预见的未来仍然是静态的,对吧?)
除此之外,我发现了两个有着类似问题的5岁线程:
他们都深入到Java SSL基础架构.我希望到现在有一个更方便的解决方案,我可以在我的代码的安全审查中轻松解释.
我有两个应用程序在同一个java虚拟机中运行,并且都使用不同的密钥库和信任库.
一个可行的选择是使用单个密钥库并将所有其他密钥库导入共享密钥库(例如keytool -import),但如果我可以为在同一个jvm中运行的单独应用程序使用单独的密钥库,它将真正有助于我的要求.
我可以将keystore和truststore设置为jvm参数或系统属性,如下所示:
java -Djavax.net.ssl.keyStore=serverKeys
-Djavax.net.ssl.keyStorePassword=password
-Djavax.net.ssl.trustStore=serverTrust
-Djavax.net.ssl.trustStorePassword=password SSLApplication
Run Code Online (Sandbox Code Playgroud)
要么
System.setProperty("javax.net.ssl.keyStore","serverKeys")
Run Code Online (Sandbox Code Playgroud)
但是这种方法的问题在于它指定了要在JVM级别使用的密钥库/信任库,因此在同一JVM中运行的所有应用程序都获得相同的密钥库/信任库.
我还尝试创建自定义SSLContext并将其设置为默认值,但它还为在同一JVM中运行的所有应用程序设置上下文.
SSLContext context = SSLContext.getInstance("SSL");
context.init(kms, tms, null);
SSLContext.setDefault(context);
Run Code Online (Sandbox Code Playgroud)
我希望能够使用不同的密钥库/信任库而无需修改单个应用程序代码.
除了jre中的默认密钥库/证书之外,还可以动态注册多个密钥库的解决方案非常棒.
解决方案将以这种方式工作:
请让我知道您的想法或解决方案.提前致谢!
此代码连接到HTTPS站点,我假设我没有验证证书.但为什么我不必在本地为该网站安装证书?我不应该在本地安装证书并为此程序加载它还是在封面下载?客户端到远程站点之间的流量是否仍然在传输中加密?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class TestSSL {
public static void main(String[] args) throws Exception {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写集成测试,我们的测试使用Simple启动嵌入式HTTPS服务器.我创建了一个自签名证书,keytool
并且能够使用浏览器访问服务器(特别是Chrome,我收到有关自签名证书的警告).
但是,当我尝试使用Spring RestTemplate进行连接时,我得到一个ResourceAccessException:
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:8088":sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:502)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:444)
at net.initech.DummySslServer.shouldConnect(DummySslServer.java:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) …
Run Code Online (Sandbox Code Playgroud) 由于POODLE漏洞,我在Amazon AWS中托管的服务器不再支持SSLv3.
因此,我的Android应用程序对服务器执行的第一个HTTPS连接会在建立连接时导致错误.
Error reading server response: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x77d8ab68: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x7339ad74:0x00000000)
[....]
Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x77d8ab68: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x7339ad74:0x00000000)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:448)
at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
at com.android.okhttp.Connection.connect(Connection.java:107)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
Run Code Online (Sandbox Code Playgroud)
该错误仅在第一个请求中发生.后续请求工作了一段时间.
为了解决这个问题,我试图从Android客户端接受的协议列表中删除SSL,并确保我只使用TLS.为此,我设置了一个自定义SSLSocketFactory,它从启用的协议和支持的密码套件列表中删除SSL.
/**
* SSLSocketFactory that wraps one existing SSLSocketFactory and delegetes into it …
Run Code Online (Sandbox Code Playgroud) ssl android amazon-web-services sslsocketfactory poodle-attack
假设我编写了两个Java应用程序:Ping.jar
并且Pong.jar
它们在两个单独的服务器上部署和运行(Ping.jar
部署到srv-01.myorg.com
并Pong.jar
部署到srv-02.myorg.com
),这两个应用程序需要通过SSL相互通信(双向).我们还假设每个应用程序都有自己的SSL证书.
Ping
和Pong
验证彼此的SSL证书?每个CA是否提供某种我可以使用的RESTful API,比如说HttpClient
?Java是否有自己的证书验证API?是否有我可以使用的开源第三方JAR或服务?当我在网上搜索这个时,我感到很惊讶.
我需要通过SSL连接到LDAP目录.
在非生产环境中,我们使用自签名证书,当然,这些证书无法通过以下方式验证:
javax.naming.CommunicationException: simple bind failed: ldapserver:636 [Root exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]
at com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:197)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2694)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
Run Code Online (Sandbox Code Playgroud)
我知道如何使用自定义信任管理器进行启用SSL的连接,但不知道如何使用与JNDI API连接的连接,我不管理实际的连接.也就是说,以下标准设置在哪里可以插入信任管理器?
提前致谢.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://ldapserver:636");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "myUser");
env.put(Context.SECURITY_CREDENTIALS, "myPassword");
LdapContext ctx = new InitialLdapContext(env, null);
ctx.search (...)
Run Code Online (Sandbox Code Playgroud) 我是REST服务的新用户.我需要使用Jersey生成的RESTful API服务.问题出现是因为该服务托管在远程主机上,并且需要https访问证书.
我从组织获得了我的证书,并且我能够使用我的任何浏览器访问该REST API服务(其上设置了证书).
我在这里阅读了很多帖子,我已经按照这个主题的答案: 在Java中使用HTTPS和REST
现在我在我的Java Keystore上设置了证书.但我不知道如何在我的Java程序中使用它,所以它使用我需要进行https连接的证书.
这是我用于本地测试的简单连接代码.
package rest.test.first;
import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class TestClient{
public static void main(String[]args){
ClientConfig config= new DefaultClientConfig();
Client client=Client.create(config);
WebResource service=client.resource(getBaseURI());
//Fluentinterfaces
System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
//Getplaintext
System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(String.class));
//GetXML
System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_XML).get(String.class));
//TheHTML
System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_HTML).get(String.class));
}
private static URI getBaseURI(){
return UriBuilder.fromUri("http://localhost:8080/rest.test").build();
}
}
Run Code Online (Sandbox Code Playgroud)
我读到了使用系统集属性来指定密钥库的路径,使用以下代码:
System.setProperty("javax.net.ssl.keyStore", "/path/to/keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
Run Code Online (Sandbox Code Playgroud)
我仍然在连接到远程服务器时遇到401错误.
但后来我不知道如何在密钥库上使用我的证书进行SSL连接.我也一直在阅读有关使用sslSocketFactory的信息,但是我无法按照这篇文章的说明工作:如何在特定连接上使用不同的证书?
我已经设法使用此代码从密钥库中检索我的证书..现在我只需要知道如何在连接中使用它:
package rest.test.first;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
public class keystore { …
Run Code Online (Sandbox Code Playgroud) 我希望我的Java代码在一个密钥库中搜索服务器的CA证书...如果它无法找到特定的证书(我认为只有当我尝试通过LDAP连接到Directory Server时才会知道),它应该是在另一个密钥库中查找证书,我知道它的路径.
我试过这个:
System.setProperty("javax.net.ssl.trustStore", System.getProperty("java.home") + "/lib/security/cacerts" + System.getProperty("path.separator") + path/to/second/keystore);
但它似乎没有用.
只添加一个路径(其中任何一个)都可以工作,即如果找到证书则会像魅力一样运行,否则就会失败.
所以我的问题是:
是否有方法将多堆密钥库路径添加到javax.net.ssl.trustStore?
如果不可能我应该如何编写我的代码(我要求算法),以便它在第一次搜索后不会抛出异常而失败?
PS:我对Java不太熟悉.
以下是我的代码的相关部分:
if(useSSL)
{
try
{
SSLContext se = SSLContext.getInstance("TLS");
Security.addProvider(se.getProvider());
}
catch(NoSuchAlgorithmException e) { }
System.setProperty("javax.net.ssl.trustStore", System.getProperty("java.home") + "/lib/security/cacerts");
com.org.ldap.LDAPSocketFactory ssf = new LDAPJSSESecureSocketFactory();
LDAPConnection.setSocketFactory(ssf);
}
try
{
lc = new LDAPConnection();
lc.connect( ldapServer, ldapPort);
lc.bind( ldapVersion, ldapUser, (userInfo[1]).getBytes() );
}
catch (LDAPException le)
{
le.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) java ×9
ssl ×9
https ×3
certificate ×2
android ×1
cryptography ×1
jndi ×1
jsse ×1
jvm ×1
keystore ×1
keytool ×1
ldap ×1
rest ×1
resttemplate ×1
security ×1
self-signed ×1
spring ×1
truststore ×1