serv.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* serv.cpp - Minimal ssleay server for Unix
  2. 30.9.1996, Sampo Kellomaki <sampo@iki.fi> */
  3. /* mangled to work with SSLeay-0.9.0b and OpenSSL 0.9.2b
  4. Simplified to be even more minimal
  5. 12/98 - 4/99 Wade Scholine <wades@mail.cybg.com> */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <memory.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include <netdb.h>
  16. #include <openssl/rsa.h> /* SSLeay stuff */
  17. #include <openssl/crypto.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/pem.h>
  20. #include <openssl/ssl.h>
  21. #include <openssl/err.h>
  22. /* define HOME to be dir for key and cert files... */
  23. #define HOME "./"
  24. /* Make these what you want for cert & key files */
  25. #define CERTF HOME "foo-cert.pem"
  26. #define KEYF HOME "foo-cert.pem"
  27. #define CHK_NULL(x) if ((x)==NULL) exit (1)
  28. #define CHK_ERR(err,s) if ((err)==-1) { perror(s); exit(1); }
  29. #define CHK_SSL(err) if ((err)==-1) { ERR_print_errors_fp(stderr); exit(2); }
  30. void main ()
  31. {
  32. int err;
  33. int listen_sd;
  34. int sd;
  35. struct sockaddr_in sa_serv;
  36. struct sockaddr_in sa_cli;
  37. size_t client_len;
  38. SSL_CTX* ctx;
  39. SSL* ssl;
  40. X509* client_cert;
  41. char* str;
  42. char buf [4096];
  43. SSL_METHOD *meth;
  44. /* SSL preliminaries. We keep the certificate and key with the context. */
  45. SSL_load_error_strings();
  46. SSLeay_add_ssl_algorithms();
  47. meth = SSLv23_server_method();
  48. ctx = SSL_CTX_new (meth);
  49. if (!ctx) {
  50. ERR_print_errors_fp(stderr);
  51. exit(2);
  52. }
  53. if (SSL_CTX_use_certificate_file(ctx, CERTF, SSL_FILETYPE_PEM) <= 0) {
  54. ERR_print_errors_fp(stderr);
  55. exit(3);
  56. }
  57. if (SSL_CTX_use_PrivateKey_file(ctx, KEYF, SSL_FILETYPE_PEM) <= 0) {
  58. ERR_print_errors_fp(stderr);
  59. exit(4);
  60. }
  61. if (!SSL_CTX_check_private_key(ctx)) {
  62. fprintf(stderr,"Private key does not match the certificate public key\n");
  63. exit(5);
  64. }
  65. /* ----------------------------------------------- */
  66. /* Prepare TCP socket for receiving connections */
  67. listen_sd = socket (AF_INET, SOCK_STREAM, 0); CHK_ERR(listen_sd, "socket");
  68. memset (&sa_serv, '\0', sizeof(sa_serv));
  69. sa_serv.sin_family = AF_INET;
  70. sa_serv.sin_addr.s_addr = INADDR_ANY;
  71. sa_serv.sin_port = htons (1111); /* Server Port number */
  72. err = bind(listen_sd, (struct sockaddr*) &sa_serv,
  73. sizeof (sa_serv)); CHK_ERR(err, "bind");
  74. /* Receive a TCP connection. */
  75. err = listen (listen_sd, 5); CHK_ERR(err, "listen");
  76. client_len = sizeof(sa_cli);
  77. sd = accept (listen_sd, (struct sockaddr*) &sa_cli, &client_len);
  78. CHK_ERR(sd, "accept");
  79. close (listen_sd);
  80. printf ("Connection from %lx, port %x\n",
  81. sa_cli.sin_addr.s_addr, sa_cli.sin_port);
  82. /* ----------------------------------------------- */
  83. /* TCP connection is ready. Do server side SSL. */
  84. ssl = SSL_new (ctx); CHK_NULL(ssl);
  85. SSL_set_fd (ssl, sd);
  86. err = SSL_accept (ssl); CHK_SSL(err);
  87. /* Get the cipher - opt */
  88. printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
  89. /* Get client's certificate (note: beware of dynamic allocation) - opt */
  90. client_cert = SSL_get_peer_certificate (ssl);
  91. if (client_cert != NULL) {
  92. printf ("Client certificate:\n");
  93. str = X509_NAME_oneline (X509_get_subject_name (client_cert), 0, 0);
  94. CHK_NULL(str);
  95. printf ("\t subject: %s\n", str);
  96. OPENSSL_free (str);
  97. str = X509_NAME_oneline (X509_get_issuer_name (client_cert), 0, 0);
  98. CHK_NULL(str);
  99. printf ("\t issuer: %s\n", str);
  100. OPENSSL_free (str);
  101. /* We could do all sorts of certificate verification stuff here before
  102. deallocating the certificate. */
  103. X509_free (client_cert);
  104. } else
  105. printf ("Client does not have certificate.\n");
  106. /* DATA EXCHANGE - Receive message and send reply. */
  107. err = SSL_read (ssl, buf, sizeof(buf) - 1); CHK_SSL(err);
  108. buf[err] = '\0';
  109. printf ("Got %d chars:'%s'\n", err, buf);
  110. err = SSL_write (ssl, "I hear you.", strlen("I hear you.")); CHK_SSL(err);
  111. /* Clean up. */
  112. close (sd);
  113. SSL_free (ssl);
  114. SSL_CTX_free (ctx);
  115. }
  116. /* EOF - serv.cpp */