Skip to content

Commit

Permalink
Amend the TLS demos to accept hostname/port as an argument
Browse files Browse the repository at this point in the history
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from #22552)
  • Loading branch information
mattcaswell authored and hlandau committed Nov 2, 2023
1 parent 660718e commit 2ec4e73
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 32 deletions.
41 changes: 25 additions & 16 deletions demos/guide/tls-client-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,30 @@ static BIO *create_socket_bio(const char *hostname, const char *port)
return bio;
}

/* Server hostname and port details. Must be in quotes */
#ifndef HOSTNAME
# define HOSTNAME "www.example.com"
#endif
#ifndef PORT
# define PORT "443"
#endif

/*
* Simple application to send a basic HTTP/1.0 request to a server and
* print the response on the screen.
*/
int main(void)
int main(int argc, char *argv[])
{
SSL_CTX *ctx = NULL;
SSL *ssl = NULL;
BIO *bio = NULL;
int res = EXIT_FAILURE;
int ret;
const char *request =
"GET / HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n";
const char *request_start = "GET / HTTP/1.0\r\nConnection: close\r\nHost: ";
const char *request_end = "\r\n\r\n";
size_t written, readbytes;
char buf[160];
char *hostname, *port;

if (argc != 3) {
printf("Usage: tls-client-block hostname port\n");
goto end;
}

hostname = argv[1];
port = argv[2];

/*
* Create an SSL_CTX which we can use to create SSL objects from. We
Expand Down Expand Up @@ -161,7 +162,7 @@ int main(void)
* Create the underlying transport socket/BIO and associate it with the
* connection.
*/
bio = create_socket_bio(HOSTNAME, PORT);
bio = create_socket_bio(hostname, port);
if (bio == NULL) {
printf("Failed to crete the BIO\n");
goto end;
Expand All @@ -172,7 +173,7 @@ int main(void)
* Tell the server during the handshake which hostname we are attempting
* to connect to in case the server supports multiple hosts.
*/
if (!SSL_set_tlsext_host_name(ssl, HOSTNAME)) {
if (!SSL_set_tlsext_host_name(ssl, hostname)) {
printf("Failed to set the SNI hostname\n");
goto end;
}
Expand All @@ -183,7 +184,7 @@ int main(void)
* Virtually all clients should do this unless you really know what you
* are doing.
*/
if (!SSL_set1_host(ssl, HOSTNAME)) {
if (!SSL_set1_host(ssl, hostname)) {
printf("Failed to set the certificate verification hostname");
goto end;
}
Expand All @@ -202,8 +203,16 @@ int main(void)
}

/* Write an HTTP GET request to the peer */
if (!SSL_write_ex(ssl, request, strlen(request), &written)) {
printf("Failed to write HTTP request\n");
if (!SSL_write_ex(ssl, request_start, strlen(request_start), &written)) {
printf("Failed to write start of HTTP request\n");
goto end;
}
if (!SSL_write_ex(ssl, hostname, strlen(hostname), &written)) {
printf("Failed to write hostname in HTTP request\n");
goto end;
}
if (!SSL_write_ex(ssl, request_end, strlen(request_end), &written)) {
printf("Failed to write end of HTTP request\n");
goto end;
}

Expand Down
45 changes: 29 additions & 16 deletions demos/guide/tls-client-non-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,30 +170,31 @@ static int handle_io_failure(SSL *ssl, int res)
}
}

/* Server hostname and port details. Must be in quotes */
#ifndef HOSTNAME
# define HOSTNAME "www.example.com"
#endif
#ifndef PORT
# define PORT "443"
#endif

/*
* Simple application to send a basic HTTP/1.0 request to a server and
* print the response on the screen.
*/
int main(void)
int main(int argc, char *argv[])
{
SSL_CTX *ctx = NULL;
SSL *ssl = NULL;
BIO *bio = NULL;
int res = EXIT_FAILURE;
int ret;
const char *request =
"GET / HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n";
const char *request_start = "GET / HTTP/1.0\r\nConnection: close\r\nHost: ";
const char *request_end = "\r\n\r\n";
size_t written, readbytes;
char buf[160];
int eof = 0;
char *hostname, *port;

if (argc != 3) {
printf("Usage: tls-client-non-block hostname port\n");
goto end;
}

hostname = argv[1];
port = argv[2];

/*
* Create an SSL_CTX which we can use to create SSL objects from. We
Expand Down Expand Up @@ -239,7 +240,7 @@ int main(void)
* Create the underlying transport socket/BIO and associate it with the
* connection.
*/
bio = create_socket_bio(HOSTNAME, PORT);
bio = create_socket_bio(hostname, port);
if (bio == NULL) {
printf("Failed to crete the BIO\n");
goto end;
Expand All @@ -250,7 +251,7 @@ int main(void)
* Tell the server during the handshake which hostname we are attempting
* to connect to in case the server supports multiple hosts.
*/
if (!SSL_set_tlsext_host_name(ssl, HOSTNAME)) {
if (!SSL_set_tlsext_host_name(ssl, hostname)) {
printf("Failed to set the SNI hostname\n");
goto end;
}
Expand All @@ -261,7 +262,7 @@ int main(void)
* Virtually all clients should do this unless you really know what you
* are doing.
*/
if (!SSL_set1_host(ssl, HOSTNAME)) {
if (!SSL_set1_host(ssl, hostname)) {
printf("Failed to set the certificate verification hostname");
goto end;
}
Expand All @@ -275,10 +276,22 @@ int main(void)
}

/* Write an HTTP GET request to the peer */
while (!SSL_write_ex(ssl, request, strlen(request), &written)) {
while (!SSL_write_ex(ssl, request_start, strlen(request_start), &written)) {
if (handle_io_failure(ssl, 0) == 1)
continue; /* Retry */
printf("Failed to write start of HTTP request\n");
goto end; /* Cannot retry: error */
}
while (!SSL_write_ex(ssl, hostname, strlen(hostname), &written)) {
if (handle_io_failure(ssl, 0) == 1)
continue; /* Retry */
printf("Failed to write hostname in HTTP request\n");
goto end; /* Cannot retry: error */
}
while (!SSL_write_ex(ssl, request_end, strlen(request_end), &written)) {
if (handle_io_failure(ssl, 0) == 1)
continue; /* Retry */
printf("Failed to write HTTP request\n");
printf("Failed to write end of HTTP request\n");
goto end; /* Cannot retry: error */
}

Expand Down

0 comments on commit 2ec4e73

Please sign in to comment.