Skip to content

Commit

Permalink
Add support SSL session resumption for SSL handshake.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Sep 13, 2023
1 parent 4daf1f8 commit 0c99b83
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 98 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP Mail Client",
"version": "3.4.12",
"version": "3.4.13",
"keywords": "communication, email, imap, smtp, esp32, esp8266, samd, arduino",
"description": "Arduino E-Mail Client Library to send, read and get incoming email notification for ESP32, ESP8266 and SAMD21 devices. The library also supported other Arduino Devices using Clients interfaces e.g. WiFiClient, EthernetClient, and GSMClient.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP Mail Client

version=3.4.12
version=3.4.13

author=Mobizt

Expand Down
5 changes: 3 additions & 2 deletions src/ESP_Mail_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

/**
* Mail Client Arduino Library for Arduino devices.
*
* Created August 28, 2023
* Created September 13, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -1272,6 +1272,7 @@ bool ESP_Mail_Client::beginConnection(Session_Config *session_config, T sessionP

sessionPtr->client.setWiFi(&wifi);
sessionPtr->client.setSession(session_config);
sessionPtr->client.setBSSLSession(&(sessionPtr->_bsslSession));

if (!reconnect<T>(sessionPtr))
return false;
Expand Down
155 changes: 79 additions & 76 deletions src/ESP_Mail_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#define ESP_MAIL_CLIENT_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

/**
* Mail Client Arduino Library for Arduino devices.
*
* Created August 28, 2023
* Created September 13, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -2285,6 +2285,82 @@ class IMAPSession
friend class foldderList;

private:
bool _sessionSSL = false;
bool _sessionLogin = false;
bool _loginStatus = false;
unsigned long _last_polling_error_ms = 0;
unsigned long _last_host_check_ms = 0;
unsigned long _last_server_connect_ms = 0;
unsigned long _last_network_error_ms = 0;
unsigned long tcpTimeout = TCP_CLIENT_DEFAULT_TCP_TIMEOUT_SEC;
struct esp_mail_imap_response_status_t _responseStatus;
int _cMsgIdx = 0;
int _cPartIdx = 0;
int _totalRead = 0;
_vectorImpl<struct esp_mail_message_header_t> _headers;

esp_mail_imap_command _imap_cmd = esp_mail_imap_command::esp_mail_imap_cmd_sasl_login;
esp_mail_imap_command _prev_imap_cmd = esp_mail_imap_command::esp_mail_imap_cmd_sasl_login;
esp_mail_imap_command _imap_custom_cmd = esp_mail_imap_cmd_custom;
esp_mail_imap_command _prev_imap_custom_cmd = esp_mail_imap_cmd_custom;
bool _idle = false;
MB_String _cmd;
_vectorImpl<struct esp_mail_imap_multipart_level_t> _multipart_levels;
int _rfc822_part_count = 0;
bool _unseen = false;
bool _readOnlyMode = true;
bool _msgDownload = false;
bool _attDownload = false;
bool _storageReady = false;
bool _storageChecked = false;

bool _auth_capability[esp_mail_auth_capability_maxType];
bool _feature_capability[esp_mail_imap_read_capability_maxType];
Session_Config *_session_cfg;
_vectorImpl<int> _configPtrList;
MB_String _currentFolder;
bool _mailboxOpened = false;
unsigned long _lastSameFolderOpenMillis = 0;
MB_String _nextUID;
MB_String _unseenMsgIndex;
MB_String _flags_tmp;
MB_String _quota_tmp;
MB_String _quota_root_tmp;
MB_String _acl_tmp;
MB_String _ns_tmp;
MB_String _server_id_tmp;
MB_String _sdFileList;

struct esp_mail_imap_data_config_t *_imap_data = nullptr;

int _userHeaderOnly = -1;
bool _headerOnly = true;
bool _uidSearch = false;
bool _headerSaved = false;
bool _debug = false;
int _debugLevel = 0;
bool _secure = false;
bool _authenticated = false;
bool _isFirmwareUpdated = false;
imapStatusCallback _statusCallback = NULL;
imapResponseCallback _customCmdResCallback = NULL;
MIMEDataStreamCallback _mimeDataStreamCallback = NULL;
imapCharacterDecodingCallback _charDecCallback = NULL;

_vectorImpl<struct esp_mail_imap_msg_num_t> _imap_msg_num;
esp_mail_session_type _sessionType = esp_mail_session_type_imap;

FoldersCollection _folders;
SelectedFolderInfo _mbif;
int _uid_tmp = 0;
int _lastProgress = -1;

ESP_Mail_TCPClient client;

IMAP_Status _cbData;

BearSSL_Session _bsslSession;

// Log in to IMAP server
bool mLogin(MB_StringPtr email, MB_StringPtr password, bool isToken);

Expand Down Expand Up @@ -2446,80 +2522,6 @@ class IMAPSession

// Print features not supported debug error message
void printDebugNotSupported();

bool _sessionSSL = false;
bool _sessionLogin = false;
bool _loginStatus = false;
unsigned long _last_polling_error_ms = 0;
unsigned long _last_host_check_ms = 0;
unsigned long _last_server_connect_ms = 0;
unsigned long _last_network_error_ms = 0;
unsigned long tcpTimeout = TCP_CLIENT_DEFAULT_TCP_TIMEOUT_SEC;
struct esp_mail_imap_response_status_t _responseStatus;
int _cMsgIdx = 0;
int _cPartIdx = 0;
int _totalRead = 0;
_vectorImpl<struct esp_mail_message_header_t> _headers;

esp_mail_imap_command _imap_cmd = esp_mail_imap_command::esp_mail_imap_cmd_sasl_login;
esp_mail_imap_command _prev_imap_cmd = esp_mail_imap_command::esp_mail_imap_cmd_sasl_login;
esp_mail_imap_command _imap_custom_cmd = esp_mail_imap_cmd_custom;
esp_mail_imap_command _prev_imap_custom_cmd = esp_mail_imap_cmd_custom;
bool _idle = false;
MB_String _cmd;
_vectorImpl<struct esp_mail_imap_multipart_level_t> _multipart_levels;
int _rfc822_part_count = 0;
bool _unseen = false;
bool _readOnlyMode = true;
bool _msgDownload = false;
bool _attDownload = false;
bool _storageReady = false;
bool _storageChecked = false;

bool _auth_capability[esp_mail_auth_capability_maxType];
bool _feature_capability[esp_mail_imap_read_capability_maxType];
Session_Config *_session_cfg;
_vectorImpl<int> _configPtrList;
MB_String _currentFolder;
bool _mailboxOpened = false;
unsigned long _lastSameFolderOpenMillis = 0;
MB_String _nextUID;
MB_String _unseenMsgIndex;
MB_String _flags_tmp;
MB_String _quota_tmp;
MB_String _quota_root_tmp;
MB_String _acl_tmp;
MB_String _ns_tmp;
MB_String _server_id_tmp;
MB_String _sdFileList;

struct esp_mail_imap_data_config_t *_imap_data = nullptr;

int _userHeaderOnly = -1;
bool _headerOnly = true;
bool _uidSearch = false;
bool _headerSaved = false;
bool _debug = false;
int _debugLevel = 0;
bool _secure = false;
bool _authenticated = false;
bool _isFirmwareUpdated = false;
imapStatusCallback _statusCallback = NULL;
imapResponseCallback _customCmdResCallback = NULL;
MIMEDataStreamCallback _mimeDataStreamCallback = NULL;
imapCharacterDecodingCallback _charDecCallback = NULL;

_vectorImpl<struct esp_mail_imap_msg_num_t> _imap_msg_num;
esp_mail_session_type _sessionType = esp_mail_session_type_imap;

FoldersCollection _folders;
SelectedFolderInfo _mbif;
int _uid_tmp = 0;
int _lastProgress = -1;

ESP_Mail_TCPClient client;

IMAP_Status _cbData;
};

#endif
Expand Down Expand Up @@ -2844,6 +2846,7 @@ class SMTPSession
SMTP_Status _cbData;
struct esp_mail_smtp_msg_type_t _msgType;
int _lastProgress = -1;
BearSSL_Session _bsslSession;

ESP_Mail_TCPClient client;

Expand Down
4 changes: 2 additions & 2 deletions src/ESP_Mail_Client_Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#ifndef ESP_MAIL_VERSION

#define ESP_MAIL_VERSION "3.4.12"
#define ESP_MAIL_VERSION_NUM 30412
#define ESP_MAIL_VERSION "3.4.13"
#define ESP_MAIL_VERSION_NUM 30413

/* The inconsistent file version checking to prevent mixed versions compilation. */
#define VALID_VERSION_CHECK(ver) (ver == ESP_MAIL_VERSION_NUM)
Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define ESP_MAIL_CONST_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define ESP_MAIL_ERROR_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define ESP_MAIL_CONFIG_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_IMAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define ESP_MAIL_IMAP_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/ESP_Mail_SMTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define ESP_MAIL_SMTP_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down
26 changes: 19 additions & 7 deletions src/ESP_Mail_TCPClient.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
*
* The Network Upgradable Arduino Secure TCP Client Class, ESP_Mail_TCPClient.h v1.0.2
* The Network Upgradable Arduino Secure TCP Client Class, ESP_Mail_TCPClient.h v1.0.3
*
* Created September 11, 2023
* Created September 13, 2023
*
* The MIT License (MIT)
* Copyright (c) 2023 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -30,7 +30,7 @@
#define ESP_MAIL_TCPCLIENT_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down Expand Up @@ -405,6 +405,10 @@ class ESP_Mail_TCPClient
gprsDisconnect();
gprsConnect();
}
else if (_client_type == esp_mail_client_type_external_ethernet_client)
{
ethernetConnect();
}
else if (_client_type == esp_mail_client_type_internal_basic_client)
{

Expand Down Expand Up @@ -507,6 +511,14 @@ class ESP_Mail_TCPClient
return true;
}

