wifi: support setting WAPI psk hex bytes via setPskPassphrase

setPsk only accepts exact 32 bytes, but the
length of WAPI psk hex bytes might vary.

It is not feasible to add a formal HAL API in R,
this would use existing API to route WAPI psk hex
to the correct internal handler.

Bug: 139257562
Test: atest VtsHalWifiSupplicantV1_3TargetTest
Change-Id: If0bf6a3d9278596e3453091283f0c5aa74f41690
diff --git a/wpa_supplicant/hidl/1.3/sta_network.cpp b/wpa_supplicant/hidl/1.3/sta_network.cpp
index 4e492b5..d3b120d 100644
--- a/wpa_supplicant/hidl/1.3/sta_network.cpp
+++ b/wpa_supplicant/hidl/1.3/sta_network.cpp
@@ -96,6 +96,7 @@
 
 #ifdef CONFIG_WAPI_INTERFACE
 std::string dummyWapiCertSuite;
+std::vector<uint8_t> dummyWapiPsk;
 #endif /* CONFIG_WAPI_INTERFACE */
 }  // namespace
 
@@ -1038,9 +1039,30 @@
 	return {SupplicantStatusCode::FAILURE_UNKNOWN, "deprecated"};
 }
 
-SupplicantStatus StaNetwork::setPskPassphraseInternal(const std::string &psk)
+SupplicantStatus StaNetwork::setPskPassphraseInternal(const std::string &rawPsk)
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
+	std::string psk = rawPsk;
+#ifdef CONFIG_WAPI_INTERFACE
+	if (wpa_ssid->key_mgmt & WPA_KEY_MGMT_WAPI_PSK) {
+		if (rawPsk.size() > 2 && rawPsk.front()== '"' && rawPsk.back() == '"') {
+			psk = rawPsk.substr(1, rawPsk.size() - 2);
+		} else {
+			if ((rawPsk.size() & 1)) {
+				return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
+			}
+			size_t len = psk.size() / 2;
+			uint8_t *buf = (uint8_t *) os_malloc(len);
+			if (hexstr2bin(psk.c_str(), buf, len) < 0) {
+			        os_free(buf);
+				return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
+			}
+			std::vector<uint8_t> bytes(buf, buf + len);
+			os_free(buf);
+			return setWapiPskInternal(bytes);
+		}
+	}
+#endif
 	if (isPskPassphraseValid(psk)) {
 		return {SupplicantStatusCode::FAILURE_ARGS_INVALID, ""};
 	}
@@ -1408,6 +1430,23 @@
 #endif
 }
 
+SupplicantStatus StaNetwork::setWapiPskInternal(const std::vector<uint8_t> &psk)
+{
+#ifdef CONFIG_WAPI_INTERFACE
+	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
+	str_clear_free(wpa_ssid->passphrase);
+	wpa_ssid->passphrase = nullptr;
+
+	dummyWapiPsk = psk;
+
+	wpa_ssid->psk_set = 1;
+	resetInternalStateAfterParamsUpdate();
+	return {SupplicantStatusCode::SUCCESS, "Dummy implementation"};
+#else
+	return {SupplicantStatusCode::FAILURE_UNKNOWN, "Not implemented"};
+#endif
+}
+
 std::pair<SupplicantStatus, std::vector<uint8_t>> StaNetwork::getSsidInternal()
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
@@ -1465,6 +1504,29 @@
 std::pair<SupplicantStatus, std::string> StaNetwork::getPskPassphraseInternal()
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
+#ifdef CONFIG_WAPI_INTERFACE
+	if (wpa_ssid->key_mgmt & WPA_KEY_MGMT_WAPI_PSK) {
+		if (wpa_ssid->psk_set) {
+			std::pair<SupplicantStatus, std::vector<uint8_t>> ret = getWapiPskInternal();
+			std::string psk;
+			char buf[3] = {0};
+			for (int i = 0; i < ret.second.size(); i++) {
+				snprintf(buf, sizeof(buf), "%02x", ret.second[i]);
+				psk.append(buf);
+			}
+			return {{SupplicantStatusCode::SUCCESS, ""}, psk};
+		} else {
+			if (!wpa_ssid->passphrase) {
+				return {{SupplicantStatusCode::FAILURE_UNKNOWN, ""}, {}};
+			}
+			std::string passphrase;
+			passphrase.append("\"");
+			passphrase.append(wpa_ssid->passphrase);
+			passphrase.append("\"");
+			return {{SupplicantStatusCode::SUCCESS, ""}, passphrase};
+		}
+	}
+#endif
 	if (!wpa_ssid->passphrase) {
 		return {{SupplicantStatusCode::FAILURE_UNKNOWN, ""}, {}};
 	}
@@ -1755,6 +1817,15 @@
 #endif
 }
 
+std::pair<SupplicantStatus, std::vector<uint8_t>> StaNetwork::getWapiPskInternal()
+{
+#ifdef CONFIG_WAPI_INTERFACE
+	return {{SupplicantStatusCode::SUCCESS, "Dummy implementation"}, dummyWapiPsk};
+#else
+	return {{SupplicantStatusCode::FAILURE_UNKNOWN, "Not implemented"}, {}};
+#endif
+}
+
 SupplicantStatus StaNetwork::enableInternal(bool no_connect)
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
diff --git a/wpa_supplicant/hidl/1.3/sta_network.h b/wpa_supplicant/hidl/1.3/sta_network.h
index 2a847a6..0057596 100644
--- a/wpa_supplicant/hidl/1.3/sta_network.h
+++ b/wpa_supplicant/hidl/1.3/sta_network.h
@@ -386,6 +386,8 @@
 	std::pair<SupplicantStatus, uint32_t> getPairwiseCipher_1_3Internal();
 	SupplicantStatus setPairwiseCipher_1_3Internal(
 	    uint32_t pairwise_cipher_mask);
+	SupplicantStatus setWapiPskInternal(const std::vector<uint8_t>& psk);
+	std::pair<SupplicantStatus, std::vector<uint8_t>> getWapiPskInternal();
 
 	struct wpa_ssid* retrieveNetworkPtr();
 	struct wpa_supplicant* retrieveIfacePtr();