0e28995ed13df7b69a041c864fc78796c4bef89c
[libeap.git] / eap_example / eap_example_server.c
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
16 #include "includes.h"
17
18 #include "common.h"
19 #include "eap_server/eap.h"
20 #include "tls.h"
21 #include "wpabuf.h"
22
23 void eap_example_peer_rx(const u8 *data, size_t data_len);
24
25
26 struct eap_server_ctx {
27         struct eap_eapol_interface *eap_if;
28         struct eap_sm *eap;
29         void *tls_ctx;
30 };
31
32 static struct eap_server_ctx eap_ctx;
33
34
35 static int server_get_eap_user(void *ctx, const u8 *identity,
36                                size_t identity_len, int phase2,
37                                struct eap_user *user)
38 {
39         os_memset(user, 0, sizeof(*user));
40
41         if (!phase2) {
42                 /* Only allow EAP-PEAP as the Phase 1 method */
43                 user->methods[0].vendor = EAP_VENDOR_IETF;
44                 user->methods[0].method = EAP_TYPE_PEAP;
45                 return 0;
46         }
47
48         if (identity_len != 4 || identity == NULL ||
49             os_memcmp(identity, "user", 4) != 0) {
50                 printf("Unknown user\n");
51                 return -1;
52         }
53
54         /* Only allow EAP-MSCHAPv2 as the Phase 2 method */
55         user->methods[0].vendor = EAP_VENDOR_IETF;
56         user->methods[0].method = EAP_TYPE_MSCHAPV2;
57         user->password = (u8 *) os_strdup("password");
58         user->password_len = 8;
59
60         return 0;
61 }
62
63
64 static const char * server_get_eap_req_id_text(void *ctx, size_t *len)
65 {
66         *len = 0;
67         return NULL;
68 }
69
70
71 static struct eapol_callbacks eap_cb;
72 static struct eap_config eap_conf;
73
74 static int eap_example_server_init_tls(void)
75 {
76         struct tls_config tconf;
77         struct tls_connection_params tparams;
78
79         os_memset(&tconf, 0, sizeof(tconf));
80         eap_ctx.tls_ctx = tls_init(&tconf);
81         if (eap_ctx.tls_ctx == NULL)
82                 return -1;
83
84         os_memset(&tparams, 0, sizeof(tparams));
85         tparams.ca_cert = "ca.pem";
86         tparams.client_cert = "server.pem";
87         /* tparams.private_key = "server.key"; */
88         tparams.private_key = "server-key.pem";
89         /* tparams.private_key_passwd = "whatever"; */
90
91         if (tls_global_set_params(eap_ctx.tls_ctx, &tparams)) {
92                 printf("Failed to set TLS parameters\n");
93                 return -1;
94         }
95
96         if (tls_global_set_verify(eap_ctx.tls_ctx, 0)) {
97                 printf("Failed to set check_crl\n");
98                 return -1;
99         }
100
101         return 0;
102 }
103
104
105 int eap_example_server_init(void)
106 {
107         if (eap_server_register_methods() < 0)
108                 return -1;
109
110         os_memset(&eap_ctx, 0, sizeof(eap_ctx));
111
112         if (eap_example_server_init_tls() < 0)
113                 return -1;
114
115         os_memset(&eap_cb, 0, sizeof(eap_cb));
116         eap_cb.get_eap_user = server_get_eap_user;
117         eap_cb.get_eap_req_id_text = server_get_eap_req_id_text;
118
119         os_memset(&eap_conf, 0, sizeof(eap_conf));
120         eap_conf.eap_server = 1;
121         eap_conf.ssl_ctx = eap_ctx.tls_ctx;
122
123         eap_ctx.eap = eap_server_sm_init(&eap_ctx, &eap_cb, &eap_conf);
124         if (eap_ctx.eap == NULL)
125                 return -1;
126
127         eap_ctx.eap_if = eap_get_interface(eap_ctx.eap);
128
129         /* Enable "port" and request EAP to start authentication. */
130         eap_ctx.eap_if->portEnabled = TRUE;
131         eap_ctx.eap_if->eapRestart = TRUE;
132
133         return 0;
134 }
135
136
137 void eap_example_server_deinit(void)
138 {
139         eap_server_sm_deinit(eap_ctx.eap);
140         eap_server_unregister_methods();
141         tls_deinit(eap_ctx.tls_ctx);
142 }
143
144
145 int eap_example_server_step(void)
146 {
147         int res, process = 0;
148
149         res = eap_server_sm_step(eap_ctx.eap);
150
151         if (eap_ctx.eap_if->eapReq) {
152                 printf("==> Request\n");
153                 process = 1;
154                 eap_ctx.eap_if->eapReq = 0;
155         }
156
157         if (eap_ctx.eap_if->eapSuccess) {
158                 printf("==> Success\n");
159                 process = 1;
160                 res = 0;
161                 eap_ctx.eap_if->eapSuccess = 0;
162
163                 if (eap_ctx.eap_if->eapKeyAvailable) {
164                         wpa_hexdump(MSG_DEBUG, "EAP keying material",
165                                     eap_ctx.eap_if->eapKeyData,
166                                     eap_ctx.eap_if->eapKeyDataLen);
167                 }
168         }
169
170         if (eap_ctx.eap_if->eapFail) {
171                 printf("==> Fail\n");
172                 process = 1;
173                 eap_ctx.eap_if->eapFail = 0;
174         }
175
176         if (process && eap_ctx.eap_if->eapReqData) {
177                 /* Send EAP response to the server */
178                 eap_example_peer_rx(wpabuf_head(eap_ctx.eap_if->eapReqData),
179                                     wpabuf_len(eap_ctx.eap_if->eapReqData));
180         }
181
182         return res;
183 }
184
185
186 void eap_example_server_rx(const u8 *data, size_t data_len)
187 {
188         /* Make received EAP message available to the EAP library */
189         wpabuf_free(eap_ctx.eap_if->eapRespData);
190         eap_ctx.eap_if->eapRespData = wpabuf_alloc_copy(data, data_len);
191         if (eap_ctx.eap_if->eapRespData)
192                 eap_ctx.eap_if->eapResp = TRUE;
193 }