Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Use builtin __Require macros from AssertMacros.h
Browse files Browse the repository at this point in the history
The `AF_Require` and `AF_Require_noErr` macros were introduced in de8b20f for solving #2015. It turns out there exist exactly equivalent macros in the AssertMacros.h header, respectively `__Require_Quiet` and `__Require_noErr_Quiet`.
  • Loading branch information
0xced committed Dec 3, 2014
1 parent be30d70 commit cf4f46a
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions AFNetworking/AFSecurityPolicy.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,13 @@

#import "AFSecurityPolicy.h"

// Equivalent of macro in <AssertMacros.h>, without causing compiler warning:
// "'DebugAssert' is deprecated: first deprecated in OS X 10.8"
#ifndef AF_Require
#define AF_Require(assertion, exceptionLabel) \
do { \
if (__builtin_expect(!(assertion), 0)) { \
goto exceptionLabel; \
} \
} while (0)
#endif

#ifndef AF_Require_noErr
#define AF_Require_noErr(errorCode, exceptionLabel) \
do { \
if (__builtin_expect(0 != (errorCode), 0)) { \
goto exceptionLabel; \
} \
} while (0)
#endif
#import <AssertMacros.h>

#if !defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
static NSData * AFSecKeyGetData(SecKeyRef key) {
CFDataRef data = NULL;

AF_Require_noErr(SecItemExport(key, kSecFormatUnknown, kSecItemPemArmour, NULL, &data), _out);
__Require_noErr_Quiet(SecItemExport(key, kSecFormatUnknown, kSecItemPemArmour, NULL, &data), _out);

return (__bridge_transfer NSData *)data;

Expand Down Expand Up @@ -77,14 +59,14 @@ static id AFPublicKeyForCertificate(NSData *certificate) {
SecTrustResultType result;

allowedCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificate);
AF_Require(allowedCertificate != NULL, _out);
__Require_Quiet(allowedCertificate != NULL, _out);

allowedCertificates[0] = allowedCertificate;
tempCertificates = CFArrayCreate(NULL, (const void **)allowedCertificates, 1, NULL);

policy = SecPolicyCreateBasicX509();
AF_Require_noErr(SecTrustCreateWithCertificates(tempCertificates, policy, &allowedTrust), _out);
AF_Require_noErr(SecTrustEvaluate(allowedTrust, &result), _out);
__Require_noErr_Quiet(SecTrustCreateWithCertificates(tempCertificates, policy, &allowedTrust), _out);
__Require_noErr_Quiet(SecTrustEvaluate(allowedTrust, &result), _out);

allowedPublicKey = (__bridge_transfer id)SecTrustCopyPublicKey(allowedTrust);

Expand All @@ -111,7 +93,7 @@ static id AFPublicKeyForCertificate(NSData *certificate) {
static BOOL AFServerTrustIsValid(SecTrustRef serverTrust) {
BOOL isValid = NO;
SecTrustResultType result;
AF_Require_noErr(SecTrustEvaluate(serverTrust, &result), _out);
__Require_noErr_Quiet(SecTrustEvaluate(serverTrust, &result), _out);

isValid = (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);

Expand Down Expand Up @@ -142,10 +124,10 @@ static BOOL AFServerTrustIsValid(SecTrustRef serverTrust) {
CFArrayRef certificates = CFArrayCreate(NULL, (const void **)someCertificates, 1, NULL);

SecTrustRef trust;
AF_Require_noErr(SecTrustCreateWithCertificates(certificates, policy, &trust), _out);
__Require_noErr_Quiet(SecTrustCreateWithCertificates(certificates, policy, &trust), _out);

SecTrustResultType result;
AF_Require_noErr(SecTrustEvaluate(trust, &result), _out);
__Require_noErr_Quiet(SecTrustEvaluate(trust, &result), _out);

[trustChain addObject:(__bridge_transfer id)SecTrustCopyPublicKey(trust)];

Expand Down

0 comments on commit cf4f46a

Please sign in to comment.