nodemcu-firmware/app/include/sys/espconn_mbedtls.h
Nathaniel Wesley Filardo af426d0315 Networking rampage and accumulated fixes (#3060)
* espconn: remove unused espconn code, take 1

This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 .
It removes a bunch of functions that were never called in our tree.

* espconn: De-orbit espconn_gethostbyname

Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004

While here, remove `mqtt`'s charming DNS-retry logic (which is neither
shared with nor duplicated in other modules) and update its :connect()
return value behavior and documentation.

* espconn: remove scary global pktinfo

A write-only global!  How about that.

* net: remove deprecated methods

All the TLS stuff moved over there a long time ago, and
net_createUDPSocket should just do what it says on the tin.

* espconn_secure: remove ESPCONN_SERVER support

We can barely function as a TLS client; being a TLS server seems like a
real stretch.  This code was never called from Lua anyway.

* espconn_secure: more code removal

* espconn_secure: simplify ssl options structure

There is nothing "ssl_packet" about this structure.  Get rid of the
terrifying "pbuffer" pointer.

Squash two structure types together and eliminate an unused field.

* espconn_secure: refactor mbedtls_msg_info_load

Split out espconn_mbedtls_parse, which we can use as part of our effort
towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032

* espconn_secure: introduce TLS cert/key callbacks

The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032
Subsequent work will remove the old mechanism.

* tls: add deprecation warnings

* luacheck: net.ifinfo is a thing now

* tls: remove use of espconn->reverse

* mqtt: stop using espconn->reverse

Instead, just place the espconn structure itself at the top of the user
data.  This enlarges the structure somewhat but removes one more layer
of dynamic heap usage and NULL checks.

While here, simplify the code a bit.

* mqtt: remove redundant pointer to connect_info

Everywhere we have the mqtt_state_t we also have the lmqtt_userdata.

* mqtt: doc fixes

* mqtt: note bug

* tls: allow :on(...,nil) to unregister a callback
2020-06-09 22:26:52 +02:00

223 lines
7.4 KiB
C

