使用React Native时实现SSL证书固定

时间:2018-09-04 06:52:28

标签: android react-native ssl ssl-certificate

我无法使用React Native在Android上使用Web服务。 Web服务使用证书可以连接。 我尝试了各种解决方案,但没有成功。目前,我正在尝试使用以下代码:https://medium.com/@jaedmuva/react-native-ssl-pinning-is-back-e317e6682642,它也看起来像此链接中的代码:How can I implement SSL Certificate Pinning while using React Native

这是我的代码:

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
            new FastImageViewPackage(),
            new RNBackgroundFetchPackage(),
            new RNSqlite2Package(),
            new VectorIconsPackage()
      );
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    OkHttpClientProvider.setOkHttpClientFactory(new CustomClientFactory()); //  I added this line
  }
}

我创建一个CustomClientFactory类:

import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.ReactCookieJarContainer;
import java.util.concurrent.TimeUnit;
import okhttp3.CertificatePinner;
import okhttp3.OkHttpClient;
public class CustomClientFactory implements OkHttpClientFactory {
private static String hostname = "https://xxx.xxx.xx.xxx:xxxx/";
@Override
public OkHttpClient createNewNetworkModuleClient() {
    CertificatePinner certificatePinner = new CertificatePinner.Builder()
    .add(hostname, "sha256/1XIqmWJAm60di+5db9kkZxF1QGUyybLb0lCaOAnosNg=")
    //.add(hostname, "sha256/YOUR_PUBLIC_KEY_HASH_BACKUP1")
    //.add(hostname, "sha256/YOUR_PUBLIC_KEY_HASH_BACKUP2")
    .build();
    OkHttpClient.Builder client = new OkHttpClient.Builder()
    .connectTimeout(0, TimeUnit.MILLISECONDS)
    .readTimeout(0, TimeUnit.MILLISECONDS)
    .writeTimeout(0, TimeUnit.MILLISECONDS)
    .cookieJar(new ReactCookieJarContainer())
    .certificatePinner(certificatePinner);
    return OkHttpClientProvider.enableTls12OnPreLollipop(client).build();
  }
}

但是我仍然有这个错误:

09-03 15:56:13.013 4811-5139/com.mweb_dmc D/SettingsInterface:  from settings cache , name = sound_effects_enabled , value = 0
09-03 15:56:13.015 4811-5319/com.mweb_dmc D/libc-netbsd: [getaddrinfo]: hostname=xxx.xxx.xx.xxx; servname=(null); netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
09-03 15:56:13.015 4811-5319/com.mweb_dmc I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
09-03 15:56:13.016 4811-5319/com.mweb_dmc D/libc-netbsd: [getaddrinfo]: hostname=xxx.xxx.xx.xxx; servname=(null); netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
09-03 15:56:13.016 4811-5319/com.mweb_dmc I/System.out: [CDS]rx timeout:0
09-03 15:56:13.017 4811-5319/com.mweb_dmc I/System.out: [socket][20] connection /xxx.xxx.xx.xxx:1443;LocalPort=43588(0)
    [CDS]connect[/xxx.xxx.xx.xxx:1443]
09-03 15:56:13.017 4811-5319/com.mweb_dmc D/Posix: [Posix_connect Debug]Process com.mweb_dmc :1443 
09-03 15:56:13.113 4811-5319/com.mweb_dmc I/System.out: [CDS]port[43588]
    [socket][/192.168.1.91:43588] connected
09-03 15:56:13.114 4811-5319/com.mweb_dmc D/libc-netbsd: [getaddrinfo]: hostname=xxx.xxx.xx.xxx; servname=(null); netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
09-03 15:56:13.115 4811-5319/com.mweb_dmc D/NativeCrypto: ssl=0xaf53cd00 NativeCrypto_SSL_do_handshake fd=0x96f1cd20 shc=0x96f1cd24 timeout_millis=0 client_mode=1 npn=0x0
    doing handshake ++
    ssl=0xaf53cd00 info_callback where=0x10 ret=1
    ssl=0xaf53cd00 handshake start in CINIT  before connect initialization
    ssl=0xaf53cd00 info_callback calling handshakeCompleted
    ssl=0xaf53cd00 info_callback completed
    ssl=0xaf53cd00 info_callback where=0x1001 ret=1
    ssl=0xaf53cd00 SSL_connect:CINIT  before connect initialization
    ssl=0xaf53cd00 info_callback ignored
    ssl=0xaf53cd00 info_callback where=0x1001 ret=1
    ssl=0xaf53cd00 SSL_connect:3WCH_A SSLv3 write client hello A
    ssl=0xaf53cd00 info_callback ignored
    ssl=0xaf53cd00 info_callback where=0x1002 ret=-1
    ssl=0xaf53cd00 SSL_connect:error exit in 3RSH_A SSLv3 read server hello A
    ssl=0xaf53cd00 info_callback ignored
    doing handshake -- ret=-1
    ssl=0xaf53cd00 NativeCrypto_SSL_do_handshake ret=-1 errno=11 sslError=2 timeout_millis=0
09-03 15:56:13.194 4811-5319/com.mweb_dmc D/NativeCrypto: doing handshake ++
    ssl=0xaf53cd00 info_callback where=0x1001 ret=1
    ssl=0xaf53cd00 SSL_connect:3RSH_A SSLv3 read server hello A
    ssl=0xaf53cd00 info_callback ignored
09-03 15:56:13.194 4811-5319/com.mweb_dmc E/NativeCrypto: ssl=0xaf53cd00 cert_verify_callback x509_store_ctx=0x96f1cb4c arg=0x0
    ssl=0xaf53cd00 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
09-03 15:56:13.196 4811-5319/com.mweb_dmc D/NativeCrypto: ssl=0xaf53cd00 cert_verify_callback => 0
09-03 15:56:13.196 4811-5319/com.mweb_dmc D/OpenSSLLib: OpensslErr:Module:16(190:126); file:external/boringssl/src/ssl/s3_clnt.c ;Line:984;Function:ssl3_get_server_certificate
09-03 15:56:13.196 4811-5319/com.mweb_dmc D/NativeCrypto: ssl=0xaf53cd00 info_callback where=0x4008 ret=558
    ssl=0xaf53cd00 SSL3 alert write:F:CU fatal certificate unknown
    ssl=0xaf53cd00 info_callback ignored
    ssl=0xaf53cd00 info_callback where=0x1002 ret=-1
    ssl=0xaf53cd00 SSL_connect:error exit in 3RSC_B SSLv3 read server certificate B
    ssl=0xaf53cd00 info_callback ignored
    doing handshake -- ret=-1
    ssl=0xaf53cd00 NativeCrypto_SSL_do_handshake exception => 0
09-03 15:56:13.197 4811-5319/com.mweb_dmc I/System.out: close [socket][/192.168.1.91:43588]
    close [socket][/:::43588]
09-03 15:56:13.304 4811-5138/com.mweb_dmc I/ReactNativeJS: { [Error: Error Connection]
      line: 119541,
      column: 24,
      sourceURL: 'http://localhost:8081/index.delta?platform=android&dev=true&minify=false' }

编辑:

有关信息,我在IOS上也遇到了同样的问题。我通过在文件RCTHTTPRequestHandler.m中添加以下代码解决了该问题:

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
  completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}

0 个答案:

没有答案