server-arg.c 4.0 KB

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