From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: "Nate(Qiang) Jiang" <qiangjiang@google.com>
Date: Thu, 13 Apr 2023 19:25:51 +0000
Subject: [PATCH] DO NOT MERGE: 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:07d112fb45449e06233b71e877e835084c76f220)
Merged-In: Ibd3ee52c5993a541abb7b373e4052091d13e9c4f
Change-Id: Ibd3ee52c5993a541abb7b373e4052091d13e9c4f
---
 .../server/wifi/WifiConfigurationUtil.java    |  4 ++
 .../wifi/WifiConfigurationUtilTest.java       | 41 +++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/service/java/com/android/server/wifi/WifiConfigurationUtil.java b/service/java/com/android/server/wifi/WifiConfigurationUtil.java
index b8992a011..2bb7b0dd2 100644
--- a/service/java/com/android/server/wifi/WifiConfigurationUtil.java
+++ b/service/java/com/android/server/wifi/WifiConfigurationUtil.java
@@ -701,6 +701,10 @@ public class WifiConfigurationUtil {
                 && !validatePassword(config.preSharedKey, isAdd, false)) {
             return false;
         }
+        if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WAPI_PSK)
+                && !validatePassword(config.preSharedKey, isAdd, false)) {
+            return false;
+        }
         if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.OWE)) {
             // PMF mandatory for OWE networks
             if (!config.requirePMF) {
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java
index 7173dae5b..aaa3f2605 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationUtilTest.java
@@ -352,6 +352,18 @@ public class WifiConfigurationUtilTest {
         assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
     }
 
+    @Test
+    public void testValidateNegativeCases_BadAsciiPskLengthWapi() {
+        WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
+        assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+
+        config.preSharedKey = "\"abcdffeeretretyetreteteteabe34tetrertertrsraaaaaaaaaaa345eqwrweewq"
+                + "weqe\"";
+        assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+        config.preSharedKey = "\"454\"";
+        assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+    }
+
     /**
      * Verify that the validate method fails to validate WifiConfiguration with bad sae length.
      */
@@ -380,6 +392,15 @@ public class WifiConfigurationUtilTest {
         assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
     }
 
+    @Test
+    public void testValidateNegativeCases_MalformedAsciiPskStringWapi() {
+        WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
+        assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+
+        config.preSharedKey = "\"abcdfefeeretrety";
+        assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+    }
+
     /**
      * Verify that the validate method fails to validate WifiConfiguration with malformed sae
      * string.
@@ -407,6 +428,17 @@ public class WifiConfigurationUtilTest {
         assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
     }
 
+    @Test
+    public void testValidateNegativeCases_BadHexPskLengthWapi() {
+        WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
+        assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+
+        config.preSharedKey = "abcd123456788990013453445345465465476546";
+        assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+        config.preSharedKey = "";
+        assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+    }
+
     /**
      * Verify that the validate method fails to validate WifiConfiguration with malformed psk
      * string.
@@ -420,6 +452,15 @@ public class WifiConfigurationUtilTest {
         assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
     }
 
+    @Test
+    public void testValidateNegativeCases_MalformedHexPskStringWapi() {
+        WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
+        assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+
+        config.preSharedKey = "adbdfgretrtyrtyrty";
+        assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD));
+    }
+
     /**
      * Verify that the validate method fails to validate WifiConfiguration with malformed sae
      * string.
