2016-05-18 13:16:36 +00:00
|
|
|
/*
|
2017-07-12 04:18:00 +00:00
|
|
|
* Copyright 1998-2017 The OpenSSL Project Authors. All Rights Reserved.
|
2016-05-18 13:16:36 +00:00
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2015-01-16 14:43:29 +00:00
|
|
|
/*-
|
|
|
|
* A minimal program to do SSL to a passed host and port.
|
1998-12-21 10:52:47 +00:00
|
|
|
* It is actually using non-blocking IO but in a very simple manner
|
|
|
|
* sconnect host:port - it does a 'GET / HTTP/1.0'
|
|
|
|
*
|
|
|
|
* cc -I../../include sconnect.c -L../.. -lssl -lcrypto
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1999-05-27 23:52:31 +00:00
|
|
|
#include <unistd.h>
|
2015-12-29 18:28:28 +00:00
|
|
|
#include <string.h>
|
2017-07-12 04:18:00 +00:00
|
|
|
#include <errno.h>
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/ssl.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-12-29 18:28:28 +00:00
|
|
|
#define HOSTPORT "localhost:4433"
|
|
|
|
#define CAFILE "root.pem"
|
|
|
|
|
2017-07-12 04:18:00 +00:00
|
|
|
int main(int argc, char *argv[])
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-12-29 18:28:28 +00:00
|
|
|
const char *hostport = HOSTPORT;
|
|
|
|
const char *CAfile = CAFILE;
|
|
|
|
char *hostname;
|
|
|
|
char *cp;
|
|
|
|
BIO *out = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
char buf[1024 * 10], *p;
|
|
|
|
SSL_CTX *ssl_ctx = NULL;
|
|
|
|
SSL *ssl;
|
|
|
|
BIO *ssl_bio;
|
2017-07-12 04:18:00 +00:00
|
|
|
int i, len, off, ret = EXIT_FAILURE;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-12-29 18:28:28 +00:00
|
|
|
if (argc > 1)
|
|
|
|
hostport = argv[1];
|
|
|
|
if (argc > 2)
|
|
|
|
CAfile = argv[2];
|
|
|
|
|
|
|
|
hostname = OPENSSL_strdup(hostport);
|
|
|
|
if ((cp = strchr(hostname, ':')) != NULL)
|
|
|
|
*cp = 0;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2014-08-09 12:02:20 +00:00
|
|
|
#ifdef WATT32
|
2015-01-22 03:40:55 +00:00
|
|
|
dbug_init();
|
|
|
|
sock_init();
|
2014-08-09 12:02:20 +00:00
|
|
|
#endif
|
|
|
|
|
2015-03-30 23:18:31 +00:00
|
|
|
ssl_ctx = SSL_CTX_new(TLS_client_method());
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-12-29 18:28:28 +00:00
|
|
|
/* Enable trust chain verification */
|
|
|
|
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
|
|
|
|
SSL_CTX_load_verify_locations(ssl_ctx, CAfile, NULL);
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Lets make a SSL structure */
|
|
|
|
ssl = SSL_new(ssl_ctx);
|
|
|
|
SSL_set_connect_state(ssl);
|
|
|
|
|
2015-12-29 18:28:28 +00:00
|
|
|
/* Enable peername verification */
|
|
|
|
if (SSL_set1_host(ssl, hostname) <= 0)
|
|
|
|
goto err;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Use it inside an SSL BIO */
|
|
|
|
ssl_bio = BIO_new(BIO_f_ssl());
|
|
|
|
BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
|
|
|
|
|
|
|
|
/* Lets use a connect BIO under the SSL BIO */
|
|
|
|
out = BIO_new(BIO_s_connect());
|
2015-12-29 18:28:28 +00:00
|
|
|
BIO_set_conn_hostname(out, hostport);
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO_set_nbio(out, 1);
|
|
|
|
out = BIO_push(ssl_bio, out);
|
|
|
|
|
|
|
|
p = "GET / HTTP/1.0\r\n\r\n";
|
|
|
|
len = strlen(p);
|
|
|
|
|
|
|
|
off = 0;
|
|
|
|
for (;;) {
|
|
|
|
i = BIO_write(out, &(p[off]), len);
|
|
|
|
if (i <= 0) {
|
|
|
|
if (BIO_should_retry(out)) {
|
|
|
|
fprintf(stderr, "write DELAY\n");
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
off += i;
|
|
|
|
len -= i;
|
|
|
|
if (len <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
i = BIO_read(out, buf, sizeof(buf));
|
|
|
|
if (i == 0)
|
|
|
|
break;
|
|
|
|
if (i < 0) {
|
|
|
|
if (BIO_should_retry(out)) {
|
|
|
|
fprintf(stderr, "read DELAY\n");
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
fwrite(buf, 1, i, stdout);
|
|
|
|
}
|
|
|
|
|
2017-07-12 04:18:00 +00:00
|
|
|
ret = EXIT_SUCCESS;
|
2015-05-01 18:29:48 +00:00
|
|
|
goto done;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
err:
|
2015-05-01 18:29:48 +00:00
|
|
|
if (ERR_peek_error() == 0) { /* system call error */
|
|
|
|
fprintf(stderr, "errno=%d ", errno);
|
|
|
|
perror("error");
|
2017-07-12 04:18:00 +00:00
|
|
|
} else {
|
2015-05-01 18:29:48 +00:00
|
|
|
ERR_print_errors_fp(stderr);
|
2017-07-12 04:18:00 +00:00
|
|
|
}
|
2015-05-01 18:29:48 +00:00
|
|
|
done:
|
2015-01-22 03:40:55 +00:00
|
|
|
BIO_free_all(out);
|
2015-04-11 14:22:36 +00:00
|
|
|
SSL_CTX_free(ssl_ctx);
|
2017-07-12 04:18:00 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|