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