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 BIO_free(ssl_bio);
74 goto err;
75 }
76
77 BIO_set_nbio(out, 1);
78 out = BIO_push(ssl_bio, out);
79
80 p = "GET / HTTP/1.0\r\n\r\n";
81 len = strlen(p);
82
83 off = 0;
84 for (;;) {
85 i = BIO_write(out, &(p[off]), len);
86 if (i <= 0) {
87 if (BIO_should_retry(out)) {
88 fprintf(stderr, "write DELAY\n");
89 sleep(1);
90 continue;
91 } else {
92 goto err;
93 }
94 }
95 off += i;
96 len -= i;
97 if (len <= 0)
98 break;
99 }
100
101 for (;;) {
102 i = BIO_read(out, buf, sizeof(buf));
103 if (i == 0)
104 break;
105 if (i < 0) {
106 if (BIO_should_retry(out)) {
107 fprintf(stderr, "read DELAY\n");
108 sleep(1);
109 continue;
110 }
111 goto err;
112 }
113 fwrite(buf, 1, i, stdout);
114 }
115
116 ret = EXIT_SUCCESS;
117 goto done;
118
119 err:
120 if (ERR_peek_error() == 0) { /* system call error */
121 fprintf(stderr, "errno=%d ", errno);
122 perror("error");
123 } else {
124 ERR_print_errors_fp(stderr);
125 }
126 done:
127 BIO_free_all(out);
128 SSL_CTX_free(ssl_ctx);
129 return ret;
130 }
131