yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1998-2017 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
| 8 | */ |
| 9 | |
| 10 | /*- |
| 11 | * A minimal program to serve an SSL connection. |
| 12 | * It uses blocking. |
| 13 | * saccept host:port |
| 14 | * host is the interface IP to use. If any interface, use *:port |
| 15 | * The default it *:4433 |
| 16 | * |
| 17 | * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <signal.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/ssl.h> |
| 25 | |
| 26 | #define CERT_FILE "server.pem" |
| 27 | |
| 28 | static volatile int done = 0; |
| 29 | |
| 30 | void interrupt(int sig) |
| 31 | { |
| 32 | done = 1; |
| 33 | } |
| 34 | |
| 35 | void sigsetup(void) |
| 36 | { |
| 37 | struct sigaction sa; |
| 38 | |
| 39 | /* |
| 40 | * Catch at most once, and don't restart the accept system call. |
| 41 | */ |
| 42 | sa.sa_flags = SA_RESETHAND; |
| 43 | sa.sa_handler = interrupt; |
| 44 | sigemptyset(&sa.sa_mask); |
| 45 | sigaction(SIGINT, &sa, NULL); |
| 46 | } |
| 47 | |
| 48 | int main(int argc, char *argv[]) |
| 49 | { |
| 50 | char *port = NULL; |
| 51 | BIO *in = NULL; |
| 52 | BIO *ssl_bio, *tmp; |
| 53 | SSL_CTX *ctx; |
| 54 | char buf[512]; |
| 55 | int ret = EXIT_FAILURE, i; |
| 56 | |
| 57 | if (argc <= 1) |
| 58 | port = "*:4433"; |
| 59 | else |
| 60 | port = argv[1]; |
| 61 | |
| 62 | ctx = SSL_CTX_new(TLS_server_method()); |
| 63 | if (!SSL_CTX_use_certificate_chain_file(ctx, CERT_FILE)) |
| 64 | goto err; |
| 65 | if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM)) |
| 66 | goto err; |
| 67 | if (!SSL_CTX_check_private_key(ctx)) |
| 68 | goto err; |
| 69 | |
| 70 | /* Setup server side SSL bio */ |
| 71 | ssl_bio = BIO_new_ssl(ctx, 0); |
| 72 | |
| 73 | if ((in = BIO_new_accept(port)) == NULL) |
| 74 | goto err; |
| 75 | |
| 76 | /* |
| 77 | * This means that when a new connection is accepted on 'in', The ssl_bio |
| 78 | * will be 'duplicated' and have the new socket BIO push into it. |
| 79 | * Basically it means the SSL BIO will be automatically setup |
| 80 | */ |
| 81 | BIO_set_accept_bios(in, ssl_bio); |
| 82 | |
| 83 | /* Arrange to leave server loop on interrupt */ |
| 84 | sigsetup(); |
| 85 | |
| 86 | again: |
| 87 | /* |
| 88 | * The first call will setup the accept socket, and the second will get a |
| 89 | * socket. In this loop, the first actual accept will occur in the |
| 90 | * BIO_read() function. |
| 91 | */ |
| 92 | |
| 93 | if (BIO_do_accept(in) <= 0) |
| 94 | goto err; |
| 95 | |
| 96 | while (!done) { |
| 97 | i = BIO_read(in, buf, 512); |
| 98 | if (i == 0) { |
| 99 | /* |
| 100 | * If we have finished, remove the underlying BIO stack so the |
| 101 | * next time we call any function for this BIO, it will attempt |
| 102 | * to do an accept |
| 103 | */ |
| 104 | printf("Done\n"); |
| 105 | tmp = BIO_pop(in); |
| 106 | BIO_free_all(tmp); |
| 107 | goto again; |
| 108 | } |
| 109 | if (i < 0) |
| 110 | goto err; |
| 111 | fwrite(buf, 1, i, stdout); |
| 112 | fflush(stdout); |
| 113 | } |
| 114 | |
| 115 | ret = EXIT_SUCCESS; |
| 116 | err: |
| 117 | if (ret != EXIT_SUCCESS) |
| 118 | ERR_print_errors_fp(stderr); |
| 119 | BIO_free(in); |
| 120 | return ret; |
| 121 | } |