replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / src / srmutility.c
index b3685e2..2bd9bcd 100644 (file)
 #include "srmresourcestrings.h"
 #include "logger.h"
 #include "oic_malloc.h"
+#include "oic_string.h"
 #include "base64.h"
+#include "doxmresource.h"
+#include "pstatresource.h"
+#include "cacommon.h"
+#include "casecurityinterface.h"
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+#include "pkix_interface.h"
+#endif
 
 #define TAG  "OIC_SRM_UTILITY"
 
@@ -133,10 +141,14 @@ const char* GetOxmString(OicSecOxm_t oxmType)
             return OXM_RANDOM_DEVICE_PIN;
         case OIC_MANUFACTURER_CERTIFICATE:
             return OXM_MANUFACTURER_CERTIFICATE;
-#ifdef _ENABLE_MULTIPLE_OWNER_
+#ifdef MULTIPLE_OWNER
         case OIC_PRECONFIG_PIN:
             return OXM_PRECONF_PIN;
-#endif //_ENABLE_MULTIPLE_OWNER_
+#endif //MULTIPLE_OWNER
+        case OIC_MV_JUST_WORKS:
+            return OXM_MV_JUST_WORKS;
+        case OIC_CON_MFG_CERT:
+            return OXM_CON_MFG_CERT;
         default:
             return NULL;
     }
@@ -218,3 +230,148 @@ OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid)
 
     return OC_STACK_OK;
 }
+
+#if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
+OCStackResult SetDeviceIdSeed(const uint8_t* seed, size_t seedSize)
+{
+    return SetDoxmDeviceIDSeed(seed, seedSize);
+}
+
+static OicSecOtmEventHandler_t gOtmEventHandler = NULL;
+static char ptAddr[256] = {0};
+static uint16_t ptPort = 0;
+
+void SetOtmEventHandler(OicSecOtmEventHandler_t otmEventHandler)
+{
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+
+    memset(ptAddr, 0x00, sizeof(ptAddr));
+    ptPort = 0;
+    gOtmEventHandler = otmEventHandler;
+    OIC_LOG_V(DEBUG, TAG, "Out%s", __func__);
+}
+
+/**
+ * Function to handle the handshake result in OTM.
+ * This function will be invoked after DTLS handshake
+ * @param   endPoint  [IN] The remote endpoint.
+ * @param   errorInfo [IN] Error information from the endpoint.
+ * @return  NONE
+ */
+static void DTLSHandshakeServerCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
+{
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+    if(NULL != endpoint && NULL != info)
+    {
+        OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
+                 endpoint->addr, endpoint->port, info->result);
+
+        //We can't know about PT's secure port, so compare only adress to identify the PT.
+        if (strncmp(endpoint->addr, ptAddr, strlen(ptAddr)) == 0)
+        {
+            OIC_LOG_V(INFO, TAG, "Normal port is [%s:%d]", ptAddr, ptPort);
+
+            //If DTLS handshake error occurred, revert secure resource and notify error event to application.
+            if (CA_STATUS_OK != info->result)
+            {
+                OIC_LOG(ERROR, TAG, "Failed to establish a secure session with owner device.");
+                OIC_LOG(ERROR, TAG, "Doxm/Pstat resource will be reverted to init state.");
+                RestoreDoxmToInitState();
+                RestorePstatToInitState();
+                InvokeOtmEventHandler(endpoint->addr, endpoint->port, NULL, OIC_OTM_ERROR);
+            }
+        }
+        else
+        {
+            OIC_LOG_V(WARNING, TAG, "[%s:%d] is not a owner device", endpoint->addr, endpoint->port);
+        }
+    }
+    else
+    {
+        OIC_LOG(WARNING, TAG, "Invalid param.");
+    }
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+}
+
+
+void InvokeOtmEventHandler(const char* addr, uint16_t port,
+                           const OicUuid_t* uuid, OicSecOtmEvent_t event)
+{
+    char* strUuid = NULL;
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+
+    //addr can be NULL for init state
+    //port can be '0' for BLE and init state
+    //uuid can be NULL for init state & coap
+
+    switch(event)
+    {
+        case OIC_OTM_READY:
+        case OIC_OTM_STARTED:
+            if (addr)
+            {
+                OICStrcpy(ptAddr, sizeof(ptAddr), addr);
+                ptPort = port;
+            }
+            else
+            {
+                memset(ptAddr, 0x00, sizeof(ptAddr));
+                ptPort = 0;
+            }
+            //Register TLS event handler to catch the tls event while handshake
+            if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeServerCB))
+            {
+                OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
+            }
+            break;
+        case OIC_OTM_DONE:
+        case OIC_OTM_ERROR:
+            memset(ptAddr, 0x00, sizeof(ptAddr));
+            ptPort = 0;
+            //Register TLS event handler to catch the tls event while handshake
+            if(CA_STATUS_OK != CAregisterSslHandshakeCallback(NULL))
+            {
+                OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
+            }
+            //Restore Pkix handler to initial state
+            CAregisterPkixInfoHandler(GetPkixInfo);
+            CAregisterGetCredentialTypesHandler(InitCipherSuiteList);
+            break;
+        default:
+            OIC_LOG_V(ERROR, TAG, "Unknow OTM event : %d", event);
+            goto exit;
+    }
+
+    if (uuid)
+    {
+        if(OC_STACK_OK != ConvertUuidToStr(uuid, &strUuid))
+        {
+            OIC_LOG(ERROR, TAG, "Failed to convert UUID to String.");
+            goto exit;
+        }
+    }
+
+    OIC_LOG(DEBUG, TAG, "=================================");
+    OIC_LOG(DEBUG, TAG, "[OTM Event]");
+    OIC_LOG_V(DEBUG, TAG, "PT UUID : %s", (strUuid ? strUuid : "NULL"));
+    OIC_LOG_V(DEBUG, TAG, "PT Addr=%s:%d", (addr ? addr : "NULL"), port);
+    OIC_LOG_V(DEBUG, TAG, "Event Code=%d", event);
+    OIC_LOG(DEBUG, TAG, "=================================");
+
+    if (NULL == gOtmEventHandler)
+    {
+        OIC_LOG(WARNING, TAG, "OTM event handler is not registered.");
+        goto exit;
+    }
+
+    OIC_LOG(DEBUG, TAG, "Invoking callback to notify OTM state..");
+    gOtmEventHandler(addr, port, strUuid, (int)event);
+
+exit:
+    if (strUuid)
+    {
+        OICFree(strUuid);
+    }
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+}
+#endif