server-conf.c 3.5 KB

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