Consolidate copyright for demos
[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     /* Lets get nice error messages */
59     SSL_load_error_strings();
60
61     /* Setup all the global SSL stuff */
62     OpenSSL_add_ssl_algorithms();
63     ssl_ctx = SSL_CTX_new(TLS_client_method());
64
65     /* Enable trust chain verification */
66     SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
67     SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL);
68
69     /* Lets make a SSL structure */
70     ssl = SSL_new(ssl_ctx);
71     SSL_set_connect_state(ssl);
72
73     /* Enable peername verification */
74     if (SSL_set1_host(ssl, hostname) <= 0)
75         goto err;
76
77     /* Use it inside an SSL BIO */
78     ssl_bio = BIO_new(BIO_f_ssl());
79     BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
80
81     /* Lets use a connect BIO under the SSL BIO */
82     out = BIO_new(BIO_s_connect());
83     BIO_set_conn_hostname(out, hostport);
84     BIO_set_nbio(out, 1);
85     out = BIO_push(ssl_bio, out);
86
87     p = "GET / HTTP/1.0\r\n\r\n";
88     len = strlen(p);
89
90     off = 0;
91     for (;;) {
92         i = BIO_write(out, &(p[off]), len);
93         if (i <= 0) {
94             if (BIO_should_retry(out)) {
95                 fprintf(stderr, "write DELAY\n");
96                 sleep(1);
97                 continue;
98             } else {
99                 goto err;
100             }
101         }
102         off += i;
103         len -= i;
104         if (len <= 0)
105             break;
106     }
107
108     for (;;) {
109         i = BIO_read(out, buf, sizeof(buf));
110         if (i == 0)
111             break;
112         if (i < 0) {
113             if (BIO_should_retry(out)) {
114                 fprintf(stderr, "read DELAY\n");
115                 sleep(1);
116                 continue;
117             }
118             goto err;
119         }
120         fwrite(buf, 1, i, stdout);
121     }
122
123     ret = 1;
124     goto done;
125
126  err:
127     if (ERR_peek_error() == 0) { /* system call error */
128         fprintf(stderr, "errno=%d ", errno);
129         perror("error");
130     } else
131         ERR_print_errors_fp(stderr);
132  done:
133     BIO_free_all(out);
134     SSL_CTX_free(ssl_ctx);
135     return (ret == 1);
136 }