From a3ea66669c3d9f1c4e93fb44c5e5170d912b5077 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Wed, 18 Dec 2013 13:39:18 +0000
Subject: [PATCH] - Fix UE security capabilities optional fields

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@4759 818b1a75-f10b-46b9-bf7c-635c3b92a50f
---
 .../EURECOM-NAS/src/api/network/nas_message.c |  4 ++
 .../EURECOM-NAS/src/emm/SecurityModeControl.c | 18 ++++----
 .../NAS/EURECOM-NAS/src/emm/sap/emm_send.c    |  1 +
 .../src/ies/UeSecurityCapability.c            | 41 +++++++++++--------
 .../src/ies/UeSecurityCapability.h            |  1 +
 5 files changed, 40 insertions(+), 25 deletions(-)

diff --git a/openair-cn/NAS/EURECOM-NAS/src/api/network/nas_message.c b/openair-cn/NAS/EURECOM-NAS/src/api/network/nas_message.c
index b8b214b6b4..dc495b2b4e 100644
--- a/openair-cn/NAS/EURECOM-NAS/src/api/network/nas_message.c
+++ b/openair-cn/NAS/EURECOM-NAS/src/api/network/nas_message.c
@@ -719,7 +719,11 @@ static UInt32_t _nas_message_get_mac(const char* buffer, UInt32_t count,
     LOG_FUNC_IN;
     /* TODO: run integrity protection algorithm */
     /* TODO: Return the message authentication code */
+#if defined(EPC_BUILD)
+    LOG_FUNC_RETURN (0);
+#else
     LOG_FUNC_RETURN (0xabababab);
+#endif
 }
 
 
diff --git a/openair-cn/NAS/EURECOM-NAS/src/emm/SecurityModeControl.c b/openair-cn/NAS/EURECOM-NAS/src/emm/SecurityModeControl.c
index cebf14c0c0..84678890d3 100644
--- a/openair-cn/NAS/EURECOM-NAS/src/emm/SecurityModeControl.c
+++ b/openair-cn/NAS/EURECOM-NAS/src/emm/SecurityModeControl.c
@@ -28,6 +28,10 @@ Description Defines the security mode control EMM procedure executed by the
 
 *****************************************************************************/
 
+#include <stdlib.h> // malloc, free
+#include <string.h> // memcpy
+#include <inttypes.h>
+
 #include "emm_proc.h"
 #include "nas_log.h"
 #include "nas_timer.h"
@@ -37,9 +41,7 @@ Description Defines the security mode control EMM procedure executed by the
 #include "emm_sap.h"
 #include "emm_cause.h"
 
-#include <stdlib.h> // malloc, free
-#include <string.h> // memcpy
-#include <inttypes.h>
+#include "UeSecurityCapability.h"
 
 #if defined(ENABLE_ITTI)
 # include "assertions.h"