void setBSSLSession(BearSSL_Session *session)
{
#if !defined(ESP_MAIL_DISABLE_SSL)
if (_tcp_client)
_tcp_client->setSession(session);
#endif
}

/**
* Start TCP connection using stored host name and port.
* @param secure The secure mode option.
Expand Down Expand Up @@ -1025,7 +1037,7 @@ class ESP_Mail_TCPClient

#if !defined(SILENT_MODE) && (defined(ENABLE_IMAP) || defined(ENABLE_SMTP))
if (_debug_level > 0)
esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Resetting Ethernet Board..."), esp_mail_debug_tag_type_info, true, false);
esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("\nResetting Ethernet Board..."), esp_mail_debug_tag_type_info, true);
#endif

pinMode(_ethernet_reset_pin, OUTPUT);
Expand All @@ -1039,7 +1051,7 @@ class ESP_Mail_TCPClient

#if !defined(SILENT_MODE) && (defined(ENABLE_IMAP) || defined(ENABLE_SMTP))
if (_debug_level > 0)
esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Starting Ethernet connection..."), esp_mail_debug_tag_type_info, true, false);
esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Starting Ethernet connection..."), esp_mail_debug_tag_type_info, true);
#endif
if (_static_ip)
{
Expand Down Expand Up @@ -1068,11 +1080,11 @@ class ESP_Mail_TCPClient
{
if (ret)
{
esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Connected with IP "), esp_mail_debug_tag_type_info, false, false);
esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Connected with IP "), esp_mail_debug_tag_type_info, false);
ESP_MAIL_DEFAULT_DEBUG_PORT.println(Ethernet.localIP());
}
else
esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Can't connect"), esp_mail_debug_tag_type_error, true, false);
esp_mail_debug_print_tag((const char *)MBSTRING_FLASH_MCR("Can't connect"), esp_mail_debug_tag_type_error, true);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/extras/MB_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define MB_Time_H

#include "./ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/extras/Networks.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "./ESP_Mail_FS.h"

#include "./ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/extras/RFC2047.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define RFC2047_CPP

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/extras/RFC2047.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define RFC2047_H

#include "ESP_Mail_Client_Version.h"
#if !VALID_VERSION_CHECK(30412)
#if !VALID_VERSION_CHECK(30413)
#error "Mixed versions compilation."
#endif

Expand Down

0 comments on commit 0c99b83

Please sign in to comment.