Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright 2016, 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.managedprovisioning.task.wifi;
     18 
     19 import android.net.IpConfiguration.ProxySettings;
     20 import android.net.ProxyInfo;
     21 import android.net.wifi.WifiConfiguration;
     22 import android.text.TextUtils;
     23 
     24 import com.android.internal.annotations.VisibleForTesting;
     25 import com.android.managedprovisioning.model.WifiInfo;
     26 
     27 /**
     28  * Utility class for configuring a new {@link WifiConfiguration} object from the provisioning
     29  * parameters represented via {@link WifiInfo}.
     30  */
     31 public class WifiConfigurationProvider {
     32 
     33     @VisibleForTesting
     34     static final String WPA = "WPA";
     35     @VisibleForTesting
     36     static final String WEP = "WEP";
     37     @VisibleForTesting
     38     static final String NONE = "NONE";
     39 
     40     /**
     41      * Create a {@link WifiConfiguration} object from the internal representation given via
     42      * {@link WifiInfo}.
     43      */
     44     public WifiConfiguration generateWifiConfiguration(WifiInfo wifiInfo) {
     45         WifiConfiguration wifiConf = new WifiConfiguration();
     46         wifiConf.SSID = wifiInfo.ssid;
     47         wifiConf.status = WifiConfiguration.Status.ENABLED;
     48         wifiConf.hiddenSSID = wifiInfo.hidden;
     49         wifiConf.userApproved = WifiConfiguration.USER_APPROVED;
     50         String securityType = wifiInfo.securityType != null ? wifiInfo.securityType : NONE;
     51         switch (securityType) {
     52             case WPA:
     53                 updateForWPAConfiguration(wifiConf, wifiInfo.password);
     54                 break;
     55             case WEP:
     56                 updateForWEPConfiguration(wifiConf, wifiInfo.password);
     57                 break;
     58             default: // NONE
     59                 wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
     60                 wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
     61                 break;
     62         }
     63 
     64         updateForProxy(
     65                 wifiConf,
     66                 wifiInfo.proxyHost,
     67                 wifiInfo.proxyPort,
     68                 wifiInfo.proxyBypassHosts,
     69                 wifiInfo.pacUrl);
     70         return wifiConf;
     71     }
     72 
     73     private void updateForWPAConfiguration(WifiConfiguration wifiConf, String wifiPassword) {
     74         wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
     75         wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
     76         wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
     77         wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
     78         wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
     79         wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
     80         wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
     81         wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
     82         if (!TextUtils.isEmpty(wifiPassword)) {
     83             wifiConf.preSharedKey = "\"" + wifiPassword + "\"";
     84         }
     85     }
     86 
     87     private void updateForWEPConfiguration(WifiConfiguration wifiConf, String password) {
     88         wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
     89         wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
     90         wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
     91         wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
     92         wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
     93         wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
     94         wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
     95         int length = password.length();
     96         if ((length == 10 || length == 26 || length == 58) && password.matches("[0-9A-Fa-f]*")) {
     97             wifiConf.wepKeys[0] = password;
     98         } else {
     99             wifiConf.wepKeys[0] = '"' + password + '"';
    100         }
    101         wifiConf.wepTxKeyIndex = 0;
    102     }
    103 
    104     private void updateForProxy(WifiConfiguration wifiConf, String proxyHost, int proxyPort,
    105             String proxyBypassHosts, String pacUrl) {
    106         if (TextUtils.isEmpty(proxyHost) && TextUtils.isEmpty(pacUrl)) {
    107             return;
    108         }
    109         if (!TextUtils.isEmpty(proxyHost)) {
    110             ProxyInfo proxy = new ProxyInfo(proxyHost, proxyPort, proxyBypassHosts);
    111             wifiConf.setProxy(ProxySettings.STATIC, proxy);
    112         } else {
    113             ProxyInfo proxy = new ProxyInfo(pacUrl);
    114             wifiConf.setProxy(ProxySettings.PAC, proxy);
    115         }
    116     }
    117 }
    118