server-conf.c 3.8 KB

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