eap_server_md5.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * hostapd / EAP-MD5 server
  3. * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "crypto/random.h"
  17. #include "eap_i.h"
  18. #include "eap_common/chap.h"
  19. #define CHALLENGE_LEN 16
  20. struct eap_md5_data {
  21. u8 challenge[CHALLENGE_LEN];
  22. enum { CONTINUE, SUCCESS, FAILURE } state;
  23. };
  24. static void * eap_md5_init(struct eap_sm *sm)
  25. {
  26. struct eap_md5_data *data;
  27. data = os_zalloc(sizeof(*data));
  28. if (data == NULL)
  29. return NULL;
  30. data->state = CONTINUE;
  31. return data;
  32. }
  33. static void eap_md5_reset(struct eap_sm *sm, void *priv)
  34. {
  35. struct eap_md5_data *data = priv;
  36. os_free(data);
  37. }
  38. static struct wpabuf * eap_md5_buildReq(struct eap_sm *sm, void *priv, u8 id)
  39. {
  40. struct eap_md5_data *data = priv;
  41. struct wpabuf *req;
  42. if (random_get_bytes(data->challenge, CHALLENGE_LEN)) {
  43. wpa_printf(MSG_ERROR, "EAP-MD5: Failed to get random data");
  44. data->state = FAILURE;
  45. return NULL;
  46. }
  47. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MD5, 1 + CHALLENGE_LEN,
  48. EAP_CODE_REQUEST, id);
  49. if (req == NULL) {
  50. wpa_printf(MSG_ERROR, "EAP-MD5: Failed to allocate memory for "
  51. "request");
  52. data->state = FAILURE;
  53. return NULL;
  54. }
  55. wpabuf_put_u8(req, CHALLENGE_LEN);
  56. wpabuf_put_data(req, data->challenge, CHALLENGE_LEN);
  57. wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Challenge", data->challenge,
  58. CHALLENGE_LEN);
  59. data->state = CONTINUE;
  60. return req;
  61. }
  62. static Boolean eap_md5_check(struct eap_sm *sm, void *priv,
  63. struct wpabuf *respData)
  64. {
  65. const u8 *pos;
  66. size_t len;
  67. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MD5, respData, &len);
  68. if (pos == NULL || len < 1) {
  69. wpa_printf(MSG_INFO, "EAP-MD5: Invalid frame");
  70. return TRUE;
  71. }
  72. if (*pos != CHAP_MD5_LEN || 1 + CHAP_MD5_LEN > len) {
  73. wpa_printf(MSG_INFO, "EAP-MD5: Invalid response "
  74. "(response_len=%d payload_len=%lu",
  75. *pos, (unsigned long) len);
  76. return TRUE;
  77. }
  78. return FALSE;
  79. }
  80. static void eap_md5_process(struct eap_sm *sm, void *priv,
  81. struct wpabuf *respData)
  82. {
  83. struct eap_md5_data *data = priv;
  84. const u8 *pos;
  85. size_t plen;
  86. u8 hash[CHAP_MD5_LEN], id;
  87. if (sm->user == NULL || sm->user->password == NULL ||
  88. sm->user->password_hash) {
  89. wpa_printf(MSG_INFO, "EAP-MD5: Plaintext password not "
  90. "configured");
  91. data->state = FAILURE;
  92. return;
  93. }
  94. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MD5, respData, &plen);
  95. if (pos == NULL || *pos != CHAP_MD5_LEN || plen < 1 + CHAP_MD5_LEN)
  96. return; /* Should not happen - frame already validated */
  97. pos++; /* Skip response len */
  98. wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Response", pos, CHAP_MD5_LEN);
  99. id = eap_get_id(respData);
  100. chap_md5(id, sm->user->password, sm->user->password_len,
  101. data->challenge, CHALLENGE_LEN, hash);
  102. if (os_memcmp(hash, pos, CHAP_MD5_LEN) == 0) {
  103. wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Success");
  104. data->state = SUCCESS;
  105. } else {
  106. wpa_printf(MSG_DEBUG, "EAP-MD5: Done - Failure");
  107. data->state = FAILURE;
  108. }
  109. }
  110. static Boolean eap_md5_isDone(struct eap_sm *sm, void *priv)
  111. {
  112. struct eap_md5_data *data = priv;
  113. return data->state != CONTINUE;
  114. }
  115. static Boolean eap_md5_isSuccess(struct eap_sm *sm, void *priv)
  116. {
  117. struct eap_md5_data *data = priv;
  118. return data->state == SUCCESS;
  119. }
  120. int eap_server_md5_register(void)
  121. {
  122. struct eap_method *eap;
  123. int ret;
  124. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  125. EAP_VENDOR_IETF, EAP_TYPE_MD5, "MD5");
  126. if (eap == NULL)
  127. return -1;
  128. eap->init = eap_md5_init;
  129. eap->reset = eap_md5_reset;
  130. eap->buildReq = eap_md5_buildReq;
  131. eap->check = eap_md5_check;
  132. eap->process = eap_md5_process;
  133. eap->isDone = eap_md5_isDone;
  134. eap->isSuccess = eap_md5_isSuccess;
  135. ret = eap_server_method_register(eap);
  136. if (ret)
  137. eap_server_method_free(eap);
  138. return ret;
  139. }