Home | History | Annotate | Download | only in connectivitymanagertest
      1 /*
      2  * Copyright (C) 2010, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.connectivitymanagertest;
     18 
     19 import javax.xml.parsers.SAXParser;
     20 import javax.xml.parsers.SAXParserFactory;
     21 
     22 import org.xml.sax.Attributes;
     23 import org.xml.sax.SAXException;
     24 import org.xml.sax.helpers.DefaultHandler;
     25 
     26 import android.net.wifi.WifiConfiguration;
     27 import android.net.wifi.WifiConfiguration.AuthAlgorithm;
     28 import android.net.wifi.WifiConfiguration.KeyMgmt;
     29 import android.net.DhcpInfo;
     30 
     31 import java.io.InputStream;
     32 import java.net.UnknownHostException;
     33 import java.util.ArrayList;
     34 import java.util.HashMap;
     35 import java.util.List;
     36 
     37 /**
     38  * Help class to process configurations of access points saved in an XML file.
     39  * The configurations of an access point is included in tag
     40  * <accesspoint></accesspoint>. The supported configuration includes: ssid,
     41  * security, eap, phase2, identity, password, anonymousidentity, cacert, usercert,
     42  * in which each is included in the corresponding tags. Static IP setting is also supported.
     43  * Tags that can be used include: ip, gateway, netmask, dns1, dns2. All access points have to be
     44  * enclosed in tags of <resources></resources>.
     45  *
     46  * The following is a sample configuration file for an access point using EAP-PEAP with MSCHAP2.
     47  * <resources>
     48  *   <accesspoint>
     49  *   <ssid>testnet</ssid>
     50  *   <security>EAP</security>
     51  *   <eap>PEAP</eap>
     52  *   <phase2>MSCHAP2</phase2>
     53  *   <identity>donut</identity</identity>
     54  *   <password>abcdefgh</password>
     55  *   </accesspoint>
     56  * </resources>
     57  *
     58  * Note:ssid and security have to be the first two tags
     59  *      for static ip setting, tag "ip" should be listed before other fields: dns, gateway, netmask.
     60  */
     61 public class AccessPointParserHelper {
     62     private static final String KEYSTORE_SPACE = "keystore://";
     63     private static final String TAG = "AccessPointParserHelper";
     64     static final int NONE = 0;
     65     static final int WEP = 1;
     66     static final int PSK = 2;
     67     static final int EAP = 3;
     68 
     69     List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>();
     70     HashMap<String, DhcpInfo> ssidToDhcpInfoHM = new HashMap<String, DhcpInfo>();
     71 
     72     private int getSecurityType (String security) {
     73         if (security.equalsIgnoreCase("NONE")) {
     74             return NONE;
     75         } else if (security.equalsIgnoreCase("WEP")) {
     76             return WEP;
     77         } else if (security.equalsIgnoreCase("PSK")) {
     78             return PSK;
     79         } else if (security.equalsIgnoreCase("EAP")) {
     80             return EAP;
     81         } else {
     82             return -1;
     83         }
     84     }
     85 
     86     private boolean validateEapValue(String value) {
     87         if (value.equalsIgnoreCase("PEAP") ||
     88                 value.equalsIgnoreCase("TLS") ||
     89                 value.equalsIgnoreCase("TTLS")) {
     90             return true;
     91         } else {
     92             return false;
     93         }
     94     }
     95 
     96     private static int stringToIpAddr(String addrString) throws UnknownHostException {
     97         try {
     98             String[] parts = addrString.split("\\.");
     99             if (parts.length != 4) {
    100                 throw new UnknownHostException(addrString);
    101             }
    102 
    103             int a = Integer.parseInt(parts[0])      ;
    104             int b = Integer.parseInt(parts[1]) <<  8;
    105             int c = Integer.parseInt(parts[2]) << 16;
    106             int d = Integer.parseInt(parts[3]) << 24;
    107 
    108             return a | b | c | d;
    109         } catch (NumberFormatException ex) {
    110             throw new UnknownHostException(addrString);
    111         }
    112     }
    113 
    114     DefaultHandler mHandler = new DefaultHandler() {
    115 
    116         boolean ssid = false;
    117         boolean security = false;
    118         boolean password = false;
    119         boolean ip = false;
    120         boolean netmask = false;
    121         boolean gateway = false;
    122         boolean dns1 = false;
    123         boolean dns2 = false;
    124         boolean eap = false;
    125         boolean phase2 = false;
    126         boolean identity = false;
    127         boolean anonymousidentity = false;
    128         boolean cacert = false;
    129         boolean usercert = false;
    130         WifiConfiguration config = null;
    131         int securityType = NONE;
    132         DhcpInfo mDhcpInfo = null;
    133 
    134         @Override
    135         public void startElement(String uri, String localName, String tagName,
    136                 Attributes attributes) throws SAXException {
    137             if (tagName.equalsIgnoreCase("accesspoint")) {
    138                 config = new WifiConfiguration();
    139             }
    140             if (tagName.equalsIgnoreCase("ssid")) {
    141                 ssid = true;
    142             }
    143             if (tagName.equalsIgnoreCase("security")) {
    144                 security = true;
    145             }
    146             if (tagName.equalsIgnoreCase("password")) {
    147                 password = true;
    148             }
    149             if (tagName.equalsIgnoreCase("eap")) {
    150                 eap = true;
    151             }
    152             if (tagName.equalsIgnoreCase("phase2")) {
    153                 phase2 = true;
    154             }
    155             if (tagName.equalsIgnoreCase("identity")) {
    156                 identity = true;
    157             }
    158             if (tagName.equalsIgnoreCase("anonymousidentity")) {
    159                 anonymousidentity = true;
    160             }
    161             if (tagName.equalsIgnoreCase("cacert")) {
    162                 cacert = true;
    163             }
    164             if (tagName.equalsIgnoreCase("usercert")) {
    165                 usercert = true;
    166             }
    167             if (tagName.equalsIgnoreCase("ip")) {
    168                 ip = true;
    169                 mDhcpInfo = new DhcpInfo();
    170             }
    171             if (tagName.equalsIgnoreCase("gateway")) {
    172                 gateway = true;
    173             }
    174             if (tagName.equalsIgnoreCase("netmask")) {
    175                 netmask = true;
    176             }
    177             if (tagName.equalsIgnoreCase("dns1")) {
    178                 dns1 = true;
    179             }
    180             if (tagName.equalsIgnoreCase("dns2")) {
    181                 dns2 = true;
    182             }
    183         }
    184 
    185         @Override
    186         public void endElement(String uri, String localName, String tagName) throws SAXException {
    187             if (tagName.equalsIgnoreCase("accesspoint")) {
    188                 networks.add(config);
    189                 if (mDhcpInfo != null) {
    190                     ssidToDhcpInfoHM.put(config.SSID, mDhcpInfo);
    191                     mDhcpInfo = null;
    192                 }
    193             }
    194         }
    195 
    196         @Override
    197         public void characters(char ch[], int start, int length) throws SAXException {
    198             if (ssid) {
    199                 config.SSID = new String(ch, start, length);
    200                 ssid = false;
    201             }
    202             if (security) {
    203                 String securityStr = (new String(ch, start, length)).toUpperCase();
    204                 securityType = getSecurityType(securityStr);
    205                 switch (securityType) {
    206                     case NONE:
    207                         config.allowedKeyManagement.set(KeyMgmt.NONE);
    208                         break;
    209                     case WEP:
    210                         config.allowedKeyManagement.set(KeyMgmt.NONE);
    211                         config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
    212                         config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
    213                         break;
    214                     case PSK:
    215                         config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
    216                         break;
    217                     case EAP:
    218                         config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
    219                         config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
    220                         // Initialize other fields.
    221                         config.phase2.setValue("");
    222                         config.ca_cert.setValue("");
    223                         config.client_cert.setValue("");
    224                         config.private_key.setValue("");
    225                         config.identity.setValue("");
    226                         config.anonymous_identity.setValue("");
    227                         break;
    228                     default:
    229                         throw new SAXException();
    230                 }
    231                 security = false;
    232             }
    233             if (password) {
    234                 String passwordStr = new String(ch, start, length);
    235                 int len = passwordStr.length();
    236                 if (len == 0) {
    237                     throw new SAXException();
    238                 }
    239                 if (securityType == WEP) {
    240                     if ((len == 10 || len == 26 || len == 58) &&
    241                             passwordStr.matches("[0-9A-Fa-f]*")) {
    242                         config.wepKeys[0] = passwordStr;
    243                     } else {
    244                         config.wepKeys[0] = '"' + passwordStr + '"';
    245                     }
    246                 } else if (securityType == PSK) {
    247                     if (passwordStr.matches("[0-9A-Fa-f]{64}")) {
    248                         config.preSharedKey = passwordStr;
    249                     } else {
    250                         config.preSharedKey = '"' + passwordStr + '"';
    251                     }
    252                 } else if (securityType == EAP) {
    253                     config.password.setValue(passwordStr);
    254                 } else {
    255                     throw new SAXException();
    256                 }
    257                 password = false;
    258             }
    259             if (eap) {
    260                 String eapValue = new String(ch, start, length);
    261                 if (!validateEapValue(eapValue)) {
    262                     throw new SAXException();
    263                 }
    264                 config.eap.setValue(eapValue);
    265                 eap = false;
    266             }
    267             if (phase2) {
    268                 String phase2Value = new String(ch, start, length);
    269                 config.phase2.setValue("auth=" + phase2Value);
    270                 phase2 = false;
    271             }
    272             if (identity) {
    273                 String identityValue = new String(ch, start, length);
    274                 config.identity.setValue(identityValue);
    275                 identity = false;
    276             }
    277             if (anonymousidentity) {
    278                 String anonyId = new String(ch, start, length);
    279                 config.anonymous_identity.setValue(anonyId);
    280                 anonymousidentity = false;
    281             }
    282             if (cacert) {
    283                 String cacertValue = new String(ch, start, length);
    284                 // need to install the credentail to "keystore://"
    285                 config.ca_cert.setValue(KEYSTORE_SPACE);
    286                 cacert = false;
    287             }
    288             if (usercert) {
    289                 String usercertValue = new String(ch, start, length);
    290                 config.client_cert.setValue(KEYSTORE_SPACE);
    291                 usercert = false;
    292             }
    293             if (ip) {
    294                 try {
    295                     mDhcpInfo.ipAddress = stringToIpAddr(new String(ch, start, length));
    296                 } catch (UnknownHostException e) {
    297                     throw new SAXException();
    298                 }
    299                 ip = false;
    300             }
    301             if (gateway) {
    302                 try {
    303                     mDhcpInfo.gateway = stringToIpAddr(new String(ch, start, length));
    304                 } catch (UnknownHostException e) {
    305                     throw new SAXException();
    306                 }
    307                 gateway = false;
    308             }
    309             if (netmask) {
    310                 try {
    311                     mDhcpInfo.netmask = stringToIpAddr(new String(ch, start, length));
    312                 } catch (UnknownHostException e) {
    313                     throw new SAXException();
    314                 }
    315                 netmask = false;
    316             }
    317             if (dns1) {
    318                 try {
    319                     mDhcpInfo.dns1 = stringToIpAddr(new String(ch, start, length));
    320                 } catch (UnknownHostException e) {
    321                     throw new SAXException();
    322                 }
    323                 dns1 = false;
    324             }
    325             if (dns2) {
    326                 try {
    327                     mDhcpInfo.dns2 = stringToIpAddr(new String(ch, start, length));
    328                 } catch (UnknownHostException e) {
    329                     throw new SAXException();
    330                 }
    331                 dns2 = false;
    332             }
    333         }
    334     };
    335 
    336     /**
    337      * Process the InputStream in
    338      * @param in is the InputStream that can be used for XML parsing
    339      * @throws Exception
    340      */
    341     public AccessPointParserHelper(InputStream in) throws Exception {
    342         SAXParserFactory factory = SAXParserFactory.newInstance();
    343         SAXParser saxParser = factory.newSAXParser();
    344         saxParser.parse(in, mHandler);
    345     }
    346 
    347     public List<WifiConfiguration> getNetworkConfigurations() throws Exception {
    348         return networks;
    349     }
    350 
    351     public HashMap<String, DhcpInfo> getSsidToDhcpInfoHashMap() {
    352         return ssidToDhcpInfoHM;
    353     }
    354 }
    355