1/*
2 * keyParserWep.c
3 *
4 * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 *  * Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *  * Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *  * Neither the name Texas Instruments nor the names of its
18 *    contributors may be used to endorse or promote products derived
19 *    from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** \file KeyParserWep.c
35 * \brief Wep key parser implementation.
36 *
37 * \see keyParser.h
38*/
39
40/****************************************************************************
41 *                                                                          *
42 *   MODULE:	Wep Key Parser                                             *
43 *   PURPOSE:   EAP parser implementation                                   *
44 *                                                                          *
45 ****************************************************************************/
46
47#define __FILE_ID__  FILE_ID_35
48#include "tidef.h"
49#include "osApi.h"
50#include "report.h"
51
52#include "keyTypes.h"
53
54#include "keyParser.h"
55#include "keyParserWep.h"
56#include "mainKeysSm.h"
57
58#include "unicastKeySM.h"
59#include "broadcastKeySM.h"
60
61
62TI_STATUS keyParserWep_recv(struct _keyParser_t *pKeyParser, TI_UINT8 *pKeyData, TI_UINT32 keyDataLen);
63TI_STATUS keyParserWep_remove(struct _keyParser_t *pKeyParser, TI_UINT8 *pKeyData, TI_UINT32 keyDataLen);
64
65/**
66*
67* Function  - Init KEY Parser module.
68*
69* \b Description:
70*
71* Called by RSN Manager.
72* Registers the function 'rsn_keyParserRecv()' at the distributor to receive KEY frames upon receiving a KEY_RECV event.
73*
74* \b ARGS:
75*
76*
77* \b RETURNS:
78*
79*  TI_STATUS - 0 on success, any other value on failure.
80*
81*/
82
83TI_STATUS keyParserWep_config(struct _keyParser_t *pKeyParser)
84{
85	pKeyParser->recv = keyParserWep_recv;
86	pKeyParser->replayReset = keyParser_nop;
87	pKeyParser->remove = keyParserWep_remove;
88	return TI_OK;
89}
90
91
92/**
93*
94* keyParserWep_recv
95*
96* \b Description:
97*
98* WEP key Parser receive function:
99*							- Called by the utility or NDIS (Windows)  upon receiving a WEP Key.
100*							- Filters the following keys:
101*								- Per Client Keys
102*								- Keys with size different than 40-bit, 104-bit and 232-bit
103*								- Keys with invalid key index
104*
105* \b ARGS:
106*
107*  I   - pKeyParser - Pointer to the keyParser context  \n
108*  I   - pKeyData - A pointer to the Key Data. \n
109*  I   - keyDataLen - The Key Data length. \n
110*
111* \b RETURNS:
112*
113*  TI_OK on success, TI_NOK otherwise.
114*
115*/
116TI_STATUS keyParserWep_recv(struct _keyParser_t *pKeyParser,
117						  TI_UINT8 *pKeyData, TI_UINT32 keyDataLen)
118{
119	TI_STATUS				        status;
120	OS_802_11_KEY 	                *pKeyDesc;
121    TSecurityKeys                  securityKey;
122
123	if (pKeyData == NULL)
124	{
125TRACE0(pKeyParser->hReport, REPORT_SEVERITY_ERROR, "WEP_KEY_PARSER: ERROR: NULL KEY Data\n");
126		return TI_NOK;
127	}
128
129	pKeyDesc = (OS_802_11_KEY*)pKeyData;
130
131	if ((pKeyDesc->KeyLength < MIN_KEY_LEN ) || (pKeyDesc->KeyLength >= MAX_KEY_LEN ))
132    {
133        TRACE1(pKeyParser->hReport, REPORT_SEVERITY_ERROR, "WEP_KEY_PARSER: ERROR: Key Length out of bounds=%d\n", pKeyDesc->KeyLength);
134        return TI_NOK;
135    }
136
137    if (pKeyDesc->KeyIndex & WEP_KEY_REMAIN_BITS_MASK)
138    {  /* the reamining bits in the key index are not 0 (when they should be) */
139TRACE0(pKeyParser->hReport, REPORT_SEVERITY_ERROR, "WEP_KEY_PARSER: ERROR: Key index bits 8-29 should be 0 !!!\n");
140		return TI_NOK;
141    }
142
143    securityKey.keyType = KEY_WEP;
144    securityKey.encLen = (TI_UINT16)pKeyDesc->KeyLength;
145    securityKey.keyIndex = pKeyDesc->KeyIndex;
146    os_memoryCopy(pKeyParser->hOs, (void *)securityKey.encKey, pKeyDesc->KeyMaterial, pKeyDesc->KeyLength);
147
148    TRACE2(pKeyParser->hReport, REPORT_SEVERITY_INFORMATION, "WEP_KEY_PARSER: Key received keyId=%x, keyLen=%d\n",						    pKeyDesc->KeyIndex, pKeyDesc->KeyLength);
149
150
151    /* We accept only 40, 104 or 232 -bit WEP keys*/
152    if (!((securityKey.encLen == WEP_KEY_LEN_40) || (securityKey.encLen == WEP_KEY_LEN_104)
153          || (securityKey.encLen == WEP_KEY_LEN_232)))
154    {	/*Invalid key length*/
155        TRACE1(pKeyParser->hReport, REPORT_SEVERITY_ERROR, "WEP_KEY_PARSER: ERROR: Invalid Key length: %d !!!\n", securityKey.encLen);
156        return TI_NOK;
157    }
158    /* configure key for Tx and Rx */
159    if (pKeyDesc->KeyIndex & WEP_KEY_TRANSMIT_MASK)
160    {	/* configure default key for Tx - unicast */
161        status = pKeyParser->pParent->setDefaultKeyId(pKeyParser->pParent, (TI_UINT8)securityKey.keyIndex);
162        if (status!=TI_OK)
163        {
164            return status;
165        }
166    }
167    /* configure key for Tx - unicast, and Rx - broadcast*/
168    status = pKeyParser->pParent->setKey(pKeyParser->pParent, &securityKey);
169
170	return status;
171}
172
173
174
175TI_STATUS keyParserWep_remove(struct _keyParser_t *pKeyParser, TI_UINT8 *pKeyData, TI_UINT32 keyDataLen)
176{
177	TI_STATUS				status;
178	OS_802_11_KEY			*pKeyDesc;
179	encodedKeyMaterial_t    encodedKeyMaterial;
180    TI_UINT8                keyBuffer[MAC_ADDR_LEN+KEY_RSC_LEN+MAX_WEP_KEY_DATA_LENGTH];
181
182	if (pKeyData == NULL)
183	{
184TRACE0(pKeyParser->hReport, REPORT_SEVERITY_ERROR, "EXT_KEY_PARSER: ERROR: NULL KEY Data\n");
185		return TI_NOK;
186	}
187
188	pKeyDesc = (OS_802_11_KEY*)pKeyData;
189
190    if (pKeyDesc->KeyIndex & WEP_KEY_TRANSMIT_MASK)
191	{	/* Bit 31 should always be zero */
192TRACE0(pKeyParser->hReport, REPORT_SEVERITY_ERROR, "WEP_KEY_PARSER: ERROR: Remove TX key index\n");
193		return TI_NOK;
194	}
195
196	encodedKeyMaterial.keyId = pKeyDesc->KeyIndex;
197	encodedKeyMaterial.keyLen = 0;
198    encodedKeyMaterial.pData = (char *) keyBuffer;
199    MAC_COPY (keyBuffer, pKeyDesc->BSSID);
200
201	/* which should we delete ????*/
202    status =  pKeyParser->pBcastKey->pKeyDerive->remove(pKeyParser->pUcastKey->pKeyDerive, &encodedKeyMaterial);
203	return status;
204
205}
206