saccept.c 3.0 KB

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