saccept.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 1998-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.
  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, *tmp;
  49. SSL_CTX *ctx;
  50. char buf[512];
  51. int ret = EXIT_FAILURE, i;
  52. if (argc <= 1)
  53. port = "*:4433";
  54. else
  55. port = argv[1];
  56. ctx = SSL_CTX_new(TLS_server_method());
  57. if (!SSL_CTX_use_certificate_chain_file(ctx, CERT_FILE))
  58. goto err;
  59. if (!SSL_CTX_use_PrivateKey_file(ctx, CERT_FILE, SSL_FILETYPE_PEM))
  60. goto err;
  61. if (!SSL_CTX_check_private_key(ctx))
  62. goto err;
  63. /* Setup server side SSL bio */
  64. ssl_bio = BIO_new_ssl(ctx, 0);
  65. if ((in = BIO_new_accept(port)) == NULL)
  66. goto err;
  67. /*
  68. * This means that when a new connection is accepted on 'in', The ssl_bio
  69. * will be 'duplicated' and have the new socket BIO push into it.
  70. * Basically it means the SSL BIO will be automatically setup
  71. */
  72. BIO_set_accept_bios(in, ssl_bio);
  73. /* Arrange to leave server loop on interrupt */
  74. sigsetup();
  75. again:
  76. /*
  77. * The first call will setup the accept socket, and the second will get a
  78. * socket. In this loop, the first actual accept will occur in the
  79. * BIO_read() function.
  80. */
  81. if (BIO_do_accept(in) <= 0)
  82. goto err;
  83. while (!done) {
  84. i = BIO_read(in, buf, 512);
  85. if (i == 0) {
  86. /*
  87. * If we have finished, remove the underlying BIO stack so the
  88. * next time we call any function for this BIO, it will attempt
  89. * to do an accept
  90. */
  91. printf("Done\n");
  92. tmp = BIO_pop(in);
  93. BIO_free_all(tmp);
  94. goto again;
  95. }
  96. if (i < 0)
  97. goto err;
  98. fwrite(buf, 1, i, stdout);
  99. fflush(stdout);
  100. }
  101. ret = EXIT_SUCCESS;
  102. err:
  103. if (ret != EXIT_SUCCESS)
  104. ERR_print_errors_fp(stderr);
  105. BIO_free(in);
  106. return ret;
  107. }