Path: blob/main/crypto/openssl/demos/bio/server-conf.c
107074 views
/*1* Copyright 2013-2025 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 = NULL;28BIO *tmp;29SSL_CTX *ctx;30SSL_CONF_CTX *cctx = NULL;31CONF *conf = NULL;32STACK_OF(CONF_VALUE) *sect = NULL;33CONF_VALUE *cnf;34long errline = -1;35char buf[512];36int ret = EXIT_FAILURE, i;3738ctx = SSL_CTX_new(TLS_server_method());3940conf = NCONF_new(NULL);4142if (NCONF_load(conf, "accept.cnf", &errline) <= 0) {43if (errline <= 0)44fprintf(stderr, "Error processing config file\n");45else46fprintf(stderr, "Error on line %ld\n", errline);47goto err;48}4950sect = NCONF_get_section(conf, "default");5152if (sect == NULL) {53fprintf(stderr, "Error retrieving default section\n");54goto err;55}5657cctx = SSL_CONF_CTX_new();58SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);59SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);60SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);61SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);62for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {63int rv;64cnf = sk_CONF_VALUE_value(sect, i);65rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);66if (rv > 0)67continue;68if (rv != -2) {69fprintf(stderr, "Error processing %s = %s\n",70cnf->name, cnf->value);71ERR_print_errors_fp(stderr);72goto err;73}74if (strcmp(cnf->name, "Port") == 0) {75port = cnf->value;76} else {77fprintf(stderr, "Unknown configuration option %s\n", cnf->name);78goto err;79}80}8182if (!SSL_CONF_CTX_finish(cctx)) {83fprintf(stderr, "Finish error\n");84ERR_print_errors_fp(stderr);85goto err;86}8788/* Setup server side SSL bio */89ssl_bio = BIO_new_ssl(ctx, 0);9091if ((in = BIO_new_accept(port)) == NULL)92goto err;9394/*95* This means that when a new connection is accepted on 'in', The ssl_bio96* will be 'duplicated' and have the new socket BIO push into it.97* Basically it means the SSL BIO will be automatically setup98*/99BIO_set_accept_bios(in, ssl_bio);100ssl_bio = NULL;101102again:103/*104* The first call will setup the accept socket, and the second will get a105* socket. In this loop, the first actual accept will occur in the106* BIO_read() function.107*/108109if (BIO_do_accept(in) <= 0)110goto err;111112for (;;) {113i = BIO_read(in, buf, 512);114if (i == 0) {115/*116* If we have finished, remove the underlying BIO stack so the117* next time we call any function for this BIO, it will attempt118* to do an accept119*/120printf("Done\n");121tmp = BIO_pop(in);122BIO_free_all(tmp);123goto again;124}125if (i < 0) {126if (BIO_should_retry(in))127continue;128goto err;129}130fwrite(buf, 1, i, stdout);131fflush(stdout);132}133134ret = EXIT_SUCCESS;135err:136if (ret != EXIT_SUCCESS)137ERR_print_errors_fp(stderr);138BIO_free(in);139BIO_free_all(ssl_bio);140return ret;141}142143144