Add pre-share key check for wapi

Bug: 275339978
Test: atest com.androi.server.wifi
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:c4142aedf5a09294cd4f2b184a1564b1c4e203ba)
Merged-In: Id37e395f4f4f05b7901b718e3ea84c56b95cdfe7
Change-Id: Id37e395f4f4f05b7901b718e3ea84c56b95cdfe7
diff --git a/service/java/com/android/server/wifi/WifiConfigurationUtil.java b/service/java/com/android/server/wifi/WifiConfigurationUtil.java
index 907ca72..4a594b7 100644
--- a/service/java/com/android/server/wifi/WifiConfigurationUtil.java
+++ b/service/java/com/android/server/wifi/WifiConfigurationUtil.java
@@ -720,6 +720,10 @@
                 && !validatePassword(config.preSharedKey, isAdd, true)) {
             return false;
         }
+        if (config.isSecurityType(WifiConfiguration.SECURITY_TYPE_WAPI_PSK)
+                && !validatePassword(config.preSharedKey, isAdd, false)) {
+            return false;
+        }
         if (config.isSecurityType(WifiConfiguration.SECURITY_TYPE_DPP)
                 && (supportedFeatureSet & WifiManager.WIFI_FEATURE_DPP_AKM) == 0) {
             Log.e(TAG, "DPP AKM is not supported");
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
index b707b1d..20baf93 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
@@ -482,7 +482,7 @@
     public void testAddWapiPskHexNetwork() {
         WifiConfiguration wapiPskNetwork = WifiConfigurationTestUtil.createWapiPskNetwork();
         wapiPskNetwork.preSharedKey =
-            "123456780abcdef0123456780abcdef0";
+            "123456780abcdef0123456780abcdef0123456780abcdef0123456780abcdef0";
         List<WifiConfiguration> networks = new ArrayList<>();
         networks.add(wapiPskNetwork);
 
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java
index e3b3253..7cabcd8 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java
@@ -369,6 +369,21 @@
                 WifiConfigurationUtil.VALIDATE_FOR_ADD));
     }
 
+    @Test
+    public void testValidateNegativeCases_BadAsciiPskLengthWapi() {
+        WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
+        assertTrue(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+
+        config.preSharedKey = "\"abcdffeeretretyetreteteteabe34tetrertertrsraaaaaaaaaaa345eqwrweewq"
+                + "weqe\"";
+        assertFalse(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+        config.preSharedKey = "\"454\"";
+        assertFalse(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+    }
+
     /**
      * Verify that the validate method fails to validate WifiConfiguration with bad sae length.
      */
@@ -402,6 +417,17 @@
                 WifiConfigurationUtil.VALIDATE_FOR_ADD));
     }
 
+    @Test
+    public void testValidateNegativeCases_MalformedAsciiPskStringWapi() {
+        WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
+        assertTrue(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+
+        config.preSharedKey = "\"abcdfefeeretrety";
+        assertFalse(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+    }
+
     /**
      * Verify that the validate method fails to validate WifiConfiguration with malformed sae
      * string.
@@ -434,6 +460,20 @@
                 WifiConfigurationUtil.VALIDATE_FOR_ADD));
     }
 
+    @Test
+    public void testValidateNegativeCases_BadHexPskLengthWapi() {
+        WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
+        assertTrue(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+
+        config.preSharedKey = "abcd123456788990013453445345465465476546";
+        assertFalse(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+        config.preSharedKey = "";
+        assertFalse(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+    }
+
     /**
      * Verify that the validate method fails to validate WifiConfiguration with malformed psk
      * string.
@@ -449,6 +489,17 @@
                 WifiConfigurationUtil.VALIDATE_FOR_ADD));
     }
 
+    @Test
+    public void testValidateNegativeCases_MalformedHexPskStringWapi() {
+        WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
+        assertTrue(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+
+        config.preSharedKey = "adbdfgretrtyrtyrty";
+        assertFalse(WifiConfigurationUtil.validate(config, SUPPORTED_FEATURES_ALL,
+                WifiConfigurationUtil.VALIDATE_FOR_ADD));
+    }
+
     /**
      * Verify that the validate method fails to validate WifiConfiguration with malformed sae
      * string.