Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTNetworkTLS.h Source File

MQTTNetworkTLS.h

00001 /*
00002  * Copyright (c) 2019, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef _MQTTNETWORKTLS_H_
00019 #define _MQTTNETWORKTLS_H_
00020 
00021 #include "NetworkInterface.h"
00022 #include "TLSSocket.h"
00023 
00024 #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
00025 
00026 class MQTTNetworkTLS {
00027 public:
00028     MQTTNetworkTLS(NetworkInterface *aNetwork) : network(aNetwork)
00029     {
00030         socket = new TLSSocket();
00031     }
00032 
00033     ~MQTTNetworkTLS()
00034     {
00035         delete socket;
00036     }
00037 
00038     int read(unsigned char *buffer, int len, int timeout)
00039     {
00040         int ret = socket->recv(buffer, len);
00041         if (ret == 0) {
00042             // A receive size of 0 indicates that the socket
00043             // was successfully closed so indicate this to MQTTClient
00044             ret = -1;
00045         }
00046         return ret;
00047     }
00048 
00049     int write(unsigned char *buffer, int len, int timeout)
00050     {
00051         int ret = socket->send(buffer, len);
00052         if (ret == 0) {
00053             // The socket is closed so indicate this to MQTTClient
00054             return -1;
00055         }
00056         return ret;
00057     }
00058 
00059     int connect(const char *hostname, int port, const char *ssl_ca_pem = NULL,
00060                 const char *ssl_cli_pem = NULL, const char *ssl_pk_pem = NULL)
00061     {
00062         int ret = socket->open(network);
00063         if (ret < 0) {
00064             return ret;
00065         }
00066         SocketAddress addr;
00067         ret = network->gethostbyname(hostname, &addr);
00068         if (ret < 0) {
00069             return ret;
00070         }
00071         addr.set_port(port);
00072         ret = socket->set_root_ca_cert(ssl_ca_pem);
00073         if (ret < 0) {
00074             return ret;
00075         }
00076         if (ssl_cli_pem != NULL && ssl_pk_pem != NULL) {
00077             ret = socket->set_client_cert_key(ssl_cli_pem, ssl_pk_pem);
00078             if (ret < 0) {
00079                 return ret;
00080             }
00081         }
00082         return socket->connect(addr);
00083     }
00084 
00085     int disconnect()
00086     {
00087         return socket->close();
00088     }
00089 
00090     int set_root_ca_cert(const char *root_ca_pem)
00091     {
00092         return socket->set_root_ca_cert(root_ca_pem);
00093     }
00094 
00095 private:
00096     NetworkInterface *network;
00097     TLSSocket *socket;
00098 };
00099 
00100 #endif // defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY)
00101 #endif // _MQTTNETWORKTLS_H_