166458Sdfr/* 266458Sdfr * EAP common peer/server definitions 366458Sdfr * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi> 4139790Simp * 566458Sdfr * This software may be distributed under the terms of the BSD license. 666458Sdfr * See README for more details. 766458Sdfr */ 866458Sdfr 966458Sdfr#include "includes.h" 1066458Sdfr 1166458Sdfr#include "common.h" 1266458Sdfr#include "eap_defs.h" 1366458Sdfr#include "eap_common.h" 1466458Sdfr 1566458Sdfr/** 1666458Sdfr * eap_hdr_len_valid - Validate EAP header length field 1766458Sdfr * @msg: EAP frame (starting with EAP header) 1866458Sdfr * @min_payload: Minimum payload length needed 1966458Sdfr * Returns: 1 for valid header, 0 for invalid 2066458Sdfr * 2166458Sdfr * This is a helper function that does minimal validation of EAP messages. The 2266458Sdfr * length field is verified to be large enough to include the header and not 2366458Sdfr * too large to go beyond the end of the buffer. 2466458Sdfr */ 2566458Sdfrint eap_hdr_len_valid(const struct wpabuf *msg, size_t min_payload) 2666458Sdfr{ 2766458Sdfr const struct eap_hdr *hdr; 2866458Sdfr size_t len; 2966458Sdfr 3066458Sdfr if (msg == NULL) 3166458Sdfr return 0; 3266458Sdfr 3366458Sdfr hdr = wpabuf_head(msg); 3466458Sdfr 35113941Skan if (wpabuf_len(msg) < sizeof(*hdr)) { 36113941Skan wpa_printf(MSG_INFO, "EAP: Too short EAP frame"); 3766458Sdfr return 0; 38114678Skan } 39114678Skan 40114678Skan len = be_to_host16(hdr->length); 41114678Skan if (len < sizeof(*hdr) + min_payload || len > wpabuf_len(msg)) { 42114678Skan wpa_printf(MSG_INFO, "EAP: Invalid EAP length"); 43114678Skan return 0; 44114678Skan } 45114678Skan 46114678Skan return 1; 47115164Skan} 48115164Skan 49113941Skan 50113941Skan/** 5166458Sdfr * eap_hdr_validate - Validate EAP header 52217145Stijl * @vendor: Expected EAP Vendor-Id (0 = IETF) 5366458Sdfr * @eap_type: Expected EAP type number 54217145Stijl * @msg: EAP frame (starting with EAP header) 55113941Skan * @plen: Pointer to variable to contain the returned payload length 56113941Skan * Returns: Pointer to EAP payload (after type field), or %NULL on failure 5766458Sdfr * 58217145Stijl * This is a helper function for EAP method implementations. This is usually 59113941Skan * called in the beginning of struct eap_method::process() function to verify 60113941Skan * that the received EAP request packet has a valid header. This function is 6166458Sdfr * able to process both legacy and expanded EAP headers and in most cases, the 62217145Stijl * caller can just use the returned payload pointer (into *plen) for processing 63217145Stijl * the payload regardless of whether the packet used the expanded EAP header or 64217145Stijl * not. 65113941Skan */ 66217145Stijlconst u8 * eap_hdr_validate(int vendor, EapType eap_type, 6767488Sobrien const struct wpabuf *msg, size_t *plen) 68113941Skan{ 69113941Skan const struct eap_hdr *hdr; 70113941Skan const u8 *pos; 7167488Sobrien size_t len; 72113941Skan 7366458Sdfr if (!eap_hdr_len_valid(msg, 1)) 74113941Skan return NULL; 7566458Sdfr 76113941Skan hdr = wpabuf_head(msg); 77113941Skan len = be_to_host16(hdr->length); 7881720Sache pos = (const u8 *) (hdr + 1); 7967488Sobrien 80113941Skan if (*pos == EAP_TYPE_EXPANDED) { 81113941Skan int exp_vendor; 82113941Skan u32 exp_type; 8366458Sdfr if (len < sizeof(*hdr) + 8) { 84113941Skan wpa_printf(MSG_INFO, "EAP: Invalid expanded EAP " 85113941Skan "length"); 8666458Sdfr return NULL; 87149337Sstefanf } 88149337Sstefanf pos++; 89149337Sstefanf exp_vendor = WPA_GET_BE24(pos); 90113941Skan pos += 3; 91 exp_type = WPA_GET_BE32(pos); 92 pos += 4; 93 if (exp_vendor != vendor || exp_type != (u32) eap_type) { 94 wpa_printf(MSG_INFO, "EAP: Invalid expanded frame " 95 "type"); 96 return NULL; 97 } 98 99 *plen = len - sizeof(*hdr) - 8; 100 return pos; 101 } else { 102 if (vendor != EAP_VENDOR_IETF || *pos != eap_type) { 103 wpa_printf(MSG_INFO, "EAP: Invalid frame type"); 104 return NULL; 105 } 106 *plen = len - sizeof(*hdr) - 1; 107 return pos + 1; 108 } 109} 110 111 112/** 113 * eap_msg_alloc - Allocate a buffer for an EAP message 114 * @vendor: Vendor-Id (0 = IETF) 115 * @type: EAP type 116 * @payload_len: Payload length in bytes (data after Type) 117 * @code: Message Code (EAP_CODE_*) 118 * @identifier: Identifier 119 * Returns: Pointer to the allocated message buffer or %NULL on error 120 * 121 * This function can be used to allocate a buffer for an EAP message and fill 122 * in the EAP header. This function is automatically using expanded EAP header 123 * if the selected Vendor-Id is not IETF. In other words, most EAP methods do 124 * not need to separately select which header type to use when using this 125 * function to allocate the message buffers. The returned buffer has room for 126 * payload_len bytes and has the EAP header and Type field already filled in. 127 */ 128struct wpabuf * eap_msg_alloc(int vendor, EapType type, size_t payload_len, 129 u8 code, u8 identifier) 130{ 131 struct wpabuf *buf; 132 struct eap_hdr *hdr; 133 size_t len; 134 135 len = sizeof(struct eap_hdr) + (vendor == EAP_VENDOR_IETF ? 1 : 8) + 136 payload_len; 137 buf = wpabuf_alloc(len); 138 if (buf == NULL) 139 return NULL; 140 141 hdr = wpabuf_put(buf, sizeof(*hdr)); 142 hdr->code = code; 143 hdr->identifier = identifier; 144 hdr->length = host_to_be16(len); 145 146 if (vendor == EAP_VENDOR_IETF) { 147 wpabuf_put_u8(buf, type); 148 } else { 149 wpabuf_put_u8(buf, EAP_TYPE_EXPANDED); 150 wpabuf_put_be24(buf, vendor); 151 wpabuf_put_be32(buf, type); 152 } 153 154 return buf; 155} 156 157 158/** 159 * eap_update_len - Update EAP header length 160 * @msg: EAP message from eap_msg_alloc 161 * 162 * This function updates the length field in the EAP header to match with the 163 * current length for the buffer. This allows eap_msg_alloc() to be used to 164 * allocate a larger buffer than the exact message length (e.g., if exact 165 * message length is not yet known). 166 */ 167void eap_update_len(struct wpabuf *msg) 168{ 169 struct eap_hdr *hdr; 170 hdr = wpabuf_mhead(msg); 171 if (wpabuf_len(msg) < sizeof(*hdr)) 172 return; 173 hdr->length = host_to_be16(wpabuf_len(msg)); 174} 175 176 177/** 178 * eap_get_id - Get EAP Identifier from wpabuf 179 * @msg: Buffer starting with an EAP header 180 * Returns: The Identifier field from the EAP header 181 */ 182u8 eap_get_id(const struct wpabuf *msg) 183{ 184 const struct eap_hdr *eap; 185 186 if (wpabuf_len(msg) < sizeof(*eap)) 187 return 0; 188 189 eap = wpabuf_head(msg); 190 return eap->identifier; 191} 192 193 194/** 195 * eap_get_id - Get EAP Type from wpabuf 196 * @msg: Buffer starting with an EAP header 197 * Returns: The EAP Type after the EAP header 198 */ 199EapType eap_get_type(const struct wpabuf *msg) 200{ 201 if (wpabuf_len(msg) < sizeof(struct eap_hdr) + 1) 202 return EAP_TYPE_NONE; 203 204 return ((const u8 *) wpabuf_head(msg))[sizeof(struct eap_hdr)]; 205} 206