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