MQTT and MQTTS with wolfSSL TSL library

Dependencies:   FP MQTTPacket

Dependents:   YoPlegma

Fork of MQTT by MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSocket.h Source File

MQTTSocket.h

00001 #if !defined(MQTTSOCKET_H)
00002 #define MQTTSOCKET_H
00003 
00004 #include "MQTTmbed.h"
00005 #include "TCPSocketConnection.h"
00006 #include "wolfssl/ssl.h"
00007 #include "wolfssl/wolfcrypt/error-crypt.h"
00008 
00009 static int SocketReceive(WOLFSSL* ssl, char *buf, int sz, void *sock)
00010 {
00011     return ((TCPSocketConnection *)sock)->receive(buf, sz) ;
00012 }
00013 
00014 static int SocketSend(WOLFSSL* ssl, char *buf, int sz, void *sock)
00015 {
00016     return ((TCPSocketConnection *)sock)->send(buf, sz);
00017 }
00018 
00019 class MQTTSocket
00020 {
00021 public:
00022     int connect(char* hostname, int port,  const char *certName = NULL, int timeout=1000)
00023     {
00024 
00025         mysock.set_blocking(false, timeout);    // 1 second Timeout
00026         isTLS = certName == NULL ? false : true ;
00027         int ret = mysock.connect(hostname, port);
00028         if((ret == 0) && isTLS) {
00029             return tls_connect(&mysock, certName) ;
00030         } else return ret ;
00031     }
00032     
00033     int read(unsigned char* buffer, int len, int timeout)
00034     {
00035         mysock.set_blocking(false, timeout);
00036         return isTLS ?
00037                wolfSSL_read(ssl, (char*)buffer, len) :
00038                mysock.receive((char *)buffer, len) ;
00039     }
00040     
00041     int write(unsigned char* buffer, int len, int timeout)
00042     {
00043         mysock.set_blocking(false, timeout);
00044         return isTLS ?
00045                wolfSSL_write(ssl, (char*)buffer, len) :
00046                mysock.send((char *)buffer, len) ;
00047     }
00048     
00049     int disconnect()
00050     {
00051         if(isTLS) {
00052             wolfSSL_free(ssl);
00053             wolfSSL_CTX_free(ctx);
00054             wolfSSL_Cleanup();
00055         }
00056         return mysock.close();
00057     }
00058 
00059 private:
00060 
00061     TCPSocketConnection mysock;
00062     bool  isTLS ;
00063     WOLFSSL_CTX* ctx;
00064     WOLFSSL*     ssl;
00065     
00066     int tls_connect(TCPSocketConnection *sock, const char *certName)
00067     {
00068         /* create and initiLize WOLFSSL_CTX structure */
00069         if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) {
00070             printf("SSL_CTX_new error.\n");
00071             return EXIT_FAILURE;
00072         }
00073         if(*certName == '\0'){
00074             wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
00075         } else {
00076             if (wolfSSL_CTX_load_verify_locations(ctx, certName,0) != SSL_SUCCESS)
00077                 printf("can't load ca file\n");
00078         }
00079         
00080         wolfSSL_SetIORecv(ctx, SocketReceive) ;
00081         wolfSSL_SetIOSend(ctx, SocketSend) ;
00082 
00083         if ((ssl = wolfSSL_new(ctx)) == NULL) {
00084             printf("wolfSSL_new error.\n");
00085             return EXIT_FAILURE;
00086         }
00087 
00088         wolfSSL_SetIOReadCtx(ssl, (void *)sock) ;
00089         wolfSSL_SetIOWriteCtx(ssl, (void *)sock) ;
00090 
00091         if (wolfSSL_connect(ssl) != SSL_SUCCESS) {
00092             printf("TLS Connect error, %s\n", wc_GetErrorString(wolfSSL_get_error(ssl, 0)));
00093             return EXIT_FAILURE;
00094         } else {
00095             return 0 ;
00096         }
00097     }
00098 };
00099 
00100 
00101 
00102 #endif