From a2883dddc054cb30c7c853867078c0968b0f0006 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Mon, 20 Apr 2015 14:28:32 -0500 Subject: [PATCH] #1306 - Adding api_mapbox_com.der (public key) and nsurlconnection checking infrastructure --- common/api_mapbox_com.der | Bin 0 -> 1969 bytes gyp/certificates.gypi | 2 +- include/mbgl/ios/MGLMapboxEvents.h | 2 +- platform/ios/MGLMapboxEvents.m | 56 +++++++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 common/api_mapbox_com.der diff --git a/common/api_mapbox_com.der b/common/api_mapbox_com.der new file mode 100644 index 0000000000000000000000000000000000000000..116144bc0ccb71925a4354b5bb2d98f98d8ef35c GIT binary patch literal 1969 zcmXqLVqa^}#5Q#SGZP~dlYm;9_(>P`_E=qm_4##0|2`VlPTyrC1Q%h6JD1#;k_O_)f=nVDE~z<*<%vb9hO!3IAY1u`MFNU4%Mwdc75t0R6Z0~w5=%1k^9%(H z_(0OE!tAC-=Ei2mhNNpi@jTQz4iOQT%;J)w%;XY<{4@pU{G8Iw(s>i*SV} z7H4GUrI!HRAZj2C4lofO-^7BX{0bdNc!?W`g2PLMFR>s~FBdGOmz>Ac;zIX!Ndl2eb6q{;Wf zi>l8mE^(M1S*afN>s0moTlqmiQ>WRvnq|GTs}u#4{7Orl{V$r z2b+ZHo@*Yx$v>@?emnC2T$5>6Ta2>}?XFw8vgtf|mvX7?`ULYug65o(e-5}dSZ~!jbl@n^L{dqgc0Gb01z;wI*+22IQt4H{1ZLs3?crSYIaY>J2lRy%NNHT^<3c3g%swv=HCM(Rs zWWWGUwzB+;jQ?3!n3-4?7-)lhDbJ!}pk$!1KyHCdn^ZsSu?AbVgd{$O=CPp!1kTJzTbIQxh z_0m)GOF$VKY*tZfaeir0a%!=DQEEYcab`(=QKf!PYIE zH9(CqU~Cgc4Vn~iu2O?5V8Ro2CBR%(%fi`&~ zVz9OD2Chts41awmnan%JuCRKi|Ac?1Ip&$(#SLHYnatN!EU*&fd*g&$^Z{!hVA03OaQxqqIlq@) zshf45>20;I%+;$qKF93pF`MKh=(hAhE_>tG*q2;lKa*CR4KOjVf$NrXKa&-*7OSInh>C1}v$L&Qm)CQSr}Kf-XLsDzPM5xOB_MoC zs>CZjAOEc^cM}A39O4R3-Chx-wQEr;M_q&QmV%qsUnkpTFdlG@ntN5M`^wTX_wY$S z%6G0(DDFw@bvACvIIG-yLxoAxa>u-;l{JY;&NYSqIJDf{^<6`*wYOa4iPAdaayRet ztNi`_6ZUJSpUPiUf9PA+tHSyPyCb;8C-X#pKC=AogPe{xd#0RkF|Ay2R?PlzkN=A~ Hi>?9yu%5e9 literal 0 HcmV?d00001 diff --git a/gyp/certificates.gypi b/gyp/certificates.gypi index 1d0c2acf8..fdd818b8d 100644 --- a/gyp/certificates.gypi +++ b/gyp/certificates.gypi @@ -4,7 +4,7 @@ 'type': 'none', 'hard_dependency': 1, 'copies': [{ - 'files': [ '../common/ca-bundle.crt' ], + 'files': [ '../common/api_mapbox_com.der', '../common/ca-bundle.crt' ], 'destination': '<(PRODUCT_DIR)' }], }, diff --git a/include/mbgl/ios/MGLMapboxEvents.h b/include/mbgl/ios/MGLMapboxEvents.h index b68b9b1b3..2c6f8151b 100644 --- a/include/mbgl/ios/MGLMapboxEvents.h +++ b/include/mbgl/ios/MGLMapboxEvents.h @@ -25,7 +25,7 @@ extern NSString *const MGLEventGesturePanStart; extern NSString *const MGLEventGesturePinchStart; extern NSString *const MGLEventGestureRotateStart; -@interface MGLMapboxEvents : NSObject +@interface MGLMapboxEvents : NSObject // You must call these methods from the main thread. // diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index e4014d494..62d4d73fb 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -395,9 +395,12 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; [request setHTTPBody:jsonData]; // Send non blocking HTTP Request to server - [NSURLConnection sendAsynchronousRequest:request - queue:nil - completionHandler:nil]; + NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:weakSelf]; + [conn start]; + +// [NSURLConnection sendAsynchronousRequest:request +// queue:nil +// completionHandler:nil]; } }); } @@ -638,4 +641,51 @@ NSString *const MGLEventGestureRotateStart = @"Rotation"; return result; } +#pragma mark NSURLConnectionDelegate +- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { + + // Load Local Copy of Server's Public Key + // NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"b.ssl.fastly.net" ofType:@"der"]; + NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"api_mapbox_com" ofType:@"der"]; + NSData *localCertData = [NSData dataWithContentsOfFile:cerPath]; + + // Get Server's Public Key + SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; + + SecTrustResultType trustResult; + SecTrustEvaluate(serverTrust, &trustResult); + switch (trustResult) { + case kSecTrustResultProceed: + break; + case kSecTrustResultConfirm: + break; + case kSecTrustResultUnspecified: + break; + default: + [[challenge sender] cancelAuthenticationChallenge:challenge]; + return; + } + + long numKeys = SecTrustGetCertificateCount(serverTrust); + NSLog(@"Number of certificaties presented: %ld", numKeys); + + BOOL found = false; + for (int lc = 0; lc < numKeys; lc++) { + SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, lc); + NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate)); + + // Compare Remote Key With Local Version + if ([remoteCertificateData isEqualToData:localCertData]) { + NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust]; + [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; + found = true; + break; + } + } + + if (!found) { + [[challenge sender] cancelAuthenticationChallenge:challenge]; + } +} + @end