Skip to content

Commit

Permalink
fix: deprecation fix (#404)
Browse files Browse the repository at this point in the history
* fix: deprecation fix

* remove AMPURLConnection.m file

* refactor to save a bool var
  • Loading branch information
liuyang1520 authored Jul 18, 2022
1 parent aba104d commit 2f1203c
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 335 deletions.
40 changes: 0 additions & 40 deletions Amplitude.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions Sources/Amplitude/AMPURLConnection.h

This file was deleted.

168 changes: 0 additions & 168 deletions Sources/Amplitude/AMPURLConnection.m

This file was deleted.

8 changes: 5 additions & 3 deletions Sources/Amplitude/AMPUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,11 @@ + (CGFloat)statusBarHeight {

+ (UIWindow *)getKeyWindow {
if (@available(iOS 13.0, *)) {
for (UIWindow *window in [[AMPUtils getSharedApplication] windows]) {
if ([window isKeyWindow]) {
return window;
for (UIWindowScene *windowScene in [[AMPUtils getSharedApplication] connectedScenes]) {
for (UIWindow *window in [windowScene windows]) {
if ([window isKeyWindow]) {
return window;
}
}
}
return nil;
Expand Down
1 change: 0 additions & 1 deletion Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
#import "AMPConstants.h"
#import "AMPConfigManager.h"
#import "AMPDeviceInfo.h"
#import "AMPURLConnection.h"
#import "AMPURLSession.h"
#import "AMPDatabaseHelper.h"
#import "AMPUtils.h"
Expand Down
24 changes: 19 additions & 5 deletions Sources/Amplitude/ISPCertificatePinning.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,15 @@ + (BOOL)verifyPinnedCertificateForTrust:(SecTrustRef)trust andDomain:(NSString *
// Unfortunately the anchor/CA certificate cannot be accessed this way
CFIndex certsNb = SecTrustGetCertificateCount(trust);
for(int i=0;i<certsNb;i++) {

SecCertificateRef certificate = nil;
// Extract the certificate
SecCertificateRef certificate = SecTrustGetCertificateAtIndex(trust, i);
if (@available(macOS 12.0, iOS 15.0, *)) {
CFArrayRef certs = SecTrustCopyCertificateChain(trust);
certificate = (SecCertificateRef)CFArrayGetValueAtIndex(certs, i);
} else {
certificate = SecTrustGetCertificateAtIndex(trust, i);
}

NSData *DERCertificate = (__bridge NSData *)SecCertificateCopyData(certificate);

// Compare the two certificates
Expand All @@ -126,9 +132,17 @@ + (BOOL)verifyPinnedCertificateForTrust:(SecTrustRef)trust andDomain:(NSString *
}
SecTrustSetAnchorCertificates(trust, NULL);

SecTrustResultType trustResult;
SecTrustEvaluate(trust, &trustResult);
if (trustResult == kSecTrustResultUnspecified) {
BOOL isTrusted = false;
if (@available(iOS 12.0, macos 10.14, *)) {
CFErrorRef error;
isTrusted = SecTrustEvaluateWithError(trust, &error);
} else {
SecTrustResultType trustResult;
SecTrustEvaluate(trust, &trustResult);
isTrusted = trustResult == kSecTrustResultUnspecified;
}

if (isTrusted) {
// The anchor certificate was pinned
CFRelease(anchorCertificate);
return YES;
Expand Down
27 changes: 0 additions & 27 deletions Sources/Amplitude/ISPPinnedNSURLConnectionDelegate.h

This file was deleted.

49 changes: 0 additions & 49 deletions Sources/Amplitude/ISPPinnedNSURLConnectionDelegate.m

This file was deleted.

20 changes: 12 additions & 8 deletions Sources/Amplitude/ISPPinnedNSURLSessionDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,29 @@ - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticat

SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];
NSString *domain = [[challenge protectionSpace] host];
SecTrustResultType trustResult;

// Validate the certificate chain with the device's trust store anyway
// This *might* give use revocation checking
SecTrustEvaluate(serverTrust, &trustResult);
if (trustResult == kSecTrustResultUnspecified) {

BOOL isTrusted = false;
if (@available(iOS 12.0, macos 10.14, *)) {
CFErrorRef error;
isTrusted = SecTrustEvaluateWithError(serverTrust, &error);
} else {
SecTrustResultType trustResult;
SecTrustEvaluate(serverTrust, &trustResult);
isTrusted = trustResult == kSecTrustResultUnspecified;
}
if (isTrusted) {
// Look for a pinned certificate in the server's certificate chain
if ([ISPCertificatePinning verifyPinnedCertificateForTrust:serverTrust andDomain:domain]) {

// Found the certificate; continue connecting
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
else {
} else {
// The certificate wasn't found in the certificate chain; cancel the connection
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
}
else {
} else {
// Certificate chain validation failed; cancel the connection
completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
Expand Down

0 comments on commit 2f1203c

Please sign in to comment.