eap_example_server.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = "server-key.pem";
  70. /* tparams.private_key_passwd = "whatever"; */
  71. if (tls_global_set_params(eap_ctx.tls_ctx, &tparams)) {
  72. printf("Failed to set TLS parameters\n");
  73. return -1;
  74. }
  75. if (tls_global_set_verify(eap_ctx.tls_ctx, 0)) {
  76. printf("Failed to set check_crl\n");
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. int eap_example_server_init(void)
  82. {
  83. if (eap_server_register_methods() < 0)
  84. return -1;
  85. os_memset(&eap_ctx, 0, sizeof(eap_ctx));
  86. if (eap_example_server_init_tls() < 0)
  87. return -1;
  88. os_memset(&eap_cb, 0, sizeof(eap_cb));
  89. eap_cb.get_eap_user = server_get_eap_user;
  90. eap_cb.get_eap_req_id_text = server_get_eap_req_id_text;
  91. os_memset(&eap_conf, 0, sizeof(eap_conf));
  92. eap_conf.eap_server = 1;
  93. eap_conf.ssl_ctx = eap_ctx.tls_ctx;
  94. eap_ctx.eap = eap_server_sm_init(&eap_ctx, &eap_cb, &eap_conf);
  95. if (eap_ctx.eap == NULL)
  96. return -1;
  97. eap_ctx.eap_if = eap_get_interface(eap_ctx.eap);
  98. /* Enable "port" and request EAP to start authentication. */
  99. eap_ctx.eap_if->portEnabled = TRUE;
  100. eap_ctx.eap_if->eapRestart = TRUE;
  101. return 0;
  102. }
  103. void eap_example_server_deinit(void)
  104. {
  105. eap_server_sm_deinit(eap_ctx.eap);
  106. eap_server_unregister_methods();
  107. tls_deinit(eap_ctx.tls_ctx);
  108. }
  109. int eap_example_server_step(void)
  110. {
  111. int res, process = 0;
  112. res = eap_server_sm_step(eap_ctx.eap);
  113. if (eap_ctx.eap_if->eapReq) {
  114. printf("==> Request\n");
  115. process = 1;
  116. eap_ctx.eap_if->eapReq = 0;
  117. }
  118. if (eap_ctx.eap_if->eapSuccess) {
  119. printf("==> Success\n");
  120. process = 1;
  121. res = 0;
  122. eap_ctx.eap_if->eapSuccess = 0;
  123. if (eap_ctx.eap_if->eapKeyAvailable) {
  124. wpa_hexdump(MSG_DEBUG, "EAP keying material",
  125. eap_ctx.eap_if->eapKeyData,
  126. eap_ctx.eap_if->eapKeyDataLen);
  127. }
  128. }
  129. if (eap_ctx.eap_if->eapFail) {
  130. printf("==> Fail\n");
  131. process = 1;
  132. eap_ctx.eap_if->eapFail = 0;
  133. }
  134. if (process && eap_ctx.eap_if->eapReqData) {
  135. /* Send EAP response to the server */
  136. eap_example_peer_rx(wpabuf_head(eap_ctx.eap_if->eapReqData),
  137. wpabuf_len(eap_ctx.eap_if->eapReqData));
  138. }
  139. return res;
  140. }
  141. void eap_example_server_rx(const u8 *data, size_t data_len)
  142. {
  143. /* Make received EAP message available to the EAP library */
  144. wpabuf_free(eap_ctx.eap_if->eapRespData);
  145. eap_ctx.eap_if->eapRespData = wpabuf_alloc_copy(data, data_len);
  146. if (eap_ctx.eap_if->eapRespData)
  147. eap_ctx.eap_if->eapResp = TRUE;
  148. }