Skip to content

Commit

Permalink
Bug Fix: Avatars (and probably other media) do not display with accou…
Browse files Browse the repository at this point in the history
…nt on a self-signed server.

The MXMediaLoader adds now all the allowed certificates to the chain of trust at the time of preparing the SSL negotiation.

element-hq/element-ios#816
  • Loading branch information
giomfo committed Jul 19, 2017
1 parent 0a62bb3 commit 854595b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 6 additions & 1 deletion MatrixSDK/Utils/MXAllowedCertificates.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
The `MXAllowedCertificates` singleton.
*/
+ (id)sharedInstance;
+ (instancetype)sharedInstance;

/**
Add a certificate in the allowed list.
Expand All @@ -47,4 +47,9 @@
*/
- (void)reset;

/**
The current list of allowed certificates.
*/
@property (readonly) NSSet<NSData*> *certificates;

@end
1 change: 1 addition & 0 deletions MatrixSDK/Utils/MXAllowedCertificates.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ @interface MXAllowedCertificates ()
@end

@implementation MXAllowedCertificates
@synthesize certificates;

+ (MXAllowedCertificates *)sharedInstance
{
Expand Down
21 changes: 15 additions & 6 deletions MatrixSDK/Utils/Media/MXMediaLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,25 @@ - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticatio
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
// List all the allowed certificates to pin against.
NSMutableArray *pinnedCertificates = [NSMutableArray array];

NSSet <NSData *> *certificates = [AFSecurityPolicy certificatesInBundle:[NSBundle mainBundle]];
if (certificates && certificates.count > 0)
for (NSData *certificateData in certificates)
{
[pinnedCertificates addObject:(__bridge_transfer id)SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificateData)];
}
certificates = [MXAllowedCertificates sharedInstance].certificates;
for (NSData *certificateData in certificates)
{
[pinnedCertificates addObject:(__bridge_transfer id)SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificateData)];
}

if (pinnedCertificates.count > 0)
{
NSMutableArray *pinnedCertificates = [NSMutableArray array];
for (NSData *certificateData in certificates)
{
[pinnedCertificates addObject:(__bridge_transfer id)SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificateData)];
}
SecTrustSetAnchorCertificates(protectionSpace.serverTrust, (__bridge CFArrayRef)pinnedCertificates);
}

SecTrustRef trust = [protectionSpace serverTrust];

// Re-evaluate the trust policy
Expand Down

0 comments on commit 854595b

Please sign in to comment.