我正在尝试连接到运行godaddy 256位SSL证书的IIS6盒子,我收到错误:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Run Code Online (Sandbox Code Playgroud)
一直试图确定可能导致这种情况的原因,但现在正在绘制空白.
这是我如何连接:
HttpsURLConnection conn;
conn = (HttpsURLConnection) (new URL(mURL)).openConnection();
conn.setConnectTimeout(20000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
String tempString = toString(conn.getInputStream());
Run Code Online (Sandbox Code Playgroud) 我正在创建一个https用于与服务器通信的android应用程序.我正在使用retrofit并OkHttp提出请求.这些适用于标准http请求.以下是我遵循的步骤.
步骤1: 使用该命令从服务器获取cert文件
echo -n | openssl s_client -connect api.****.tk:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > gtux.cert
Run Code Online (Sandbox Code Playgroud)
步骤2: 使用以下命令将证书转换为BKS格式
keytool -importcert -v -trustcacerts -file "gtux.cert" -alias imeto_alias -keystore "my_keystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk16-146.jar" -storetype BKS
Run Code Online (Sandbox Code Playgroud)
它问我密码并且文件已成功创建.
第3步:
创建一个OkHttpClient并使用它来发出https请求
public class MySSLTrust {
public static OkHttpClient trustcert(Context context){
OkHttpClient okHttpClient = new OkHttpClient();
try {
KeyStore ksTrust = KeyStore.getInstance("BKS");
InputStream instream = context.getResources().openRawResource(R.raw.my_keystore);
ksTrust.load(instream, "secret".toCharArray());
// TrustManager decides which certificate authorities to …Run Code Online (Sandbox Code Playgroud) 我已经有一个 .pem 和一个 .key 文件,我不想在本地安装/导入它,只需告诉我的客户使用它们,这可能吗?它不是自签名证书
基本上,在我的卷曲中,我做了这样的事情:
curl --key mykey.key --cert mycert.pem https://someurl.com/my-endpoint
Run Code Online (Sandbox Code Playgroud)
我已经检查过这个答案
还有这个https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/CustomTrust.java(但可能没有意义,因为我没有得到该类型的对象我需要)
基本上我有我的 okHttpClient
val okHttpClient = OkHttpClient.Builder()
.sslSocketFactory(?, ?) //here I tried to call sslSocketFactory, trustManager following the example from the CustomTrust.java
.build()
Run Code Online (Sandbox Code Playgroud)
有什么解决方案的想法吗?
也检查了此文档,但 ssl 部分未完成,示例中也未完成
https://square.github.io/okhttp/https/#customizing-trusted-certificates-kt-java
所以我尝试这样做(基于 okhttp 示例)
private fun trustedCertificatesInputStream(): InputStream {
val comodoRsaCertificationAuthority = (""
+ "-----BEGIN CERTIFICATE-----\n" +
"-----END CERTIFICATE-----")
return Buffer()
.writeUtf8(comodoRsaCertificationAuthority)
.inputStream()
}
val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
fun createClient() : OkHttpClient …Run Code Online (Sandbox Code Playgroud)