664a1e038cf9e8e000c576472f083c709fa2ac3a
[openssl.git] / demos / bio / sconnect.c
1 /*
2  * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <unistd.h>
20 #include <string.h>
21 #include <openssl/err.h>
22 #include <openssl/ssl.h>
23
24 #define HOSTPORT "localhost:4433"
25 #define CAFILE "root.pem"
26
27 extern int errno;
28
29 int main(argc, argv)
30 int argc;
31 char *argv[];
32 {
33     const char *hostport = HOSTPORT;
34     const char *CAfile = CAFILE;
35     char *hostname;
36     char *cp;
37     BIO *out = NULL;
38     char buf[1024 * 10], *p;
39     SSL_CTX *ssl_ctx = NULL;
40     SSL *ssl;
41     BIO *ssl_bio;
42     int i, len, off, ret = 1;
43
44     if (argc > 1)
45         hostport = argv[1];
46     if (argc > 2)
47         CAfile = argv[2];
48
49     hostname = OPENSSL_strdup(hostport);
50     if ((cp = strchr(hostname, ':')) != NULL)
51         *cp = 0;
52
53 #ifdef WATT32
54     dbug_init();
55     sock_init();
56 #endif
57
58     ssl_ctx = SSL_CTX_new(TLS_client_method());
59
60     /* Enable trust chain verification */
61     SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
62     SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL);
63
64     /* Lets make a SSL structure */
65     ssl = SSL_new(ssl_ctx);
66     SSL_set_connect_state(ssl);
67
68     /* Enable peername verification */
69     if (SSL_set1_host(ssl, hostname) <= 0)
70         goto err;
71
72     /* Use it inside an SSL BIO */
73     ssl_bio = BIO_new(BIO_f_ssl());
74     BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
75
76     /* Lets use a connect BIO under the SSL BIO */
77     out = BIO_new(BIO_s_connect());
78     BIO_set_conn_hostname(out, hostport);
79     BIO_set_nbio(out, 1);
80     out = BIO_push(ssl_bio, out);
81
82     p = "GET / HTTP/1.0\r\n\r\n";
83     len = strlen(p);
84
85     off = 0;
86     for (;;) {
87         i = BIO_write(out, &(p[off]), len);
88         if (i <= 0) {
89             if (BIO_should_retry(out)) {
90                 fprintf(stderr, "write DELAY\n");
91                 sleep(1);
92                 continue;
93             } else {
94                 goto err;
95             }
96         }
97         off += i;
98         len -= i;
99         if (len <= 0)
100             break;
101     }
102
103     for (;;) {
104         i = BIO_read(out, buf, sizeof(buf));
105         if (i == 0)
106             break;
107         if (i < 0) {
108             if (BIO_should_retry(out)) {
109                 fprintf(stderr, "read DELAY\n");
110                 sleep(1);
111                 continue;
112             }
113             goto err;
114         }
115         fwrite(buf, 1, i, stdout);
116     }
117
118     ret = 1;
119     goto done;
120
121  err:
122     if (ERR_peek_error() == 0) { /* system call error */
123         fprintf(stderr, "errno=%d ", errno);
124         perror("error");
125     } else
126         ERR_print_errors_fp(stderr);
127  done:
128     BIO_free_all(out);
129     SSL_CTX_free(ssl_ctx);
130     return (ret == 1);
131 }