saccept.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 1998-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.
  11. * It uses blocking.
  12. * saccept host:port
  13. * host is the interface IP to use. If any interface, use *:port
  14. * The default it *:4433
  15. *
  16. * cc -I../../include saccept.c -L../.. -lssl -lcrypto -ldl
  17. */
  18. #include <stdio.h>
  19. #include <signal.h>
  20. #include <stdlib.h>
  21. #include <openssl/err.h>
  22. #include <openssl/ssl.h>
  23. #define CERT_FILE "server.pem"
  24. static volatile int done = 0;
  25. static void interrupt(int sig)
  26. {
  27. done = 1;
  28. }
  29. static void sigsetup(void)
  30. {
  31. #if defined(OPENSSL_SYS_WINDOWS)
  32. signal(SIGINT, interrupt);
  33. #else
  34. struct sigaction sa;
  35. /*
  36. * Catch at most once, and don't restart the accept system call.
  37. */
  38. sa.sa_flags = SA_RESETHAND;
  39. sa.sa_handler = interrupt;
  40. sigemptyset(&sa.sa_mask);
  41. sigaction(SIGINT, &sa, NULL);
  42. #endif
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. char *port = NULL;
  47. BIO *in = NULL;
  48. BIO *ssl_bio = NULL;
  49. BIO *tmp;
  50. SSL_CTX *ctx;
  51. char buf[512];
  52. int ret = EXIT_FAILURE, i;
  53. if (argc <= 1)
  54. port = "*:4433";
  55. else
  56. port = argv[1];
  57. ctx = SSL_CTX_new(TLS_server_method());
  58. if (!SSL_CTX_use_certificate_chain_file(ctx, CERT_FILE))
  59. goto err;
  60. if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
  61. goto err;
  62. if (!SSL_CTX_check_private_key(ctx))
  63. goto err;
  64. /* Setup server side SSL bio */
  65. ssl_bio = BIO_new_ssl(ctx, 0);
  66. if ((in = BIO_new_accept(port)) == NULL)
  67. goto err;
  68. /*
  69. * This means that when a new connection is accepted on 'in', The ssl_bio
  70. * will be 'duplicated' and have the new socket BIO push into it.
  71. * Basically it means the SSL BIO will be automatically setup
  72. */
  73. BIO_set_accept_bios(in, ssl_bio);
  74. ssl_bio = NULL;
  75. /* Arrange to leave server loop on interrupt */
  76. sigsetup();
  77. again:
  78. /*
  79. * The first call will setup the accept socket, and the second will get a
  80. * socket. In this loop, the first actual accept will occur in the
  81. * BIO_read() function.
  82. */
  83. if (BIO_do_accept(in) <= 0)
  84. goto err;
  85. while (!done) {
  86. i = BIO_read(in, buf, 512);
  87. if (i == 0) {
  88. /*
  89. * If we have finished, remove the underlying BIO stack so the
  90. * next time we call any function for this BIO, it will attempt
  91. * to do an accept
  92. */
  93. printf("Done\n");
  94. tmp = BIO_pop(in);
  95. BIO_free_all(tmp);
  96. goto again;
  97. }
  98. if (i < 0)
  99. goto err;
  100. fwrite(buf, 1, i, stdout);
  101. fflush(stdout);
  102. }
  103. ret = EXIT_SUCCESS;
  104. err:
  105. if (ret != EXIT_SUCCESS)
  106. ERR_print_errors_fp(stderr);
  107. BIO_free(in);
  108. BIO_free_all(ssl_bio);
  109. return ret;
  110. }