server-arg.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 use the
  11. * SSL_CONF API with the command line. cc -I../../include server-arg.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. int main(int argc, char *argv[])
  21. {
  22. char *port = "*:4433";
  23. BIO *ssl_bio = NULL;
  24. BIO *tmp;
  25. SSL_CTX *ctx;
  26. SSL_CONF_CTX *cctx;
  27. char buf[512];
  28. BIO *in = NULL;
  29. int ret = EXIT_FAILURE, i;
  30. char **args = argv + 1;
  31. int nargs = argc - 1;
  32. ctx = SSL_CTX_new(TLS_server_method());
  33. cctx = SSL_CONF_CTX_new();
  34. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER);
  35. SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CERTIFICATE);
  36. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  37. while (*args && **args == '-') {
  38. int rv;
  39. /* Parse standard arguments */
  40. rv = SSL_CONF_cmd_argv(cctx, &nargs, &args);
  41. if (rv == -3) {
  42. fprintf(stderr, "Missing argument for %s\n", *args);
  43. goto err;
  44. }
  45. if (rv < 0) {
  46. fprintf(stderr, "Error in command %s\n", *args);
  47. ERR_print_errors_fp(stderr);
  48. goto err;
  49. }
  50. /* If rv > 0 we processed something so proceed to next arg */
  51. if (rv > 0)
  52. continue;
  53. /* Otherwise application specific argument processing */
  54. if (strcmp(*args, "-port") == 0) {
  55. port = args[1];
  56. if (port == NULL) {
  57. fprintf(stderr, "Missing -port argument\n");
  58. goto err;
  59. }
  60. args += 2;
  61. nargs -= 2;
  62. continue;
  63. } else {
  64. fprintf(stderr, "Unknown argument %s\n", *args);
  65. goto err;
  66. }
  67. }
  68. if (!SSL_CONF_CTX_finish(cctx)) {
  69. fprintf(stderr, "Finish error\n");
  70. ERR_print_errors_fp(stderr);
  71. goto err;
  72. }
  73. #ifdef ITERATE_CERTS
  74. /*
  75. * Demo of how to iterate over all certificates in an SSL_CTX structure.
  76. */
  77. {
  78. X509 *x;
  79. int rv;
  80. rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_FIRST);
  81. while (rv) {
  82. X509 *x = SSL_CTX_get0_certificate(ctx);
  83. X509_NAME_print_ex_fp(stdout, X509_get_subject_name(x), 0,
  84. XN_FLAG_ONELINE);
  85. printf("\n");
  86. rv = SSL_CTX_set_current_cert(ctx, SSL_CERT_SET_NEXT);
  87. }
  88. fflush(stdout);
  89. }
  90. #endif
  91. /* Setup server side SSL bio */
  92. ssl_bio = BIO_new_ssl(ctx, 0);
  93. if ((in = BIO_new_accept(port)) == NULL)
  94. goto err;
  95. /*
  96. * This means that when a new connection is accepted on 'in', The ssl_bio
  97. * will be 'duplicated' and have the new socket BIO push into it.
  98. * Basically it means the SSL BIO will be automatically setup
  99. */
  100. BIO_set_accept_bios(in, ssl_bio);
  101. ssl_bio = NULL;
  102. again:
  103. /*
  104. * The first call will setup the accept socket, and the second will get a
  105. * socket. In this loop, the first actual accept will occur in the
  106. * BIO_read() function.
  107. */
  108. if (BIO_do_accept(in) <= 0)
  109. goto err;
  110. for (;;) {
  111. i = BIO_read(in, buf, 512);
  112. if (i == 0) {
  113. /*
  114. * If we have finished, remove the underlying BIO stack so the
  115. * next time we call any function for this BIO, it will attempt
  116. * to do an accept
  117. */
  118. printf("Done\n");
  119. tmp = BIO_pop(in);
  120. BIO_free_all(tmp);
  121. goto again;
  122. }
  123. if (i < 0)
  124. goto err;
  125. fwrite(buf, 1, i, stdout);
  126. fflush(stdout);
  127. }
  128. ret = EXIT_SUCCESS;
  129. err:
  130. if (ret != EXIT_SUCCESS)
  131. ERR_print_errors_fp(stderr);
  132. BIO_free(in);
  133. BIO_free_all(ssl_bio);
  134. return ret;
  135. }