]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/eap_tlv.c
This commit was generated by cvs2svn to compensate for changes in r168609,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / eap_tlv.c
1 /*
2  * WPA Supplicant / EAP-TLV (draft-josefsson-pppext-eap-tls-eap-07.txt)
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.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 <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "common.h"
20 #include "wpa_supplicant.h"
21 #include "eap_i.h"
22 #include "eap_tlv.h"
23
24
25 u8 * eap_tlv_build_nak(int id, u16 nak_type, size_t *resp_len)
26 {
27         struct eap_hdr *hdr;
28         u8 *pos;
29
30         *resp_len = sizeof(struct eap_hdr) + 1 + 10;
31         hdr = malloc(*resp_len);
32         if (hdr == NULL)
33                 return NULL;
34
35         hdr->code = EAP_CODE_RESPONSE;
36         hdr->identifier = id;
37         hdr->length = host_to_be16(*resp_len);
38         pos = (u8 *) (hdr + 1);
39         *pos++ = EAP_TYPE_TLV;
40         *pos++ = 0x80; /* Mandatory */
41         *pos++ = EAP_TLV_NAK_TLV;
42         /* Length */
43         *pos++ = 0;
44         *pos++ = 6;
45         /* Vendor-Id */
46         *pos++ = 0;
47         *pos++ = 0;
48         *pos++ = 0;
49         *pos++ = 0;
50         /* NAK-Type */
51         WPA_PUT_BE16(pos, nak_type);
52
53         return (u8 *) hdr;
54 }
55
56
57 u8 * eap_tlv_build_result(int id, u16 status, size_t *resp_len)
58 {
59         struct eap_hdr *hdr;
60         u8 *pos;
61
62         *resp_len = sizeof(struct eap_hdr) + 1 + 6;
63         hdr = malloc(*resp_len);
64         if (hdr == NULL)
65                 return NULL;
66
67         hdr->code = EAP_CODE_RESPONSE;
68         hdr->identifier = id;
69         hdr->length = host_to_be16(*resp_len);
70         pos = (u8 *) (hdr + 1);
71         *pos++ = EAP_TYPE_TLV;
72         *pos++ = 0x80; /* Mandatory */
73         *pos++ = EAP_TLV_RESULT_TLV;
74         /* Length */
75         *pos++ = 0;
76         *pos++ = 2;
77         /* Status */
78         WPA_PUT_BE16(pos, status);
79
80         return (u8 *) hdr;
81 }
82
83
84 int eap_tlv_process(struct eap_sm *sm, struct eap_method_ret *ret,
85                     const struct eap_hdr *hdr, u8 **resp, size_t *resp_len)
86 {
87         size_t left;
88         const u8 *pos;
89         const u8 *result_tlv = NULL;
90         size_t result_tlv_len = 0;
91         int tlv_type, mandatory, tlv_len;
92
93         /* Parse TLVs */
94         left = be_to_host16(hdr->length) - sizeof(struct eap_hdr) - 1;
95         pos = (const u8 *) (hdr + 1);
96         pos++;
97         wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
98         while (left >= 4) {
99                 mandatory = !!(pos[0] & 0x80);
100                 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
101                 pos += 2;
102                 tlv_len = WPA_GET_BE16(pos);
103                 pos += 2;
104                 left -= 4;
105                 if (tlv_len > left) {
106                         wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
107                                    "(tlv_len=%d left=%lu)", tlv_len,
108                                    (unsigned long) left);
109                         return -1;
110                 }
111                 switch (tlv_type) {
112                 case EAP_TLV_RESULT_TLV:
113                         result_tlv = pos;
114                         result_tlv_len = tlv_len;
115                         break;
116                 default:
117                         wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
118                                    "%d%s", tlv_type,
119                                    mandatory ? " (mandatory)" : "");
120                         if (mandatory) {
121                                 /* NAK TLV and ignore all TLVs in this packet.
122                                  */
123                                 *resp = eap_tlv_build_nak(hdr->identifier,
124                                                           tlv_type, resp_len);
125                                 return *resp == NULL ? -1 : 0;
126                         }
127                         /* Ignore this TLV, but process other TLVs */
128                         break;
129                 }
130
131                 pos += tlv_len;
132                 left -= tlv_len;
133         }
134         if (left) {
135                 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
136                            "Request (left=%lu)", (unsigned long) left);
137                 return -1;
138         }
139
140         /* Process supported TLVs */
141         if (result_tlv) {
142                 int status, resp_status;
143                 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
144                             result_tlv, result_tlv_len);
145                 if (result_tlv_len < 2) {
146                         wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
147                                    "(len=%lu)",
148                                    (unsigned long) result_tlv_len);
149                         return -1;
150                 }
151                 status = WPA_GET_BE16(result_tlv);
152                 if (status == EAP_TLV_RESULT_SUCCESS) {
153                         wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
154                                    "- EAP-TLV/Phase2 Completed");
155                         resp_status = EAP_TLV_RESULT_SUCCESS;
156                         ret->decision = DECISION_UNCOND_SUCC;
157                 } else if (status == EAP_TLV_RESULT_FAILURE) {
158                         wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
159                         resp_status = EAP_TLV_RESULT_FAILURE;
160                         ret->decision = DECISION_FAIL;
161                 } else {
162                         wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
163                                    "Status %d", status);
164                         resp_status = EAP_TLV_RESULT_FAILURE;
165                         ret->decision = DECISION_FAIL;
166                 }
167                 ret->methodState = METHOD_DONE;
168
169                 *resp = eap_tlv_build_result(hdr->identifier, resp_status,
170                                              resp_len);
171         }
172
173         return 0;
174 }