securitysettings/cpwlansecurityuiplugins/cpwpa2ui/src/wpa2keyvalidator.cpp
branchGCC_SURGE
changeset 40 60f08993dad0
parent 29 77618ad58aba
parent 39 fe6b6762fccd
equal deleted inserted replaced
29:77618ad58aba 40:60f08993dad0
     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 WPA2 only keys
       
    16  *
       
    17  */
       
    18 /*
       
    19  * %version: 1 %
       
    20  */
       
    21 
       
    22 #include <QString>
       
    23 #include "wpa2keyvalidator.h"
       
    24 
       
    25 /*!
       
    26     \class Wpa2KeyValidator wpa2keyvalidator.cpp
       
    27     \brief Utilities for WPA2 only key validations.
       
    28     
       
    29 */
       
    30 /*!
       
    31     \enum Wpa2KeyValidator::KeyStatus
       
    32     This enum defines the validation results.
       
    33 
       
    34     \var Wpa2KeyValidator::KeyStatusOk
       
    35     Key is valid.
       
    36     
       
    37     \var Wpa2KeyValidator::KeyStatusIllegalCharacters
       
    38     Key contains illegal characters.
       
    39     
       
    40     \var Wpa2KeyValidator::KeyStatusWpa2TooShort
       
    41     WPA key is too short. Minimum allowed length is 8. See 
       
    42     WlanWizardUtils::validateWpaKey().
       
    43     
       
    44     \var Wpa2KeyValidator::KeyStatusWpa2TooLong
       
    45     WPA key is too long. Minimum allowed length is 64 for hex key and 63 for 
       
    46     ascii key. See WlanWizardUtils::validateWpaKey().
       
    47 */
       
    48 
       
    49 /*! 
       
    50  * Process WPA2 key validation. A passphrase can contain from 8 to 63 ASCII
       
    51  * characters where each character MUST have a decimal encoding in the range of
       
    52  * 32 to 126, inclusive.
       
    53  *
       
    54  * A preshared key is stored as 64 character hex string.
       
    55  * 
       
    56  * @param key PSK to be validated
       
    57  * 
       
    58  * @return Following values are possible
       
    59  * - KeyStatusOk
       
    60  * - KeyStatusIllegalCharacters
       
    61  * - KeyStatusWpa2TooShort
       
    62  * - KeyStatusWpa2TooLong
       
    63  */
       
    64 Wpa2KeyValidator::KeyStatus Wpa2KeyValidator::validateWpa2Key(const QString &key)
       
    65 {
       
    66     int length = key.length();
       
    67     KeyStatus ret = KeyStatusOk;
       
    68 
       
    69     if (length < Wpa2MinLenght) {
       
    70         ret = KeyStatusWpa2TooShort;
       
    71     }
       
    72     else if (length > Wpa2MaxLenght) {
       
    73         ret = KeyStatusWpa2TooLong;
       
    74     }
       
    75     // hex string
       
    76     else if (length == Wpa2MaxLenght) {
       
    77         ret = isHex(key);
       
    78     }
       
    79     else {
       
    80         ret = isAscii(key);
       
    81     }
       
    82 
       
    83     return ret;
       
    84 }
       
    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 Wpa2KeyValidator::KeyStatus Wpa2KeyValidator::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 Wpa2KeyValidator::KeyStatus Wpa2KeyValidator::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     const QChar *data = key.data();
       
   131     while (!data->isNull()) {
       
   132         if (data->isDigit() ||
       
   133             (*data) >= ch_a && (*data) <= ch_f ||
       
   134             (*data) >= ch_A && (*data) <= ch_F) {
       
   135             ++data;
       
   136         }
       
   137         else {
       
   138             return KeyStatusIllegalCharacters;
       
   139         }
       
   140     }
       
   141     return KeyStatusOk;
       
   142 }