securitysettings/cpwlansecurityuiplugins/cpwepui/src/wepkeyvalidator.cpp
changeset 22 093cf0757204
child 26 9abfd4f00d37
equal deleted inserted replaced
20:8b3129ac4c0f 22:093cf0757204
       
     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  *    Validation methods for WEP keys
       
    16  *
       
    17  */
       
    18 
       
    19 /*
       
    20  * %version: 1 %
       
    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 if (length == WepKeyEmptyString) {
       
    81        ret =  KeyStatusOk ;  
       
    82     }
       
    83     
       
    84     else {
       
    85         ret = KeyStatusWepInvalidLength;
       
    86     }
       
    87 
       
    88     return ret;
       
    89 }
       
    90 
       
    91 /*!
       
    92  * Process Ascii validation. Allowed characters are from 32 to 126.
       
    93  * 
       
    94  * @param key to be validated.
       
    95  * 
       
    96  * @return Following values are possible
       
    97  * - KeyStatusOk
       
    98  * - KeyStatusIllegalCharacters
       
    99  */
       
   100 WepKeyValidator::KeyStatus WepKeyValidator::isAscii(const QString &key)
       
   101 {
       
   102     QChar ch32(32);
       
   103     QChar ch126(126);
       
   104  
       
   105     const QChar *data = key.data();
       
   106     while (!data->isNull()) {
       
   107         if ((*data) < ch32 || (*data) > ch126) {
       
   108             return KeyStatusIllegalCharacters;
       
   109         }
       
   110         ++data;
       
   111     }
       
   112     return KeyStatusOk;
       
   113 }
       
   114 
       
   115 /*!
       
   116  * Process Hex validation. Allowed characters are
       
   117  * - from 0 to 9
       
   118  * - from a to f
       
   119  * - from A to F
       
   120  * 
       
   121  * @param key to be validated.
       
   122  * 
       
   123  * @return Following values are possible
       
   124  * - KeyStatusOk
       
   125  * - KeyStatusIllegalCharacters
       
   126  */
       
   127 WepKeyValidator::KeyStatus WepKeyValidator::isHex(const QString &key)
       
   128 {
       
   129     QChar ch_A(65); // Character: A
       
   130     QChar ch_F(70); // Character: F
       
   131     QChar ch_a(97); // Character: a
       
   132     QChar ch_f(102);// Character: f
       
   133 
       
   134    
       
   135     const QChar *data = key.data();
       
   136     while (!data->isNull()) {
       
   137         if (data->isDigit() ||
       
   138             (*data) >= ch_a && (*data) <= ch_f ||
       
   139             (*data) >= ch_A && (*data) <= ch_F) {
       
   140             ++data;
       
   141         }
       
   142         else {
       
   143             return KeyStatusIllegalCharacters;
       
   144         }
       
   145     }
       
   146     return KeyStatusOk;
       
   147 }