wlanutilities/wlanwizard/src/wlanwizardutils.cpp
changeset 31 e8f4211554fb
child 43 72ebcbb64834
equal deleted inserted replaced
30:ab513c8439db 31:e8f4211554fb
       
     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  *   WLAN Wizard Utilities
       
    16  *
       
    17  */
       
    18 
       
    19 // System includes
       
    20 #include <QString>
       
    21 #include <QDebug>
       
    22 #include <QVariant>
       
    23 
       
    24 // User includes
       
    25 #include "wlanwizardutils.h"
       
    26 #include "OstTraceDefinitions.h"
       
    27 #ifdef OST_TRACE_COMPILER_IN_USE
       
    28 #include "wlanwizardutilsTraces.h"
       
    29 #endif
       
    30 
       
    31 /*!
       
    32     \class WlanWizardUtils wlanwizardutils.cpp
       
    33     \brief Utilities for WLAN key validations.
       
    34     
       
    35 */
       
    36 /*!
       
    37     \enum WlanWizardUtils::KeyStatus
       
    38     This enum defines the validation results.
       
    39 
       
    40     \var WlanWizardUtils::KeyStatusOk
       
    41     Key is valid.
       
    42     
       
    43     \var WlanWizardUtils::KeyStatusIllegalCharacters
       
    44     Key contains illegal characters.
       
    45     
       
    46     \var WlanWizardUtils::KeyStatusWpaTooShort
       
    47     WPA key is too short. Minimum allowed length is 8. See 
       
    48     WlanWizardUtils::validateWpaKey().
       
    49     
       
    50     \var WlanWizardUtils::KeyStatusWpaTooLong
       
    51     WPA key is too long. Minimum allowed length is 64 for hex key and 63 for 
       
    52     ascii key. See WlanWizardUtils::validateWpaKey().
       
    53     
       
    54     \var WlanWizardUtils::KeyStatusWepInvalidLength
       
    55     WEP key length is not valid. Valid keys are 
       
    56     - hex: 10 or 26 characters
       
    57     - ascii: 5 or 13 characters. See WlanWizardUtils::validateWpaKey().
       
    58     
       
    59     \var WlanWizardUtils::SsidMinLength
       
    60     Minumum acceptable length for SSID.
       
    61     
       
    62     \var WlanWizardUtils::SsidMaxLength
       
    63     Maximum acceptable length for SSID.
       
    64     
       
    65     \var WlanWizardUtils::WpaMinLength
       
    66     Minumum acceptable length for WPA key.
       
    67     
       
    68     \var WlanWizardUtils::WpaMaxLength
       
    69     Maximum acceptable length for WPA key.
       
    70     
       
    71     \var WlanWizardUtils::WepHex64BitMaxLength
       
    72     Length of WEP key: format hex 64-bits.
       
    73     
       
    74     \var WlanWizardUtils::WepHex128BitMaxLength
       
    75     Length of WEP key: format hex 128-bits.
       
    76     
       
    77     \var WlanWizardUtils::WepAscii64BitMaxLength
       
    78     Length of WEP key: format ascii 64-bits.
       
    79     
       
    80     \var WlanWizardUtils::WepAscii128BitMaxLength
       
    81     Length of WEP key: format ascii 128-bits.
       
    82     
       
    83 */
       
    84 
       
    85 // External function prototypes
       
    86 
       
    87 // Local constants
       
    88 
       
    89 // ======== LOCAL FUNCTIONS ========
       
    90 
       
    91 // ======== MEMBER FUNCTIONS ========
       
    92 
       
    93 
       
    94 /*! 
       
    95    Process WPA key validation. A passphrase can contain from 8 to 63 ASCII
       
    96    characters where each character MUST have a decimal encoding in the range of
       
    97    32 to 126, inclusive.
       
    98  
       
    99    A preshared key is stored as 64 character hex string.
       
   100    
       
   101    @param [in] key PSK to be validated
       
   102    
       
   103    @return Following values are possible
       
   104    - KeyStatusOk
       
   105    - KeyStatusIllegalCharacters
       
   106    - KeyStatusWpaTooShort
       
   107    - KeyStatusWpaTooLong
       
   108  */
       
   109 WlanWizardUtils::KeyStatus WlanWizardUtils::validateWpaKey(const QString &key)
       
   110 {
       
   111 #ifdef OST_TRACE_COMPILER_IN_USE
       
   112     TPtrC tmp(key.utf16(),key.length() );
       
   113     OstTraceExt1( TRACE_NORMAL, WLANWIZARDUTILS_VALIDATEWPAKEY, 
       
   114         "WlanWizardUtils::validateWpaKey;key=%S", tmp );
       
   115 #endif
       
   116     
       
   117     int length = key.length();
       
   118     KeyStatus ret = KeyStatusOk;
       
   119 
       
   120     if (length < WpaMinLength) {
       
   121         ret = KeyStatusWpaTooShort;
       
   122     } else if (length > WpaMaxLength) {
       
   123         ret = KeyStatusWpaTooLong;
       
   124     } else if (length == WpaMaxLength) {
       
   125         ret = isHex(key);
       
   126     } else {
       
   127         ret = isAscii(key);
       
   128     }
       
   129 
       
   130     OstTrace1( 
       
   131         TRACE_NORMAL, WLANWIZARDUTILS_VALIDATEWPAKEY_RETURN, 
       
   132         "WlanWizardUtils::validateWpaKey - Return;ret=%{KeyStatus}", ret );
       
   133     
       
   134     return ret;
       
   135 }
       
   136 
       
   137 /*!
       
   138    Process WEP key validation. Following keys are allowed:
       
   139    
       
   140    HEX: 
       
   141    - 64 bit: allowed key length = 10
       
   142    - 128 bit: allowed key length = 26
       
   143    
       
   144    ASCII:
       
   145    - 64 bit: allowed key length = 5   
       
   146    - 128 bit: allowed key length = 13
       
   147    
       
   148    @param [in] key WEP Key to be validated
       
   149    
       
   150    @return Following values are possible
       
   151    - KeyStatusOk
       
   152    - KeyStatusIllegalCharacters
       
   153    - KeyStatusWepInvalidLength
       
   154  */
       
   155 WlanWizardUtils::KeyStatus WlanWizardUtils::validateWepKey(const QString &key)
       
   156 {
       
   157 #ifdef OST_TRACE_COMPILER_IN_USE
       
   158     TPtrC tmp(key.utf16(),key.length() );
       
   159     OstTraceExt1( TRACE_NORMAL, WLANWIZARDUTILS_VALIDATEWEPKEY, 
       
   160         "WlanWizardUtils::validateWepKey;key=%S", tmp );
       
   161 #endif
       
   162     
       
   163     int length = key.length();
       
   164     KeyStatus ret = KeyStatusOk;
       
   165     
       
   166     if (length == WepHex64BitMaxLength || length == WepHex128BitMaxLength) {
       
   167         ret = isHex(key);
       
   168     } else if (length == WepAscii64BitMaxLength || length == WepAscii128BitMaxLength) {
       
   169         ret = isAscii(key);
       
   170     } else {
       
   171         ret = KeyStatusWepInvalidLength;
       
   172     }
       
   173 
       
   174     OstTrace1( 
       
   175         TRACE_NORMAL, WLANWIZARDUTILS_VALIDATEWEPKEY_RETURN, 
       
   176         "WlanWizardUtils::validateWepKey - Return;ret=%{KeyStatus}", ret );
       
   177     
       
   178     return ret;
       
   179 }
       
   180 
       
   181 /*!
       
   182    Process Ascii validation. Allowed characters are from 32 to 126.
       
   183    
       
   184    @param [in] key to be validated.
       
   185    
       
   186    @return Following values are possible
       
   187    - KeyStatusOk
       
   188    - KeyStatusIllegalCharacters
       
   189  */
       
   190 WlanWizardUtils::KeyStatus WlanWizardUtils::isAscii(const QString &key)
       
   191 {
       
   192     static const QChar ch32(32);   // First visible ascii character
       
   193     static const QChar ch126(126); // Last visible ascii character
       
   194   
       
   195     const QChar *data = key.data();
       
   196     while (!data->isNull()) {
       
   197         if ((*data) < ch32 || (*data) > ch126) {
       
   198             return KeyStatusIllegalCharacters;
       
   199         }
       
   200         ++data;
       
   201     }
       
   202     return KeyStatusOk;
       
   203 }
       
   204 
       
   205 /*!
       
   206    Process Hex validation. Allowed characters are
       
   207    - from 0 to 9
       
   208    - from a to f
       
   209    - from A to F
       
   210    
       
   211    @param [in] key to be validated.
       
   212    
       
   213    @return Following values are possible
       
   214    - KeyStatusOk
       
   215    - KeyStatusIllegalCharacters
       
   216  */
       
   217 WlanWizardUtils::KeyStatus WlanWizardUtils::isHex(const QString &key)
       
   218 {
       
   219     static const QChar ch_A(65); // Character: A
       
   220     static const QChar ch_F(70); // Character: F
       
   221     static const QChar ch_a(97); // Character: a
       
   222     static const QChar ch_f(102);// Character: f
       
   223 
       
   224     const QChar *data = key.data();
       
   225     while (!data->isNull()) {
       
   226         if (data->isDigit() ||
       
   227             (*data) >= ch_a && (*data) <= ch_f ||
       
   228             (*data) >= ch_A && (*data) <= ch_F) {
       
   229             ++data;
       
   230         } else {
       
   231             return KeyStatusIllegalCharacters;
       
   232         }
       
   233     }
       
   234     return KeyStatusOk;
       
   235 }
       
   236 
       
   237 /*!
       
   238    Process SSID validation.
       
   239  
       
   240    @param [in] ssid to be validated.
       
   241    
       
   242    @return Following values are possible
       
   243    - SsidStatusOk
       
   244    - SsidStatusInvalidLength
       
   245  */
       
   246 WlanWizardUtils::SsidStatus WlanWizardUtils::validateSsid(const QString &ssid)
       
   247 {
       
   248     TPtrC tmp(ssid.utf16(), ssid.length() );
       
   249     OstTraceExt1( TRACE_NORMAL, WLANWIZARDUTILS_VALIDATESSID, 
       
   250         "WlanWizardUtils::validateSsid;ssid=%S", tmp );
       
   251         
       
   252     SsidStatus ret = SsidStatusOk;
       
   253     
       
   254     int len = ssid.length();
       
   255     if (len < SsidMinLength || len > SsidMaxLength) {
       
   256         ret = SsidStatusInvalidLength;
       
   257     }
       
   258     OstTrace1( 
       
   259         TRACE_NORMAL, WLANWIZARDUTILS_VALIDATESSID_RETURN, 
       
   260         "WlanWizardUtils::validateSsid - Return;ret=%{SsidStatus}", ret );
       
   261     return ret;
       
   262 }