1 /*
2 * EAP peer method: EAP-MD5 (RFC 3748 and RFC 1994)
3 * Copyright (c) 2004-2006, 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
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eap_i.h"
19 #include "md5.h"
20 #include "crypto.h"
21
22
eap_md5_init(struct eap_sm * sm)23 static void * eap_md5_init(struct eap_sm *sm)
24 {
25 /* No need for private data. However, must return non-NULL to indicate
26 * success. */
27 return (void *) 1;
28 }
29
30
eap_md5_deinit(struct eap_sm * sm,void * priv)31 static void eap_md5_deinit(struct eap_sm *sm, void *priv)
32 {
33 }
34
35
eap_md5_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const u8 * reqData,size_t reqDataLen,size_t * respDataLen)36 static u8 * eap_md5_process(struct eap_sm *sm, void *priv,
37 struct eap_method_ret *ret,
38 const u8 *reqData, size_t reqDataLen,
39 size_t *respDataLen)
40 {
41 const struct eap_hdr *req;
42 struct eap_hdr *resp;
43 const u8 *pos, *challenge, *password;
44 u8 *rpos;
45 size_t len, challenge_len, password_len;
46 const u8 *addr[3];
47 size_t elen[3];
48
49 password = eap_get_config_password(sm, &password_len);
50 if (password == NULL) {
51 wpa_printf(MSG_INFO, "EAP-MD5: Password not configured");
52 eap_sm_request_password(sm);
53 ret->ignore = TRUE;
54 return NULL;
55 }
56
57 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MD5,
58 reqData, reqDataLen, &len);
59 if (pos == NULL || len == 0) {
60 wpa_printf(MSG_INFO, "EAP-MD5: Invalid frame (pos=%p len=%lu)",
61 pos, (unsigned long) len);
62 ret->ignore = TRUE;
63 return NULL;
64 }
65
66 /*
67 * CHAP Challenge:
68 * Value-Size (1 octet) | Value(Challenge) | Name(optional)
69 */
70 req = (const struct eap_hdr *) reqData;
71 challenge_len = *pos++;
72 if (challenge_len == 0 || challenge_len > len - 1) {
73 wpa_printf(MSG_INFO, "EAP-MD5: Invalid challenge "
74 "(challenge_len=%lu len=%lu)",
75 (unsigned long) challenge_len, (unsigned long) len);
76 ret->ignore = TRUE;
77 return NULL;
78 }
79 ret->ignore = FALSE;
80 challenge = pos;
81 wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Challenge",
82 challenge, challenge_len);
83
84 wpa_printf(MSG_DEBUG, "EAP-MD5: Generating Challenge Response");
85 ret->methodState = METHOD_DONE;
86 ret->decision = DECISION_UNCOND_SUCC;
87 ret->allowNotifications = TRUE;
88
89 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MD5, respDataLen,
90 1 + MD5_MAC_LEN, EAP_CODE_RESPONSE,
91 req->identifier, &rpos);
92 if (resp == NULL)
93 return NULL;
94
95 /*
96 * CHAP Response:
97 * Value-Size (1 octet) | Value(Response) | Name(optional)
98 */
99 *rpos++ = MD5_MAC_LEN;
100
101 addr[0] = &resp->identifier;
102 elen[0] = 1;
103 addr[1] = password;
104 elen[1] = password_len;
105 addr[2] = challenge;
106 elen[2] = challenge_len;
107 md5_vector(3, addr, elen, rpos);
108 wpa_hexdump(MSG_MSGDUMP, "EAP-MD5: Response", rpos, MD5_MAC_LEN);
109
110 return (u8 *) resp;
111 }
112
113
eap_peer_md5_register(void)114 int eap_peer_md5_register(void)
115 {
116 struct eap_method *eap;
117 int ret;
118
119 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
120 EAP_VENDOR_IETF, EAP_TYPE_MD5, "MD5");
121 if (eap == NULL)
122 return -1;
123
124 eap->init = eap_md5_init;
125 eap->deinit = eap_md5_deinit;
126 eap->process = eap_md5_process;
127
128 ret = eap_peer_method_register(eap);
129 if (ret)
130 eap_peer_method_free(eap);
131 return ret;
132 }
133