Skip to content

Commit 1154d0a

Browse files
author
Slavey Karadzhov
committed
Changed the code to reserve bytes for hostname only if needed.
1 parent 63da899 commit 1154d0a

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

ssl/ssl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ EXP_FUNC int STDCALL ssl_get_config(int offset);
360360
* - 1 on success
361361
* - 0 on failure
362362
*/
363-
EXP_FUNC int STDCALL ssl_set_hostname(const SSL *ssl, const char* host_name);
363+
EXP_FUNC int STDCALL ssl_set_hostname(SSL *ssl, const char* host_name);
364364

365365
/**
366366
* @brief Display why the handshake failed.

ssl/tls1.c

+18-6
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
568568
ssl->encrypt_ctx = malloc(sizeof(AES_CTX));
569569
ssl->decrypt_ctx = malloc(sizeof(AES_CTX));
570570

571+
ssl->host_name = NULL;
572+
571573
SSL_CTX_UNLOCK(ssl_ctx->mutex);
572574
return ssl;
573575
}
@@ -1852,14 +1854,24 @@ EXP_FUNC int STDCALL ssl_get_config(int offset)
18521854
/**
18531855
* Sets the SNI hostname
18541856
*/
1855-
EXP_FUNC int STDCALL ssl_set_hostname(const SSL *ssl, const char* host_name) {
1856-
if(host_name == NULL || strlen(host_name) == 0 || strlen(host_name) > 255 ) {
1857-
return 0;
1858-
}
1857+
EXP_FUNC int STDCALL ssl_set_hostname(SSL *ssl, const char* host_name) {
1858+
if(host_name == NULL || strlen(host_name) == 0 || strlen(host_name) > 255 ) {
1859+
return 0;
1860+
}
1861+
1862+
if(ssl->host_name != NULL) {
1863+
free(ssl->host_name);
1864+
}
1865+
1866+
ssl->host_name = (char *)malloc(strlen(host_name)+1);
1867+
if(ssl->host_name == NULL) {
1868+
// most probably there was no memory available
1869+
return 0;
1870+
}
18591871

1860-
strncpy((char*)&ssl->host_name, host_name, strlen(host_name));
1872+
strcpy(ssl->host_name, host_name);
18611873

1862-
return 1;
1874+
return 1;
18631875
}
18641876

18651877
#ifdef CONFIG_SSL_CERT_VERIFICATION

ssl/tls1.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ struct _SSL
198198
uint8_t read_sequence[8]; /* 64 bit sequence number */
199199
uint8_t write_sequence[8]; /* 64 bit sequence number */
200200
uint8_t hmac_header[SSL_RECORD_SIZE]; /* rx hmac */
201-
const char host_name[255]; /* Needed for the SNI support */
201+
char *host_name; /* Needed for the SNI support */
202202
};
203203

204204
typedef struct _SSL SSL;

ssl/tls1_clnt.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,23 @@ static int send_client_hello(SSL *ssl)
221221
buf[offset++] = 1; /* no compression */
222222
buf[offset++] = 0;
223223

224-
if (ssl->host_name[0] != 0) {
225-
unsigned int host_len = strnlen((char*) ssl->host_name, 255);
226-
227-
buf[offset++] = 0;
228-
buf[offset++] = host_len+9; /* extensions length */
229-
230-
buf[offset++] = 0;
231-
buf[offset++] = 0; /* server_name(0) (65535) */
232-
buf[offset++] = 0;
233-
buf[offset++] = host_len+5; /* server_name length */
234-
buf[offset++] = 0;
235-
buf[offset++] = host_len+3; /* server_list length */
236-
buf[offset++] = 0; /* host_name(0) (255) */
237-
buf[offset++] = 0;
238-
buf[offset++] = host_len; /* host_name length */
239-
strncpy((char*) &buf[offset], ssl->host_name, host_len);
240-
offset += host_len;
224+
if (ssl->host_name != NULL) {
225+
unsigned int host_len = strlen(ssl->host_name);
226+
227+
buf[offset++] = 0;
228+
buf[offset++] = host_len+9; /* extensions length */
229+
230+
buf[offset++] = 0;
231+
buf[offset++] = 0; /* server_name(0) (65535) */
232+
buf[offset++] = 0;
233+
buf[offset++] = host_len+5; /* server_name length */
234+
buf[offset++] = 0;
235+
buf[offset++] = host_len+3; /* server_list length */
236+
buf[offset++] = 0; /* host_name(0) (255) */
237+
buf[offset++] = 0;
238+
buf[offset++] = host_len; /* host_name length */
239+
strncpy((char*) &buf[offset], ssl->host_name, host_len);
240+
offset += host_len;
241241
}
242242

243243
buf[3] = offset - 4; /* handshake size */

0 commit comments

Comments
 (0)