title: Eclipse Paho C Client SSL connection on iOS
body: <p>I've been trying for days to establish a secure (<strong>SSL/TLS</strong>) connection to an <strong>IBM MessageSight Virtual Appliance</strong> using the equivalent of the latest <strong>Eclipse Paho C Client</strong> library, which is <strong>IBM WebSphere MQ Client Pack MA9B for Mobile V1.0.0.4</strong>.</p>
  
  <h1>Client Side:</h1>
  
  <h2>Objective-C Code:</h2>
  
  <pre><code>...
  client = [client initWithHosts:hosts ports:ports clientId:clientId];
  
  ConnectOptions *opts = [[ConnectOptions alloc] init];
  opts.timeout         = 3600;
  /*
  opts.userName        = @"******";
  opts.password        = @"******";
   */
  opts.cleanSession    = cleanSession;
  opts.willMessage     = nil;
  
  NSBundle *mainBundle = [NSBundle mainBundle];
  NSString *ksFile     = [mainBundle pathForResource: @"ClientKeyStore" ofType: @"pem"];
  NSString *pkFile     = [mainBundle pathForResource: @"ClientKey" ofType: @"pem"];
  NSString *tsFile     = [mainBundle pathForResource: @"RootCAKey" ofType: @"pem"];
  
  if (DEBUG) {
      NSLog(@"Bundle         ==&gt; %@", mainBundle);
      NSLog(@"ClientKeyStore ==&gt; %@", ksFile);
      NSLog(@"ClientKey      ==&gt; %@", pkFile);
      NSLog(@"TrustStore     ==&gt; %@", tsFile);
  }
  
  SSLOptions *ssl          = [[SSLOptions alloc] init];
  ssl.enableServerCertAuth = NO;
  // ssl.enabledCipherSuites  = @"SHA2";
  ssl.keyStore             = ksFile;
  ssl.privateKey           = pkFile;
  ssl.privateKeyPassword   = @"******";
  ssl.trustStore           = tsFile;
  
  opts.sslProperties = ssl;
  
  [client connectWithOptions:opts invocationContext:self onCompletion:callback];
  </code></pre>
  
  <p>This is the Objective-C code I've used to interact with the C library through it's wrapper (MqttOCClient.h/m).</p>
  
  <h2>Now the specs:</h2>
  
  <ul>
  <li>iOS 7.1 onwards, Xcode 6.3.1 (Simulator).</li>
  <li>iOS 8.3, Xcode 6.3.1 (iPhone 5S).</li>
  <li>MQTT iOS Client: IBM WebSphere MQ Client Pack MA9B for Mobile V1.0.0.4 (<a href="http://www-01.ibm.com/support/knowledgecenter/?lang=en#!/SSFKSJ_7.5.0/com.ibm.mm.tc.doc/tc10120_.htm" rel="nofollow">http://www-01.ibm.com/support/knowledgecenter/?lang=en#!/SSFKSJ_7.5.0/com.ibm.mm.tc.doc/tc10120_.htm</a>).</li>
  </ul>
  
  <h2>Trace:</h2>
  
  <p>I've enabled trace mode on the library through the 2 Environment Variables:</p>
  
  <pre><code>MQTT_C_CLIENT_TRACE_LEVEL = MAXIMUM
  MQTT_C_CLIENT_TRACE       = ON
  </code></pre>
  
  <p>and this is the output:</p>
  
  <pre><code>D] &gt;&gt;MessagingClient::connectWithOptions
  I] first serverURI is ssl://example.com:1884
  =========================================================
                     Trace Output
  Product name: Paho Asynchronous MQTT C Client Library
  Version: ##MQTTCLIENT_VERSION_TAG##
  Build level: ##MQTTCLIENT_BUILD_TAG##
  OpenSSL version: OpenSSL 1.0.1h 5 Jun 2014
  OpenSSL flags: compiler: /Applications/Xcode.app/Contents/Developer/usr/bin/gcc -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk -miphoneos-version-min=7.1  -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -O3 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk -fomit-frame-pointer -fno-common
  OpenSSL build timestamp: built on: Thu Jun  5 14:59:07 BST 2014
  OpenSSL platform: platform: iphoneos-cross
  OpenSSL directory: OPENSSLDIR: "/tmp/openssl-1.0.1h-iOS-i386"
  =========================================================
  19691231 200000.000 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x7a068df0
  19691231 200000.000 (42856916)  (1)&gt; Socket_outInitialize:124
  19691231 200000.000 (42856916)   (2)&gt; SocketBuffer_initialize:85
  19691231 200000.000 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/SocketBuffer.c line 73 ptr 0x79f75480
  19691231 200000.000 Allocating 1008 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/SocketBuffer.c line 75 ptr 0x7a917000
  19691231 200000.000 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x79f791c0
  19691231 200000.000 (42856916)   (2)&lt; SocketBuffer_initialize:89
  19691231 200000.000 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x79f792a0
  19691231 200000.000 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x79f786f0
  19691231 200000.000 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x7a26ba90
  19691231 200000.000 (42856916)  (1)&lt; Socket_outInitialize:137
  19691231 200000.000 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x7a26bcb0
  19691231 200000.000 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x7a26ba20
  19691231 200000.000 (42856916)  (1)&gt; SSLSocket_initialize:398
  19691231 200000.000 Allocating 1808 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/SSLSocket.c line 414 ptr 0x7b8fd600
  ...
  20150514 130126.866 (42856916)   (2)&lt; SSL_create_mutex:313 (0)
  20150514 130126.866 (42856916)   (2)&gt; SSL_create_mutex:307
  ...
  20150514 130126.867 (42856916)  (1)&lt; SSLSocket_initialize:438 (1)
  20150514 130126.867 Allocating 144 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/MQTTAsync.c line 374 ptr 0x79f75f10
  20150514 130126.867 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/MQTTAsync.c line 386 ptr 0x79f75fb0
  20150514 130126.867 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x79ec7160
  20150514 130126.867 Allocating 16 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 93 ptr 0x79e78970
  20150514 130126.867 Allocating 96 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/MQTTAsync.c line 391 ptr 0x79ec1840
  20150514 130126.867 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x79ec1780
  20150514 130126.867 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x79ed00c0
  20150514 130126.867 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 56 ptr 0x79ec9090
  20150514 130126.867 Allocating 32 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/MQTTAsync.c line 397 ptr 0x79ec2110
  20150514 130126.867 (42856916)  (1)&gt; MQTTPersistence_create:47
  20150514 130126.867 (42856916)  (1)&lt; MQTTPersistence_create:93 (0)
  20150514 130126.867 (42856916)  (1)&gt; MQTTPersistence_initialize:108
  20150514 130126.867 (42856916)  (1)&lt; MQTTPersistence_initialize:116 (0)
  20150514 130126.867 (42856916)  (1)&gt; MQTTAsync_restoreCommands:666
  20150514 130126.867 0 commands restored for client 32c94ab93d29fda895b02f6
  20150514 130126.867 (42856916)  (1)&lt; MQTTAsync_restoreCommands:698 (0)
  20150514 130126.867 (42856916)  (1)&gt; MQTTAsync_restoreMessageQueue:1872
  20150514 130126.867 0 queued messages restored for client 32c94ab93d29fda895b02f6
  20150514 130126.867 (42856916)  (1)&lt; MQTTAsync_restoreMessageQueue:1903 (0)
  20150514 130126.867 Allocating 16 bytes in heap at file /Users/asm/workspace/Client/client_ios/iosMQTT/iosMQTT/mqttCClient/LinkedList.c line 93 ptr 0x79e7cef0
  20150514 130126.867 (42856916) (0)&lt; MQTTAsync_create:416 (0)
  2015-05-14 13:01:26.867 SmartBanking[2616:607] D] C Client created
  20150514 130126.867 (42856916) (0)&gt; MQTTAsync_setCallbacks:1658
  20150514 130126.867 (42856916) (0)&lt; MQTTAsync_setCallbacks:1672 (0)
  2015-05-14 13:01:26.868 SmartBanking[2616:607] D] Calling C client to make connection
  20150514 130126.867 (42856916) (0)&gt; MQTTAsync_connect:1990
  20150514 130126.867 (42856916) (0)&lt; MQTTAsync_connect:2177 (-8)
  E] C client connect failed
  Connection Error: ErrorCode=8  ErrorMessage=Unable to connect
  D] &lt;&lt;MessagingClient::connectWithOptions
  </code></pre>
  
  <h1>Server Side:</h1>
  
  <ul>
  <li>IBM MessageSight v1.1.0</li>
  <li>Server Certificate: 2048bits Self-Signed.</li>
  <li>Security is enabled and the Endpoint is up.</li>
  <li>User and Pass enabled.</li>
  </ul>
  
  <h1>Side Notes:</h1>
  
  <ul>
  <li>If I use a browser I can view the certificate, which means it can connect to the server.</li>
  <li>Works if I use Eclipse Paho for Java, validating the Server Certificate with the same TrustStore (which contains the Self-Signed CA).</li>
  </ul>
  
  <p>I will appreciate if somebody who has make an SSL connection to MQTT from iOS using this library can point me to the right direction.</p>
  
  <p>Regards, </p>