wlan_bearer/wlanengine/wlan_common/wlanengine_common_3.1/src/genscaninfoie.cpp
changeset 0 c40eb8fe8501
equal deleted inserted replaced
-1:000000000000 0:c40eb8fe8501
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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:  Implementation of the ScanInfoIe class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "genscaninfoie.h"
       
    20 
       
    21 // Defines the id byte of the RSN Information Element.
       
    22 const u8_t SCANINFOIE_RSNIE_ID = 48;
       
    23 
       
    24 // Defines the static offsets for different fields in RSN IE.
       
    25 const u32_t SCANINFOIE_RSNIE_GROUP_SUITE_OFFSET = 2;
       
    26 const u32_t SCANINFOIE_RSNIE_PAIRWISE_SUITE_COUNT_OFFSET = 6;
       
    27 const u32_t SCANINFOIE_RSNIE_PAIRWISE_SUITE_OFFSET = 8;
       
    28 
       
    29 // Defines the OUIs used in RSN IEs.
       
    30 const u32_t SCANINFOIE_OUI_LENGTH = 4;
       
    31 const u8_t SCANINFOIE_RSNIE_OUI_CCMP[] = { 0x00, 0x0F, 0xAC, 0x04 };
       
    32 const u8_t SCANINFOIE_RSNIE_OUI_EAP[] = { 0x00, 0x0F, 0xAC, 0x01 };
       
    33 const u8_t SCANINFOIE_RSNIE_OUI_PSK[] = { 0x00, 0x0F, 0xAC, 0x02 };
       
    34 
       
    35 // Defines the static offsets for different fields in RSN IE.
       
    36 const u32_t SCANINFOIE_WPAIE_PAIRWISE_SUITE_COUNT_OFFSET = 10;
       
    37 const u32_t SCANINFOIE_WPAIE_PAIRWISE_SUITE_OFFSET = 12;
       
    38 
       
    39 // Defines the OUIs used in WPA IEs.
       
    40 const u8_t SCANINFOIE_WPAIE_OUI_EAP[] = { 0x00, 0x50, 0xF2, 0x01 };
       
    41 const u8_t SCANINFOIE_WPAIE_OUI_PSK[] = { 0x00, 0x50, 0xF2, 0x02 };
       
    42 
       
    43 // Defines the id byte of the WAPI Information Element.
       
    44 const u8_t SCANINFOIE_WAPI_ID = 68;
       
    45 
       
    46 // Defines the static offsets for different fields in WAPI IE.
       
    47 const u32_t SCANINFOIE_WAPI_KEY_MANAGEMENT_COUNT_OFFSET = 2;
       
    48 
       
    49 // Defines the OUIs used in WAPI IEs.
       
    50 const u8_t SCANINFOIE_WAPI_OUI_CERTIFICATE[] = { 0x00, 0x14, 0x72, 0x01 };
       
    51 const u8_t SCANINFOIE_WAPI_OUI_PSK[] = { 0x00, 0x14, 0x72, 0x02 };
       
    52 
       
    53 
       
    54 // ============================ MEMBER FUNCTIONS ===============================
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 ScanInfoIe::ScanInfoIe()
       
    60     {
       
    61     }
       
    62     
       
    63 // Destructor
       
    64 ScanInfoIe::~ScanInfoIe()
       
    65     {    
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 WlanSecurityMode ScanInfoIe::SecurityMode(
       
    72     ScanInfo& info ) const
       
    73     {
       
    74     u8_t wpaie_length( 0 );
       
    75     const u8_t* wpaie_data = NULL;
       
    76     u8_t rsnie_length( 0 );
       
    77     const u8_t* rsnie_data = NULL;
       
    78     u8_t wapi_length( 0 );
       
    79     const u8_t* wapi_data = NULL;
       
    80 
       
    81     info.InformationElement( SCANINFOIE_RSNIE_ID, rsnie_length, &rsnie_data );
       
    82     info.WpaIE( wpaie_length, &wpaie_data );
       
    83     info.InformationElement( SCANINFOIE_WAPI_ID, wapi_length, &wapi_data );
       
    84 
       
    85     if ( !info.Privacy() )
       
    86         {
       
    87         if ( !wpaie_length && !rsnie_length )
       
    88             {
       
    89             return WlanSecurityModeOpen;
       
    90             }
       
    91         return WlanSecurityMode802_1x;
       
    92         }
       
    93         
       
    94     if ( !wpaie_length && !rsnie_length && !wapi_length )
       
    95         {
       
    96         return WlanSecurityModeWep;
       
    97         }
       
    98 
       
    99     if ( rsnie_length )
       
   100         {        
       
   101         if ( IsKeyManagement(
       
   102             ScanInfoIeTypeRsn,
       
   103             ScanInfoIeKeyManagementPsk,
       
   104             rsnie_length,
       
   105             rsnie_data ) )
       
   106             {
       
   107             if ( IsWpa2Ciphers(
       
   108                 ScanInfoIeTypeRsn,
       
   109                 rsnie_length,
       
   110                 rsnie_data ) )
       
   111                 {
       
   112                 return WlanSecurityModeWpa2Psk;
       
   113                 }
       
   114             
       
   115             return WlanSecurityModeWpaPsk;            
       
   116             }
       
   117         else if ( IsKeyManagement(
       
   118             ScanInfoIeTypeRsn,
       
   119             ScanInfoIeKeyManagementEap,
       
   120             rsnie_length,
       
   121             rsnie_data ) )
       
   122             {
       
   123             if ( IsWpa2Ciphers(
       
   124                 ScanInfoIeTypeRsn,
       
   125                 rsnie_length,
       
   126                 rsnie_data ) )
       
   127                 {                
       
   128                 return WlanSecurityModeWpa2Eap;
       
   129                 }
       
   130             
       
   131             return WlanSecurityModeWpaEap;            
       
   132             }                        
       
   133         }
       
   134         
       
   135     if ( wpaie_length )
       
   136         {
       
   137         if ( IsKeyManagement(
       
   138             ScanInfoIeTypeWpa,
       
   139             ScanInfoIeKeyManagementPsk,
       
   140             wpaie_length,
       
   141             wpaie_data ) )
       
   142             {
       
   143             return WlanSecurityModeWpaPsk;            
       
   144             }
       
   145         else if ( IsKeyManagement(
       
   146             ScanInfoIeTypeWpa,
       
   147             ScanInfoIeKeyManagementEap,
       
   148             wpaie_length,
       
   149             wpaie_data ) )
       
   150             {
       
   151             return WlanSecurityModeWpaEap;            
       
   152             }        
       
   153         }
       
   154 
       
   155     if ( wapi_length )
       
   156         {
       
   157         if ( IsKeyManagement(
       
   158             ScanInfoIeTypeWapi,
       
   159             ScanInfoIeKeyManagementWapiCertificate,
       
   160             wapi_length,
       
   161             wapi_data ) )
       
   162             {
       
   163             return WlanSecurityModeWapi;            
       
   164             }
       
   165         else if ( IsKeyManagement(
       
   166             ScanInfoIeTypeWapi,
       
   167             ScanInfoIeKeyManagementWapiPsk,
       
   168             wapi_length,
       
   169             wapi_data ) )
       
   170             {
       
   171             return WlanSecurityModeWapiPsk;            
       
   172             }
       
   173         }
       
   174 
       
   175     return WlanSecurityMode802_1x;
       
   176     }
       
   177  
       
   178 // -----------------------------------------------------------------------------
       
   179 // -----------------------------------------------------------------------------
       
   180 //   
       
   181 int ScanInfoIe::Compare(
       
   182     const unsigned char* pl,
       
   183     int ll, 
       
   184     const unsigned char* pr, 
       
   185     int rl ) const
       
   186     {
       
   187     if ( ll != rl )
       
   188         {
       
   189         return ll - rl;
       
   190         }        
       
   191 
       
   192     if ( pl == pr )
       
   193         {
       
   194         return 0;
       
   195         }        
       
   196 
       
   197     for ( int i( 0 ); i < ll; ++i )
       
   198         {
       
   199         if ( *(pl+i) != *(pr+i) )
       
   200             {
       
   201             return *(pl+i) - *(pr+i);
       
   202             }
       
   203         }
       
   204 
       
   205     return 0;    
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 
       
   212 bool_t ScanInfoIe::IsKeyManagement(
       
   213     ScanInfoIeType ie_type,
       
   214     ScanInfoIeKeyManagement key_type,
       
   215     u8_t /* ie_length */,
       
   216     const u8_t* ie_data ) const
       
   217     {
       
   218     const u8_t* key_data = NULL;
       
   219     const u8_t* key_comp = NULL;
       
   220    
       
   221     if ( ie_type == ScanInfoIeTypeRsn )
       
   222         {
       
   223         if ( key_type == ScanInfoIeKeyManagementEap )
       
   224             {
       
   225             key_comp = &SCANINFOIE_RSNIE_OUI_EAP[0];
       
   226             }
       
   227         else
       
   228             {
       
   229             key_comp = &SCANINFOIE_RSNIE_OUI_PSK[0];
       
   230             }
       
   231         u32_t suites( *( ie_data + SCANINFOIE_RSNIE_PAIRWISE_SUITE_COUNT_OFFSET ) );
       
   232         key_data = ie_data + SCANINFOIE_RSNIE_PAIRWISE_SUITE_OFFSET +
       
   233             ( suites * SCANINFOIE_OUI_LENGTH );
       
   234         }
       
   235     else if ( ie_type == ScanInfoIeTypeWpa )
       
   236         {
       
   237         if ( key_type == ScanInfoIeKeyManagementEap )
       
   238             {
       
   239             key_comp = &SCANINFOIE_WPAIE_OUI_EAP[0];
       
   240             }
       
   241         else
       
   242             {
       
   243             key_comp = &SCANINFOIE_WPAIE_OUI_PSK[0];
       
   244             }
       
   245         u32_t suites( *( ie_data + SCANINFOIE_WPAIE_PAIRWISE_SUITE_COUNT_OFFSET ) );
       
   246         key_data = ie_data + SCANINFOIE_WPAIE_PAIRWISE_SUITE_OFFSET +
       
   247             ( suites * SCANINFOIE_OUI_LENGTH );        
       
   248         }
       
   249     else
       
   250         {
       
   251         if ( key_type == ScanInfoIeKeyManagementWapiCertificate )
       
   252             {
       
   253             key_comp = &SCANINFOIE_WAPI_OUI_CERTIFICATE[0];
       
   254             }
       
   255         else
       
   256             {
       
   257             key_comp = &SCANINFOIE_WAPI_OUI_PSK[0];
       
   258             }
       
   259         key_data = ie_data + SCANINFOIE_WAPI_KEY_MANAGEMENT_COUNT_OFFSET;
       
   260         }
       
   261     
       
   262     u16_t key_suites( *key_data );
       
   263     key_data += sizeof( key_suites );
       
   264 
       
   265     while ( key_suites-- )
       
   266         {
       
   267         if ( !Compare(
       
   268             key_data + ( key_suites * SCANINFOIE_OUI_LENGTH ), SCANINFOIE_OUI_LENGTH,
       
   269             key_comp, SCANINFOIE_OUI_LENGTH ) )
       
   270             {
       
   271             return true_t;
       
   272             }
       
   273         }
       
   274 
       
   275     return false_t;
       
   276     };
       
   277    
       
   278 // -----------------------------------------------------------------------------
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 bool_t ScanInfoIe::IsWpa2Ciphers(
       
   282     ScanInfoIeType ie_type,
       
   283     u8_t /* ie_length */,
       
   284     const u8_t* ie_data ) const
       
   285     {
       
   286     /**
       
   287     * WPA IE is not used in WPA2
       
   288     */    
       
   289     if ( ie_type == ScanInfoIeTypeWpa )
       
   290         {
       
   291         return false_t;
       
   292         }
       
   293 
       
   294     const u8_t* cipher =
       
   295         ie_data + SCANINFOIE_RSNIE_GROUP_SUITE_OFFSET;
       
   296 
       
   297     if ( Compare(
       
   298         cipher, SCANINFOIE_OUI_LENGTH,
       
   299         &SCANINFOIE_RSNIE_OUI_CCMP[0], SCANINFOIE_OUI_LENGTH ) )
       
   300         {
       
   301         return false_t;
       
   302         }
       
   303 
       
   304     u32_t suites( *( ie_data + SCANINFOIE_RSNIE_PAIRWISE_SUITE_COUNT_OFFSET ) );
       
   305     cipher = ie_data + SCANINFOIE_RSNIE_PAIRWISE_SUITE_OFFSET;
       
   306 
       
   307     while ( suites-- )
       
   308         {
       
   309         if ( !Compare(
       
   310             cipher + ( suites * SCANINFOIE_OUI_LENGTH ), SCANINFOIE_OUI_LENGTH,
       
   311             &SCANINFOIE_RSNIE_OUI_CCMP[0], SCANINFOIE_OUI_LENGTH ) )
       
   312             {
       
   313             return true_t;
       
   314             }
       
   315         }
       
   316 
       
   317     return false_t;
       
   318     }