binder: Implement Set/Get network params (Part 3)

Implement the following Set/Get methods:
1. KeyMgmt
2. Proto
3. Auth Alg
3. PairWise Cipher
4. Group Cipher

BUG: 1252274
TEST: Ran 'wpa_supplicant_binder_test'

Change-Id: Ic473e8976db2e0ee8a551c3ecfa1133d14c8b290
Signed-off-by: Roshan Pius <rpius@google.com>
diff --git a/wpa_supplicant/binder/binder_manager.h b/wpa_supplicant/binder/binder_manager.h
index 0d5a914..01ff2fc 100644
--- a/wpa_supplicant/binder/binder_manager.h
+++ b/wpa_supplicant/binder/binder_manager.h
@@ -172,6 +172,68 @@
 	};
 };
 
-} // namespace wpa_supplicant_binder
+// The binder interface uses some values which are the same as internal ones to
+// avoid nasty runtime conversion functions.  So, adding compile time asserts
+// to guard against any internal changes breaking the binder interface.
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::KEY_MGMT_MASK_NONE == WPA_KEY_MGMT_NONE,
+    "KeyMgmt value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::KEY_MGMT_MASK_WPA_PSK == WPA_KEY_MGMT_PSK,
+    "KeyMgmt value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::KEY_MGMT_MASK_WPA_EAP ==
+	WPA_KEY_MGMT_IEEE8021X,
+    "KeyMgmt value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::KEY_MGMT_MASK_IEEE8021X ==
+	WPA_KEY_MGMT_IEEE8021X_NO_WPA,
+    "KeyMgmt value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::PROTO_MASK_WPA == WPA_PROTO_WPA,
+    "Proto value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::PROTO_MASK_RSN == WPA_PROTO_RSN,
+    "Proto value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::PROTO_MASK_OSEN == WPA_PROTO_OSEN,
+    "Proto value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::AUTH_ALG_MASK_OPEN == WPA_AUTH_ALG_OPEN,
+    "AuthAlg value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::AUTH_ALG_MASK_SHARED ==
+	WPA_AUTH_ALG_SHARED,
+    "AuthAlg value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::AUTH_ALG_MASK_LEAP == WPA_AUTH_ALG_LEAP,
+    "AuthAlg value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::GROUP_CIPHER_MASK_WEP40 ==
+	WPA_CIPHER_WEP40,
+    "GroupCipher value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::GROUP_CIPHER_MASK_WEP104 ==
+	WPA_CIPHER_WEP104,
+    "GroupCipher value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::GROUP_CIPHER_MASK_TKIP == WPA_CIPHER_TKIP,
+    "GroupCipher value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::GROUP_CIPHER_MASK_CCMP == WPA_CIPHER_CCMP,
+    "GroupCipher value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::PAIRWISE_CIPHER_MASK_NONE ==
+	WPA_CIPHER_NONE,
+    "PairwiseCipher value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::PAIRWISE_CIPHER_MASK_TKIP ==
+	WPA_CIPHER_TKIP,
+    "PairwiseCipher value mismatch");
+static_assert(
+    fi::w1::wpa_supplicant::INetwork::PAIRWISE_CIPHER_MASK_CCMP ==
+	WPA_CIPHER_CCMP,
+    "PairwiseCipher value mismatch");
 
+} // namespace wpa_supplicant_binder
 #endif // WPA_SUPPLICANT_BINDER_BINDER_MANAGER_H
diff --git a/wpa_supplicant/binder/fi/w1/wpa_supplicant/INetwork.aidl b/wpa_supplicant/binder/fi/w1/wpa_supplicant/INetwork.aidl
index a04fc7b..4f94d85 100644
--- a/wpa_supplicant/binder/fi/w1/wpa_supplicant/INetwork.aidl
+++ b/wpa_supplicant/binder/fi/w1/wpa_supplicant/INetwork.aidl
@@ -48,15 +48,15 @@
 	const int WEP104_KEY_LEN = 13;
 
 	/** Possble mask of values for KeyMgmt param. */
-	const int KEY_MGMT_MASK_NONE = 0x01;
+	const int KEY_MGMT_MASK_WPA_EAP = 0x01;
 	const int KEY_MGMT_MASK_WPA_PSK = 0x02;
-	const int KEY_MGMT_MASK_WPA_EAP = 0x04;
-	const int KEY_MGMT_MASK_IEEE8021X = 0xFF;
+	const int KEY_MGMT_MASK_NONE = 0x04;
+	const int KEY_MGMT_MASK_IEEE8021X = 0x08;
 
 	/** Possble mask of values for Proto param. */
 	const int PROTO_MASK_WPA = 0x01;
 	const int PROTO_MASK_RSN = 0x02;