@@ -368,17 +370,17 @@ int emm_proc_security_mode_control(unsigned int ueid, int ksi, int eea, int eia,
                                    emm_common_reject_callback_t reject,
                                    emm_common_failure_callback_t failure)
 {
-    LOG_FUNC_IN;
-
     int rc = RETURNerror;
     int security_context_is_new = FALSE;
 
-    LOG_TRACE(INFO, "EMM-PROC  - Initiate security mode control procedure "
-              "KSI = %d", ksi);
-
     /* Get the UE context */
     emm_data_context_t *emm_ctx = NULL;
 
+    LOG_FUNC_IN;
+
+    LOG_TRACE(INFO, "EMM-PROC  - Initiate security mode control procedure "
+              "KSI = %d", ksi);
+
 #if defined(EPC_BUILD)
     if (ueid > 0) {
         emm_ctx = emm_data_context_get(&_emm_data, ueid);
diff --git a/openair-cn/NAS/EURECOM-NAS/src/emm/sap/emm_send.c b/openair-cn/NAS/EURECOM-NAS/src/emm/sap/emm_send.c
index a92d0b7fa7..b81484eae2 100644
--- a/openair-cn/NAS/EURECOM-NAS/src/emm/sap/emm_send.c
+++ b/openair-cn/NAS/EURECOM-NAS/src/emm/sap/emm_send.c
@@ -1158,6 +1158,7 @@ int emm_send_security_mode_command(const emm_as_security_t *msg,
     size += UE_SECURITY_CAPABILITY_MAXIMUM_LENGTH;
     emm_msg->replayeduesecuritycapabilities.eea = msg->eea;
     emm_msg->replayeduesecuritycapabilities.eia = msg->eia;
+    emm_msg->replayeduesecuritycapabilities.non_eps_security_present = 0;
     emm_msg->replayeduesecuritycapabilities.uea = 0x00;
     emm_msg->replayeduesecuritycapabilities.uia = 0x00;
     emm_msg->replayeduesecuritycapabilities.gea = 0x00;
diff --git a/openair-cn/NAS/EURECOM-NAS/src/ies/UeSecurityCapability.c b/openair-cn/NAS/EURECOM-NAS/src/ies/UeSecurityCapability.c
index b5c67f75a9..e92ec389ef 100644
--- a/openair-cn/NAS/EURECOM-NAS/src/ies/UeSecurityCapability.c
+++ b/openair-cn/NAS/EURECOM-NAS/src/ies/UeSecurityCapability.c
@@ -23,12 +23,15 @@ int decode_ue_security_capability(UeSecurityCapability *uesecuritycapability, ui
     decoded++;
     uesecuritycapability->eia = *(buffer + decoded);
     decoded++;
-    uesecuritycapability->uea = *(buffer + decoded);
-    decoded++;
-    uesecuritycapability->uia = *(buffer + decoded) & 0x7f;
-    decoded++;
-    uesecuritycapability->gea = *(buffer + decoded) & 0x7f;
-    decoded++;
+    if (len == decoded + 3) {
+        uesecuritycapability->non_eps_security_present = 1;
+        uesecuritycapability->uea = *(buffer + decoded);
+        decoded++;
+        uesecuritycapability->uia = *(buffer + decoded) & 0x7f;
+        decoded++;
+        uesecuritycapability->gea = *(buffer + decoded) & 0x7f;
+        decoded++;
+    }
 #if defined (NAS_DEBUG)
     dump_ue_security_capability_xml(uesecuritycapability, iei);
 #endif
@@ -54,14 +57,16 @@ int encode_ue_security_capability(UeSecurityCapability *uesecuritycapability, ui
     encoded++;
     *(buffer + encoded) =  uesecuritycapability->eia;
     encoded++;
-    *(buffer + encoded) = uesecuritycapability->uea;
-    encoded++;
-    *(buffer + encoded) = 0x00 |
-    (uesecuritycapability->uia & 0x7f);
-    encoded++;
-    *(buffer + encoded) = 0x00 |
-    (uesecuritycapability->gea & 0x7f);
-    encoded++;
+    if (uesecuritycapability->non_eps_security_present == 1) {
+        *(buffer + encoded) = uesecuritycapability->uea;
+        encoded++;
+        *(buffer + encoded) = 0x00 |
+        (uesecuritycapability->uia & 0x7f);
+        encoded++;
+        *(buffer + encoded) = 0x00 |
+        (uesecuritycapability->gea & 0x7f);
+        encoded++;
+    }
     *lenPtr = encoded - 1 - ((iei > 0) ? 1 : 0);
     return encoded;
 }
@@ -74,9 +79,11 @@ void dump_ue_security_capability_xml(UeSecurityCapability *uesecuritycapability,
         printf("    <IEI>0x%X</IEI>\n", iei);
     printf("    <EEA>%u</EEA>\n", uesecuritycapability->eea);
     printf("    <EIA>%u</EIA>\n", uesecuritycapability->eia);
-    printf("    <UEA>%u</UEA>\n", uesecuritycapability->uea);
-    printf("    <UIA>%u</UIA>\n", uesecuritycapability->uia);
-    printf("    <GEA>%u</GEA>\n", uesecuritycapability->gea);
+    if (uesecuritycapability->non_eps_security_present == 1) {
+        printf("    <UEA>%u</UEA>\n", uesecuritycapability->uea);
+        printf("    <UIA>%u</UIA>\n", uesecuritycapability->uia);
+        printf("    <GEA>%u</GEA>\n", uesecuritycapability->gea);
+    }
     printf("</Ue Security Capability>\n");
 }
 
diff --git a/openair-cn/NAS/EURECOM-NAS/src/ies/UeSecurityCapability.h b/openair-cn/NAS/EURECOM-NAS/src/ies/UeSecurityCapability.h
index c0c1cd8adb..5ef6c4351b 100644
--- a/openair-cn/NAS/EURECOM-NAS/src/ies/UeSecurityCapability.h
+++ b/openair-cn/NAS/EURECOM-NAS/src/ies/UeSecurityCapability.h
@@ -31,6 +31,7 @@ typedef struct UeSecurityCapability_tag {
 #define UE_SECURITY_CAPABILITY_EIA6	0b00000010
 #define UE_SECURITY_CAPABILITY_EIA7	0b00000001
     uint8_t  eia;
+    unsigned non_eps_security_present:1;
     /* UMTS encryption algorithms supported (octet 5) */
 #define UE_SECURITY_CAPABILITY_UEA0	0b10000000
 #define UE_SECURITY_CAPABILITY_UEA1	0b01000000
-- 
2.26.2