Path: blob/main/crypto/openssl/demos/bio/server-arg.c
107605 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 use the11* SSL_CONF API with the command line. cc -I../../include server-arg.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>2122int main(int argc, char *argv[])23{24char *port = "*:4433";25BIO *ssl_bio = NULL;26BIO *tmp;27SSL_CTX *ctx;28SSL_CONF_CTX *cctx;29char buf[512];30BIO *in = NULL;31int ret = EXIT_FAILURE, i;32char **args = argv + 1;33int nargs = argc - 1;3435ctx = SSL_CTX_new(TLS_server_method());3637cctx = SSL_CONF_CTX_new();38SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);39SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);40SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);41while (*args && **args == '-') {42int rv;43/* Parse standard arguments */44rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);45if (rv == -3) {46fprintf(stderr, "Missing argument for %s\n", *args);47goto err;48}49if (rv < 0) {50fprintf(stderr, "Error in command %s\n", *args);51ERR_print_errors_fp(stderr);52goto err;53}54/* If rv > 0 we processed something so proceed to next arg */55if (rv > 0)56continue;57/* Otherwise application specific argument processing */58if (strcmp(*args, "-port") == 0) {59port = args[1];60if (port == NULL) {61fprintf(stderr, "Missing -port argument\n");62goto err;63}64args += 2;65nargs -= 2;66continue;67} else {68fprintf(stderr, "Unknown argument %s\n", *args);69goto err;70}71}7273if (!SSL_CONF_CTX_finish(cctx)) {74fprintf(stderr, "Finish error\n");75ERR_print_errors_fp(stderr);76goto err;77}78#ifdef ITERATE_CERTS79/*80* Demo of how to iterate over all certificates in an SSL_CTX structure.81*/82{83X509 *x;84int rv;85rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);86while (rv) {87X509 *x = SSL_CTX_get0_certificate(ctx);88X509_NAME_print_ex_fp(stdout, X509_get_subject_name(x), 0,89XN_FLAG_ONELINE);90printf("\n");91rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT);92}93fflush(stdout);94}95#endif96/* Setup server side SSL bio */97ssl_bio = BIO_new_ssl(ctx, 0);9899if ((in = BIO_new_accept(port)) == NULL)100goto err;101102/*103* This means that when a new connection is accepted on 'in', The ssl_bio104* will be 'duplicated' and have the new socket BIO push into it.105* Basically it means the SSL BIO will be automatically setup106*/107BIO_set_accept_bios(in, ssl_bio);108ssl_bio = NULL;109110again:111/*112* The first call will setup the accept socket, and the second will get a113* socket. In this loop, the first actual accept will occur in the114* BIO_read() function.115*/116117if (BIO_do_accept(in) <= 0)118goto err;119120for (;;) {121i = BIO_read(in, buf, 512);122if (i == 0) {123/*124* If we have finished, remove the underlying BIO stack so the125* next time we call any function for this BIO, it will attempt126* to do an accept127*/128printf("Done\n");129tmp = BIO_pop(in);130BIO_free_all(tmp);131goto again;132}133if (i < 0)134goto err;135fwrite(buf, 1, i, stdout);136fflush(stdout);137}138139ret = EXIT_SUCCESS;140err:141if (ret != EXIT_SUCCESS)142ERR_print_errors_fp(stderr);143BIO_free(in);144BIO_free_all(ssl_bio);145return ret;146}147148149