1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * WLAN Wizard Utilities |
|
16 * |
|
17 */ |
|
18 |
|
19 // System includes |
|
20 #include <QString> |
|
21 #include <QDebug> |
|
22 #include <QVariant> |
|
23 |
|
24 // User includes |
|
25 #include "wlanwizardutils.h" |
|
26 #include "OstTraceDefinitions.h" |
|
27 #ifdef OST_TRACE_COMPILER_IN_USE |
|
28 #include "wlanwizardutilsTraces.h" |
|
29 #endif |
|
30 |
|
31 /*! |
|
32 \class WlanWizardUtils wlanwizardutils.cpp |
|
33 \brief Utilities for WLAN key validations. |
|
34 |
|
35 */ |
|
36 /*! |
|
37 \enum WlanWizardUtils::KeyStatus |
|
38 This enum defines the validation results. |
|
39 |
|
40 \var WlanWizardUtils::KeyStatusOk |
|
41 Key is valid. |
|
42 |
|
43 \var WlanWizardUtils::KeyStatusIllegalCharacters |
|
44 Key contains illegal characters. |
|
45 |
|
46 \var WlanWizardUtils::KeyStatusWpaTooShort |
|
47 WPA key is too short. Minimum allowed length is 8. See |
|
48 WlanWizardUtils::validateWpaKey(). |
|
49 |
|
50 \var WlanWizardUtils::KeyStatusWpaTooLong |
|
51 WPA key is too long. Minimum allowed length is 64 for hex key and 63 for |
|
52 ascii key. See WlanWizardUtils::validateWpaKey(). |
|
53 |
|
54 \var WlanWizardUtils::KeyStatusWepInvalidLength |
|
55 WEP key length is not valid. Valid keys are |
|
56 - hex: 10 or 26 characters |
|
57 - ascii: 5 or 13 characters. See WlanWizardUtils::validateWpaKey(). |
|
58 |
|
59 \var WlanWizardUtils::SsidMinLength |
|
60 Minumum acceptable length for SSID. |
|
61 |
|
62 \var WlanWizardUtils::SsidMaxLength |
|
63 Maximum acceptable length for SSID. |
|
64 |
|
65 \var WlanWizardUtils::WpaMinLength |
|
66 Minumum acceptable length for WPA key. |
|
67 |
|
68 \var WlanWizardUtils::WpaMaxLength |
|
69 Maximum acceptable length for WPA key. |
|
70 |
|
71 \var WlanWizardUtils::WepHex64BitMaxLength |
|
72 Length of WEP key: format hex 64-bits. |
|
73 |
|
74 \var WlanWizardUtils::WepHex128BitMaxLength |
|
75 Length of WEP key: format hex 128-bits. |
|
76 |
|
77 \var WlanWizardUtils::WepAscii64BitMaxLength |
|
78 Length of WEP key: format ascii 64-bits. |
|
79 |
|
80 \var WlanWizardUtils::WepAscii128BitMaxLength |
|
81 Length of WEP key: format ascii 128-bits. |
|
82 |
|
83 */ |
|
84 |
|
85 // External function prototypes |
|
86 |
|
87 // Local constants |
|
88 |
|
89 // ======== LOCAL FUNCTIONS ======== |
|
90 |
|
91 // ======== MEMBER FUNCTIONS ======== |
|
92 |
|
93 |
|
94 /*! |
|
95 Process WPA key validation. A passphrase can contain from 8 to 63 ASCII |
|
96 characters where each character MUST have a decimal encoding in the range of |
|
97 32 to 126, inclusive. |
|
98 |
|
99 A preshared key is stored as 64 character hex string. |
|
100 |
|
101 @param [in] key PSK to be validated |
|
102 |
|
103 @return Following values are possible |
|
104 - KeyStatusOk |
|
105 - KeyStatusIllegalCharacters |
|
106 - KeyStatusWpaTooShort |
|
107 - KeyStatusWpaTooLong |
|
108 */ |
|
109 WlanWizardUtils::KeyStatus WlanWizardUtils::validateWpaKey(const QString &key) |
|
110 { |
|
111 OstTraceFunctionEntry0( WLANWIZARDUTILS_VALIDATEWPAKEY_ENTRY ); |
|
112 #ifdef OST_TRACE_COMPILER_IN_USE |
|
113 TPtrC tmp(key.utf16(),key.length() ); |
|
114 OstTraceExt1( TRACE_NORMAL, WLANWIZARDUTILS_VALIDATEWPAKEY, |
|
115 "WlanWizardUtils::validateWpaKey;key=%S", tmp ); |
|
116 #endif |
|
117 |
|
118 int length = key.length(); |
|
119 KeyStatus ret = KeyStatusOk; |
|
120 |
|
121 if (length < WpaMinLength) { |
|
122 ret = KeyStatusWpaTooShort; |
|
123 } else if (length > WpaMaxLength) { |
|
124 ret = KeyStatusWpaTooLong; |
|
125 } else if (length == WpaMaxLength) { |
|
126 ret = isHex(key); |
|
127 } else { |
|
128 ret = isAscii(key); |
|
129 } |
|
130 |
|
131 OstTrace1( |
|
132 TRACE_NORMAL, WLANWIZARDUTILS_VALIDATEWPAKEY_RETURN, |
|
133 "WlanWizardUtils::validateWpaKey - Return;ret=%{KeyStatus}", ret ); |
|
134 |
|
135 OstTraceFunctionExit0( WLANWIZARDUTILS_VALIDATEWPAKEY_EXIT ); |
|
136 return ret; |
|
137 } |
|
138 |
|
139 /*! |
|
140 Process WEP key validation. Following keys are allowed: |
|
141 |
|
142 HEX: |
|
143 - 64 bit: allowed key length = 10 |
|
144 - 128 bit: allowed key length = 26 |
|
145 |
|
146 ASCII: |
|
147 - 64 bit: allowed key length = 5 |
|
148 - 128 bit: allowed key length = 13 |
|
149 |
|
150 @param [in] key WEP Key to be validated |
|
151 |
|
152 @return Following values are possible |
|
153 - KeyStatusOk |
|
154 - KeyStatusIllegalCharacters |
|
155 - KeyStatusWepInvalidLength |
|
156 */ |
|
157 WlanWizardUtils::KeyStatus WlanWizardUtils::validateWepKey(const QString &key) |
|
158 { |
|
159 OstTraceFunctionEntry0( WLANWIZARDUTILS_VALIDATEWEPKEY_ENTRY ); |
|
160 #ifdef OST_TRACE_COMPILER_IN_USE |
|
161 TPtrC tmp(key.utf16(),key.length() ); |
|
162 OstTraceExt1( TRACE_NORMAL, WLANWIZARDUTILS_VALIDATEWEPKEY, |
|
163 "WlanWizardUtils::validateWepKey;key=%S", tmp ); |
|
164 #endif |
|
165 |
|
166 int length = key.length(); |
|
167 KeyStatus ret = KeyStatusOk; |
|
168 |
|
169 if (length == WepHex64BitMaxLength || length == WepHex128BitMaxLength) { |
|
170 ret = isHex(key); |
|
171 } else if (length == WepAscii64BitMaxLength || length == WepAscii128BitMaxLength) { |
|
172 ret = isAscii(key); |
|
173 } else { |
|
174 ret = KeyStatusWepInvalidLength; |
|
175 } |
|
176 |
|
177 OstTrace1( |
|
178 TRACE_NORMAL, WLANWIZARDUTILS_VALIDATEWEPKEY_RETURN, |
|
179 "WlanWizardUtils::validateWepKey - Return;ret=%{KeyStatus}", ret ); |
|
180 |
|
181 OstTraceFunctionExit0( WLANWIZARDUTILS_VALIDATEWEPKEY_EXIT ); |
|
182 return ret; |
|
183 } |
|
184 |
|
185 /*! |
|
186 Process Ascii validation. Allowed characters are from 32 to 126. |
|
187 |
|
188 @param [in] key to be validated. |
|
189 |
|
190 @return Following values are possible |
|
191 - KeyStatusOk |
|
192 - KeyStatusIllegalCharacters |
|
193 */ |
|
194 WlanWizardUtils::KeyStatus WlanWizardUtils::isAscii(const QString &key) |
|
195 { |
|
196 OstTraceFunctionEntry0( WLANWIZARDUTILS_ISASCII_ENTRY ); |
|
197 static const QChar ch32(32); // First visible ascii character |
|
198 static const QChar ch126(126); // Last visible ascii character |
|
199 |
|
200 const QChar *data = key.data(); |
|
201 while (!data->isNull()) { |
|
202 if ((*data) < ch32 || (*data) > ch126) { |
|
203 OstTraceFunctionExit0( WLANWIZARDUTILS_ISASCII_EXIT ); |
|
204 return KeyStatusIllegalCharacters; |
|
205 } |
|
206 ++data; |
|
207 } |
|
208 OstTraceFunctionExit0( DUP1_WLANWIZARDUTILS_ISASCII_EXIT ); |
|
209 return KeyStatusOk; |
|
210 } |
|
211 |
|
212 /*! |
|
213 Process Hex validation. Allowed characters are |
|
214 - from 0 to 9 |
|
215 - from a to f |
|
216 - from A to F |
|
217 |
|
218 @param [in] key to be validated. |
|
219 |
|
220 @return Following values are possible |
|
221 - KeyStatusOk |
|
222 - KeyStatusIllegalCharacters |
|
223 */ |
|
224 WlanWizardUtils::KeyStatus WlanWizardUtils::isHex(const QString &key) |
|
225 { |
|
226 OstTraceFunctionEntry0( WLANWIZARDUTILS_ISHEX_ENTRY ); |
|
227 static const QChar ch_A(65); // Character: A |
|
228 static const QChar ch_F(70); // Character: F |
|
229 static const QChar ch_a(97); // Character: a |
|
230 static const QChar ch_f(102);// Character: f |
|
231 |
|
232 const QChar *data = key.data(); |
|
233 while (!data->isNull()) { |
|
234 if (data->isDigit() || |
|
235 (*data) >= ch_a && (*data) <= ch_f || |
|
236 (*data) >= ch_A && (*data) <= ch_F) { |
|
237 ++data; |
|
238 } else { |
|
239 OstTraceFunctionExit0( WLANWIZARDUTILS_ISHEX_EXIT ); |
|
240 return KeyStatusIllegalCharacters; |
|
241 } |
|
242 } |
|
243 OstTraceFunctionExit0( DUP1_WLANWIZARDUTILS_ISHEX_EXIT ); |
|
244 return KeyStatusOk; |
|
245 } |
|
246 |
|
247 /*! |
|
248 Process SSID validation. |
|
249 |
|
250 @param [in] ssid to be validated. |
|
251 |
|
252 @return Following values are possible |
|
253 - SsidStatusOk |
|
254 - SsidStatusInvalidLength |
|
255 */ |
|
256 WlanWizardUtils::SsidStatus WlanWizardUtils::validateSsid(const QString &ssid) |
|
257 { |
|
258 OstTraceFunctionEntry0( WLANWIZARDUTILS_VALIDATESSID_ENTRY ); |
|
259 TPtrC tmp(ssid.utf16(), ssid.length() ); |
|
260 OstTraceExt1( TRACE_NORMAL, WLANWIZARDUTILS_VALIDATESSID, |
|
261 "WlanWizardUtils::validateSsid;ssid=%S", tmp ); |
|
262 |
|
263 SsidStatus ret = SsidStatusOk; |
|
264 |
|
265 int len = ssid.length(); |
|
266 if (len < SsidMinLength || len > SsidMaxLength) { |
|
267 ret = SsidStatusInvalidLength; |
|
268 } |
|
269 OstTrace1( |
|
270 TRACE_NORMAL, WLANWIZARDUTILS_VALIDATESSID_RETURN, |
|
271 "WlanWizardUtils::validateSsid - Return;ret=%{SsidStatus}", ret ); |
|
272 OstTraceFunctionExit0( WLANWIZARDUTILS_VALIDATESSID_EXIT ); |
|
273 return ret; |
|
274 } |
|