saccept.c 2.7 KB

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