]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Amend the TLS demos to accept hostname/port as an argument
authorMatt Caswell <matt@openssl.org>
Mon, 30 Oct 2023 11:22:00 +0000 (11:22 +0000)
committerHugo Landau <hlandau@openssl.org>
Thu, 2 Nov 2023 08:14:38 +0000 (08:14 +0000)
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)

demos/guide/tls-client-block.c
demos/guide/tls-client-non-block.c

index 576fc7b32512970a161e69c514ce88987bda6e84..ea7d68467ac6ccaebd59b9221094df4077372f9d 100644 (file)
@@ -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;
     }
 
index 14448c968523e69d0c5e9372ad134046becc56c9..8748e4fffc772e904b0d4b54e62af8f5b7a00c13 100644 (file)
@@ -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
@@ -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;
@@ -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;
     }
@@ -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;
     }
@@ -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 */
     }