Home | History | Annotate | Line # | Download | only in bio
      1 /*
      2  * Copyright 1998-2025 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 /*-
     11  * A minimal program to do SSL to a passed host and port.
     12  * It is actually using non-blocking IO but in a very simple manner
     13  * sconnect host:port - it does a 'GET / HTTP/1.0'
     14  *
     15  * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
     16  */
     17 #include <stdio.h>
     18 #include <stdlib.h>
     19 #include <string.h>
     20 #include <errno.h>
     21 #include <openssl/err.h>
     22 #include <openssl/ssl.h>
     23 #if !defined(OPENSSL_SYS_WINDOWS)
     24 #include <unistd.h>
     25 #else
     26 #include <windows.h>
     27 #define sleep(x) Sleep(x * 1000)
     28 #endif
     29 
     30 #define HOSTPORT "localhost:4433"
     31 #define CAFILE "root.pem"
     32 
     33 int main(int argc, char *argv[])
     34 {
     35     const char *hostport = HOSTPORT;
     36     const char *CAfile = CAFILE;
     37     const char *hostname;
     38     BIO *out = NULL;
     39     char buf[1024 * 10], *p;
     40     SSL_CTX *ssl_ctx = NULL;
     41     SSL *ssl;
     42     BIO *ssl_bio;
     43     int i, len, off, ret = EXIT_FAILURE;
     44 
     45     if (argc > 1)
     46         hostport = argv[1];
     47     if (argc > 2)
     48         CAfile = argv[2];
     49 
     50 #ifdef WATT32
     51     dbug_init();
     52     sock_init();
     53 #endif
     54 
     55     ssl_ctx = SSL_CTX_new(TLS_client_method());
     56 
     57     /* Enable trust chain verification */
     58     SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
     59     if (!SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL))
     60         goto err;
     61 
     62     /* Let's make an SSL structure */
     63     ssl = SSL_new(ssl_ctx);
     64     SSL_set_connect_state(ssl);
     65 
     66     /* Use it inside an SSL BIO */
     67     ssl_bio = BIO_new(BIO_f_ssl());
     68     BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
     69 
     70     /* Lets use a connect BIO under the SSL BIO */
     71     out = BIO_new(BIO_s_connect());
     72     BIO_set_conn_hostname(out, hostport);
     73 
     74     /* The BIO has parsed the host:port and even IPv6 literals in [] */
     75     hostname = BIO_get_conn_hostname(out);
     76     if (!hostname || SSL_set1_host(ssl, hostname) <= 0) {
     77         BIO_free(ssl_bio);
     78         goto err;
     79     }
     80 
     81     BIO_set_nbio(out, 1);
     82     out = BIO_push(ssl_bio, out);
     83 
     84     p = "GET / HTTP/1.0\r\n\r\n";
     85     len = strlen(p);
     86 
     87     off = 0;
     88     for (;;) {
     89         i = BIO_write(out, &(p[off]), len);
     90         if (i <= 0) {
     91             if (BIO_should_retry(out)) {
     92                 fprintf(stderr, "write DELAY\n");
     93                 sleep(1);
     94                 continue;
     95             } else {
     96                 goto err;
     97             }
     98         }
     99         off += i;
    100         len -= i;
    101         if (len <= 0)
    102             break;
    103     }
    104 
    105     for (;;) {
    106         i = BIO_read(out, buf, sizeof(buf));
    107         if (i == 0)
    108             break;
    109         if (i < 0) {
    110             if (BIO_should_retry(out)) {
    111                 fprintf(stderr, "read DELAY\n");
    112                 sleep(1);
    113                 continue;
    114             }
    115             goto err;
    116         }
    117         fwrite(buf, 1, i, stdout);
    118     }
    119 
    120     ret = EXIT_SUCCESS;
    121     goto done;
    122 
    123 err:
    124     if (ERR_peek_error() == 0) { /* system call error */
    125         fprintf(stderr, "errno=%d ", errno);
    126         perror("error");
    127     } else {
    128         ERR_print_errors_fp(stderr);
    129     }
    130 done:
    131     BIO_free_all(out);
    132     SSL_CTX_free(ssl_ctx);
    133     return ret;
    134 }
    135