Path: blob/main/crypto/openssl/demos/bio/server-conf.c
34869 views
/*1* Copyright 2013-2017 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* A minimal program to serve an SSL connection. It uses blocking. It uses11* the SSL_CONF API with a configuration file. cc -I../../include saccept.c12* -L../.. -lssl -lcrypto -ldl13*/1415#include <stdio.h>16#include <string.h>17#include <signal.h>18#include <stdlib.h>19#include <openssl/err.h>20#include <openssl/ssl.h>21#include <openssl/conf.h>2223int main(int argc, char *argv[])24{25char *port = "*:4433";26BIO *in = NULL;27BIO *ssl_bio, *tmp;28SSL_CTX *ctx;29SSL_CONF_CTX *cctx = NULL;30CONF *conf = NULL;31STACK_OF(CONF_VALUE) *sect = NULL;32CONF_VALUE *cnf;33long errline = -1;34char buf[512];35int ret = EXIT_FAILURE, i;3637ctx = SSL_CTX_new(TLS_server_method());3839conf = NCONF_new(NULL);4041if (NCONF_load(conf, "accept.cnf", &errline) <= 0) {42if (errline <= 0)43fprintf(stderr, "Error processing config file\n");44else45fprintf(stderr, "Error on line %ld\n", errline);46goto err;47}4849sect = NCONF_get_section(conf, "default");5051if (sect == NULL) {52fprintf(stderr, "Error retrieving default section\n");53goto err;54}5556cctx = SSL_CONF_CTX_new();57SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);58SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);59SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);60SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);61for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {62int rv;63cnf = sk_CONF_VALUE_value(sect, i);64rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);65if (rv > 0)66continue;67if (rv != -2) {68fprintf(stderr, "Error processing %s = %s\n",69cnf->name, cnf->value);70ERR_print_errors_fp(stderr);71goto err;72}73if (strcmp(cnf->name, "Port") == 0) {74port = cnf->value;75} else {76fprintf(stderr, "Unknown configuration option %s\n", cnf->name);77goto err;78}79}8081if (!SSL_CONF_CTX_finish(cctx)) {82fprintf(stderr, "Finish error\n");83ERR_print_errors_fp(stderr);84goto err;85}8687/* Setup server side SSL bio */88ssl_bio = BIO_new_ssl(ctx, 0);8990if ((in = BIO_new_accept(port)) == NULL)91goto err;9293/*94* This means that when a new connection is accepted on 'in', The ssl_bio95* will be 'duplicated' and have the new socket BIO push into it.96* Basically it means the SSL BIO will be automatically setup97*/98BIO_set_accept_bios(in, ssl_bio);99100again:101/*102* The first call will setup the accept socket, and the second will get a103* socket. In this loop, the first actual accept will occur in the104* BIO_read() function.105*/106107if (BIO_do_accept(in) <= 0)108goto err;109110for (;;) {111i = BIO_read(in, buf, 512);112if (i == 0) {113/*114* If we have finished, remove the underlying BIO stack so the115* next time we call any function for this BIO, it will attempt116* to do an accept117*/118printf("Done\n");119tmp = BIO_pop(in);120BIO_free_all(tmp);121goto again;122}123if (i < 0) {124if (BIO_should_retry(in))125continue;126goto err;127}128fwrite(buf, 1, i, stdout);129fflush(stdout);130}131132ret = EXIT_SUCCESS;133err:134if (ret != EXIT_SUCCESS)135ERR_print_errors_fp(stderr);136BIO_free(in);137return ret;138}139140141