1 /* 2 * Copyright (C) 2017 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 package com.android.networkrecommendation.util; 17 18 import static com.android.networkrecommendation.Constants.TAG; 19 20 import android.net.WifiKey; 21 import android.support.annotation.Nullable; 22 import android.text.TextUtils; 23 import com.android.networkrecommendation.config.G; 24 25 /** Utility methods for Wifi Network SSID and BSSID manipulation. */ 26 public final class SsidUtil { 27 28 // A special BSSID used to indicate a wildcard/ignore. 29 // The MAC address this refers to is reserved by IANA 30 // http://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml 31 public static final String BSSID_IGNORE = "00:00:5E:00:00:00"; 32 33 /** Quote an SSID if it hasn't already been quoted. */ 34 @Nullable quoteSsid(@ullable String ssid)35 public static String quoteSsid(@Nullable String ssid) { 36 if (ssid == null) { 37 return null; 38 } 39 if (isValidQuotedSsid(ssid)) { 40 return ssid; 41 } 42 return "\"" + ssid + "\""; 43 } 44 45 /** Strip initial and final quotations marks from an SSID. */ unquoteSsid(@ullable String ssid)46 public static String unquoteSsid(@Nullable String ssid) { 47 if (ssid == null) { 48 return null; 49 } 50 return ssid.replaceAll("^\"", "").replaceAll("\"$", ""); 51 } 52 53 /** 54 * Create a WifiKey for the given SSID/BSSID. Returns null if the key could not be created 55 * (ssid/bssid are not valid patterns). 56 */ 57 @Nullable createWifiKey(String ssid, String bssid)58 public static WifiKey createWifiKey(String ssid, String bssid) { 59 try { 60 return new WifiKey(quoteSsid(ssid), bssid); 61 } catch (IllegalArgumentException | NullPointerException e) { 62 // Expect IllegalArgumentException only in Android O. 63 Blog.e( 64 TAG, 65 e, 66 "Couldn't make a wifi key from %s/%s", 67 Blog.pii(ssid, G.Netrec.enableSensitiveLogging.get()), 68 Blog.pii(bssid, G.Netrec.enableSensitiveLogging.get())); 69 return null; 70 } 71 } 72 73 /** 74 * Returns true if the given string will be accepted as an SSID by WifiKey, especially meaning 75 * it is quoted. 76 */ isValidQuotedSsid(@ullable String ssid)77 public static boolean isValidQuotedSsid(@Nullable String ssid) { 78 return ssid != null && ssid.startsWith("\"") && ssid.endsWith("\""); 79 } 80 81 /** Thows IllegalArgumentException if the given string cannot be used for an SSID in WifiKey. */ checkIsValidQuotedSsid(String ssid)82 public static void checkIsValidQuotedSsid(String ssid) { 83 if (!isValidQuotedSsid(ssid)) { 84 throw new IllegalArgumentException("SSID " + ssid + " expected to be quoted"); 85 } 86 } 87 88 /** 89 * Returns true if the canonical version of two SSIDs (ignoring wrapping quotations) is equal. 90 */ areEqual(@ullable String ssid1, @Nullable String ssid2)91 public static boolean areEqual(@Nullable String ssid1, @Nullable String ssid2) { 92 String quotedSsid1 = quoteSsid(ssid1); 93 String quotedSsid2 = quoteSsid(ssid2); 94 return TextUtils.equals(quotedSsid1, quotedSsid2); 95 } 96 97 /** 98 * Returns a string version of the SSID for logging which is typically redacted. 99 * 100 * <p>The ID will only be returned verbatim if the enableSensitiveLogging flag is set. 101 */ getRedactedId(String ssid)102 public static String getRedactedId(String ssid) { 103 return Blog.pii(String.format("%s", ssid), G.Netrec.enableSensitiveLogging.get()); 104 } 105 106 /** 107 * Returns a string version of the SSID/BSSID pair for logging which is typically redacted. 108 * 109 * <p>The IDs will only be returned verbatim if the enableSensitiveLogging flag is set. 110 */ getRedactedId(String ssid, String bssid)111 public static String getRedactedId(String ssid, String bssid) { 112 return Blog.pii(String.format("%s/%s", ssid, bssid), G.Netrec.enableSensitiveLogging.get()); 113 } 114 115 // Can't instantiate. SsidUtil()116 private SsidUtil() {} 117 } 118