Library for MQTT
mbed-mqtt-master/src/MQTTNetworkTLS.h@0:ba7e439238ab, 2022-06-20 (annotated)
- Committer:
- pavleradojkovic
- Date:
- Mon Jun 20 16:24:43 2022 +0000
- Revision:
- 0:ba7e439238ab
Inital commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pavleradojkovic | 0:ba7e439238ab | 1 | /* |
pavleradojkovic | 0:ba7e439238ab | 2 | * Copyright (c) 2019, ARM Limited, All Rights Reserved |
pavleradojkovic | 0:ba7e439238ab | 3 | * SPDX-License-Identifier: Apache-2.0 |
pavleradojkovic | 0:ba7e439238ab | 4 | * |
pavleradojkovic | 0:ba7e439238ab | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
pavleradojkovic | 0:ba7e439238ab | 6 | * not use this file except in compliance with the License. |
pavleradojkovic | 0:ba7e439238ab | 7 | * You may obtain a copy of the License at |
pavleradojkovic | 0:ba7e439238ab | 8 | * |
pavleradojkovic | 0:ba7e439238ab | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
pavleradojkovic | 0:ba7e439238ab | 10 | * |
pavleradojkovic | 0:ba7e439238ab | 11 | * Unless required by applicable law or agreed to in writing, software |
pavleradojkovic | 0:ba7e439238ab | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
pavleradojkovic | 0:ba7e439238ab | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
pavleradojkovic | 0:ba7e439238ab | 14 | * See the License for the specific language governing permissions and |
pavleradojkovic | 0:ba7e439238ab | 15 | * limitations under the License. |
pavleradojkovic | 0:ba7e439238ab | 16 | */ |
pavleradojkovic | 0:ba7e439238ab | 17 | |
pavleradojkovic | 0:ba7e439238ab | 18 | #ifndef _MQTTNETWORKTLS_H_ |
pavleradojkovic | 0:ba7e439238ab | 19 | #define _MQTTNETWORKTLS_H_ |
pavleradojkovic | 0:ba7e439238ab | 20 | |
pavleradojkovic | 0:ba7e439238ab | 21 | #include "NetworkInterface.h" |
pavleradojkovic | 0:ba7e439238ab | 22 | #include "TLSSocket.h" |
pavleradojkovic | 0:ba7e439238ab | 23 | #include "MQTTNetworkUtil.h" |
pavleradojkovic | 0:ba7e439238ab | 24 | |
pavleradojkovic | 0:ba7e439238ab | 25 | #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) |
pavleradojkovic | 0:ba7e439238ab | 26 | |
pavleradojkovic | 0:ba7e439238ab | 27 | class MQTTNetworkTLS { |
pavleradojkovic | 0:ba7e439238ab | 28 | public: |
pavleradojkovic | 0:ba7e439238ab | 29 | MQTTNetworkTLS(NetworkInterface *aNetwork) : network(aNetwork) |
pavleradojkovic | 0:ba7e439238ab | 30 | { |
pavleradojkovic | 0:ba7e439238ab | 31 | socket = new TLSSocket(); |
pavleradojkovic | 0:ba7e439238ab | 32 | } |
pavleradojkovic | 0:ba7e439238ab | 33 | |
pavleradojkovic | 0:ba7e439238ab | 34 | ~MQTTNetworkTLS() |
pavleradojkovic | 0:ba7e439238ab | 35 | { |
pavleradojkovic | 0:ba7e439238ab | 36 | delete socket; |
pavleradojkovic | 0:ba7e439238ab | 37 | } |
pavleradojkovic | 0:ba7e439238ab | 38 | |
pavleradojkovic | 0:ba7e439238ab | 39 | int read(unsigned char *buffer, int len, int timeout) |
pavleradojkovic | 0:ba7e439238ab | 40 | { |
pavleradojkovic | 0:ba7e439238ab | 41 | return accumulate_mqtt_read(socket, buffer, len, timeout); |
pavleradojkovic | 0:ba7e439238ab | 42 | } |
pavleradojkovic | 0:ba7e439238ab | 43 | |
pavleradojkovic | 0:ba7e439238ab | 44 | int write(unsigned char *buffer, int len, int timeout) |
pavleradojkovic | 0:ba7e439238ab | 45 | { |
pavleradojkovic | 0:ba7e439238ab | 46 | return mqtt_write(socket, buffer, len, timeout); |
pavleradojkovic | 0:ba7e439238ab | 47 | } |
pavleradojkovic | 0:ba7e439238ab | 48 | |
pavleradojkovic | 0:ba7e439238ab | 49 | int connect(const char *hostname, int port, const char *ssl_ca_pem = NULL, |
pavleradojkovic | 0:ba7e439238ab | 50 | const char *ssl_cli_pem = NULL, const char *ssl_pk_pem = NULL) |
pavleradojkovic | 0:ba7e439238ab | 51 | { |
pavleradojkovic | 0:ba7e439238ab | 52 | int ret = socket->open(network); |
pavleradojkovic | 0:ba7e439238ab | 53 | if (ret < 0) { |
pavleradojkovic | 0:ba7e439238ab | 54 | return ret; |
pavleradojkovic | 0:ba7e439238ab | 55 | } |
pavleradojkovic | 0:ba7e439238ab | 56 | SocketAddress addr; |
pavleradojkovic | 0:ba7e439238ab | 57 | ret = network->gethostbyname(hostname, &addr); |
pavleradojkovic | 0:ba7e439238ab | 58 | if (ret < 0) { |
pavleradojkovic | 0:ba7e439238ab | 59 | return ret; |
pavleradojkovic | 0:ba7e439238ab | 60 | } |
pavleradojkovic | 0:ba7e439238ab | 61 | addr.set_port(port); |
pavleradojkovic | 0:ba7e439238ab | 62 | ret = socket->set_root_ca_cert(ssl_ca_pem); |
pavleradojkovic | 0:ba7e439238ab | 63 | if (ret < 0) { |
pavleradojkovic | 0:ba7e439238ab | 64 | return ret; |
pavleradojkovic | 0:ba7e439238ab | 65 | } |
pavleradojkovic | 0:ba7e439238ab | 66 | if (ssl_cli_pem != NULL && ssl_pk_pem != NULL) { |
pavleradojkovic | 0:ba7e439238ab | 67 | ret = socket->set_client_cert_key(ssl_cli_pem, ssl_pk_pem); |
pavleradojkovic | 0:ba7e439238ab | 68 | if (ret < 0) { |
pavleradojkovic | 0:ba7e439238ab | 69 | return ret; |
pavleradojkovic | 0:ba7e439238ab | 70 | } |
pavleradojkovic | 0:ba7e439238ab | 71 | } |
pavleradojkovic | 0:ba7e439238ab | 72 | return socket->connect(addr); |
pavleradojkovic | 0:ba7e439238ab | 73 | } |
pavleradojkovic | 0:ba7e439238ab | 74 | |
pavleradojkovic | 0:ba7e439238ab | 75 | int disconnect() |
pavleradojkovic | 0:ba7e439238ab | 76 | { |
pavleradojkovic | 0:ba7e439238ab | 77 | return socket->close(); |
pavleradojkovic | 0:ba7e439238ab | 78 | } |
pavleradojkovic | 0:ba7e439238ab | 79 | |
pavleradojkovic | 0:ba7e439238ab | 80 | int set_root_ca_cert(const char *root_ca_pem) |
pavleradojkovic | 0:ba7e439238ab | 81 | { |
pavleradojkovic | 0:ba7e439238ab | 82 | return socket->set_root_ca_cert(root_ca_pem); |
pavleradojkovic | 0:ba7e439238ab | 83 | } |
pavleradojkovic | 0:ba7e439238ab | 84 | |
pavleradojkovic | 0:ba7e439238ab | 85 | private: |
pavleradojkovic | 0:ba7e439238ab | 86 | NetworkInterface *network; |
pavleradojkovic | 0:ba7e439238ab | 87 | TLSSocket *socket; |
pavleradojkovic | 0:ba7e439238ab | 88 | }; |
pavleradojkovic | 0:ba7e439238ab | 89 | |
pavleradojkovic | 0:ba7e439238ab | 90 | #endif // defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) |
pavleradojkovic | 0:ba7e439238ab | 91 | #endif // _MQTTNETWORKTLS_H_ |