Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f64f904

Browse files
authored
Merge pull request #436 from pennam/ssl-fixes
C33 SSL client fix stop() and connect( .. ) with psk
2 parents 4c73daf + deca0ab commit f64f904

File tree

5 files changed

+40
-41
lines changed

5 files changed

+40
-41
lines changed

‎libraries/SSLClient/src/SSLClient.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,9 @@ void SSLClient::setClient(Client& client)
9797

9898
void SSLClient::stop()
9999
{
100-
if (sslclient->client >= 0) {
101-
//sslclient->client->stop();
102-
_connected = false;
103-
_peek = -1;
104-
}
105-
stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
100+
stop_ssl_socket(sslclient);
101+
_connected = false;
102+
_peek = -1;
106103
}
107104

108105
int SSLClient::connect(IPAddress ip, uint16_t port)
@@ -150,12 +147,12 @@ int SSLClient::connect(const char *host, uint16_t port, const char *_CA_cert, co
150147
}
151148

152149
int SSLClient::connect(IPAddress ip, uint16_t port, const char *pskIdent, const char *psKey) {
153-
return connect(ip.toString().c_str(), port,_pskIdent, _psKey);
150+
return connect(ip.toString().c_str(), port, pskIdent, psKey);
154151
}
155152

156153
int SSLClient::connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey) {
157154
log_v("start_ssl_client with PSK");
158-
int ret = start_ssl_client(sslclient, host, port, _timeout, NULL, NULL, NULL, NULL, _pskIdent, _psKey, _use_insecure);
155+
int ret = start_ssl_client(sslclient, host, port, _timeout, NULL, NULL, NULL, NULL, pskIdent, psKey, _use_insecure);
159156
_lastError = ret;
160157
if (ret < 0) {
161158
log_e("start_ssl_client: %d", ret);

‎libraries/SSLClient/src/ssl_client.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static int _handle_error(int err, const char * file, int line)
3838

3939
#define handle_error(e) _handle_error(e, __FUNCTION__, __LINE__)
4040

41+
#if defined(SSL_CLIENT_RECV_DISABLE_TIMEOUT)
4142
/**
4243
* \brief Read at most 'len' characters. If no error occurs,
4344
* the actual amount read is returned.
@@ -52,11 +53,11 @@ static int _handle_error(int err, const char * file, int line)
5253
*/
5354
static int client_net_recv( void *ctx, unsigned char *buf, size_t len ) {
5455
Client *client = (Client*)ctx;
55-
if (!client) {
56+
if (!client) {
5657
log_e("Uninitialised!");
5758
return -1;
5859
}
59-
60+
6061
//if (!client->connected()) {
6162
// log_e("Not connected!");
6263
// return -2;
@@ -68,31 +69,31 @@ static int client_net_recv( void *ctx, unsigned char *buf, size_t len ) {
6869
if (result > 0) {
6970
//esp_log_buffer_hexdump_internal("SSL.RD", buf, (uint16_t)result, ESP_LOG_VERBOSE);
7071
}
71-
72+
7273
return result;
7374
}
74-
75-
int client_net_recv_timeout( void *ctx, unsigned char *buf,
75+
#else
76+
staticint client_net_recv_timeout( void *ctx, unsigned char *buf,
7677
size_t len, uint32_t timeout ) {
7778
Client *client = (Client*)ctx;
78-
if (!client) {
79+
if (!client) {
7980
log_e("Uninitialised!");
8081
return -1;
8182
}
8283
unsigned long start = millis();
8384
unsigned long tms = start + timeout;
84-
int pending = client->available();
85+
uint16_t pending = client->available();
8586
// If there is data in the client, wait for message completion
8687
if((pending > 0) && (pending < len))
8788
do {
88-
int pending = client->available();
89+
uint16_t pending = client->available();
8990
if (pending < len && timeout > 0) {
9091
delay(1);
9192
} else break;
9293
} while (millis() < tms);
93-
94+
9495
int result = client->read(buf, len);
95-
96+
9697
// lwIP interface return -1 if there is no data to read
9798
// report without throwing errors or block
9899
if (result <= 0) return MBEDTLS_ERR_SSL_WANT_READ;
@@ -102,10 +103,10 @@ int client_net_recv_timeout( void *ctx, unsigned char *buf,
102103
if (result > 0) {
103104
//esp_log_buffer_hexdump_internal("SSL.RD", buf, (uint16_t)result, ESP_LOG_VERBOSE);
104105
}
105-
106+
106107
return result;
107108
}
108-
109+
#endif
109110

110111
/**
111112
* \brief Write at most 'len' characters. If no error occurs,
@@ -121,20 +122,20 @@ int client_net_recv_timeout( void *ctx, unsigned char *buf,
121122
*/
122123
static int client_net_send( void *ctx, const unsigned char *buf, size_t len ) {
123124
Client *client = (Client*)ctx;
124-
if (!client) {
125+
if (!client) {
125126
log_e("Uninitialised!");
126127
return -1;
127128
}
128-
129+
129130
//if (!client->connected()) {
130131
// log_e("Not connected!");
131132
// return -2;
132133
//}
133-
134+
134135
//esp_log_buffer_hexdump_internal("SSL.WR", buf, (uint16_t)len, ESP_LOG_VERBOSE);
135-
136+
136137
int result = client->write(buf, len);
137-
138+
138139
log_d("SSL client TX res=%d len=%d", result, len);
139140
return result;
140141
}
@@ -152,7 +153,7 @@ void ssl_init(sslclient_context *ssl_client, Client *client, const char * ca_pat
152153
mbedtls_ssl_conf_ciphersuites(&ssl_client->ssl_conf, mbedtls_ssl_list_ciphersuites());
153154

154155
mbedtls_ssl_conf_dbg(&ssl_client->ssl_conf, mbedtls_debug_print, NULL);
155-
mbedtls_debug_set_threshold(DEBUG_LEVEL);
156+
mbedtls_debug_set_threshold(SSL_DEBUG_LEVEL);
156157

157158
mbedtls_fs_init(ca_path);
158159
}
@@ -225,7 +226,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
225226
}
226227
unsigned char psk[MBEDTLS_PSK_MAX_LEN];
227228
size_t psk_len = strlen(psKey)/2;
228-
for (int j=0; j<strlen(psKey); j+= 2) {
229+
for (size_t j=0; j<strlen(psKey); j+= 2) {
229230
char c = psKey[j];
230231
if (c >= '0' && c <= '9') c -= '0';
231232
else if (c >= 'A' && c <= 'F') c -= 'A' - 10;
@@ -336,13 +337,13 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
336337
memset(buf, 0, sizeof(buf));
337338
mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", flags);
338339
log_e("Failed to verify peer certificate! verification info: %s", buf);
339-
stop_ssl_socket(ssl_client, rootCABuff, cli_cert, cli_key); //It's not safe continue.
340+
stop_ssl_socket(ssl_client); //It's not safe continue.
340341

341342
return handle_error(ret);
342343
} else {
343344
log_v("Certificate verified.");
344345
}
345-
346+
346347
if ((rootCABuff != NULL) || ((rootCAPath != NULL))) {
347348
log_d("free buffer");
348349
mbedtls_x509_crt_free(&ssl_client->ca_cert);
@@ -354,14 +355,14 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
354355

355356
if (cli_key != NULL) {
356357
mbedtls_pk_free(&ssl_client->client_key);
357-
}
358+
}
358359

359360
//return ssl_client->socket;
360361
return 1;
361362
}
362363

363364

364-
void stop_ssl_socket(sslclient_context *ssl_client, constchar *rootCABuff, constchar *cli_cert, constchar *cli_key)
365+
void stop_ssl_socket(sslclient_context *ssl_client)
365366
{
366367
log_v("Cleaning SSL connection.");
367368

‎libraries/SSLClient/src/ssl_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef struct sslclient_context {
4343

4444
void ssl_init(sslclient_context *ssl_client, Client *client, const char *ca_path);
4545
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, int timeout, const char *rootCABuff, const char *rootCAPath, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey, bool insecure);
46-
void stop_ssl_socket(sslclient_context *ssl_client, constchar*rootCABuff, constchar*cli_cert, constchar*cli_key);
46+
void stop_ssl_socket(sslclient_context *ssl_client);
4747
int data_to_read(sslclient_context *ssl_client);
4848
int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, uint16_t len);
4949
int get_ssl_receive(sslclient_context *ssl_client, uint8_t *data, int length);

‎libraries/SSLClient/src/ssl_debug.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "ssl_debug.h"
2121

2222
void ssl_debug_print(const char *format, ...) {
23-
char debug_buf[1024];
23+
char debug_buf[1024];
2424
va_list argptr;
2525
va_start(argptr, format);
2626
vsnprintf(debug_buf, sizeof(debug_buf), format, argptr);
@@ -29,7 +29,7 @@ void ssl_debug_print(const char *format, ...) {
2929
}
3030

3131
void ssl_debug_println(const char *format, ...) {
32-
char debug_buf[1024];
32+
char debug_buf[1024];
3333
va_list argptr;
3434
va_start(argptr, format);
3535
vsnprintf(debug_buf, sizeof(debug_buf), format, argptr);
@@ -43,6 +43,7 @@ void ssl_debug_none(const char *format, ...) {
4343

4444
void mbedtls_debug_print(void *ctx, int level, const char *file, int line, const char *str)
4545
{
46+
((void) ctx);
4647
((void) level);
4748
ssl_debug_print("%s:%04d: %s", file, line, str);
4849
}

‎libraries/SSLClient/src/ssl_debug.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,33 @@
2929
* 4: DEBUG
3030
* 5: VERBOSE
3131
*/
32-
#define DEBUG_LEVEL 1
32+
#define SSL_DEBUG_LEVEL 1
3333

34-
#if DEBUG_LEVEL > 0
34+
#if SSL_DEBUG_LEVEL > 0
3535
#define log_e ssl_debug_println
3636
#else
3737
#define log_e ssl_debug_none
3838
#endif
3939

40-
#if DEBUG_LEVEL > 1
40+
#if SSL_DEBUG_LEVEL > 1
4141
#define log_w ssl_debug_println
4242
#else
4343
#define log_w ssl_debug_none
4444
#endif
4545

46-
#if DEBUG_LEVEL > 2
46+
#if SSL_DEBUG_LEVEL > 2
4747
#define log_i ssl_debug_println
4848
#else
4949
#define log_i ssl_debug_none
5050
#endif
5151

52-
#if DEBUG_LEVEL > 3
52+
#if SSL_DEBUG_LEVEL > 3
5353
#define log_d ssl_debug_println
5454
#else
5555
#define log_d ssl_debug_none
5656
#endif
57-
58-
#if DEBUG_LEVEL > 4
57+
58+
#if SSL_DEBUG_LEVEL > 4
5959
#define log_v ssl_debug_println
6060
#else
6161
#define log_v ssl_debug_none

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /