Path: blob/main/crypto/openssl/demos/bio/server-arg.c
34868 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 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, *tmp;26SSL_CTX *ctx;27SSL_CONF_CTX *cctx;28char buf[512];29BIO *in = NULL;30int ret = EXIT_FAILURE, i;31char **args = argv + 1;32int nargs = argc - 1;3334ctx = SSL_CTX_new(TLS_server_method());3536cctx = SSL_CONF_CTX_new();37SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);38SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);39SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);40while (*args && **args == '-') {41int rv;42/* Parse standard arguments */43rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);44if (rv == -3) {45fprintf(stderr, "Missing argument for %s\n", *args);46goto err;47}48if (rv < 0) {49fprintf(stderr, "Error in command %s\n", *args);50ERR_print_errors_fp(stderr);51goto err;52}53/* If rv > 0 we processed something so proceed to next arg */54if (rv > 0)55continue;56/* Otherwise application specific argument processing */57if (strcmp(*args, "-port") == 0) {58port = args[1];59if (port == NULL) {60fprintf(stderr, "Missing -port argument\n");61goto err;62}63args += 2;64nargs -= 2;65continue;66} else {67fprintf(stderr, "Unknown argument %s\n", *args);68goto err;69}70}7172if (!SSL_CONF_CTX_finish(cctx)) {73fprintf(stderr, "Finish error\n");74ERR_print_errors_fp(stderr);75goto err;76}77#ifdef ITERATE_CERTS78/*79* Demo of how to iterate over all certificates in an SSL_CTX structure.80*/81{82X509 *x;83int rv;84rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);85while (rv) {86X509 *x = SSL_CTX_get0_certificate(ctx);87X509_NAME_print_ex_fp(stdout, X509_get_subject_name(x), 0,88XN_FLAG_ONELINE);89printf("\n");90rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT);91}92fflush(stdout);93}94#endif95/* Setup server side SSL bio */96ssl_bio = BIO_new_ssl(ctx, 0);9798if ((in = BIO_new_accept(port)) == NULL)99goto err;100101/*102* This means that when a new connection is accepted on 'in', The ssl_bio103* will be 'duplicated' and have the new socket BIO push into it.104* Basically it means the SSL BIO will be automatically setup105*/106BIO_set_accept_bios(in, ssl_bio);107108again:109/*110* The first call will setup the accept socket, and the second will get a111* socket. In this loop, the first actual accept will occur in the112* BIO_read() function.113*/114115if (BIO_do_accept(in) <= 0)116goto err;117118for (;;) {119i = BIO_read(in, buf, 512);120if (i == 0) {121/*122* If we have finished, remove the underlying BIO stack so the123* next time we call any function for this BIO, it will attempt124* to do an accept125*/126printf("Done\n");127tmp = BIO_pop(in);128BIO_free_all(tmp);129goto again;130}131if (i < 0)132goto err;133fwrite(buf, 1, i, stdout);134fflush(stdout);135}136137ret = EXIT_SUCCESS;138err:139if (ret != EXIT_SUCCESS)140ERR_print_errors_fp(stderr);141BIO_free(in);142return ret;143}144145146