/*
* ESPRSSIF MIT License
*
* Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef ESPCONN_MBEDTLS_H_
#define ESPCONN_MBEDTLS_H_
#include "lwip/ip.h"
#include "lwip/app/espconn.h"
#include "user_interface.h"
#if !defined(ESPCONN_MBEDTLS)
#include "mbedtls/net_sockets.h"
#include "mbedtls/debug.h"
#include "mbedtls/ssl.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
typedef struct espconn *pmbedtls_espconn;
typedef struct espconn mbedtls_espconn;
typedef struct{
// mbedtls_entropy_context entropy;
mbedtls_x509_crt cacert;
mbedtls_x509_crt clicert;
mbedtls_pk_context pkey;
}mbedtls_session, *pmbedtls_session;
typedef struct{
bool quiet;
int record_len;
pmbedtls_session psession;
mbedtls_net_context fd;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_entropy_context entropy;
bool SentFnFlag;
sint32 verify_result;
}mbedtls_msg, *pmbedtls_msg;
typedef enum {
ESPCONN_CERT_OWN,
ESPCONN_CERT_AUTH,
ESPCONN_PK,
}mbedtls_auth_type;
typedef enum {
ESPCONN_IDLE = 0,
ESPCONN_CLIENT,
ESPCONN_MAX
}espconn_level;
typedef struct _file_head{
char file_name[32];
uint16_t file_length;
}file_head;
typedef struct _file_param{
file_head file_head;
int32 file_offerset;
}file_param;
typedef struct _ssl_sector{
uint32 sector;
bool flag;
}ssl_sector;
struct ssl_options {
uint16 buffer_size;
ssl_sector cert_ca_sector;
ssl_sector cert_req_sector;
int cert_verify_callback;
int cert_auth_callback;
};
#define SSL_KEEP_INTVL 1
#define SSL_KEEP_CNT 5
#define SSL_KEEP_IDLE 90
#define ssl_keepalive_enable(pcb) ((pcb)->so_options |= SOF_KEEPALIVE)
#define ssl_keepalive_disable(pcb) ((pcb)->so_options &= ~SOF_KEEPALIVE)
enum {
SIG_ESPCONN_TLS_ERRER = 0x3B
};
#define ESPCONN_SECURE_MAX_SIZE 8192
#define ESPCONN_SECURE_DEFAULT_HEAP 0x3800
#define ESPCONN_HANDSHAKE_TIMEOUT 0x3C
#define ESPCONN_INVALID_TYPE 0xFFFFFFFF
#define MBEDTLS_SSL_PLAIN_ADD TCP_MSS
#define FLASH_SECTOR_SIZE 4096
extern struct ssl_options ssl_client_options;
typedef struct{
uint32 parame_sec;
uint32 parame_type;
uint32 parame_datalen;
char* parame_data;
}mbedtls_parame, *pmbedtls_parame;
/*
* Storage format identifiers
* Recognized formats: PEM and DER
*/
typedef enum{
ESPCONN_FORMAT_INIT = 0,
ESPCONN_FORMAT_DER = 1,
ESPCONN_FORMAT_PEM = 2,
ESPCONN_FORMAT_INVALID
}espconn_format;
#define ESPCONN_EVENT_RECV(pcb,p,err) \
do { \
if((pcb)!= NULL && (pcb)->recv_callback != NULL) { \
(pcb)->state = ESPCONN_READ; \
(pcb)->recv_callback((pcb),(p),(err));\
(pcb)->state = ESPCONN_CONNECT; \
} else { \
ESP_LOG("%s %d\n", __FILE__, __LINE__); \
} \
} while (0)
#define ESPCONN_EVENT_SEND(pcb) \
do { \
if((pcb)!= NULL && (pcb)->sent_callback != NULL) { \
(pcb)->state = ESPCONN_CONNECT; \
(pcb)->sent_callback(pcb);\
} else { \
ESP_LOG("%s %d\n", __FILE__, __LINE__); \
} \
} while (0)
#define ESPCONN_EVENT_CONNECTED(pcb) \
do { \
if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->connect_callback != NULL) { \
(pcb)->state = ESPCONN_CONNECT; \
(pcb)->proto.tcp->connect_callback(pcb);\
} else { \
ESP_LOG("%s %d\n", __FILE__, __LINE__); \
} \
} while (0)
#define ESPCONN_EVENT_CLOSED(pcb) \
do { \
if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->disconnect_callback != NULL) { \
(pcb)->state = ESPCONN_CLOSE; \
(pcb)->proto.tcp->disconnect_callback(pcb);\
} else { \
ESP_LOG("%s %d\n", __FILE__, __LINE__); \
} \
} while (0)
#define ESPCONN_EVENT_ERROR(pcb,err) \
do { \
if((pcb)!= NULL && (pcb)->proto.tcp != NULL && (pcb)->proto.tcp->reconnect_callback != NULL) { \
(pcb)->state = ESPCONN_CLOSE; \
(pcb)->proto.tcp->reconnect_callback(pcb,err);\
} else { \
ESP_LOG("%s %d\n", __FILE__, __LINE__); \
} \
} while (0)
/******************************************************************************
* FunctionName : espconn_ssl_client
* Description : Initialize the client: set up a connect PCB and bind it to
* the defined port
* Parameters : espconn -- the espconn used to build client
* Returns : none
*******************************************************************************/
extern sint8 espconn_ssl_client(struct espconn *espconn);
/******************************************************************************
* FunctionName : espconn_ssl_write
* Description : sent data for client or server
* Parameters : void *arg -- client or server to send
* uint8* psent -- Data to send
* uint16 length -- Length of data to send
* Returns : none
*******************************************************************************/
extern void espconn_ssl_sent(void *arg, uint8 *psent, uint16 length);
/******************************************************************************
* FunctionName : espconn_ssl_disconnect
* Description : A new incoming connection has been disconnected.
* Parameters : espconn -- the espconn used to disconnect with host
* Returns : none
*******************************************************************************/
extern void espconn_ssl_disconnect(espconn_msg *pdis);
#endif
#endif /* ESPCONN_MBEDTLS_H_ */