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