tlsauthentication.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 char *
  21. string_to_base64 (const char *message)
  22. {
  23. const char *lookup =
  24. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  25. unsigned long l;
  26. size_t i;
  27. size_t j;
  28. char *tmp;
  29. size_t length = strlen (message);
  30. tmp = malloc (length * 2 + 1);
  31. if (NULL == tmp)
  32. return NULL;
  33. j = 0;
  34. for (i = 0; i < length; i += 3)
  35. {
  36. l = (((unsigned long) message[i]) << 16)
  37. | (((i + 1) < length) ? (((unsigned long) message[i + 1]) << 8) : 0)
  38. | (((i + 2) < length) ? ((unsigned long) message[i + 2]) : 0);
  39. tmp [j++] = lookup[(l >> 18) & 0x3F];
  40. tmp [j++] = lookup[(l >> 12) & 0x3F];
  41. if (i + 1 < length)
  42. tmp [j++] = lookup[(l >> 6) & 0x3F];
  43. if (i + 2 < length)
  44. tmp [j++] = lookup[l & 0x3F];
  45. }
  46. if (0 != length % 3)
  47. tmp [j++] = '=';
  48. if (1 == length % 3)
  49. tmp [j++] = '=';
  50. tmp [j] = 0;
  51. return tmp;
  52. }
  53. static long
  54. get_file_size (const char *filename)
  55. {
  56. FILE *fp;
  57. fp = fopen (filename, "rb");
  58. if (fp)
  59. {
  60. long size;
  61. if ((0 != fseek (fp, 0, SEEK_END)) || (-1 == (size = ftell (fp))))
  62. size = 0;
  63. fclose (fp);
  64. return size;
  65. }
  66. else
  67. return 0;
  68. }
  69. static char *
  70. load_file (const char *filename)
  71. {
  72. FILE *fp;
  73. char *buffer;
  74. long size;
  75. size = get_file_size (filename);
  76. if (0 == size)
  77. return NULL;
  78. fp = fopen (filename, "rb");
  79. if (! fp)
  80. return NULL;
  81. buffer = malloc (size + 1);
  82. if (! buffer)
  83. {
  84. fclose (fp);
  85. return NULL;
  86. }
  87. buffer[size] = '\0';
  88. if (size != (long) fread (buffer, 1, size, fp))
  89. {
  90. free (buffer);
  91. buffer = NULL;
  92. }
  93. fclose (fp);
  94. return buffer;
  95. }
  96. static enum MHD_Result
  97. ask_for_authentication (struct MHD_Connection *connection, const char *realm)
  98. {
  99. enum MHD_Result ret;
  100. struct MHD_Response *response;
  101. char *headervalue;
  102. size_t slen;
  103. const char *strbase = "Basic realm=";
  104. response = MHD_create_response_from_buffer (0, NULL,
  105. MHD_RESPMEM_PERSISTENT);
  106. if (! response)
  107. return MHD_NO;
  108. slen = strlen (strbase) + strlen (realm) + 1;
  109. if (NULL == (headervalue = malloc (slen)))
  110. return MHD_NO;
  111. snprintf (headervalue,
  112. slen,
  113. "%s%s",
  114. strbase,
  115. realm);
  116. ret = MHD_add_response_header (response,
  117. "WWW-Authenticate",
  118. headervalue);
  119. free (headervalue);
  120. if (! ret)
  121. {
  122. MHD_destroy_response (response);
  123. return MHD_NO;
  124. }
  125. ret = MHD_queue_response (connection,
  126. MHD_HTTP_UNAUTHORIZED,
  127. response);
  128. MHD_destroy_response (response);
  129. return ret;
  130. }
  131. static int
  132. is_authenticated (struct MHD_Connection *connection,
  133. const char *username,
  134. const char *password)
  135. {
  136. const char *headervalue;
  137. char *expected_b64;
  138. char *expected;
  139. const char *strbase = "Basic ";
  140. int authenticated;
  141. size_t slen;
  142. headervalue =
  143. MHD_lookup_connection_value (connection, MHD_HEADER_KIND,
  144. "Authorization");
  145. if (NULL == headervalue)
  146. return 0;
  147. if (0 != strncmp (headervalue, strbase, strlen (strbase)))
  148. return 0;
  149. slen = strlen (username) + 1 + strlen (password) + 1;
  150. if (NULL == (expected = malloc (slen)))
  151. return 0;
  152. snprintf (expected,
  153. slen,
  154. "%s:%s",
  155. username,
  156. password);
  157. expected_b64 = string_to_base64 (expected);
  158. free (expected);
  159. if (NULL == expected_b64)
  160. return 0;
  161. authenticated =
  162. (strcmp (headervalue + strlen (strbase), expected_b64) == 0);
  163. free (expected_b64);
  164. return authenticated;
  165. }
  166. static enum MHD_Result
  167. secret_page (struct MHD_Connection *connection)
  168. {
  169. enum MHD_Result ret;
  170. struct MHD_Response *response;
  171. const char *page = "<html><body>A secret.</body></html>";
  172. response =
  173. MHD_create_response_from_buffer (strlen (page), (void *) page,
  174. MHD_RESPMEM_PERSISTENT);
  175. if (! response)
  176. return MHD_NO;
  177. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  178. MHD_destroy_response (response);
  179. return ret;
  180. }
  181. static enum MHD_Result
  182. answer_to_connection (void *cls, struct MHD_Connection *connection,
  183. const char *url, const char *method,
  184. const char *version, const char *upload_data,
  185. size_t *upload_data_size, void **con_cls)
  186. {
  187. (void) cls; /* Unused. Silent compiler warning. */
  188. (void) url; /* Unused. Silent compiler warning. */
  189. (void) version; /* Unused. Silent compiler warning. */
  190. (void) upload_data; /* Unused. Silent compiler warning. */
  191. (void) upload_data_size; /* Unused. Silent compiler warning. */
  192. if (0 != strcmp (method, "GET"))
  193. return MHD_NO;
  194. if (NULL == *con_cls)
  195. {
  196. *con_cls = connection;
  197. return MHD_YES;
  198. }
  199. if (! is_authenticated (connection, USER, PASSWORD))
  200. return ask_for_authentication (connection, REALM);
  201. return secret_page (connection);
  202. }
  203. int
  204. main ()
  205. {
  206. struct MHD_Daemon *daemon;
  207. char *key_pem;
  208. char *cert_pem;
  209. key_pem = load_file (SERVERKEYFILE);
  210. cert_pem = load_file (SERVERCERTFILE);
  211. if ((key_pem == NULL) || (cert_pem == NULL))
  212. {
  213. printf ("The key/certificate files could not be read.\n");
  214. if (NULL != key_pem)
  215. free (key_pem);
  216. if (NULL != cert_pem)
  217. free (cert_pem);
  218. return 1;
  219. }
  220. daemon =
  221. MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS, PORT, NULL,
  222. NULL, &answer_to_connection, NULL,
  223. MHD_OPTION_HTTPS_MEM_KEY, key_pem,
  224. MHD_OPTION_HTTPS_MEM_CERT, cert_pem, MHD_OPTION_END);
  225. if (NULL == daemon)
  226. {
  227. printf ("%s\n", cert_pem);
  228. free (key_pem);
  229. free (cert_pem);
  230. return 1;
  231. }
  232. (void) getchar ();
  233. MHD_stop_daemon (daemon);
  234. free (key_pem);
  235. free (cert_pem);
  236. return 0;
  237. }