Skip to content

Commit fe4518d

Browse files
committed
Make SNI host name an ssl_client_new argument
ssl_set_hostname was mostly useless, because it allowed setting host name of an existing SSL object. However SNI was sent as part of client_hello, which was done in ssl_client_new. So it wasn't possible to actually set host name before connection would start.
1 parent 5b4be7d commit fe4518d

File tree

3 files changed

+8
-35
lines changed

3 files changed

+8
-35
lines changed

Diff for: ssl/ssl.h

+2-11
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,11 @@ EXP_FUNC SSL * STDCALL ssl_server_new(SSL_CTX *ssl_ctx, int client_fd);
241241
* can be null if no session resumption is being used or required. This option
242242
* is not used in skeleton mode.
243243
* @param sess_id_size The size of the session id (max 32)
244+
* @param host_name If non-zero, host name to be sent to server for SNI support
244245
* @return An SSL object reference. Use ssl_handshake_status() to check
245246
* if a handshake succeeded.
246247
*/
247-
EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const uint8_t *session_id, uint8_t sess_id_size);
248+
EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const uint8_t *session_id, uint8_t sess_id_size, const char* host_name);
248249

249250
/**
250251
* @brief Free any used resources on this connection.
@@ -352,16 +353,6 @@ EXP_FUNC int STDCALL ssl_handshake_status(const SSL *ssl);
352353
*/
353354
EXP_FUNC int STDCALL ssl_get_config(int offset);
354355

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(SSL *ssl, const char* host_name);
364-
365356
/**
366357
* @brief Display why the handshake failed.
367358
*

Diff for: ssl/tls1.c

+1-23
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ EXP_FUNC void STDCALL ssl_free(SSL *ssl)
251251
disposable_free(ssl);
252252
certificate_free(ssl);
253253
free(ssl->bm_all_data);
254+
free(ssl->host_name);
254255
free(ssl);
255256
}
256257

@@ -1876,29 +1877,6 @@ EXP_FUNC int STDCALL ssl_get_config(int offset)
18761877
}
18771878
}
18781879

1879-
/**
1880-
* Sets the SNI hostname
1881-
*/
1882-
EXP_FUNC int STDCALL ssl_set_hostname(SSL *ssl, const char* host_name) {
1883-
if(host_name == NULL || strlen(host_name) == 0 || strlen(host_name) > 255 ) {
1884-
return 0;
1885-
}
1886-
1887-
if(ssl->host_name != NULL) {
1888-
free(ssl->host_name);
1889-
}
1890-
1891-
ssl->host_name = (char *)malloc(strlen(host_name)+1);
1892-
if(ssl->host_name == NULL) {
1893-
// most probably there was no memory available
1894-
return 0;
1895-
}
1896-
1897-
strcpy(ssl->host_name, host_name);
1898-
1899-
return 1;
1900-
}
1901-
19021880
#ifdef CONFIG_SSL_CERT_VERIFICATION
19031881
/**
19041882
* Authenticate a received certificate.

Diff for: ssl/tls1_clnt.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static int send_cert_verify(SSL *ssl);
4848
* Establish a new SSL connection to an SSL server.
4949
*/
5050
EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const
51-
uint8_t *session_id, uint8_t sess_id_size)
51+
uint8_t *session_id, uint8_t sess_id_size, const char* host_name)
5252
{
5353
SSL *ssl = ssl_new(ssl_ctx, client_fd);
5454
ssl->version = SSL_PROTOCOL_VERSION_MAX; /* try top version first */
@@ -66,6 +66,10 @@ EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const
6666
SET_SSL_FLAG(SSL_SESSION_RESUME); /* just flag for later */
6767
}
6868

69+
if(host_name != NULL && strlen(host_name) > 0 || strlen(host_name) < 255 ) {
70+
ssl->host_name = (char *)strdup(host_name);
71+
}
72+
6973
SET_SSL_FLAG(SSL_IS_CLIENT);
7074
do_client_connect(ssl);
7175
return ssl;

0 commit comments

Comments
 (0)