diff options
author | Matt Caswell <matt@openssl.org> | 2023-10-30 11:22:00 +0000 |
---|---|---|
committer | Hugo Landau <hlandau@openssl.org> | 2023-11-02 08:16:23 +0000 |
commit | 151af35f560a696ddb2793e4b8a5e675dc5994ac (patch) | |
tree | b8f9c5545ad41a87f5b63a1ec8be425b8da5bd0d /demos/guide/tls-client-block.c | |
parent | 6aa921f27bba5c55acd4ea83d4f221b6c876c59d (diff) |
Amend the TLS demos to accept hostname/port as an argument
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 https://github.com/openssl/openssl/pull/22552)
(cherry picked from commit 2ec4e73c0188425890329ae7f0372c66fb0c1234)
Diffstat (limited to 'demos/guide/tls-client-block.c')
-rw-r--r-- | demos/guide/tls-client-block.c | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/demos/guide/tls-client-block.c b/demos/guide/tls-client-block.c index 576fc7b325..ea7d68467a 100644 --- a/demos/guide/tls-client-block.c +++ b/demos/guide/tls-client-block.c @@ -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 @@ -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; @@ -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; } @@ -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; } @@ -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; } |