eap_md5.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * EAP peer method: EAP-MD5 (RFC 3748 and RFC 1994)
  3. * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "eap_i.h"
  11. #include "eap_common/chap.h"
  12. static void * eap_md5_init(struct eap_sm *sm)
  13. {
  14. /* No need for private data. However, must return non-NULL to indicate
  15. * success. */
  16. return (void *) 1;
  17. }
  18. static void eap_md5_deinit(struct eap_sm *sm, void *priv)
  19. {
  20. }
  21. static struct wpabuf * eap_md5_process(struct eap_sm *sm, void *priv,
  22. struct eap_method_ret *ret,
  23. const struct wpabuf *reqData)
  24. {
  25. struct wpabuf *resp;
  26. const u8 *pos, *challenge, *password;
  27. u8 *rpos, id;
  28. size_t len, challenge_len, password_len;
  29. password = eap_get_config_password(sm, &password_len);
  30. if (password == NULL) {
  31. wpa_printf(MSG_INFO, "EAP-MD5: Password not configured");
  32. eap_sm_request_password(sm);
  33. ret->ignore = TRUE;
  34. return NULL;
  35. }
  36. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MD5, reqData, &len);
  37. if (pos == NULL || len == 0) {
  38. wpa_printf(MSG_INFO, "EAP-MD5: Invalid frame (pos=%p len=%lu)",
  39. pos, (unsigned long) len);
  40. ret->ignore = TRUE;
  41. return NULL;
  42. }
  43. /*
  44. * CHAP Challenge:
  45. * Value-Size (1 octet) | Value(Challenge) | Name(optional)
  46. */
  47. challenge_len = *pos++;
  48. if (challenge_len == 0 || challenge_len > len - 1) {
  49. wpa_printf(MSG_INFO, "EAP-MD5: Invalid challenge "
  50. "(challenge_len=%lu len=%lu)",
  51. (unsigned long) challenge_len, (unsigned long) len);
  52. ret->ignore = TRUE;
  53. return NULL;
  54. }
  55. ret->ignore = FALSE;
  56. challenge = pos;
  57. wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Challenge",
  58. challenge, challenge_len);
  59. wpa_printf(MSG_DEBUG, "EAP-MD5: Generating Challenge Response");
  60. ret->methodState = METHOD_DONE;
  61. ret->decision = DECISION_COND_SUCC;
  62. ret->allowNotifications = TRUE;
  63. resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MD5, 1 + CHAP_MD5_LEN,
  64. EAP_CODE_RESPONSE, eap_get_id(reqData));
  65. if (resp == NULL)
  66. return NULL;
  67. /*
  68. * CHAP Response:
  69. * Value-Size (1 octet) | Value(Response) | Name(optional)
  70. */
  71. wpabuf_put_u8(resp, CHAP_MD5_LEN);
  72. id = eap_get_id(resp);
  73. rpos = wpabuf_put(resp, CHAP_MD5_LEN);
  74. if (chap_md5(id, password, password_len, challenge, challenge_len,
  75. rpos)) {
  76. wpa_printf(MSG_INFO, "EAP-MD5: CHAP MD5 operation failed");
  77. ret->ignore = TRUE;
  78. wpabuf_free(resp);
  79. return NULL;
  80. }
  81. wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Response", rpos, CHAP_MD5_LEN);
  82. return resp;
  83. }
  84. int eap_peer_md5_register(void)
  85. {
  86. struct eap_method *eap;
  87. int ret;
  88. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  89. EAP_VENDOR_IETF, EAP_TYPE_MD5, "MD5");
  90. if (eap == NULL)
  91. return -1;
  92. eap->init = eap_md5_init;
  93. eap->deinit = eap_md5_deinit;
  94. eap->process = eap_md5_process;
  95. ret = eap_peer_method_register(eap);
  96. if (ret)
  97. eap_peer_method_free(eap);
  98. return ret;
  99. }