Skip to content

Commit 8ea4286

Browse files
committed
Merge branch 'invalid-ssl' of git://github.com/kcharwood/AFNetworking into kcharwood-invalid-ssl
2 parents 472ba1c + 22d6747 commit 8ea4286

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

AFNetworking/AFHTTPClient.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ typedef enum {
145145
@property (nonatomic, assign) AFURLConnectionOperationSSLPinningMode defaultSSLPinningMode;
146146
#endif
147147

148+
/**
149+
The flag to determine if each `AFHTTPRequestOperation` that is created in `HTTPRequestOperationWithRequest` should accept an invalid SSL certificate. If `_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_` is set, this property defaults to YES for backwards compatibility support. Otherwise, this property defaults to NO.
150+
*/
151+
@property (nonatomic,assign) BOOL allowInvalidSSLCertificate;
152+
148153
///---------------------------------------------
149154
/// @name Creating and Initializing HTTP Clients
150155
///---------------------------------------------

AFNetworking/AFHTTPClient.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ - (id)initWithBaseURL:(NSURL *)url {
263263
self.operationQueue = [[NSOperationQueue alloc] init];
264264
[self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
265265

266+
//This ifdef has been added for backwards compatibility purposes
267+
#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
268+
self.allowInvalidSSLCertificate = YES;
269+
#endif
270+
266271
return self;
267272
}
268273

@@ -536,6 +541,7 @@ - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlR
536541
#ifdef _AFNETWORKING_PIN_SSL_CERTIFICATES_
537542
operation.SSLPinningMode = self.defaultSSLPinningMode;
538543
#endif
544+
operation.allowInvalidSSLCertificate = self.allowInvalidSSLCertificate;
539545

540546
return operation;
541547
}

AFNetworking/AFURLConnectionOperation.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ NSCoding, NSCopying>
127127
*/
128128
@property (readonly, nonatomic, strong) NSError *error;
129129

130+
/**
131+
The flag to determine if the connection should accept an invalid SSL certificate. If `_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_` is set, this property defaults to YES for backwards compatibility support. Otherwise, this property defaults to NO.
132+
*/
133+
@property (nonatomic,assign) BOOL allowInvalidSSLCertificate;
134+
130135
///----------------------------
131136
/// @name Getting Response Data
132137
///----------------------------
@@ -280,7 +285,7 @@ NSCoding, NSCopying>
280285
281286
@param block A block object to be executed to determine whether the connection should be able to respond to a protection space's form of authentication. The block has a `BOOL` return type and takes two arguments: the URL connection object, and the protection space to authenticate against.
282287
283-
If `_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_` is defined, `connection:canAuthenticateAgainstProtectionSpace:` will accept invalid SSL certificates, returning `YES` if the protection space authentication method is `NSURLAuthenticationMethodServerTrust`.
288+
If `allowInvalidSSLCertificate` is set to YES, `connection:canAuthenticateAgainstProtectionSpace:` will accept invalid SSL certificates, returning `YES` if the protection space authentication method is `NSURLAuthenticationMethodServerTrust`.
284289
*/
285290
- (void)setAuthenticationAgainstProtectionSpaceBlock:(BOOL (^)(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace))block;
286291

@@ -289,7 +294,7 @@ NSCoding, NSCopying>
289294
290295
@param block A block object to be executed when the connection must authenticate a challenge in order to download its request. The block has no return type and takes two arguments: the URL connection object, and the challenge that must be authenticated.
291296
292-
If `_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_` is defined, `connection:didReceiveAuthenticationChallenge:` will attempt to have the challenge sender use credentials with invalid SSL certificates.
297+
If `allowInvalidSSLCertificate` is set to YES, `connection:didReceiveAuthenticationChallenge:` will attempt to have the challenge sender use credentials with invalid SSL certificates.
293298
*/
294299
- (void)setAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block;
295300

AFNetworking/AFURLConnectionOperation.m

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ - (id)initWithRequest:(NSURLRequest *)urlRequest {
258258

259259
self.state = AFOperationReadyState;
260260

261+
//This ifdef has been added for backwards compatibility purposes
262+
#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
263+
self.allowInvalidSSLCertificate = YES;
264+
#endif
265+
261266
return self;
262267
}
263268

@@ -606,25 +611,25 @@ - (void)connection:(NSURLConnection *)connection
606611
break;
607612
}
608613
case AFSSLPinningModeNone: {
609-
#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
610-
NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
611-
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
612-
#else
613-
SecTrustResultType result = 0;
614-
OSStatus status = SecTrustEvaluate(serverTrust, &result);
615-
NSAssert(status == errSecSuccess, @"SecTrustEvaluate error: %ld", (long int)status);
616-
617-
if (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed) {
614+
if(self.allowInvalidSSLCertificate == YES){
618615
NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
619616
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
620-
} else {
621-
[[challenge sender] cancelAuthenticationChallenge:challenge];
622617
}
623-
#endif
618+
else {
619+
SecTrustResultType result = 0;
620+
OSStatus status = SecTrustEvaluate(serverTrust, &result);
621+
NSAssert(status == errSecSuccess, @"SecTrustEvaluate error: %ld", (long int)status);
622+
623+
if (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed) {
624+
NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
625+
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
626+
} else {
627+
[[challenge sender] cancelAuthenticationChallenge:challenge];
628+
}
629+
}
624630
break;
625631
}
626632
}
627-
628633
}
629634
}
630635
#endif
@@ -633,11 +638,10 @@ - (void)connection:(NSURLConnection *)connection
633638
- (BOOL)connection:(NSURLConnection *)connection
634639
canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
635640
{
636-
#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
637-
if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
638-
return YES;
641+
if(self.allowInvalidSSLCertificate == YES &&
642+
[protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
643+
return YES;
639644
}
640-
#endif
641645

642646
if (self.authenticationAgainstProtectionSpace) {
643647
return self.authenticationAgainstProtectionSpace(connection, protectionSpace);
@@ -651,13 +655,14 @@ - (BOOL)connection:(NSURLConnection *)connection
651655
- (void)connection:(NSURLConnection *)connection
652656
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
653657
{
654-
#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
655-
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
658+
659+
if(self.allowInvalidSSLCertificate == YES
660+
&& [challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
656661
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
657662
return;
658663
}
659-
#endif
660-
664+
665+
661666
if (self.authenticationChallenge) {
662667
self.authenticationChallenge(connection, challenge);
663668
} else {
@@ -805,7 +810,8 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
805810
self.error = [aDecoder decodeObjectForKey:@"error"];
806811
self.responseData = [aDecoder decodeObjectForKey:@"responseData"];
807812
self.totalBytesRead = [[aDecoder decodeObjectForKey:@"totalBytesRead"] longLongValue];
808-
813+
self.allowInvalidSSLCertificate = [[aDecoder decodeObjectForKey:@"allowInvalidSSLCertificate"] boolValue];
814+
809815
return self;
810816
}
811817

@@ -829,6 +835,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
829835
[aCoder encodeObject:self.error forKey:@"error"];
830836
[aCoder encodeObject:self.responseData forKey:@"responseData"];
831837
[aCoder encodeObject:[NSNumber numberWithLongLong:self.totalBytesRead] forKey:@"totalBytesRead"];
838+
[aCoder encodeObject:[NSNumber numberWithBool:self.allowInvalidSSLCertificate] forKey:@"allowInvalidSSLCertificate"];
832839
}
833840

834841
#pragma mark - NSCopying
@@ -842,6 +849,7 @@ - (id)copyWithZone:(NSZone *)zone {
842849
operation.authenticationChallenge = self.authenticationChallenge;
843850
operation.cacheResponse = self.cacheResponse;
844851
operation.redirectResponse = self.redirectResponse;
852+
operation.allowInvalidSSLCertificate = self.allowInvalidSSLCertificate;
845853

846854
return operation;
847855
}

0 commit comments

Comments
 (0)