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 * Validation methods for WPA/WPA2 & WPA2 only keys |
|
16 * |
|
17 */ |
|
18 |
|
19 /* |
|
20 * %version: tr1cfwln#3 % |
|
21 */ |
|
22 |
|
23 //System Includes |
|
24 #include <QString> |
|
25 |
|
26 //User Includes |
|
27 #include "wpakeyvalidator.h" |
|
28 #include "OstTraceDefinitions.h" |
|
29 |
|
30 //Trace Definition |
|
31 #ifdef OST_TRACE_COMPILER_IN_USE |
|
32 #include "wpakeyvalidatorTraces.h" |
|
33 #endif |
|
34 |
|
35 /*! |
|
36 \class WpaKeyValidator wpakeyvalidator.cpp |
|
37 \brief Utilities for WPA/WPA2 key validations. |
|
38 |
|
39 */ |
|
40 /*! |
|
41 \enum WpaKeyValidator::KeyStatus |
|
42 This enum defines the validation results. |
|
43 |
|
44 \var WpaKeyValidator::KeyStatusOk |
|
45 Key is valid. |
|
46 |
|
47 \var WpaKeyValidator::KeyStatusIllegalCharacters |
|
48 Key contains illegal characters. |
|
49 |
|
50 \var WpaKeyValidator::KeyStatusWpaTooShort |
|
51 WPA key is too short. Minimum allowed length is 8. See |
|
52 WlanWizardUtils::validateWpaKey(). |
|
53 |
|
54 \var WpaKeyValidator::KeyStatusWpaTooLong |
|
55 WPA key is too long. Minimum allowed length is 64 for hex key and 63 for |
|
56 ascii key. See WlanWizardUtils::validateWpaKey(). |
|
57 |
|
58 */ |
|
59 |
|
60 /*! |
|
61 * Process WPA key validation. A passphrase can contain from 8 to 63 ASCII |
|
62 * characters where each character MUST have a decimal encoding in the range of |
|
63 * 32 to 126, inclusive. |
|
64 * |
|
65 * A preshared key is stored as 64 character hex string. |
|
66 * |
|
67 * @param key PSK to be validated |
|
68 * |
|
69 * @return Following values are possible |
|
70 * - KeyStatusOk |
|
71 * - KeyStatusIllegalCharacters |
|
72 * - KeyStatusWpaTooShort |
|
73 * - KeyStatusWpaTooLong |
|
74 */ |
|
75 WpaKeyValidator::KeyStatus WpaKeyValidator::validateWpaKey(const QString &key) |
|
76 { |
|
77 OstTraceFunctionEntry0( WPAKEYVALIDATOR_VALIDATEWPAKEY_ENTRY ); |
|
78 |
|
79 int length = key.length(); |
|
80 KeyStatus ret = KeyStatusOk; |
|
81 |
|
82 if (length < WpaMinLenght) { |
|
83 ret = KeyStatusWpaTooShort; |
|
84 } |
|
85 else if (length > WpaMaxLenght) { |
|
86 ret = KeyStatusWpaTooLong; |
|
87 } |
|
88 // hex string |
|
89 else if (length == WpaMaxLenght) { |
|
90 ret = isHex(key); |
|
91 } |
|
92 else { |
|
93 ret = isAscii(key); |
|
94 } |
|
95 |
|
96 OstTraceFunctionExit0( WPAKEYVALIDATOR_VALIDATEWPAKEY_EXIT ); |
|
97 return ret; |
|
98 } |
|
99 |
|
100 /*! |
|
101 * Process Ascii validation. Allowed characters are from 32 to 126. |
|
102 * |
|
103 * @param key to be validated. |
|
104 * |
|
105 * @return Following values are possible |
|
106 * - KeyStatusOk |
|
107 * - KeyStatusIllegalCharacters |
|
108 */ |
|
109 WpaKeyValidator::KeyStatus WpaKeyValidator::isAscii(const QString &key) |
|
110 { |
|
111 OstTraceFunctionEntry0( WPAKEYVALIDATOR_ISASCII_ENTRY ); |
|
112 |
|
113 QChar ch32(32); |
|
114 QChar ch126(126); |
|
115 |
|
116 const QChar *data = key.data(); |
|
117 while (!data->isNull()) { |
|
118 if ((*data) < ch32 || (*data) > ch126) { |
|
119 |
|
120 OstTraceFunctionExit0( WPAKEYVALIDATOR_ISASCII_EXIT ); |
|
121 return KeyStatusIllegalCharacters; |
|
122 } |
|
123 ++data; |
|
124 } |
|
125 |
|
126 OstTraceFunctionExit0( DUP1_WPAKEYVALIDATOR_ISASCII_EXIT ); |
|
127 return KeyStatusOk; |
|
128 } |
|
129 |
|
130 /*! |
|
131 * Process Hex validation. Allowed characters are |
|
132 * - from 0 to 9 |
|
133 * - from a to f |
|
134 * - from A to F |
|
135 * |
|
136 * @param key to be validated. |
|
137 * |
|
138 * @return Following values are possible |
|
139 * - KeyStatusOk |
|
140 * - KeyStatusIllegalCharacters |
|
141 */ |
|
142 WpaKeyValidator::KeyStatus WpaKeyValidator::isHex(const QString &key) |
|
143 { |
|
144 OstTraceFunctionEntry0( WPAKEYVALIDATOR_ISHEX_ENTRY ); |
|
145 |
|
146 QChar ch_A(65); // Character: A |
|
147 QChar ch_F(70); // Character: F |
|
148 QChar ch_a(97); // Character: a |
|
149 QChar ch_f(102);// Character: f |
|
150 |
|
151 const QChar *data = key.data(); |
|
152 while (!data->isNull()) { |
|
153 if (data->isDigit() || (*data) >= ch_a && (*data) <= ch_f || (*data) |
|
154 >= ch_A && (*data) <= ch_F) { |
|
155 ++data; |
|
156 } |
|
157 else { |
|
158 |
|
159 OstTraceFunctionExit0( WPAKEYVALIDATOR_ISHEX_EXIT ); |
|
160 return KeyStatusIllegalCharacters; |
|
161 } |
|
162 } |
|
163 |
|
164 OstTraceFunctionExit0( DUP1_WPAKEYVALIDATOR_ISHEX_EXIT ); |
|
165 return KeyStatusOk; |
|
166 } |
|