Iu: add UEA encryption

Add vty 'encryption uea 0 1 2', defaults to 'encryption uea 0' to yield
previous behavior.

If any UEA above 0 is enabled, include the UEA key in the Iu Security
Mode Command.

I noticed that only the code bit in st_iu_security_cmd_on_enter()
affects the test. The same code in gsm48_gmm_authorize() seems to be
dead code? But applying the patch there as well just to be safe.

We cannot yet verify the chosen UEA to match a configured UEA level,
because the iu_client.c does not send us message details with the
RANAP_IU_EVENT_SECURITY_MODE_COMPLETE.
Also we cannot yet send the set of configured UEA to the hNodeB, since,
again, iu_client.c does not provide the proper API for it.
The proper solution here is to completely dissolve iu_client.c and do
all Iu handling in osmo-sgsn itself -- see OS#5487.

Related: SYS#5516
Related: I1a7c3b156830058c43f15f55883ea301d2d01d5f (osmo-ttcn3-hacks)
Change-Id: I27e8e0078c45426bf227bb44aac82a4875d18d0f
diff --git a/src/sgsn/sgsn_vty.c b/src/sgsn/sgsn_vty.c
index 56a2d78..a394c41 100644
--- a/src/sgsn/sgsn_vty.c
+++ b/src/sgsn/sgsn_vty.c
@@ -47,6 +47,7 @@
 #include <osmocom/vty/vty.h>
 #include <osmocom/vty/misc.h>
 #include <osmocom/crypt/gprs_cipher.h>
+#include <osmocom/crypt/utran_cipher.h>
 #include <osmocom/abis/ipa.h>
 
 #include <osmocom/gprs/gprs_bssgp.h>
@@ -775,9 +776,11 @@
 	return CMD_SUCCESS;
 }
 
+#define ENCRYPTION_STR "Set encryption algorithms for SGSN\n"
+
 DEFUN(cfg_encrypt2, cfg_encrypt2_cmd,
 	"encryption gea <0-4> [<0-4>] [<0-4>] [<0-4>] [<0-4>]",
-	"Set encryption algorithms for SGSN\n"
+	ENCRYPTION_STR
 	"GPRS Encryption Algorithm\n"
 	"GEAn Algorithm Number\n"
 	"GEAn Algorithm Number\n"
@@ -835,6 +838,23 @@
 	return CMD_SUCCESS;
 }
 
+DEFUN(cfg_encryption_uea, cfg_encryption_uea_cmd,
+      "encryption uea <0-2> [<0-2>] [<0-2>]",
+      ENCRYPTION_STR
+      "UTRAN (3G) encryption algorithms to allow: 0 = UEA0 (no encryption), 1 = UEA1, 2 = UEA2.\n"
+      "UEAn Algorithm Number\n"
+      "UEAn Algorithm Number\n"
+      "UEAn Algorithm Number\n")
+{
+	unsigned int i;
+
+	g_cfg->uea_encryption_mask = 0;
+	for (i = 0; i < argc; i++)
+		g_cfg->uea_encryption_mask |= (1 << atoi(argv[i]));
+
+	return CMD_SUCCESS;
+}
+
 DEFUN(cfg_auth_policy, cfg_auth_policy_cmd,
 	"auth-policy (accept-all|closed|acl-only|remote)",
 	"Configure the Authorization policy of the SGSN. This setting determines which subscribers are"
@@ -1732,6 +1752,7 @@
 	/* order matters here: ensure we attempt to parse our new command first! */
 	install_element(SGSN_NODE, &cfg_encrypt2_cmd);
 	install_element(SGSN_NODE, &cfg_encrypt_cmd);
+	install_element(SGSN_NODE, &cfg_encryption_uea_cmd);
 
 	install_element(SGSN_NODE, &cfg_gsup_ipa_name_cmd);
 	install_element(SGSN_NODE, &cfg_gsup_remote_ip_cmd);
@@ -1784,6 +1805,7 @@
 	OSMO_ASSERT(g_cfg);
 
 	g_cfg->gea_encryption_mask = 0x1; /* support GEA0 by default unless specific encryption config exists */
+	g_cfg->uea_encryption_mask = (1 << OSMO_UTRAN_UEA0); /* support UEA0 by default unless specific encryption config exists */
 
 	rc = vty_read_config_file(config_file, NULL);
 	if (rc < 0) {