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 WEP keys |
|
16 * |
|
17 */ |
|
18 |
|
19 /* |
|
20 * %version: tr1cfwln#5 % |
|
21 */ |
|
22 |
|
23 #include <QString> |
|
24 #include <QDebug> |
|
25 #include "wepkeyvalidator.h" |
|
26 |
|
27 /*! |
|
28 \class WepKeyValidator wepkeyvalidator.cpp |
|
29 \brief Utilities for WEP key validations. |
|
30 |
|
31 */ |
|
32 /*! |
|
33 \enum WepKeyValidator::KeyStatus |
|
34 This enum defines the validation results. |
|
35 |
|
36 \var WepKeyValidator::KeyStatusOk |
|
37 Key is valid. |
|
38 |
|
39 \var WepKeyValidator::KeyStatusIllegalCharacters |
|
40 Key contains illegal characters. |
|
41 |
|
42 \var WepKeyValidator::KeyStatusWepInvalidLength |
|
43 WEP key length is not valid. Valid keys are |
|
44 - hex: 10 or 26 characters |
|
45 - ascii: 5 or 13 characters. See WlanWizardUtils::validateWpaKey(). |
|
46 */ |
|
47 |
|
48 |
|
49 /*! |
|
50 * Process WEP key validation. Following keys are allowed: |
|
51 * |
|
52 * HEX: |
|
53 * - 64 bit: allowed key length = 10 |
|
54 * - 128 bit: allowed key length = 26 |
|
55 * |
|
56 * ASCII: |
|
57 * - 64 bit: allowed key length = 5 |
|
58 * - 128 bit: allowed key length = 13 |
|
59 * |
|
60 * @param key WEP Key to be validated |
|
61 * |
|
62 * @return Following values are possible |
|
63 * - KeyStatusOk |
|
64 * - KeyStatusIllegalCharacters |
|
65 * - KeyStatusWepInvalidLength |
|
66 */ |
|
67 WepKeyValidator::KeyStatus WepKeyValidator::validateWepKey(const QString &key) |
|
68 { |
|
69 int length = key.length(); |
|
70 KeyStatus ret = KeyStatusOk; |
|
71 // HEX |
|
72 if (length == WepHex64BitMaxLength || length == WepHex128BitMaxLength) { |
|
73 ret = isHex(key); |
|
74 } |
|
75 // ASCII |
|
76 else if (length == WepAscii64BitMaxLength || length == WepAscii128BitMaxLength) { |
|
77 ret = isAscii(key); |
|
78 } |
|
79 |
|
80 else { |
|
81 ret = KeyStatusWepInvalidLength; |
|
82 } |
|
83 |
|
84 return ret; |
|
85 } |
|
86 |
|
87 /*! |
|
88 * Process Ascii validation. Allowed characters are from 32 to 126. |
|
89 * |
|
90 * @param key to be validated. |
|
91 * |
|
92 * @return Following values are possible |
|
93 * - KeyStatusOk |
|
94 * - KeyStatusIllegalCharacters |
|
95 */ |
|
96 WepKeyValidator::KeyStatus WepKeyValidator::isAscii(const QString &key) |
|
97 { |
|
98 QChar ch32(32); |
|
99 QChar ch126(126); |
|
100 |
|
101 const QChar *data = key.data(); |
|
102 while (!data->isNull()) { |
|
103 if ((*data) < ch32 || (*data) > ch126) { |
|
104 return KeyStatusIllegalCharacters; |
|
105 } |
|
106 ++data; |
|
107 } |
|
108 return KeyStatusOk; |
|
109 } |
|
110 |
|
111 /*! |
|
112 * Process Hex validation. Allowed characters are |
|
113 * - from 0 to 9 |
|
114 * - from a to f |
|
115 * - from A to F |
|
116 * |
|
117 * @param key to be validated. |
|
118 * |
|
119 * @return Following values are possible |
|
120 * - KeyStatusOk |
|
121 * - KeyStatusIllegalCharacters |
|
122 */ |
|
123 WepKeyValidator::KeyStatus WepKeyValidator::isHex(const QString &key) |
|
124 { |
|
125 QChar ch_A(65); // Character: A |
|
126 QChar ch_F(70); // Character: F |
|
127 QChar ch_a(97); // Character: a |
|
128 QChar ch_f(102);// Character: f |
|
129 |
|
130 |
|
131 const QChar *data = key.data(); |
|
132 while (!data->isNull()) { |
|
133 if (data->isDigit() || |
|
134 (*data) >= ch_a && (*data) <= ch_f || |
|
135 (*data) >= ch_A && (*data) <= ch_F) { |
|
136 ++data; |
|
137 } |
|
138 else { |
|
139 return KeyStatusIllegalCharacters; |
|
140 } |
|
141 } |
|
142 return KeyStatusOk; |
|
143 } |
|