tlsauthentication.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Feel free to use this example code in any way
  2. you see fit (Public Domain) */
  3. #include <sys/types.h>
  4. #ifndef _WIN32
  5. #include <sys/select.h>
  6. #include <sys/socket.h>
  7. #else
  8. #include <winsock2.h>
  9. #endif
  10. #include <microhttpd.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #define PORT 8888
  15. #define REALM "Maintenance"
  16. #define USER "a legitimate user"
  17. #define PASSWORD "and his password"
  18. #define SERVERKEYFILE "server.key"
  19. #define SERVERCERTFILE "server.pem"
  20. static size_t
  21. get_file_size (const char *filename)
  22. {
  23. FILE *fp;
  24. fp = fopen (filename, "rb");
  25. if (fp)
  26. {
  27. long size;
  28. if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
  29. size = 0;
  30. fclose (fp);
  31. return (size_t) size;
  32. }
  33. else
  34. return 0;
  35. }
  36. static char *
  37. load_file (const char *filename)
  38. {
  39. FILE *fp;
  40. char *buffer;
  41. size_t size;
  42. size = get_file_size (filename);
  43. if (0 == size)
  44. return NULL;
  45. fp = fopen (filename, "rb");
  46. if (! fp)
  47. return NULL;
  48. buffer = malloc (size + 1);
  49. if (! buffer)
  50. {
  51. fclose (fp);
  52. return NULL;
  53. }
  54. buffer[size] = '\0';
  55. if (size != fread (buffer, 1, size, fp))
  56. {
  57. free (buffer);
  58. buffer = NULL;
  59. }
  60. fclose (fp);
  61. return buffer;
  62. }
  63. static enum MHD_Result
  64. ask_for_authentication (struct MHD_Connection *connection, const char *realm)
  65. {
  66. enum MHD_Result ret;
  67. struct MHD_Response *response;
  68. response = MHD_create_response_empty (MHD_RF_NONE);
  69. if (! response)
  70. return MHD_NO;
  71. ret = MHD_queue_basic_auth_required_response3 (connection,
  72. realm,
  73. MHD_YES,
  74. response);
  75. MHD_destroy_response (response);
  76. return ret;
  77. }
  78. static int
  79. is_authenticated (struct MHD_Connection *connection,
  80. const char *username,
  81. const char *password)
  82. {
  83. struct MHD_BasicAuthInfo *auth_info;
  84. int authenticated;
  85. auth_info = MHD_basic_auth_get_username_password3 (connection);
  86. if (NULL == auth_info)
  87. return 0;
  88. authenticated =
  89. ( (strlen (username) == auth_info->username_len) &&
  90. (0 == memcmp (auth_info->username, username, auth_info->username_len)) &&
  91. /* The next check against NULL is optional,
  92. * if 'password' is NULL then 'password_len' is always zero. */
  93. (NULL != auth_info->password) &&
  94. (strlen (password) == auth_info->password_len) &&
  95. (0 == memcmp (auth_info->password, password, auth_info->password_len)) );
  96. MHD_free (auth_info);
  97. return authenticated;
  98. }
  99. static enum MHD_Result
  100. secret_page (struct MHD_Connection *connection)
  101. {
  102. enum MHD_Result ret;
  103. struct MHD_Response *response;
  104. const char *page = "<html><body>A secret.</body></html>";
  105. response = MHD_create_response_from_buffer_static (strlen (page), page);
  106. if (! response)
  107. return MHD_NO;
  108. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  109. MHD_destroy_response (response);
  110. return ret;
  111. }
  112. static enum MHD_Result
  113. answer_to_connection (void *cls, struct MHD_Connection *connection,
  114. const char *url, const char *method,
  115. const char *version, const char *upload_data,
  116. size_t *upload_data_size, void **req_cls)
  117. {
  118. (void) cls; /* Unused. Silent compiler warning. */
  119. (void) url; /* Unused. Silent compiler warning. */
  120. (void) version; /* Unused. Silent compiler warning. */
  121. (void) upload_data; /* Unused. Silent compiler warning. */
  122. (void) upload_data_size; /* Unused. Silent compiler warning. */
  123. if (0 != strcmp (method, "GET"))
  124. return MHD_NO;
  125. if (NULL == *req_cls)
  126. {
  127. *req_cls = connection;
  128. return MHD_YES;
  129. }
  130. if (! is_authenticated (connection, USER, PASSWORD))
  131. return ask_for_authentication (connection, REALM);
  132. return secret_page (connection);
  133. }
  134. int
  135. main (void)
  136. {
  137. struct MHD_Daemon *daemon;
  138. char *key_pem;
  139. char *cert_pem;
  140. key_pem = load_file (SERVERKEYFILE);
  141. cert_pem = load_file (SERVERCERTFILE);
  142. if ((key_pem == NULL) || (cert_pem == NULL))
  143. {
  144. printf ("The key/certificate files could not be read.\n");
  145. if (NULL != key_pem)
  146. free (key_pem);
  147. if (NULL != cert_pem)
  148. free (cert_pem);
  149. return 1;
  150. }
  151. daemon =
  152. MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS, PORT, NULL,
  153. NULL, &answer_to_connection, NULL,
  154. MHD_OPTION_HTTPS_MEM_KEY, key_pem,
  155. MHD_OPTION_HTTPS_MEM_CERT, cert_pem, MHD_OPTION_END);
  156. if (NULL == daemon)
  157. {
  158. printf ("%s\n", cert_pem);
  159. free (key_pem);
  160. free (cert_pem);
  161. return 1;
  162. }
  163. (void) getchar ();
  164. MHD_stop_daemon (daemon);
  165. free (key_pem);
  166. free (cert_pem);
  167. return 0;
  168. }