eap_server_vendor_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * hostapd / Test method for vendor specific (expanded) EAP type
  3. * Copyright (c) 2005-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 "eap_i.h"
  17. #define EAP_VENDOR_ID 0xfffefd
  18. #define EAP_VENDOR_TYPE 0xfcfbfaf9
  19. struct eap_vendor_test_data {
  20. enum { INIT, CONFIRM, SUCCESS, FAILURE } state;
  21. };
  22. static const char * eap_vendor_test_state_txt(int state)
  23. {
  24. switch (state) {
  25. case INIT:
  26. return "INIT";
  27. case CONFIRM:
  28. return "CONFIRM";
  29. case SUCCESS:
  30. return "SUCCESS";
  31. case FAILURE:
  32. return "FAILURE";
  33. default:
  34. return "?";
  35. }
  36. }
  37. static void eap_vendor_test_state(struct eap_vendor_test_data *data,
  38. int state)
  39. {
  40. wpa_printf(MSG_DEBUG, "EAP-VENDOR-TEST: %s -> %s",
  41. eap_vendor_test_state_txt(data->state),
  42. eap_vendor_test_state_txt(state));
  43. data->state = state;
  44. }
  45. static void * eap_vendor_test_init(struct eap_sm *sm)
  46. {
  47. struct eap_vendor_test_data *data;
  48. data = os_zalloc(sizeof(*data));
  49. if (data == NULL)
  50. return NULL;
  51. data->state = INIT;
  52. return data;
  53. }
  54. static void eap_vendor_test_reset(struct eap_sm *sm, void *priv)
  55. {
  56. struct eap_vendor_test_data *data = priv;
  57. os_free(data);
  58. }
  59. static struct wpabuf * eap_vendor_test_buildReq(struct eap_sm *sm, void *priv,
  60. u8 id)
  61. {
  62. struct eap_vendor_test_data *data = priv;
  63. struct wpabuf *req;
  64. req = eap_msg_alloc(EAP_VENDOR_ID, EAP_VENDOR_TYPE, 1,
  65. EAP_CODE_REQUEST, id);
  66. if (req == NULL) {
  67. wpa_printf(MSG_ERROR, "EAP-VENDOR-TEST: Failed to allocate "
  68. "memory for request");
  69. return NULL;
  70. }
  71. wpabuf_put_u8(req, data->state == INIT ? 1 : 3);
  72. return req;
  73. }
  74. static Boolean eap_vendor_test_check(struct eap_sm *sm, void *priv,
  75. struct wpabuf *respData)
  76. {
  77. const u8 *pos;
  78. size_t len;
  79. pos = eap_hdr_validate(EAP_VENDOR_ID, EAP_VENDOR_TYPE, respData, &len);
  80. if (pos == NULL || len < 1) {
  81. wpa_printf(MSG_INFO, "EAP-VENDOR-TEST: Invalid frame");
  82. return TRUE;
  83. }
  84. return FALSE;
  85. }
  86. static void eap_vendor_test_process(struct eap_sm *sm, void *priv,
  87. struct wpabuf *respData)
  88. {
  89. struct eap_vendor_test_data *data = priv;
  90. const u8 *pos;
  91. size_t len;
  92. pos = eap_hdr_validate(EAP_VENDOR_ID, EAP_VENDOR_TYPE, respData, &len);
  93. if (pos == NULL || len < 1)
  94. return;
  95. if (data->state == INIT) {
  96. if (*pos == 2)
  97. eap_vendor_test_state(data, CONFIRM);
  98. else
  99. eap_vendor_test_state(data, FAILURE);
  100. } else if (data->state == CONFIRM) {
  101. if (*pos == 4)
  102. eap_vendor_test_state(data, SUCCESS);
  103. else
  104. eap_vendor_test_state(data, FAILURE);
  105. } else
  106. eap_vendor_test_state(data, FAILURE);
  107. }
  108. static Boolean eap_vendor_test_isDone(struct eap_sm *sm, void *priv)
  109. {
  110. struct eap_vendor_test_data *data = priv;
  111. return data->state == SUCCESS;
  112. }
  113. static u8 * eap_vendor_test_getKey(struct eap_sm *sm, void *priv, size_t *len)
  114. {
  115. struct eap_vendor_test_data *data = priv;
  116. u8 *key;
  117. const int key_len = 64;
  118. if (data->state != SUCCESS)
  119. return NULL;
  120. key = os_malloc(key_len);
  121. if (key == NULL)
  122. return NULL;
  123. os_memset(key, 0x11, key_len / 2);
  124. os_memset(key + key_len / 2, 0x22, key_len / 2);
  125. *len = key_len;
  126. return key;
  127. }
  128. static Boolean eap_vendor_test_isSuccess(struct eap_sm *sm, void *priv)
  129. {
  130. struct eap_vendor_test_data *data = priv;
  131. return data->state == SUCCESS;
  132. }
  133. int eap_server_vendor_test_register(void)
  134. {
  135. struct eap_method *eap;
  136. int ret;
  137. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  138. EAP_VENDOR_ID, EAP_VENDOR_TYPE,
  139. "VENDOR-TEST");
  140. if (eap == NULL)
  141. return -1;
  142. eap->init = eap_vendor_test_init;
  143. eap->reset = eap_vendor_test_reset;
  144. eap->buildReq = eap_vendor_test_buildReq;
  145. eap->check = eap_vendor_test_check;
  146. eap->process = eap_vendor_test_process;
  147. eap->isDone = eap_vendor_test_isDone;
  148. eap->getKey = eap_vendor_test_getKey;
  149. eap->isSuccess = eap_vendor_test_isSuccess;
  150. ret = eap_server_method_register(eap);
  151. if (ret)
  152. eap_server_method_free(eap);
  153. return ret;
  154. }