eap_example_server.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Alternatively, this software may be distributed under the terms of BSD
  11. * license.
  12. *
  13. * See README and COPYING for more details.
  14. */
  15. #include "includes.h"
  16. #include "common.h"
  17. #include "eap_server/eap.h"
  18. #include "tls.h"
  19. #include "wpabuf.h"
  20. void eap_example_peer_rx(const u8 *data, size_t data_len);
  21. struct eap_server_ctx {
  22. struct eap_eapol_interface *eap_if;
  23. struct eap_sm *eap;
  24. void *tls_ctx;
  25. };
  26. static struct eap_server_ctx eap_ctx;
  27. static int server_get_eap_user(void *ctx, const u8 *identity,
  28. size_t identity_len, int phase2,
  29. struct eap_user *user)
  30. {
  31. os_memset(user, 0, sizeof(*user));
  32. if (!phase2) {
  33. /* Only allow EAP-PEAP as the Phase 1 method */
  34. user->methods[0].vendor = EAP_VENDOR_IETF;
  35. user->methods[0].method = EAP_TYPE_PEAP;
  36. return 0;
  37. }
  38. if (identity_len != 4 || identity == NULL ||
  39. os_memcmp(identity, "user", 4) != 0) {
  40. printf("Unknown user\n");
  41. return -1;
  42. }
  43. /* Only allow EAP-MSCHAPv2 as the Phase 2 method */
  44. user->methods[0].vendor = EAP_VENDOR_IETF;
  45. user->methods[0].method = EAP_TYPE_MSCHAPV2;
  46. user->password = (u8 *) os_strdup("password");
  47. user->password_len = 8;
  48. return 0;
  49. }
  50. static const char * server_get_eap_req_id_text(void *ctx, size_t *len)
  51. {
  52. *len = 0;
  53. return NULL;
  54. }
  55. static struct eapol_callbacks eap_cb;
  56. static struct eap_config eap_conf;
  57. static int eap_example_server_init_tls(void)
  58. {
  59. struct tls_config tconf;
  60. struct tls_connection_params tparams;
  61. os_memset(&tconf, 0, sizeof(tconf));
  62. eap_ctx.tls_ctx = tls_init(&tconf);
  63. if (eap_ctx.tls_ctx == NULL)
  64. return -1;
  65. os_memset(&tparams, 0, sizeof(tparams));
  66. tparams.ca_cert = "ca.pem";
  67. tparams.client_cert = "server.pem";
  68. tparams.private_key = "server.key";
  69. tparams.private_key_passwd = "whatever";
  70. if (tls_global_set_params(eap_ctx.tls_ctx, &tparams)) {
  71. printf("Failed to set TLS parameters\n");
  72. return -1;
  73. }
  74. if (tls_global_set_verify(eap_ctx.tls_ctx, 0)) {
  75. printf("Failed to set check_crl\n");
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. int eap_example_server_init(void)
  81. {
  82. if (eap_server_register_methods() < 0)
  83. return -1;
  84. os_memset(&eap_ctx, 0, sizeof(eap_ctx));
  85. if (eap_example_server_init_tls() < 0)
  86. return -1;
  87. os_memset(&eap_cb, 0, sizeof(eap_cb));
  88. eap_cb.get_eap_user = server_get_eap_user;
  89. eap_cb.get_eap_req_id_text = server_get_eap_req_id_text;
  90. os_memset(&eap_conf, 0, sizeof(eap_conf));
  91. eap_conf.eap_server = 1;
  92. eap_conf.ssl_ctx = eap_ctx.tls_ctx;
  93. eap_ctx.eap = eap_server_sm_init(&eap_ctx, &eap_cb, &eap_conf);
  94. if (eap_ctx.eap == NULL)
  95. return -1;
  96. eap_ctx.eap_if = eap_get_interface(eap_ctx.eap);
  97. /* Enable "port" and request EAP to start authentication. */
  98. eap_ctx.eap_if->portEnabled = TRUE;
  99. eap_ctx.eap_if->eapRestart = TRUE;
  100. return 0;
  101. }
  102. void eap_example_server_deinit(void)
  103. {
  104. eap_server_sm_deinit(eap_ctx.eap);
  105. eap_server_unregister_methods();
  106. tls_deinit(eap_ctx.tls_ctx);
  107. }
  108. int eap_example_server_step(void)
  109. {
  110. int res, process = 0;
  111. res = eap_server_sm_step(eap_ctx.eap);
  112. if (eap_ctx.eap_if->eapReq) {
  113. printf("==> Request\n");
  114. process = 1;
  115. eap_ctx.eap_if->eapReq = 0;
  116. }
  117. if (eap_ctx.eap_if->eapSuccess) {
  118. printf("==> Success\n");
  119. process = 1;
  120. res = 0;
  121. eap_ctx.eap_if->eapSuccess = 0;
  122. if (eap_ctx.eap_if->eapKeyAvailable) {
  123. wpa_hexdump(MSG_DEBUG, "EAP keying material",
  124. eap_ctx.eap_if->eapKeyData,
  125. eap_ctx.eap_if->eapKeyDataLen);
  126. }
  127. }
  128. if (eap_ctx.eap_if->eapFail) {
  129. printf("==> Fail\n");
  130. process = 1;
  131. eap_ctx.eap_if->eapFail = 0;
  132. }
  133. if (process && eap_ctx.eap_if->eapReqData) {
  134. /* Send EAP response to the server */
  135. eap_example_peer_rx(wpabuf_head(eap_ctx.eap_if->eapReqData),
  136. wpabuf_len(eap_ctx.eap_if->eapReqData));
  137. }
  138. return res;
  139. }
  140. void eap_example_server_rx(const u8 *data, size_t data_len)
  141. {
  142. /* Make received EAP message available to the EAP library */
  143. wpabuf_free(eap_ctx.eap_if->eapRespData);
  144. eap_ctx.eap_if->eapRespData = wpabuf_alloc_copy(data, data_len);
  145. if (eap_ctx.eap_if->eapRespData)
  146. eap_ctx.eap_if->eapResp = TRUE;
  147. }