react-native icon indicating copy to clipboard operation
react-native copied to clipboard

WebSocket certificates support

Open wielski opened this issue 4 years ago • 3 comments

Description

Is it possible to provide certificate for WebSocket connection, or ignore TLS error? I tried to provide rejectUnauthorized: false, but this options is not supported by react-native-websocket. I can't make any changes to server, cause my app should connect to IoT device.

React Native version:

System:
    OS: macOS 10.15.7
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Memory: 63.58 MB / 8.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 15.0.1 - /usr/local/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 7.0.3 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.0 - /Users/wielski/.rvm/gems/ruby-2.7.0/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.1, DriverKit 19.0, macOS 10.15, tvOS 14.0, watchOS 7.0
    Android SDK:
      API Levels: 29, 30
      Build Tools: 28.0.3, 29.0.2, 30.0.2
      System Images: android-30 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.1 AI-201.8743.12.41.6858069
    Xcode: 12.1/12A7403 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_212 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.3 => 0.63.3 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps To Reproduce

  1. Start server with self-signed certificates
  2. Try to connect by Websocket

Expected Results

I expecting WebSocket connections will be established, but it fails on certificate checking. On iOS in debug-mode everything working properly. In production build it fails. On android device it fails in debug-mode too.

Snack, code example, screenshot, or link to a repository:

new WebSocket(`wss://192.168.0.10:1916/`);

wielski avatar Nov 08 '20 16:11 wielski

I just copied default WebSocketModule and added certificate support. For iOS it should look like this, with some additions to specs (added "ca" string).

  NSMutableURLRequest *mutableRequest;
  mutableRequest = request.mutableCopy;

  if ([options.ca() isKindOfClass:NSString.class]) {
    NSRange r1 = [options.ca() rangeOfString:@"-----BEGIN CERTIFICATE-----"];
    NSRange r2 = [options.ca() rangeOfString:@"-----END CERTIFICATE-----"];
    NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);
    NSString *subCert = [options.ca() substringWithRange:rSub];

    NSData *rawCertificate = [[NSData alloc] initWithBase64Encoding:subCert];
    SecCertificateRef parsedCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)rawCertificate);

    NSArray* certArray = @[ (__bridge id)parsedCertificate ];

    [mutableRequest setRCTSR_SSLPinnedCertificates:certArray];
  }

  RCTSRWebSocket *webSocket = [[RCTSRWebSocket alloc] initWithURLRequest:mutableRequest protocols:protocols];

Java version I get here: https://github.com/Lipo11/react-native-wss/blob/master/patches/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java

It will be great if certificates support will be added to react-native by default.

wielski avatar Nov 09 '20 12:11 wielski

@wielski does it work?

FrozenPyrozen avatar Dec 04 '20 11:12 FrozenPyrozen

Any updates on this? I'd also like to be able to either bypass cert validation on wss or, even better, be able to provide pinned SSL certificates to validate against.

matallui avatar Jan 10 '23 00:01 matallui

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Sep 06 '23 05:09 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Sep 13 '23 05:09 github-actions[bot]