1

I'm working with SocketRocket, so far everything has been working fine and today I wanted to try to pin down a (self signed) certificate but I get an error:

- (void)connectWebSocket {
    webSocket.delegate = nil;
    webSocket = nil;

    NSString *urlString = [NSString stringWithFormat: @"wss://%@:%@", server_ip, server_port];

    //NSLog(@"%@", urlString);

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                             cachePolicy: NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:5.0];


    // pin down certificate
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"myOwnCertificate" ofType:@"cer"];
    NSData *certData = [[NSData alloc] initWithContentsOfFile:cerPath];
    CFDataRef certDataRef = (__bridge CFDataRef)certData;
    SecCertificateRef certRef = SecCertificateCreateWithData(NULL, certDataRef);
    id certificate = (__bridge id)certRef;
    [request setSR_SSLPinnedCertificates:@[certificate]];

    SRWebSocket *newWebSocket = [[SRWebSocket alloc] initWithURLRequest: request];
    newWebSocket.delegate = self;

    [newWebSocket open];

    socketIsOpen = true;

}

Error: No visible @interface for 'NSURLRequest' declares the selector 'setSR_SSLPinnedCertificates:'

Am I missing something?

Thanks!

Kostas
  • 367
  • 1
  • 3
  • 17

2 Answers2

0

You need #import "SRWebSocket.h".

SR_SSLPinnedCertificates is a property on the NSURLRequest (CertificateAdditions) category in https://github.com/square/SocketRocket/blob/master/SocketRocket/SRWebSocket.h

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39
0

The request needs to be a NSMutableURLRequest type. When your request is a NSURLRequest type, the SR_SSLPinnedCertificates is readonly, so you can NOT set it.

#pragma mark - NSURLRequest (CertificateAdditions)

@interface NSURLRequest (CertificateAdditions)

@property (nonatomic, retain, readonly) NSArray *SR_SSLPinnedCertificates;

@end

#pragma mark - NSMutableURLRequest (CertificateAdditions)

@interface NSMutableURLRequest (CertificateAdditions)

@property (nonatomic, retain) NSArray *SR_SSLPinnedCertificates;

@end

Change your code to this:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                             cachePolicy: NSURLRequestUseProtocolCachePolicy
                                         timeoutInterval:5.0];

This should works :)

Hanton
  • 616
  • 1
  • 6
  • 13