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