Initial Commit
This commit is contained in:
22
openssl-1.0.2f/demos/bio/Makefile
Normal file
22
openssl-1.0.2f/demos/bio/Makefile
Normal file
@@ -0,0 +1,22 @@
|
||||
CC=cc
|
||||
CFLAGS= -g -I../../include
|
||||
LIBS= -L../.. ../../libssl.a ../../libcrypto.a -ldl
|
||||
EXAMPLES=saccept sconnect client-arg client-conf
|
||||
|
||||
all: $(EXAMPLES)
|
||||
|
||||
saccept: saccept.o
|
||||
$(CC) -o saccept saccept.o $(LIBS)
|
||||
|
||||
sconnect: sconnect.o
|
||||
$(CC) -o sconnect sconnect.o $(LIBS)
|
||||
|
||||
client-arg: client-arg.o
|
||||
$(CC) -o client-arg client-arg.o $(LIBS)
|
||||
|
||||
client-conf: client-conf.o
|
||||
$(CC) -o client-conf client-conf.o $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f $(EXAMPLES) *.o
|
||||
|
||||
7
openssl-1.0.2f/demos/bio/README
Normal file
7
openssl-1.0.2f/demos/bio/README
Normal file
@@ -0,0 +1,7 @@
|
||||
This directory contains some simple examples of the use of BIO's
|
||||
to simplify socket programming.
|
||||
|
||||
The client-conf, server-conf, client-arg and client-conf include examples
|
||||
of how to use the SSL_CONF API for configuration file or command line
|
||||
processing.
|
||||
|
||||
13
openssl-1.0.2f/demos/bio/accept.cnf
Normal file
13
openssl-1.0.2f/demos/bio/accept.cnf
Normal file
@@ -0,0 +1,13 @@
|
||||
# Example configuration file
|
||||
# Port to listen on
|
||||
Port = 4433
|
||||
# Disable TLS v1.2 for test.
|
||||
# Protocol = ALL, -TLSv1.2
|
||||
# Only support 3 curves
|
||||
Curves = P-521:P-384:P-256
|
||||
# Automatic curve selection
|
||||
ECDHParameters = Automatic
|
||||
# Restricted signature algorithms
|
||||
SignatureAlgorithms = RSA+SHA512:ECDSA+SHA512
|
||||
Certificate=server.pem
|
||||
PrivateKey=server.pem
|
||||
111
openssl-1.0.2f/demos/bio/client-arg.c
Normal file
111
openssl-1.0.2f/demos/bio/client-arg.c
Normal file
@@ -0,0 +1,111 @@
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
BIO *sbio = NULL, *out = NULL;
|
||||
int len;
|
||||
char tmpbuf[1024];
|
||||
SSL_CTX *ctx;
|
||||
SSL_CONF_CTX *cctx;
|
||||
SSL *ssl;
|
||||
char **args = argv + 1;
|
||||
const char *connect_str = "localhost:4433";
|
||||
int nargs = argc - 1;
|
||||
|
||||
ERR_load_crypto_strings();
|
||||
ERR_load_SSL_strings();
|
||||
SSL_library_init();
|
||||
|
||||
ctx = SSL_CTX_new(SSLv23_client_method());
|
||||
cctx = SSL_CONF_CTX_new();
|
||||
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
|
||||
SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
|
||||
while (*args && **args == '-') {
|
||||
int rv;
|
||||
/* Parse standard arguments */
|
||||
rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
|
||||
if (rv == -3) {
|
||||
fprintf(stderr, "Missing argument for %s\n", *args);
|
||||
goto end;
|
||||
}
|
||||
if (rv < 0) {
|
||||
fprintf(stderr, "Error in command %s\n", *args);
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
/* If rv > 0 we processed something so proceed to next arg */
|
||||
if (rv > 0)
|
||||
continue;
|
||||
/* Otherwise application specific argument processing */
|
||||
if (!strcmp(*args, "-connect")) {
|
||||
connect_str = args[1];
|
||||
if (connect_str == NULL) {
|
||||
fprintf(stderr, "Missing -connect argument\n");
|
||||
goto end;
|
||||
}
|
||||
args += 2;
|
||||
nargs -= 2;
|
||||
continue;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown argument %s\n", *args);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (!SSL_CONF_CTX_finish(cctx)) {
|
||||
fprintf(stderr, "Finish error\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* We'd normally set some stuff like the verify paths and * mode here
|
||||
* because as things stand this will connect to * any server whose
|
||||
* certificate is signed by any CA.
|
||||
*/
|
||||
|
||||
sbio = BIO_new_ssl_connect(ctx);
|
||||
|
||||
BIO_get_ssl(sbio, &ssl);
|
||||
|
||||
if (!ssl) {
|
||||
fprintf(stderr, "Can't locate SSL pointer\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* Don't want any retries */
|
||||
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
|
||||
|
||||
/* We might want to do other things with ssl here */
|
||||
|
||||
BIO_set_conn_hostname(sbio, connect_str);
|
||||
|
||||
out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||
if (BIO_do_connect(sbio) <= 0) {
|
||||
fprintf(stderr, "Error connecting to server\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (BIO_do_handshake(sbio) <= 0) {
|
||||
fprintf(stderr, "Error establishing SSL connection\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* Could examine ssl here to get connection info */
|
||||
|
||||
BIO_puts(sbio, "GET / HTTP/1.0\n\n");
|
||||
for (;;) {
|
||||
len = BIO_read(sbio, tmpbuf, 1024);
|
||||
if (len <= 0)
|
||||
break;
|
||||
BIO_write(out, tmpbuf, len);
|
||||
}
|
||||
end:
|
||||
SSL_CONF_CTX_free(cctx);
|
||||
BIO_free_all(sbio);
|
||||
BIO_free(out);
|
||||
return 0;
|
||||
}
|
||||
120
openssl-1.0.2f/demos/bio/client-conf.c
Normal file
120
openssl-1.0.2f/demos/bio/client-conf.c
Normal file
@@ -0,0 +1,120 @@
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/conf.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
BIO *sbio = NULL, *out = NULL;
|
||||
int i, len, rv;
|
||||
char tmpbuf[1024];
|
||||
SSL_CTX *ctx = NULL;
|
||||
SSL_CONF_CTX *cctx = NULL;
|
||||
SSL *ssl = NULL;
|
||||
CONF *conf = NULL;
|
||||
STACK_OF(CONF_VALUE) *sect = NULL;
|
||||
CONF_VALUE *cnf;
|
||||
const char *connect_str = "localhost:4433";
|
||||
long errline = -1;
|
||||
|
||||
ERR_load_crypto_strings();
|
||||
ERR_load_SSL_strings();
|
||||
SSL_library_init();
|
||||
|
||||
conf = NCONF_new(NULL);
|
||||
|
||||
if (NCONF_load(conf, "connect.cnf", &errline) <= 0) {
|
||||
if (errline <= 0)
|
||||
fprintf(stderr, "Error processing config file\n");
|
||||
else
|
||||
fprintf(stderr, "Error on line %ld\n", errline);
|
||||
goto end;
|
||||
}
|
||||
|
||||
sect = NCONF_get_section(conf, "default");
|
||||
|
||||
if (sect == NULL) {
|
||||
fprintf(stderr, "Error retrieving default section\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
ctx = SSL_CTX_new(SSLv23_client_method());
|
||||
cctx = SSL_CONF_CTX_new();
|
||||
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT);
|
||||
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
|
||||
SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
|
||||
for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
|
||||
cnf = sk_CONF_VALUE_value(sect, i);
|
||||
rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
|
||||
if (rv > 0)
|
||||
continue;
|
||||
if (rv != -2) {
|
||||
fprintf(stderr, "Error processing %s = %s\n",
|
||||
cnf->name, cnf->value);
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
if (!strcmp(cnf->name, "Connect")) {
|
||||
connect_str = cnf->value;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (!SSL_CONF_CTX_finish(cctx)) {
|
||||
fprintf(stderr, "Finish error\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* We'd normally set some stuff like the verify paths and * mode here
|
||||
* because as things stand this will connect to * any server whose
|
||||
* certificate is signed by any CA.
|
||||
*/
|
||||
|
||||
sbio = BIO_new_ssl_connect(ctx);
|
||||
|
||||
BIO_get_ssl(sbio, &ssl);
|
||||
|
||||
if (!ssl) {
|
||||
fprintf(stderr, "Can't locate SSL pointer\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* Don't want any retries */
|
||||
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
|
||||
|
||||
/* We might want to do other things with ssl here */
|
||||
|
||||
BIO_set_conn_hostname(sbio, connect_str);
|
||||
|
||||
out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||
if (BIO_do_connect(sbio) <= 0) {
|
||||
fprintf(stderr, "Error connecting to server\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (BIO_do_handshake(sbio) <= 0) {
|
||||
fprintf(stderr, "Error establishing SSL connection\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* Could examine ssl here to get connection info */
|
||||
|
||||
BIO_puts(sbio, "GET / HTTP/1.0\n\n");
|
||||
for (;;) {
|
||||
len = BIO_read(sbio, tmpbuf, 1024);
|
||||
if (len <= 0)
|
||||
break;
|
||||
BIO_write(out, tmpbuf, len);
|
||||
}
|
||||
end:
|
||||
SSL_CONF_CTX_free(cctx);
|
||||
BIO_free_all(sbio);
|
||||
BIO_free(out);
|
||||
NCONF_free(conf);
|
||||
return 0;
|
||||
}
|
||||
9
openssl-1.0.2f/demos/bio/connect.cnf
Normal file
9
openssl-1.0.2f/demos/bio/connect.cnf
Normal file
@@ -0,0 +1,9 @@
|
||||
# Example configuration file
|
||||
# Connects to the default port of s_server
|
||||
Connect = localhost:4433
|
||||
# Disable TLS v1.2 for test.
|
||||
# Protocol = ALL, -TLSv1.2
|
||||
# Only support 3 curves
|
||||
Curves = P-521:P-384:P-256
|
||||
# Restricted signature algorithms
|
||||
SignatureAlgorithms = RSA+SHA512:ECDSA+SHA512
|
||||
117
openssl-1.0.2f/demos/bio/saccept.c
Normal file
117
openssl-1.0.2f/demos/bio/saccept.c
Normal file
@@ -0,0 +1,117 @@
|
||||
/* NOCW */
|
||||
/* demos/bio/saccept.c */
|
||||
|
||||
/*-
|
||||
* A minimal program to serve an SSL connection.
|
||||
* It uses blocking.
|
||||
* saccept host:port
|
||||
* host is the interface IP to use. If any interface, use *:port
|
||||
* The default it *:4433
|
||||
*
|
||||
* cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#define CERT_FILE "server.pem"
|
||||
|
||||
BIO *in = NULL;
|
||||
|
||||
void close_up()
|
||||
{
|
||||
if (in != NULL)
|
||||
BIO_free(in);
|
||||
}
|
||||
|
||||
int main(argc, argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
{
|
||||
char *port = NULL;
|
||||
BIO *ssl_bio, *tmp;
|
||||
SSL_CTX *ctx;
|
||||
SSL *ssl;
|
||||
char buf[512];
|
||||
int ret = 1, i;
|
||||
|
||||
if (argc <= 1)
|
||||
port = "*:4433";
|
||||
else
|
||||
port = argv[1];
|
||||
|
||||
signal(SIGINT, close_up);
|
||||
|
||||
SSL_load_error_strings();
|
||||
|
||||
#ifdef WATT32
|
||||
dbug_init();
|
||||
sock_init();
|
||||
#endif
|
||||
|
||||
/* Add ciphers and message digests */
|
||||
OpenSSL_add_ssl_algorithms();
|
||||
|
||||
ctx = SSL_CTX_new(SSLv23_server_method());
|
||||
if (!SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
|
||||
goto err;
|
||||
if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
|
||||
goto err;
|
||||
if (!SSL_CTX_check_private_key(ctx))
|
||||
goto err;
|
||||
|
||||
/* Setup server side SSL bio */
|
||||
ssl = SSL_new(ctx);
|
||||
ssl_bio = BIO_new_ssl(ctx, 0);
|
||||
|
||||
if ((in = BIO_new_accept(port)) == NULL)
|
||||
goto err;
|
||||
|
||||
/*
|
||||
* This means that when a new connection is accepted on 'in', The ssl_bio
|
||||
* will be 'duplicated' and have the new socket BIO push into it.
|
||||
* Basically it means the SSL BIO will be automatically setup
|
||||
*/
|
||||
BIO_set_accept_bios(in, ssl_bio);
|
||||
|
||||
again:
|
||||
/*
|
||||
* The first call will setup the accept socket, and the second will get a
|
||||
* socket. In this loop, the first actual accept will occur in the
|
||||
* BIO_read() function.
|
||||
*/
|
||||
|
||||
if (BIO_do_accept(in) <= 0)
|
||||
goto err;
|
||||
|
||||
for (;;) {
|
||||
i = BIO_read(in, buf, 512);
|
||||
if (i == 0) {
|
||||
/*
|
||||
* If we have finished, remove the underlying BIO stack so the
|
||||
* next time we call any function for this BIO, it will attempt
|
||||
* to do an accept
|
||||
*/
|
||||
printf("Done\n");
|
||||
tmp = BIO_pop(in);
|
||||
BIO_free_all(tmp);
|
||||
goto again;
|
||||
}
|
||||
if (i < 0)
|
||||
goto err;
|
||||
fwrite(buf, 1, i, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
err:
|
||||
if (ret) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
}
|
||||
if (in != NULL)
|
||||
BIO_free(in);
|
||||
exit(ret);
|
||||
return (!ret);
|
||||
}
|
||||
113
openssl-1.0.2f/demos/bio/sconnect.c
Normal file
113
openssl-1.0.2f/demos/bio/sconnect.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/* NOCW */
|
||||
/* demos/bio/sconnect.c */
|
||||
|
||||
/*-
|
||||
* A minimal program to do SSL to a passed host and port.
|
||||
* 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>
|
||||
#include <unistd.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
extern int errno;
|
||||
|
||||
int main(argc, argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
{
|
||||
char *host;
|
||||
BIO *out;
|
||||
char buf[1024 * 10], *p;
|
||||
SSL_CTX *ssl_ctx = NULL;
|
||||
SSL *ssl;
|
||||
BIO *ssl_bio;
|
||||
int i, len, off, ret = 1;
|
||||
|
||||
if (argc <= 1)
|
||||
host = "localhost:4433";
|
||||
else
|
||||
host = argv[1];
|
||||
|
||||
#ifdef WATT32
|
||||
dbug_init();
|
||||
sock_init();
|
||||
#endif
|
||||
|
||||
/* Lets get nice error messages */
|
||||
SSL_load_error_strings();
|
||||
|
||||
/* Setup all the global SSL stuff */
|
||||
OpenSSL_add_ssl_algorithms();
|
||||
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
|
||||
|
||||
/* Lets make a SSL structure */
|
||||
ssl = SSL_new(ssl_ctx);
|
||||
SSL_set_connect_state(ssl);
|
||||
|
||||
/* 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());
|
||||
BIO_set_conn_hostname(out, host);
|
||||
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);
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
if (0) {
|
||||
err:
|
||||
if (ERR_peek_error() == 0) { /* system call error */
|
||||
fprintf(stderr, "errno=%d ", errno);
|
||||
perror("error");
|
||||
} else
|
||||
ERR_print_errors_fp(stderr);
|
||||
}
|
||||
BIO_free_all(out);
|
||||
if (ssl_ctx != NULL)
|
||||
SSL_CTX_free(ssl_ctx);
|
||||
exit(!ret);
|
||||
return (ret);
|
||||
}
|
||||
144
openssl-1.0.2f/demos/bio/server-arg.c
Normal file
144
openssl-1.0.2f/demos/bio/server-arg.c
Normal file
@@ -0,0 +1,144 @@
|
||||
/* NOCW */
|
||||
/* demos/bio/server-arg.c */
|
||||
|
||||
/*
|
||||
* A minimal program to serve an SSL connection. It uses blocking. It use the
|
||||
* SSL_CONF API with the command line. cc -I../../include server-arg.c
|
||||
* -L../.. -lssl -lcrypto -ldl
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *port = "*:4433";
|
||||
BIO *ssl_bio, *tmp;
|
||||
SSL_CTX *ctx;
|
||||
SSL_CONF_CTX *cctx;
|
||||
char buf[512];
|
||||
BIO *in = NULL;
|
||||
int ret = 1, i;
|
||||
char **args = argv + 1;
|
||||
int nargs = argc - 1;
|
||||
|
||||
SSL_load_error_strings();
|
||||
|
||||
/* Add ciphers and message digests */
|
||||
OpenSSL_add_ssl_algorithms();
|
||||
|
||||
ctx = SSL_CTX_new(SSLv23_server_method());
|
||||
|
||||
cctx = SSL_CONF_CTX_new();
|
||||
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
|
||||
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);
|
||||
SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
|
||||
while (*args && **args == '-') {
|
||||
int rv;
|
||||
/* Parse standard arguments */
|
||||
rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
|
||||
if (rv == -3) {
|
||||
fprintf(stderr, "Missing argument for %s\n", *args);
|
||||
goto err;
|
||||
}
|
||||
if (rv < 0) {
|
||||
fprintf(stderr, "Error in command %s\n", *args);
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
/* If rv > 0 we processed something so proceed to next arg */
|
||||
if (rv > 0)
|
||||
continue;
|
||||
/* Otherwise application specific argument processing */
|
||||
if (!strcmp(*args, "-port")) {
|
||||
port = args[1];
|
||||
if (port == NULL) {
|
||||
fprintf(stderr, "Missing -port argument\n");
|
||||
goto err;
|
||||
}
|
||||
args += 2;
|
||||
nargs -= 2;
|
||||
continue;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown argument %s\n", *args);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!SSL_CONF_CTX_finish(cctx)) {
|
||||
fprintf(stderr, "Finish error\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
#if 0
|
||||
/*
|
||||
* Demo of how to iterate over all certificates in an SSL_CTX structure.
|
||||
*/
|
||||
{
|
||||
X509 *x;
|
||||
int rv;
|
||||
rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
|
||||
while (rv) {
|
||||
X509 *x = SSL_CTX_get0_certificate(ctx);
|
||||
X509_NAME_print_ex_fp(stdout, X509_get_subject_name(x), 0,
|
||||
XN_FLAG_ONELINE);
|
||||
printf("\n");
|
||||
rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
#endif
|
||||
/* Setup server side SSL bio */
|
||||
ssl_bio = BIO_new_ssl(ctx, 0);
|
||||
|
||||
if ((in = BIO_new_accept(port)) == NULL)
|
||||
goto err;
|
||||
|
||||
/*
|
||||
* This means that when a new connection is accepted on 'in', The ssl_bio
|
||||
* will be 'duplicated' and have the new socket BIO push into it.
|
||||
* Basically it means the SSL BIO will be automatically setup
|
||||
*/
|
||||
BIO_set_accept_bios(in, ssl_bio);
|
||||
|
||||
again:
|
||||
/*
|
||||
* The first call will setup the accept socket, and the second will get a
|
||||
* socket. In this loop, the first actual accept will occur in the
|
||||
* BIO_read() function.
|
||||
*/
|
||||
|
||||
if (BIO_do_accept(in) <= 0)
|
||||
goto err;
|
||||
|
||||
for (;;) {
|
||||
i = BIO_read(in, buf, 512);
|
||||
if (i == 0) {
|
||||
/*
|
||||
* If we have finished, remove the underlying BIO stack so the
|
||||
* next time we call any function for this BIO, it will attempt
|
||||
* to do an accept
|
||||
*/
|
||||
printf("Done\n");
|
||||
tmp = BIO_pop(in);
|
||||
BIO_free_all(tmp);
|
||||
goto again;
|
||||
}
|
||||
if (i < 0)
|
||||
goto err;
|
||||
fwrite(buf, 1, i, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
err:
|
||||
if (ret) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
}
|
||||
if (in != NULL)
|
||||
BIO_free(in);
|
||||
exit(ret);
|
||||
return (!ret);
|
||||
}
|
||||
138
openssl-1.0.2f/demos/bio/server-conf.c
Normal file
138
openssl-1.0.2f/demos/bio/server-conf.c
Normal file
@@ -0,0 +1,138 @@
|
||||
/* NOCW */
|
||||
/* demos/bio/saccept-conf.c */
|
||||
|
||||
/*
|
||||
* A minimal program to serve an SSL connection. It uses blocking. It uses
|
||||
* the SSL_CONF API with a configuration file. cc -I../../include saccept.c
|
||||
* -L../.. -lssl -lcrypto -ldl
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/conf.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *port = "*:4433";
|
||||
BIO *in = NULL;
|
||||
BIO *ssl_bio, *tmp;
|
||||
SSL_CTX *ctx;
|
||||
SSL_CONF_CTX *cctx = NULL;
|
||||
CONF *conf = NULL;
|
||||
STACK_OF(CONF_VALUE) *sect = NULL;
|
||||
CONF_VALUE *cnf;
|
||||
long errline = -1;
|
||||
char buf[512];
|
||||
int ret = 1, i;
|
||||
|
||||
SSL_load_error_strings();
|
||||
|
||||
/* Add ciphers and message digests */
|
||||
OpenSSL_add_ssl_algorithms();
|
||||
|
||||
conf = NCONF_new(NULL);
|
||||
|
||||
if (NCONF_load(conf, "accept.cnf", &errline) <= 0) {
|
||||
if (errline <= 0)
|
||||
fprintf(stderr, "Error processing config file\n");
|
||||
else
|
||||
fprintf(stderr, "Error on line %ld\n", errline);
|
||||
goto err;
|
||||
}
|
||||
|
||||
sect = NCONF_get_section(conf, "default");
|
||||
|
||||
if (sect == NULL) {
|
||||
fprintf(stderr, "Error retrieving default section\n");
|
||||
goto err;
|
||||
}
|
||||
|
||||
ctx = SSL_CTX_new(SSLv23_server_method());
|
||||
cctx = SSL_CONF_CTX_new();
|
||||
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
|
||||
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);
|
||||
SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
|
||||
SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
|
||||
for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
|
||||
int rv;
|
||||
cnf = sk_CONF_VALUE_value(sect, i);
|
||||
rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
|
||||
if (rv > 0)
|
||||
continue;
|
||||
if (rv != -2) {
|
||||
fprintf(stderr, "Error processing %s = %s\n",
|
||||
cnf->name, cnf->value);
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
if (!strcmp(cnf->name, "Port")) {
|
||||
port = cnf->value;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!SSL_CONF_CTX_finish(cctx)) {
|
||||
fprintf(stderr, "Finish error\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Setup server side SSL bio */
|
||||
ssl_bio = BIO_new_ssl(ctx, 0);
|
||||
|
||||
if ((in = BIO_new_accept(port)) == NULL)
|
||||
goto err;
|
||||
|
||||
/*
|
||||
* This means that when a new connection is accepted on 'in', The ssl_bio
|
||||
* will be 'duplicated' and have the new socket BIO push into it.
|
||||
* Basically it means the SSL BIO will be automatically setup
|
||||
*/
|
||||
BIO_set_accept_bios(in, ssl_bio);
|
||||
|
||||
again:
|
||||
/*
|
||||
* The first call will setup the accept socket, and the second will get a
|
||||
* socket. In this loop, the first actual accept will occur in the
|
||||
* BIO_read() function.
|
||||
*/
|
||||
|
||||
if (BIO_do_accept(in) <= 0)
|
||||
goto err;
|
||||
|
||||
for (;;) {
|
||||
i = BIO_read(in, buf, 512);
|
||||
if (i == 0) {
|
||||
/*
|
||||
* If we have finished, remove the underlying BIO stack so the
|
||||
* next time we call any function for this BIO, it will attempt
|
||||
* to do an accept
|
||||
*/
|
||||
printf("Done\n");
|
||||
tmp = BIO_pop(in);
|
||||
BIO_free_all(tmp);
|
||||
goto again;
|
||||
}
|
||||
if (i < 0) {
|
||||
if (BIO_should_retry(in))
|
||||
continue;
|
||||
goto err;
|
||||
}
|
||||
fwrite(buf, 1, i, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
err:
|
||||
if (ret) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
}
|
||||
if (in != NULL)
|
||||
BIO_free(in);
|
||||
exit(ret);
|
||||
return (!ret);
|
||||
}
|
||||
52
openssl-1.0.2f/demos/bio/server.pem
Normal file
52
openssl-1.0.2f/demos/bio/server.pem
Normal file
@@ -0,0 +1,52 @@
|
||||
subject= C = UK, O = OpenSSL Group, OU = FOR TESTING PURPOSES ONLY, CN = Test Server Cert
|
||||
issuer= C = UK, O = OpenSSL Group, OU = FOR TESTING PURPOSES ONLY, CN = OpenSSL Test Intermediate CA
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIID5zCCAs+gAwIBAgIJALnu1NlVpZ6zMA0GCSqGSIb3DQEBBQUAMHAxCzAJBgNV
|
||||
BAYTAlVLMRYwFAYDVQQKDA1PcGVuU1NMIEdyb3VwMSIwIAYDVQQLDBlGT1IgVEVT
|
||||
VElORyBQVVJQT1NFUyBPTkxZMSUwIwYDVQQDDBxPcGVuU1NMIFRlc3QgSW50ZXJt
|
||||
ZWRpYXRlIENBMB4XDTExMTIwODE0MDE0OFoXDTIxMTAxNjE0MDE0OFowZDELMAkG
|
||||
A1UEBhMCVUsxFjAUBgNVBAoMDU9wZW5TU0wgR3JvdXAxIjAgBgNVBAsMGUZPUiBU
|
||||
RVNUSU5HIFBVUlBPU0VTIE9OTFkxGTAXBgNVBAMMEFRlc3QgU2VydmVyIENlcnQw
|
||||
ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDzhPOSNtyyRspmeuUpxfNJ
|
||||
KCLTuf7g3uQ4zu4iHOmRO5TQci+HhVlLZrHF9XqFXcIP0y4pWDbMSGuiorUmzmfi
|
||||
R7bfSdI/+qIQt8KXRH6HNG1t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zWFd+RB3kHWrCjnqODQoI6GbGr/4JepHUpre1ex
|
||||
4UEN5oSS3G0ru0rC3U4C59dZ5KwDHFm7ffZ1pr52ljfQDUsrjjIMRtuiwNK2OoRa
|
||||
WSsqiaL+SDzSB+nBmpnAizECgYBdt/y6rerWUx4MhDwwtTnel7JwHyo2MDFS6/5g
|
||||
n8qC2Lj6/fMDRE22w+CA2esp7EJNQJGv+b27iFpbJEDh+/Lf5YzIT4MwVskQ5bYB
|
||||
JFcmRxUVmf4e09D7o705U/DjCgMH09iCsbLmqQ38ONIRSHZaJtMDtNTHD1yi+jF+
|
||||
OT43gQKBgQC/2OHZoko6iRlNOAQ/tMVFNq7fL81GivoQ9F1U0Qr+DH3ZfaH8eIkX
|
||||
xT0ToMPJUzWAn8pZv0snA0um6SIgvkCuxO84OkANCVbttzXImIsL7pFzfcwV/ERK
|
||||
UM6j0ZuSMFOCr/lGPAoOQU0fskidGEHi1/kW+suSr28TqsyYZpwBDQ==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
Reference in New Issue
Block a user