1*4724848cSchristos /*
2*4724848cSchristos * Copyright 1998-2017 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9c9496f6bSchristos
10c9496f6bSchristos /*-
11c9496f6bSchristos * A minimal program to do SSL to a passed host and port.
12c9496f6bSchristos * It is actually using non-blocking IO but in a very simple manner
13c9496f6bSchristos * sconnect host:port - it does a 'GET / HTTP/1.0'
14c9496f6bSchristos *
15c9496f6bSchristos * cc -I../../include sconnect.c -L../.. -lssl -lcrypto
16c9496f6bSchristos */
17c9496f6bSchristos #include <stdio.h>
18c9496f6bSchristos #include <stdlib.h>
19c9496f6bSchristos #include <unistd.h>
20*4724848cSchristos #include <string.h>
21*4724848cSchristos #include <errno.h>
22c9496f6bSchristos #include <openssl/err.h>
23c9496f6bSchristos #include <openssl/ssl.h>
24c9496f6bSchristos
25*4724848cSchristos #define HOSTPORT "localhost:4433"
26*4724848cSchristos #define CAFILE "root.pem"
27c9496f6bSchristos
main(int argc,char * argv[])28*4724848cSchristos int main(int argc, char *argv[])
29c9496f6bSchristos {
30*4724848cSchristos const char *hostport = HOSTPORT;
31*4724848cSchristos const char *CAfile = CAFILE;
32*4724848cSchristos char *hostname;
33*4724848cSchristos char *cp;
34*4724848cSchristos BIO *out = NULL;
35c9496f6bSchristos char buf[1024 * 10], *p;
36c9496f6bSchristos SSL_CTX *ssl_ctx = NULL;
37c9496f6bSchristos SSL *ssl;
38c9496f6bSchristos BIO *ssl_bio;
39*4724848cSchristos int i, len, off, ret = EXIT_FAILURE;
40c9496f6bSchristos
41*4724848cSchristos if (argc > 1)
42*4724848cSchristos hostport = argv[1];
43*4724848cSchristos if (argc > 2)
44*4724848cSchristos CAfile = argv[2];
45*4724848cSchristos
46*4724848cSchristos hostname = OPENSSL_strdup(hostport);
47*4724848cSchristos if ((cp = strchr(hostname, ':')) != NULL)
48*4724848cSchristos *cp = 0;
49c9496f6bSchristos
50c9496f6bSchristos #ifdef WATT32
51c9496f6bSchristos dbug_init();
52c9496f6bSchristos sock_init();
53c9496f6bSchristos #endif
54c9496f6bSchristos
55*4724848cSchristos ssl_ctx = SSL_CTX_new(TLS_client_method());
56c9496f6bSchristos
57*4724848cSchristos /* Enable trust chain verification */
58*4724848cSchristos SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
59*4724848cSchristos SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL);
60c9496f6bSchristos
61c9496f6bSchristos /* Lets make a SSL structure */
62c9496f6bSchristos ssl = SSL_new(ssl_ctx);
63c9496f6bSchristos SSL_set_connect_state(ssl);
64c9496f6bSchristos
65*4724848cSchristos /* Enable peername verification */
66*4724848cSchristos if (SSL_set1_host(ssl, hostname) <= 0)
67*4724848cSchristos goto err;
68*4724848cSchristos
69c9496f6bSchristos /* Use it inside an SSL BIO */
70c9496f6bSchristos ssl_bio = BIO_new(BIO_f_ssl());
71c9496f6bSchristos BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
72c9496f6bSchristos
73c9496f6bSchristos /* Lets use a connect BIO under the SSL BIO */
74c9496f6bSchristos out = BIO_new(BIO_s_connect());
75*4724848cSchristos BIO_set_conn_hostname(out, hostport);
76c9496f6bSchristos BIO_set_nbio(out, 1);
77c9496f6bSchristos out = BIO_push(ssl_bio, out);
78c9496f6bSchristos
79c9496f6bSchristos p = "GET / HTTP/1.0\r\n\r\n";
80c9496f6bSchristos len = strlen(p);
81c9496f6bSchristos
82c9496f6bSchristos off = 0;
83c9496f6bSchristos for (;;) {
84c9496f6bSchristos i = BIO_write(out, &(p[off]), len);
85c9496f6bSchristos if (i <= 0) {
86c9496f6bSchristos if (BIO_should_retry(out)) {
87c9496f6bSchristos fprintf(stderr, "write DELAY\n");
88c9496f6bSchristos sleep(1);
89c9496f6bSchristos continue;
90c9496f6bSchristos } else {
91c9496f6bSchristos goto err;
92c9496f6bSchristos }
93c9496f6bSchristos }
94c9496f6bSchristos off += i;
95c9496f6bSchristos len -= i;
96c9496f6bSchristos if (len <= 0)
97c9496f6bSchristos break;
98c9496f6bSchristos }
99c9496f6bSchristos
100c9496f6bSchristos for (;;) {
101c9496f6bSchristos i = BIO_read(out, buf, sizeof(buf));
102c9496f6bSchristos if (i == 0)
103c9496f6bSchristos break;
104c9496f6bSchristos if (i < 0) {
105c9496f6bSchristos if (BIO_should_retry(out)) {
106c9496f6bSchristos fprintf(stderr, "read DELAY\n");
107c9496f6bSchristos sleep(1);
108c9496f6bSchristos continue;
109c9496f6bSchristos }
110c9496f6bSchristos goto err;
111c9496f6bSchristos }
112c9496f6bSchristos fwrite(buf, 1, i, stdout);
113c9496f6bSchristos }
114c9496f6bSchristos
115*4724848cSchristos ret = EXIT_SUCCESS;
116*4724848cSchristos goto done;
117c9496f6bSchristos
118c9496f6bSchristos err:
119c9496f6bSchristos if (ERR_peek_error() == 0) { /* system call error */
120c9496f6bSchristos fprintf(stderr, "errno=%d ", errno);
121c9496f6bSchristos perror("error");
122*4724848cSchristos } else {
123c9496f6bSchristos ERR_print_errors_fp(stderr);
124c9496f6bSchristos }
125*4724848cSchristos done:
126c9496f6bSchristos BIO_free_all(out);
127c9496f6bSchristos SSL_CTX_free(ssl_ctx);
128*4724848cSchristos return ret;
129c9496f6bSchristos }
130