-	const int PROTO_MASK_OSEN = 0x04;
+	const int PROTO_MASK_OSEN = 0x08;
 
 	/** Possble mask of values for AuthAlg param. */
 	const int AUTH_ALG_MASK_OPEN = 0x01;
@@ -64,15 +64,15 @@
 	const int AUTH_ALG_MASK_LEAP = 0x04;
 
 	/** Possble mask of values for GroupCipher param. */
-	const int GROUP_CIPHER_MASK_WEP40 = 0x01;
-	const int GROUP_CIPHER_MASK_WEP104 = 0x02;
-	const int GROUP_CIPHER_MASK_TKIP = 0x04;
-	const int GROUP_CIPHER_MASK_CCMP = 0x08;
+	const int GROUP_CIPHER_MASK_WEP40 = 0x02;
+	const int GROUP_CIPHER_MASK_WEP104 = 0x04;
+	const int GROUP_CIPHER_MASK_TKIP = 0x08;
+	const int GROUP_CIPHER_MASK_CCMP = 0x10;
 
 	/** Possble mask of values for PairwiseCipher param. */
 	const int PAIRWISE_CIPHER_MASK_NONE = 0x01;
-	const int PAIRWISE_CIPHER_MASK_TKIP = 0x02;
-	const int PAIRWISE_CIPHER_MASK_CCMP = 0x04;
+	const int PAIRWISE_CIPHER_MASK_TKIP = 0x08;
+	const int PAIRWISE_CIPHER_MASK_CCMP = 0x10;
 
 	/**
 	 * Retrieves the ID allocated to this network by wpa_supplicant.
diff --git a/wpa_supplicant/binder/network.cpp b/wpa_supplicant/binder/network.cpp
index 12cfec0..41bcf7d 100644
--- a/wpa_supplicant/binder/network.cpp
+++ b/wpa_supplicant/binder/network.cpp
@@ -10,6 +10,31 @@
 #include "binder_manager.h"
 #include "network.h"
 
+namespace {
+constexpr int kAllowedKeyMgmtMask =
+    (fi::w1::wpa_supplicant::INetwork::KEY_MGMT_MASK_NONE |
+     fi::w1::wpa_supplicant::INetwork::KEY_MGMT_MASK_WPA_PSK |
+     fi::w1::wpa_supplicant::INetwork::KEY_MGMT_MASK_WPA_EAP |
+     fi::w1::wpa_supplicant::INetwork::KEY_MGMT_MASK_IEEE8021X);
+constexpr int kAllowedProtoMask =
+    (fi::w1::wpa_supplicant::INetwork::PROTO_MASK_WPA |
+     fi::w1::wpa_supplicant::INetwork::PROTO_MASK_RSN |
+     fi::w1::wpa_supplicant::INetwork::PROTO_MASK_OSEN);
+constexpr int kAllowedAuthAlgMask =
+    (fi::w1::wpa_supplicant::INetwork::AUTH_ALG_MASK_OPEN |
+     fi::w1::wpa_supplicant::INetwork::AUTH_ALG_MASK_SHARED |
+     fi::w1::wpa_supplicant::INetwork::AUTH_ALG_MASK_LEAP);
+constexpr int kAllowedGroupCipherMask =
+    (fi::w1::wpa_supplicant::INetwork::GROUP_CIPHER_MASK_WEP40 |
+     fi::w1::wpa_supplicant::INetwork::GROUP_CIPHER_MASK_WEP104 |
+     fi::w1::wpa_supplicant::INetwork::GROUP_CIPHER_MASK_TKIP |
+     fi::w1::wpa_supplicant::INetwork::GROUP_CIPHER_MASK_CCMP);
+constexpr int kAllowedPairwiseCipherMask =
+    (fi::w1::wpa_supplicant::INetwork::PAIRWISE_CIPHER_MASK_NONE |
+     fi::w1::wpa_supplicant::INetwork::PAIRWISE_CIPHER_MASK_TKIP |
+     fi::w1::wpa_supplicant::INetwork::PAIRWISE_CIPHER_MASK_CCMP);
+} // namespace
+
 namespace wpa_supplicant_binder {
 
 #define RETURN_IF_NETWORK_INVALID(wpa_ssid)                                    \
@@ -129,6 +154,17 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+
+	if (key_mgmt_mask & ~kAllowedKeyMgmtMask) {
+		const std::string error_msg = "Invalid key_mgmt value: " +
+					      std::to_string(key_mgmt_mask) +
+					      ".";
+		return android::binder::Status::fromExceptionCode(
+		    android::binder::Status::EX_ILLEGAL_ARGUMENT,
+		    error_msg.c_str());
+	}
+	wpa_ssid->key_mgmt = key_mgmt_mask;
+	wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", wpa_ssid->key_mgmt);
 	return android::binder::Status::ok();
 }
 
@@ -136,6 +172,16 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+
+	if (proto_mask & ~kAllowedProtoMask) {
+		const std::string error_msg =
+		    "Invalid proto value: " + std::to_string(proto_mask) + ".";
+		return android::binder::Status::fromExceptionCode(
+		    android::binder::Status::EX_ILLEGAL_ARGUMENT,
+		    error_msg.c_str());
+	}
+	wpa_ssid->proto = proto_mask;
+	wpa_printf(MSG_MSGDUMP, "proto: 0x%x", wpa_ssid->proto);
 	return android::binder::Status::ok();
 }
 
@@ -143,6 +189,17 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+
+	if (auth_alg_mask & ~kAllowedAuthAlgMask) {
+		const std::string error_msg = "Invalid auth_alg value: " +
+					      std::to_string(auth_alg_mask) +
+					      ".";
+		return android::binder::Status::fromExceptionCode(
+		    android::binder::Status::EX_ILLEGAL_ARGUMENT,
+		    error_msg.c_str());
+	}
+	wpa_ssid->auth_alg = auth_alg_mask;
+	wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", wpa_ssid->auth_alg);
 	return android::binder::Status::ok();
 }
 
@@ -150,6 +207,17 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+
+	if (group_cipher_mask & ~kAllowedGroupCipherMask) {
+		const std::string error_msg =
+		    "Invalid group_cipher value: " +
+		    std::to_string(group_cipher_mask) + ".";
+		return android::binder::Status::fromExceptionCode(
+		    android::binder::Status::EX_ILLEGAL_ARGUMENT,
+		    error_msg.c_str());
+	}
+	wpa_ssid->group_cipher = group_cipher_mask;
+	wpa_printf(MSG_MSGDUMP, "group_cipher: 0x%x", wpa_ssid->group_cipher);
 	return android::binder::Status::ok();
 }
 
@@ -157,6 +225,18 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+
+	if (pairwise_cipher_mask & ~kAllowedPairwiseCipherMask) {
+		const std::string error_msg =
+		    "Invalid pairwise_cipher value: " +
+		    std::to_string(pairwise_cipher_mask) + ".";
+		return android::binder::Status::fromExceptionCode(
+		    android::binder::Status::EX_ILLEGAL_ARGUMENT,
+		    error_msg.c_str());
+	}
+	wpa_ssid->pairwise_cipher = pairwise_cipher_mask;
+	wpa_printf(
+	    MSG_MSGDUMP, "pairwise_cipher: 0x%x", wpa_ssid->pairwise_cipher);
 	return android::binder::Status::ok();
 }
 
@@ -245,6 +325,9 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+
+	wpa_ssid->ieee80211w =
+	    enable ? MGMT_FRAME_PROTECTION_REQUIRED : NO_MGMT_FRAME_PROTECTION;
 	return android::binder::Status::ok();
 }
 
@@ -283,6 +366,7 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+	*key_mgmt_mask = wpa_ssid->key_mgmt;
 	return android::binder::Status::ok();
 }
 
@@ -290,6 +374,7 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+	*proto_mask = wpa_ssid->proto;
 	return android::binder::Status::ok();
 }
 
@@ -297,6 +382,7 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+	*auth_alg_mask = wpa_ssid->auth_alg;
 	return android::binder::Status::ok();
 }
 
@@ -304,6 +390,7 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+	*group_cipher_mask = wpa_ssid->group_cipher;
 	return android::binder::Status::ok();
 }
 
@@ -312,6 +399,7 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+	*pairwise_cipher_mask = wpa_ssid->pairwise_cipher;
 	return android::binder::Status::ok();
 }
 
@@ -360,6 +448,8 @@
 {
 	struct wpa_ssid *wpa_ssid = retrieveNetworkPtr();
 	RETURN_IF_NETWORK_INVALID(wpa_ssid);
+
+	*enable = (wpa_ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED);
 	return android::binder::Status::ok();
 }