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