【问题标题】:Square okHTTP certificate pinning - sslSocketFactory errorSquare okHTTP 证书固定 - sslSocketFactory 错误
【发布时间】:2016-06-09 22:53:14
【问题描述】:

我正在尝试导入我自己的 BKS 文件,其中包含我的自签名证书,但我遇到了 okHTTP 问题。我想用 bks 文件来做这个,我也通过 sha512/ 让它工作。

我从几个教程中获得了这段代码,我知道问题出在哪里,但无法解决。

import android.content.Context;
import android.util.Log;

import java.io.InputStream;
import java.security.KeyStore;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Pinning
{
    Context context;
    public static String TRUST_STORE_PASSWORD = "your_secret";
    private static final String ENDPOINT = "https://api.yourdomain.com/";

public Pinning(Context c) {
    this.context = c;
}

private SSLSocketFactory getPinnedCertSslSocketFactory(Context context) {
    try {
        KeyStore trusted = KeyStore.getInstance("BKS");
        InputStream in = context.getResources().openRawResource(R.raw.mytruststore);
        trusted.load(in, "mypass".toCharArray());
        SSLContext sslContext = SSLContext.getInstance("TLS");
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
                TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trusted);
        sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
        return sslContext.getSocketFactory();
    } catch (Exception e) {
        Log.e("MyApp", e.getMessage(), e);
    }
    return null;
}


public void makeRequest() {
    try {
        OkHttpClient client = new OkHttpClient().sslSocketFactory(getPinnedCertSslSocketFactory(this.context));

        Request request = new Request.Builder()
                .url(ENDPOINT)
                .build();

        Response response = client.newCall(request).execute();

        Log.d("MyApp", response.body().string());

    } catch (Exception e) {
        Log.e("MyApp", e.getMessage(), e);

    }
}
}

现在我在以下行中收到以下错误:

OkHttpClient client = new OkHttpClient().sslSocketFactory(getPinnedCertSslSocketFactory(this.context)).;

错误:

okHTTPClient 中的 SSLSocketFactory 不能应用于 javax.net.SSLSocketFactory。

如果您查看导入内容,您会看到来自 javax 的以下 3 个。

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

所以我有一个错配。好像我需要一个 okHTTP - sslSocketFactory 和我的 bks 文件。但我找不到对此的有效解释。我也在使用 okHTTP 3.3.1

compile 'com.squareup.okhttp3:okhttp:3.3.1'

我在这里找到了解释/解决方案:How can I pin a certificate with Square OKHTTP?。正如先前所说。我想使用 bks 文件,而不是 sha512/ 方法。

因此我需要知道,如何将 javax.ssl.Factory 解析为 okHTTP-sslFactory 或如何使用生成的 bks 文件创建 okHTTPsslFactory。

此外,方法 setSSLSocketFactory 在 okHTTP 3.1.1 中不再可用。

非常感谢您的帮助,感谢您抽出宝贵时间来解决这个问题!

【问题讨论】:

    标签: android client-certificates okhttp3 pinning sslsocketfactory


    【解决方案1】:

    您应该改用以下语法

    OkHttpClient client = new OkHttpClient.Builder()
                 .sslSocketFactory(getPinnedCertSslSocketFactory(this.context))
                 .build();
    

    【讨论】:

    • 天哪,谢谢!我是迟钝!我忘了添加 .Builder()。真丢人:/
    猜你喜欢
    • 2014-07-23
    • 2018-04-07
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多