saccept.c 2.9 KB

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