Skip to content

Commit 63da899

Browse files
author
Slavey Karadzhov
committed
1 parent 885ff3e commit 63da899

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

ssl/ssl.h

+10
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ EXP_FUNC int STDCALL ssl_handshake_status(const SSL *ssl);
352352
*/
353353
EXP_FUNC int STDCALL ssl_get_config(int offset);
354354

355+
/**
356+
* @brief Sets the hostname to be used for SNI
357+
* @see https://en.wikipedia.org/wiki/Server_Name_Indication
358+
* @param char* hostname
359+
* @return success from the operation
360+
* - 1 on success
361+
* - 0 on failure
362+
*/
363+
EXP_FUNC int STDCALL ssl_set_hostname(const SSL *ssl, const char* host_name);
364+
355365
/**
356366
* @brief Display why the handshake failed.
357367
*

ssl/tls1.c

+13
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,19 @@ EXP_FUNC int STDCALL ssl_get_config(int offset)
18491849
}
18501850
}
18511851

1852+
/**
1853+
* Sets the SNI hostname
1854+
*/
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+
}
1859+
1860+
strncpy((char*)&ssl->host_name, host_name, strlen(host_name));
1861+
1862+
return 1;
1863+
}
1864+
18521865
#ifdef CONFIG_SSL_CERT_VERIFICATION
18531866
/**
18541867
* Authenticate a received certificate.

ssl/tls1.h

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +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 */
201202
};
202203

203204
typedef struct _SSL SSL;

ssl/tls1_clnt.c

+20
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ static int send_client_hello(SSL *ssl)
220220

221221
buf[offset++] = 1; /* no compression */
222222
buf[offset++] = 0;
223+
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;
241+
}
242+
223243
buf[3] = offset - 4; /* handshake size */
224244

225245
return send_packet(ssl, PT_HANDSHAKE_PROTOCOL, NULL, offset);

0 commit comments

Comments
 (0)