1 /*
2  * Copyright (C) 2023 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.settingslib.wifi.dpp;
18 
19 import android.content.Intent;
20 import android.net.wifi.WifiConfiguration;
21 import android.net.wifi.WifiManager;
22 import android.text.TextUtils;
23 
24 import java.util.List;
25 
26 
27 /**
28  * Wifi dpp intent helper functions to share between the Settings App and SystemUI.
29  */
30 public class WifiDppIntentHelper {
31     /**
32      * Action added to the intent when app wants to launch the QR code generator with lock screen.
33      */
34     public static final String ACTION_CONFIGURATOR_AUTH_QR_CODE_GENERATOR =
35             "android.settings.WIFI_DPP_CONFIGURATOR_AUTH_QR_CODE_GENERATOR";
36     static final String EXTRA_WIFI_SECURITY = "security";
37 
38     /** The data corresponding to {@code WifiConfiguration} SSID */
39     static final String EXTRA_WIFI_SSID = "ssid";
40 
41     /** The data corresponding to {@code WifiConfiguration} preSharedKey */
42     static final String EXTRA_WIFI_PRE_SHARED_KEY = "preSharedKey";
43 
44     /** The data corresponding to {@code WifiConfiguration} hiddenSSID */
45     static final String EXTRA_WIFI_HIDDEN_SSID = "hiddenSsid";
46     static final String SECURITY_NO_PASSWORD = "nopass"; //open network or OWE
47     static final String SECURITY_WEP = "WEP";
48     static final String SECURITY_WPA_PSK = "WPA";
49     static final String SECURITY_SAE = "SAE";
50 
51     /**
52      * Set all extra except {@code EXTRA_WIFI_NETWORK_ID} for the intent to
53      * launch configurator activity later.
54      *
55      * @param intent the target to set extra
56      * @param wifiManager an instance of {@code WifiManager}
57      * @param wifiConfiguration the Wi-Fi network for launching configurator activity
58      */
setConfiguratorIntentExtra(Intent intent, WifiManager wifiManager, WifiConfiguration wifiConfiguration)59     public static void setConfiguratorIntentExtra(Intent intent, WifiManager wifiManager,
60             WifiConfiguration wifiConfiguration) {
61         String ssid = removeFirstAndLastDoubleQuotes(wifiConfiguration.SSID);
62         String security = getSecurityString(wifiConfiguration);
63 
64         // When the value of this key is read, the actual key is not returned, just a "*".
65         // Call privileged system API to obtain actual key.
66         String preSharedKey = removeFirstAndLastDoubleQuotes(getPresharedKey(wifiManager,
67                 wifiConfiguration));
68 
69         if (!TextUtils.isEmpty(ssid)) {
70             intent.putExtra(EXTRA_WIFI_SSID, ssid);
71         }
72         if (!TextUtils.isEmpty(security)) {
73             intent.putExtra(EXTRA_WIFI_SECURITY, security);
74         }
75         if (!TextUtils.isEmpty(preSharedKey)) {
76             intent.putExtra(EXTRA_WIFI_PRE_SHARED_KEY, preSharedKey);
77         }
78         intent.putExtra(EXTRA_WIFI_HIDDEN_SSID, wifiConfiguration.hiddenSSID);
79     }
80 
getSecurityString(WifiConfiguration config)81     private static String getSecurityString(WifiConfiguration config) {
82         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SAE)) {
83             return SECURITY_SAE;
84         }
85         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.OWE)) {
86             return SECURITY_NO_PASSWORD;
87         }
88         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)
89                 || config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA2_PSK)) {
90             return SECURITY_WPA_PSK;
91         }
92         return (config.wepKeys[0] == null) ? SECURITY_NO_PASSWORD : SECURITY_WEP;
93     }
94 
removeFirstAndLastDoubleQuotes(String str)95     private static String removeFirstAndLastDoubleQuotes(String str) {
96         if (TextUtils.isEmpty(str)) {
97             return str;
98         }
99 
100         int begin = 0;
101         int end = str.length() - 1;
102         if (str.charAt(begin) == '\"') {
103             begin++;
104         }
105         if (str.charAt(end) == '\"') {
106             end--;
107         }
108         return str.substring(begin, end + 1);
109     }
110 
getPresharedKey(WifiManager wifiManager, WifiConfiguration wifiConfiguration)111     private static String getPresharedKey(WifiManager wifiManager,
112             WifiConfiguration wifiConfiguration) {
113         List<WifiConfiguration> privilegedWifiConfigurations =
114                 wifiManager.getPrivilegedConfiguredNetworks();
115 
116         for (WifiConfiguration privilegedWifiConfiguration : privilegedWifiConfigurations) {
117             if (privilegedWifiConfiguration.networkId == wifiConfiguration.networkId) {
118                 // WEP uses a shared key hence the AuthAlgorithm.SHARED is used
119                 // to identify it.
120                 if (wifiConfiguration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE)
121                         && wifiConfiguration.allowedAuthAlgorithms.get(
122                         WifiConfiguration.AuthAlgorithm.SHARED)) {
123                     return privilegedWifiConfiguration
124                             .wepKeys[privilegedWifiConfiguration.wepTxKeyIndex];
125                 } else {
126                     return privilegedWifiConfiguration.preSharedKey;
127                 }
128             }
129         }
130         return wifiConfiguration.preSharedKey;
131     }
132 }
133