eap_example_server.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Example application showing how EAP server code from hostapd can be used as
  3. * a library.
  4. * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "crypto/tls.h"
  12. #include "eap_server/eap.h"
  13. #include "wpabuf.h"
  14. void eap_example_peer_rx(const u8 *data, size_t data_len);
  15. struct eap_server_ctx {
  16. struct eap_eapol_interface *eap_if;
  17. struct eap_sm *eap;
  18. void *tls_ctx;
  19. };
  20. static struct eap_server_ctx eap_ctx;
  21. static int server_get_eap_user(void *ctx, const u8 *identity,
  22. size_t identity_len, int phase2,
  23. struct eap_user *user)
  24. {
  25. os_memset(user, 0, sizeof(*user));
  26. if (!phase2) {
  27. /* Only allow EAP-PEAP as the Phase 1 method */
  28. user->methods[0].vendor = EAP_VENDOR_IETF;
  29. user->methods[0].method = EAP_TYPE_PEAP;
  30. return 0;
  31. }
  32. if (identity_len != 4 || identity == NULL ||
  33. os_memcmp(identity, "user", 4) != 0) {
  34. printf("Unknown user\n");
  35. return -1;
  36. }
  37. /* Only allow EAP-MSCHAPv2 as the Phase 2 method */
  38. user->methods[0].vendor = EAP_VENDOR_IETF;
  39. user->methods[0].method = EAP_TYPE_MSCHAPV2;
  40. user->password = (u8 *) os_strdup("password");
  41. user->password_len = 8;
  42. return 0;
  43. }
  44. static const char * server_get_eap_req_id_text(void *ctx, size_t *len)
  45. {
  46. *len = 0;
  47. return NULL;
  48. }
  49. static struct eapol_callbacks eap_cb;
  50. static struct eap_config eap_conf;
  51. static int eap_example_server_init_tls(void)
  52. {
  53. struct tls_config tconf;
  54. struct tls_connection_params tparams;
  55. os_memset(&tconf, 0, sizeof(tconf));
  56. eap_ctx.tls_ctx = tls_init(&tconf);
  57. if (eap_ctx.tls_ctx == NULL)
  58. return -1;
  59. os_memset(&tparams, 0, sizeof(tparams));
  60. tparams.ca_cert = "ca.pem";
  61. tparams.client_cert = "server.pem";
  62. /* tparams.private_key = "server.key"; */
  63. tparams.private_key = "server-key.pem";
  64. /* tparams.private_key_passwd = "whatever"; */
  65. tparams.dh_file = "dh.conf";
  66. if (tls_global_set_params(eap_ctx.tls_ctx, &tparams)) {
  67. printf("Failed to set TLS parameters\n");
  68. return -1;
  69. }
  70. if (tls_global_set_verify(eap_ctx.tls_ctx, 0)) {
  71. printf("Failed to set check_crl\n");
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static int eap_server_register_methods(void)
  77. {
  78. int ret = 0;
  79. #ifdef EAP_SERVER_IDENTITY
  80. if (ret == 0)
  81. ret = eap_server_identity_register();
  82. #endif /* EAP_SERVER_IDENTITY */
  83. #ifdef EAP_SERVER_MD5
  84. if (ret == 0)
  85. ret = eap_server_md5_register();
  86. #endif /* EAP_SERVER_MD5 */
  87. #ifdef EAP_SERVER_TLS
  88. if (ret == 0)
  89. ret = eap_server_tls_register();
  90. #endif /* EAP_SERVER_TLS */
  91. #ifdef EAP_SERVER_MSCHAPV2
  92. if (ret == 0)
  93. ret = eap_server_mschapv2_register();
  94. #endif /* EAP_SERVER_MSCHAPV2 */
  95. #ifdef EAP_SERVER_PEAP
  96. if (ret == 0)
  97. ret = eap_server_peap_register();
  98. #endif /* EAP_SERVER_PEAP */
  99. #ifdef EAP_SERVER_TLV
  100. if (ret == 0)
  101. ret = eap_server_tlv_register();
  102. #endif /* EAP_SERVER_TLV */
  103. #ifdef EAP_SERVER_GTC
  104. if (ret == 0)
  105. ret = eap_server_gtc_register();
  106. #endif /* EAP_SERVER_GTC */
  107. #ifdef EAP_SERVER_TTLS
  108. if (ret == 0)
  109. ret = eap_server_ttls_register();
  110. #endif /* EAP_SERVER_TTLS */
  111. #ifdef EAP_SERVER_SIM
  112. if (ret == 0)
  113. ret = eap_server_sim_register();
  114. #endif /* EAP_SERVER_SIM */
  115. #ifdef EAP_SERVER_AKA
  116. if (ret == 0)
  117. ret = eap_server_aka_register();
  118. #endif /* EAP_SERVER_AKA */
  119. #ifdef EAP_SERVER_AKA_PRIME
  120. if (ret == 0)
  121. ret = eap_server_aka_prime_register();
  122. #endif /* EAP_SERVER_AKA_PRIME */
  123. #ifdef EAP_SERVER_PAX
  124. if (ret == 0)
  125. ret = eap_server_pax_register();
  126. #endif /* EAP_SERVER_PAX */
  127. #ifdef EAP_SERVER_PSK
  128. if (ret == 0)
  129. ret = eap_server_psk_register();
  130. #endif /* EAP_SERVER_PSK */
  131. #ifdef EAP_SERVER_SAKE
  132. if (ret == 0)
  133. ret = eap_server_sake_register();
  134. #endif /* EAP_SERVER_SAKE */
  135. #ifdef EAP_SERVER_GPSK
  136. if (ret == 0)
  137. ret = eap_server_gpsk_register();
  138. #endif /* EAP_SERVER_GPSK */
  139. #ifdef EAP_SERVER_VENDOR_TEST
  140. if (ret == 0)
  141. ret = eap_server_vendor_test_register();
  142. #endif /* EAP_SERVER_VENDOR_TEST */
  143. #ifdef EAP_SERVER_FAST
  144. if (ret == 0)
  145. ret = eap_server_fast_register();
  146. #endif /* EAP_SERVER_FAST */
  147. #ifdef EAP_SERVER_WSC
  148. if (ret == 0)
  149. ret = eap_server_wsc_register();
  150. #endif /* EAP_SERVER_WSC */
  151. #ifdef EAP_SERVER_IKEV2
  152. if (ret == 0)
  153. ret = eap_server_ikev2_register();
  154. #endif /* EAP_SERVER_IKEV2 */
  155. #ifdef EAP_SERVER_TNC
  156. if (ret == 0)
  157. ret = eap_server_tnc_register();
  158. #endif /* EAP_SERVER_TNC */
  159. return ret;
  160. }
  161. int eap_example_server_init(void)
  162. {
  163. if (eap_server_register_methods() < 0)
  164. return -1;
  165. os_memset(&eap_ctx, 0, sizeof(eap_ctx));
  166. if (eap_example_server_init_tls() < 0)
  167. return -1;
  168. os_memset(&eap_cb, 0, sizeof(eap_cb));
  169. eap_cb.get_eap_user = server_get_eap_user;
  170. eap_cb.get_eap_req_id_text = server_get_eap_req_id_text;
  171. os_memset(&eap_conf, 0, sizeof(eap_conf));
  172. eap_conf.eap_server = 1;
  173. eap_conf.ssl_ctx = eap_ctx.tls_ctx;
  174. eap_ctx.eap = eap_server_sm_init(&eap_ctx, &eap_cb, &eap_conf);
  175. if (eap_ctx.eap == NULL)
  176. return -1;
  177. eap_ctx.eap_if = eap_get_interface(eap_ctx.eap);
  178. /* Enable "port" and request EAP to start authentication. */
  179. eap_ctx.eap_if->portEnabled = TRUE;
  180. eap_ctx.eap_if->eapRestart = TRUE;
  181. return 0;
  182. }
  183. void eap_example_server_deinit(void)
  184. {
  185. eap_server_sm_deinit(eap_ctx.eap);
  186. eap_server_unregister_methods();
  187. tls_deinit(eap_ctx.tls_ctx);
  188. }
  189. int eap_example_server_step(void)
  190. {
  191. int res, process = 0;
  192. res = eap_server_sm_step(eap_ctx.eap);
  193. if (eap_ctx.eap_if->eapReq) {
  194. printf("==> Request\n");
  195. process = 1;
  196. eap_ctx.eap_if->eapReq = 0;
  197. }
  198. if (eap_ctx.eap_if->eapSuccess) {
  199. printf("==> Success\n");
  200. process = 1;
  201. res = 0;
  202. eap_ctx.eap_if->eapSuccess = 0;
  203. if (eap_ctx.eap_if->eapKeyAvailable) {
  204. wpa_hexdump(MSG_DEBUG, "EAP keying material",
  205. eap_ctx.eap_if->eapKeyData,
  206. eap_ctx.eap_if->eapKeyDataLen);
  207. }
  208. }
  209. if (eap_ctx.eap_if->eapFail) {
  210. printf("==> Fail\n");
  211. process = 1;
  212. eap_ctx.eap_if->eapFail = 0;
  213. }
  214. if (process && eap_ctx.eap_if->eapReqData) {
  215. /* Send EAP request to the peer */
  216. eap_example_peer_rx(wpabuf_head(eap_ctx.eap_if->eapReqData),
  217. wpabuf_len(eap_ctx.eap_if->eapReqData));
  218. }
  219. return res;
  220. }
  221. void eap_example_server_rx(const u8 *data, size_t data_len)
  222. {
  223. /* Make received EAP message available to the EAP library */
  224. wpabuf_free(eap_ctx.eap_if->eapRespData);
  225. eap_ctx.eap_if->eapRespData = wpabuf_alloc_copy(data, data_len);
  226. if (eap_ctx.eap_if->eapRespData)
  227. eap_ctx.eap_if->eapResp = TRUE;
  228. }