From fe5e4917cc0a65bf28ecdf6a24832fdb3d525fc1 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Wed, 12 Jul 2023 10:48:42 -0300 Subject: [PATCH] Swizzle kinda working? --- ios/RCTHTTPRequestHandler+Challenge.mm | 68 ++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/ios/RCTHTTPRequestHandler+Challenge.mm b/ios/RCTHTTPRequestHandler+Challenge.mm index 79a981fe4..8ec7d0639 100644 --- a/ios/RCTHTTPRequestHandler+Challenge.mm +++ b/ios/RCTHTTPRequestHandler+Challenge.mm @@ -6,6 +6,7 @@ // Copyright © 2023 Facebook. All rights reserved. // +#import #import "RCTHTTPRequestHandler+Challenge.h" #import "RNFetchBlobRequest.h" #import @@ -130,4 +131,71 @@ @end +@implementation SRWebSocket (Challenge) + +- (void)setClientSSL:(NSString *)path password:(NSString *)password options:(NSMutableDictionary *)options; +{ + if ([[NSFileManager defaultManager] fileExistsAtPath:path]) + { + NSData *pkcs12data = [[NSData alloc] initWithContentsOfFile:path]; + NSDictionary* certOptions = @{ (id)kSecImportExportPassphrase:password }; + CFArrayRef keyref = NULL; + OSStatus sanityChesk = SecPKCS12Import((__bridge CFDataRef)pkcs12data, + (__bridge CFDictionaryRef)certOptions, + &keyref); + if (sanityChesk == noErr) { + CFDictionaryRef identityDict = (CFDictionaryRef)CFArrayGetValueAtIndex(keyref, 0); + SecIdentityRef identityRef = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity); + SecCertificateRef cert = NULL; + OSStatus status = SecIdentityCopyCertificate(identityRef, &cert); + if (!status) { + NSArray *certificates = [[NSArray alloc] initWithObjects:(__bridge id)identityRef, (__bridge id)cert, nil]; + [options setObject:certificates forKey:(NSString *)kCFStreamSSLCertificates]; + } + } + } +} + ++(void)load +{ + Method original, swizzled; + + original = class_getInstanceMethod(objc_getClass("SRWebSocket"), @selector(_updateSecureStreamOptions)); + swizzled = class_getInstanceMethod(self, @selector(xxx_updateSecureStreamOptions)); + method_exchangeImplementations(original, swizzled); +} + +#pragma mark - Method Swizzling + +- (void)xxx_updateSecureStreamOptions { + [self xxx_updateSecureStreamOptions]; + NSLog(@"_updateSecureStreamOptions: %@", self); + + // Read the clientSSL info from MMKV + NSMutableDictionary *SSLOptions = [NSMutableDictionary new]; + __block NSString *clientSSL; + SecureStorage *secureStorage = [[SecureStorage alloc] init]; + + // https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/src/loader.js#L31 + NSString *key = [secureStorage getSecureKey:[Challenge stringToHex:@"com.MMKV.default"]]; + + if (key != NULL) { + NSData *cryptKey = [key dataUsingEncoding:NSUTF8StringEncoding]; + MMKV *mmkv = [MMKV mmkvWithID:@"default" cryptKey:cryptKey mode:MMKVMultiProcess]; + NSURLRequest *_urlRequest = [self valueForKey:@"_urlRequest"]; + + clientSSL = [mmkv getStringForKey:_urlRequest.URL.host]; + if (clientSSL) { + NSData *data = [clientSSL dataUsingEncoding:NSUTF8StringEncoding]; + id dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + NSString *path = [dict objectForKey:@"path"]; + NSString *password = [dict objectForKey:@"password"]; + [self setClientSSL:path password:password options:SSLOptions]; + } + } + + id _outputStream = [self valueForKey:@"_outputStream"]; + [_outputStream setProperty:SSLOptions forKey:(__bridge id)kCFStreamPropertySSLSettings]; +} + @end