fork of wolfSSL/MQTTS library with increased message buffer & increased number of subscribed topics

Dependencies:   FP MQTTPacket

Fork of MQTTS by wolf SSL

Files at this revision

API Documentation at this revision

Comitter:
wolfSSL
Date:
Sun Jul 26 06:10:10 2015 +0000
Parent:
44:c299463ae853
Child:
46:d8968fcc21b8
Commit message:
MQTT-TLS

Changed in this revision

MQTTPacket.lib Show annotated file Show diff for this revision Revisions of this file
MQTTSocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/MQTTPacket.lib	Mon Oct 06 11:41:05 2014 +0000
+++ b/MQTTPacket.lib	Sun Jul 26 06:10:10 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/mqtt/code/MQTTPacket/#bf36e077e7b8
+http://mbed.org/teams/mqtt/code/MQTTPacket/#99773f597e90
--- a/MQTTSocket.h	Mon Oct 06 11:41:05 2014 +0000
+++ b/MQTTSocket.h	Sun Jul 26 06:10:10 2015 +0000
@@ -3,37 +3,93 @@
 
 #include "MQTTmbed.h"
 #include "TCPSocketConnection.h"
+#include "wolfssl/ssl.h"
+#include    <wolfssl/wolfcrypt/error-crypt.h>
+
+static int SocketReceive(WOLFSSL* ssl, char *buf, int sz, void *sock)
+{
+    return ((TCPSocketConnection *)sock)->receive(buf, sz) ;
+}
+
+static int SocketSend(WOLFSSL* ssl, char *buf, int sz, void *sock)
+{
+    return ((TCPSocketConnection *)sock)->send(buf, sz);
+}
 
 class MQTTSocket
 {
-public:    
-    int connect(char* hostname, int port, int timeout=1000)
+public:
+    int connect(char* hostname, int port,  bool tls = false, int timeout=1000)
     {
-        mysock.set_blocking(false, timeout);    // 1 second Timeout 
-        return mysock.connect(hostname, port);
+
+        mysock.set_blocking(false, timeout);    // 1 second Timeout
+        isTLS = tls ;
+        int ret = mysock.connect(hostname, port);
+        if((ret == 0) && isTLS) {
+            return tls_connect(&mysock) ;
+        } else return ret ;
     }
-
+    
     int read(unsigned char* buffer, int len, int timeout)
     {
-        mysock.set_blocking(false, timeout);  
-        return mysock.receive((char*)buffer, len);
+        mysock.set_blocking(false, timeout);
+        return isTLS ?
+               wolfSSL_read(ssl, (char*)buffer, len) :
+               mysock.receive((char *)buffer, len) ;
     }
     
     int write(unsigned char* buffer, int len, int timeout)
     {
-        mysock.set_blocking(false, timeout);  
-        return mysock.send((char*)buffer, len);
+        mysock.set_blocking(false, timeout);
+        return isTLS ?
+               wolfSSL_write(ssl, (char*)buffer, len) :
+               mysock.send((char *)buffer, len) ;
     }
     
     int disconnect()
     {
+        if(isTLS) {
+            wolfSSL_free(ssl);
+            wolfSSL_CTX_free(ctx);
+            wolfSSL_Cleanup();
+        }
         return mysock.close();
     }
     
 private:
 
-    TCPSocketConnection mysock; 
-    
+    TCPSocketConnection mysock;
+    bool  isTLS ;
+    WOLFSSL_CTX* ctx;
+    WOLFSSL*     ssl;
+
+    int tls_connect(TCPSocketConnection *sock)
+    {
+        /* create and initiLize WOLFSSL_CTX structure */
+        if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
+            printf("SSL_CTX_new error.\n");
+            return EXIT_FAILURE;
+        }
+
+        wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
+        wolfSSL_SetIORecv(ctx, SocketReceive) ;
+        wolfSSL_SetIOSend(ctx, SocketSend) ;
+
+        if ((ssl = wolfSSL_new(ctx)) == NULL) {
+            printf("wolfSSL_new error.\n");
+            return EXIT_FAILURE;
+        }
+
+        wolfSSL_SetIOReadCtx(ssl, (void *)sock) ;
+        wolfSSL_SetIOWriteCtx(ssl, (void *)sock) ;
+
+        if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
+            printf("TLS Connect error, %s\n", wc_GetErrorString(wolfSSL_get_error(ssl, 0)));
+            return EXIT_FAILURE;
+        } else {
+            return 0 ;
+        }
+    }
 };