server-conf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2013-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. * A minimal program to serve an SSL connection. It uses blocking. It uses
  11. * the SSL_CONF API with a configuration file. cc -I../../include saccept.c
  12. * -L../.. -lssl -lcrypto -ldl
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <signal.h>
  17. #include <stdlib.h>
  18. #include <openssl/err.h>
  19. #include <openssl/ssl.h>
  20. #include <openssl/conf.h>
  21. int main(int argc, char *argv[])
  22. {
  23. char *port = "*:4433";
  24. BIO *in = NULL;
  25. BIO *ssl_bio = NULL;
  26. BIO *tmp;
  27. SSL_CTX *ctx;
  28. SSL_CONF_CTX *cctx = NULL;
  29. CONF *conf = NULL;
  30. STACK_OF(CONF_VALUE) *sect = NULL;
  31. CONF_VALUE *cnf;
  32. long errline = -1;
  33. char buf[512];
  34. int ret = EXIT_FAILURE, i;
  35. ctx = SSL_CTX_new(TLS_server_method());
  36. conf = NCONF_new(NULL);
  37. if (NCONF_load(conf, "accept.cnf", &errline) <= 0) {
  38. if (errline <= 0)
  39. fprintf(stderr, "Error processing config file\n");
  40. else
  41. fprintf(stderr, "Error on line %ld\n", errline);
  42. goto err;
  43. }
  44. sect = NCONF_get_section(conf, "default");
  45. if (sect == NULL) {
  46. fprintf(stderr, "Error retrieving default section\n");
  47. goto err;
  48. }
  49. cctx = SSL_CONF_CTX_new();
  50. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
  51. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);
  52. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE);
  53. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  54. for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
  55. int rv;
  56. cnf = sk_CONF_VALUE_value(sect, i);
  57. rv = SSL_CONF_cmd(cctx, cnf->name, cnf->value);
  58. if (rv > 0)
  59. continue;
  60. if (rv != -2) {
  61. fprintf(stderr, "Error processing %s = %s\n",
  62. cnf->name, cnf->value);
  63. ERR_print_errors_fp(stderr);
  64. goto err;
  65. }
  66. if (strcmp(cnf->name, "Port") == 0) {
  67. port = cnf->value;
  68. } else {
  69. fprintf(stderr, "Unknown configuration option %s\n", cnf->name);
  70. goto err;
  71. }
  72. }
  73. if (!SSL_CONF_CTX_finish(cctx)) {
  74. fprintf(stderr, "Finish error\n");
  75. ERR_print_errors_fp(stderr);
  76. goto err;
  77. }
  78. /* Setup server side SSL bio */
  79. ssl_bio = BIO_new_ssl(ctx, 0);
  80. if ((in = BIO_new_accept(port)) == NULL)
  81. goto err;
  82. /*
  83. * This means that when a new connection is accepted on 'in', The ssl_bio
  84. * will be 'duplicated' and have the new socket BIO push into it.
  85. * Basically it means the SSL BIO will be automatically setup
  86. */
  87. BIO_set_accept_bios(in, ssl_bio);
  88. ssl_bio = NULL;
  89. again:
  90. /*
  91. * The first call will setup the accept socket, and the second will get a
  92. * socket. In this loop, the first actual accept will occur in the
  93. * BIO_read() function.
  94. */
  95. if (BIO_do_accept(in) <= 0)
  96. goto err;
  97. for (;;) {
  98. i = BIO_read(in, buf, 512);
  99. if (i == 0) {
  100. /*
  101. * If we have finished, remove the underlying BIO stack so the
  102. * next time we call any function for this BIO, it will attempt
  103. * to do an accept
  104. */
  105. printf("Done\n");
  106. tmp = BIO_pop(in);
  107. BIO_free_all(tmp);
  108. goto again;
  109. }
  110. if (i < 0) {
  111. if (BIO_should_retry(in))
  112. continue;
  113. goto err;
  114. }
  115. fwrite(buf, 1, i, stdout);
  116. fflush(stdout);
  117. }
  118. ret = EXIT_SUCCESS;
  119. err:
  120. if (ret != EXIT_SUCCESS)
  121. ERR_print_errors_fp(stderr);
  122. BIO_free(in);
  123. BIO_free_all(ssl_bio);
  124. return ret;
  125. }