Easysetup - Enable Justworks security option in Linux Enrollee
authorlankamadan <lanka.madan@samsung.com>
Thu, 18 Feb 2016 10:28:06 +0000 (19:28 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 19 Feb 2016 01:57:23 +0000 (01:57 +0000)
- Enabled Justworks security option in Linux Enrollee

Change-Id: I8ed43278e27aad19b68dc9dc65bddf671df8fe9f
Signed-off-by: lankamadan <lanka.madan@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5041
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
service/easy-setup/enrollee/inc/easysetup.h [changed mode: 0644->0755]
service/easy-setup/enrollee/inc/resourcehandler.h [changed mode: 0644->0755]
service/easy-setup/enrollee/src/easysetup.cpp [changed mode: 0644->0755]
service/easy-setup/enrollee/src/resourcehandler.cpp [changed mode: 0644->0755]
service/easy-setup/inc/escommon.h [changed mode: 0644->0755]
service/easy-setup/sampleapp/enrollee/linux/SConscript
service/easy-setup/sampleapp/enrollee/linux/enrolleewifi.cpp [changed mode: 0644->0755]
service/easy-setup/sampleapp/enrollee/tizen-sdb/EnrolleeSample/enrolleewifi.cpp [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 12c6324..7de994f
@@ -44,19 +44,20 @@ typedef void (*EventCallback)(ESResult esResult, EnrolleeState enrolleeState);
  * This function Initializes the EasySetup. This API must be called prior to invoking any other API
  *
  * @param networkType       NetworkType on which OnBoarding has to be performed.
- * @param ssid                   SSID of the target SoftAP network to which the Enrollee is connecting.
- * @param passwd              Password of the target SoftAP network to which the Enrollee is connecting
- * @param eventCallback     EventCallback for for updating the Enrollee OnBoarding and Provisioning status
- *                                    result to the application
+ * @param ssid              SSID of the target SoftAP network to which the Enrollee is connecting.
+ * @param passwd            Password of the target SoftAP network to which the Enrollee is connecting
+ * @param isSecured         True if the Enrollee is operating in secured mode.
+ * @param eventCallback     EventCallback for for updating the Enrollee OnBoarding status result to
+ *                          the application
  * @return ::ES_OK on success, some other value upon failure.
  */
-ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid,
-                          const char *passwd,
-                          EventCallback eventCallback);
+ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
+        bool isSecured,
+        EventCallback eventCallback);
 
 /**
- * This function performs initialization of Provisioning and Network resources needed for EasySetup process.
- *
+ * This function performs initialization of Provisioning and Network resources needed for EasySetup
+ * process.
  * @return ::ES_OK on success, some other value upon failure.
  */
 ESResult InitProvisioning();
old mode 100644 (file)
new mode 100755 (executable)
index 8fafb7e..51938f1
@@ -36,7 +36,7 @@ typedef void (*ResourceEventCallback)(ESResult);
 typedef struct PROVRESOURCE
 {
     OCResourceHandle handle;
-    int ps; // provisiong status, 1 : need to provisioning, 2 : Connected to Internet
+    int ps; // provisiong status, 1 : need to provisioning, 2 : Connected to Enroller
     int tnt; // target network type, 1: WLAN, 2: BT, 3: BLE, 4: Zigbee, ...
     char tnn[MAXSSIDLEN]; // target network name, i.e. SSID for WLAN, MAC address for BT
     char cd[MAXNETCREDLEN]; // credential information
@@ -52,7 +52,7 @@ typedef struct NETRESOURCE
     char cnn[MAXSSIDLEN]; // current network name
 } NetResource;
 
-OCStackResult CreateProvisioningResource();
+OCStackResult CreateProvisioningResource(bool isSecured);
 OCStackResult DeleteProvisioningResource();
 OCStackResult DeleteNetworkResource();
 
old mode 100644 (file)
new mode 100755 (executable)
index 64f3d6c..4d55ff8
 //-----------------------------------------------------------------------------
 
 /**
- * @var targetSsid
+ * @var gTargetSsid
  * @brief Target SSID of the Soft Access point to which the device has to connect
  */
-static char *targetSsid;
+static char gTargetSsid[MAXSSIDLEN];
 
 /**
- * @var targetPass
+ * @var gTargetPass
  * @brief Password of the target access point to which the device has to connect
  */
-static char *targetPass;
+static char gTargetPass[MAXNETCREDLEN];
 
 /**
  * @var gEnrolleeStatusCb
@@ -58,13 +58,19 @@ static char *targetPass;
  */
 static EventCallback gEnrolleeStatusCb = NULL;
 
+/**
+ * @var gIsSecured
+ * @brief Variable to check if secure mode is enabled or not.
+ */
+static bool gIsSecured = false;
+
 //-----------------------------------------------------------------------------
 // Private internal function prototypes
 //-----------------------------------------------------------------------------
 void OnboardingCallback(ESResult esResult);
 void ProvisioningCallback(ESResult esResult);
 void OnboardingCallbackTargetNet(ESResult esResult);
-bool validateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
+static bool ValidateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
               EventCallback cb);
 
 
@@ -86,18 +92,15 @@ void OnboardingCallback(ESResult esResult)
 void ProvisioningCallback(ESResult esResult)
 {
     OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ProvisioningCallback with  result = %d", esResult);
-    ESResult res = ES_OK;
+
     if (esResult == ES_RECVTRIGGEROFPROVRES)
     {
-        targetSsid = (char *) malloc(MAXSSIDLEN);
-        targetPass = (char *) malloc(MAXNETCREDLEN);
-
-        GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
+        GetTargetNetworkInfoFromProvResource(gTargetSsid, gTargetPass);
         gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
         OC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
 
         // Connecting/onboarding to target network
-        ConnectToWiFiNetwork(targetSsid, targetPass, OnboardingCallbackTargetNet);
+        ConnectToWiFiNetwork(gTargetSsid, gTargetPass, OnboardingCallbackTargetNet);
     }
     else
     {
@@ -125,22 +128,12 @@ void OnboardingCallbackTargetNet(ESResult esResult)
     }
 }
 
-static FILE* server_fopen(const char* /*path*/, const char *mode)
-{
-    OC_LOG_V(INFO,ES_ENROLLEE_TAG,"oic_svr_db_server open %s",mode);
-    FILE *file= fopen("/opt/usr/media/Images/oic_svr_db_server.json", mode);
-    if(file==NULL)
-    {
-        OC_LOG(ERROR,ES_ENROLLEE_TAG,"oic_svr_db_server failed");
-    }
-    return file;
-}
-
-ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid,
-                            const char *passwd, EventCallback cb)
+ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
+        bool isSecured,
+        EventCallback cb)
 {
     OC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup IN");
-    if(!validateParam(networkType,ssid,passwd,cb))
+    if(!ValidateParam(networkType,ssid,passwd,cb))
     {
         OC_LOG(ERROR, ES_ENROLLEE_TAG,
                             "InitEasySetup::Stopping Easy setup due to invalid parameters");
@@ -150,6 +143,8 @@ ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid,
     //Init callback
     gEnrolleeStatusCb = cb;
 
+    gIsSecured = isSecured;
+
     // TODO : This onboarding state has to be set by lower layer, as they better
     // knows when actually on-boarding started.
     cb(ES_ERROR,ES_ON_BOARDING_STATE);
@@ -196,7 +191,7 @@ ESResult InitProvisioning()
 {
     OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <<IN>>");
 
-    if (CreateProvisioningResource() != OC_STACK_OK)
+    if (CreateProvisioningResource(gIsSecured) != OC_STACK_OK)
     {
         OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
         return ES_ERROR;
@@ -216,12 +211,12 @@ ESResult InitProvisioning()
     return ES_RESOURCECREATED;
 }
 
-bool validateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
+static bool ValidateParam(OCConnectivityType networkType, const char *ssid, const char *passwd,
               EventCallback cb)
 {
     if (!ssid || !passwd || !cb)
     {
-        OC_LOG(ERROR, ES_ENROLLEE_TAG, "validateParam - Invalid parameters");
+        OC_LOG(ERROR, ES_ENROLLEE_TAG, "ValidateParam - Invalid parameters");
         return false;
     }
     return true;
old mode 100644 (file)
new mode 100755 (executable)
index 905b5ae..828e5cb
@@ -27,7 +27,6 @@
  * @brief Logging tag for module name.
  */
 #define ES_RH_TAG "ES_RH"
-
 //-----------------------------------------------------------------------------
 // Private variables
 //-----------------------------------------------------------------------------
@@ -84,16 +83,34 @@ void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
     }
 }
 
-OCStackResult CreateProvisioningResource()
+OCStackResult CreateProvisioningResource(bool isSecured)
 {
-    gProvResource.ps = 1; // need to do provisioning
+    gProvResource.ps = ES_PS_NEED_PROVISIONING;
+
     gProvResource.tnt = CT_ADAPTER_IP;
     sprintf(gProvResource.tnn, "Unknown");
     sprintf(gProvResource.cd, "Unknown");
 
-    OCStackResult res = OCCreateResource(&gProvResource.handle, "oic.r.prov", OC_RSRVD_INTERFACE_DEFAULT,
-                                                OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb, NULL,
-                                                OC_DISCOVERABLE | OC_OBSERVABLE);
+    OCStackResult res = OC_STACK_ERROR;
+    if (isSecured)
+    {
+        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
+                OC_RSRVD_INTERFACE_DEFAULT,
+                OC_RSRVD_ES_URI_PROV,
+                OCEntityHandlerCb,
+                NULL,
+                OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
+    }
+    else
+    {
+        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
+                OC_RSRVD_INTERFACE_DEFAULT,
+                OC_RSRVD_ES_URI_PROV,
+                OCEntityHandlerCb,
+                NULL,
+                OC_DISCOVERABLE | OC_OBSERVABLE);
+    }
+
     OC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
     return res;
 }
old mode 100644 (file)
new mode 100755 (executable)
index 6ee898e..1798ade
@@ -53,6 +53,11 @@ using namespace std;
 #define OC_RSRVD_ES_TR                     "tr"
 #define OC_RSRVD_ES_TNT                    "tnt"
 #define OC_RSRVD_ES_ANT                    "ant"
+
+/**
+ * Easysetup defined resoruce types and uris.
+ */
+#define OC_RSRVD_ES_PROV_RES_TYPE           "oic.r.prov"
 #define OC_RSRVD_ES_URI_PROV               "/oic/prov"
 #define OC_RSRVD_ES_URI_NET                "/oic/net"
 
index 683854b..03d77fb 100644 (file)
@@ -57,10 +57,7 @@ enrollee_env.PrependUnique(CPPPATH = [
 
 enrollee_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'ocsrm', 'pthread', 'connectivity_abstraction','coap', 'ESEnrolleeSDK'])
 
-if env.get('SECURED') == '1':
-       enrollee = enrollee_env.Program('enrollee', 'enrolleewifisecured.cpp')
-else :
-       enrollee = enrollee_env.Program('enrollee', 'enrolleewifi.cpp')
+enrollee = enrollee_env.Program('enrollee', 'enrolleewifi.cpp')
 
 i_enrollee = enrollee_env.Install(env.get('BUILD_DIR'), enrollee)
 
old mode 100644 (file)
new mode 100755 (executable)
index 4c15e22..fac4c5d
@@ -21,6 +21,7 @@
 
 #include "easysetup.h"
 
+#include <unistd.h>
 #include <string.h>
 #include <iostream>
 #include <pthread.h>
@@ -43,10 +44,27 @@ static char ssid[] = "EasySetup123";
  */
 static char passwd[] = "EasySetup123";
 
+/**
+ * Secure Virtual Resource database for Iotivity Server
+ * It contains Server's Identity and the PSK credentials
+ * of other devices which the server trusts
+ */
+static char CRED_FILE[] = "oic_svr_db_server.json";
+
+OCPersistentStorage ps ;
+
+
+/**
+ * @var gIsSecured
+ * @brief Variable to check if secure mode is enabled or not.
+ */
+static bool gIsSecured = false;
+
 void PrintMenu()
 {
     cout<<"============"<<endl;
-    cout<<"S: start easy setup"<<endl;
+    cout<<"S: Enabled Security"<<endl;
+    cout<<"I: Init easy setup"<<endl;
     cout<<"P: start provisioning resources"<<endl;
     cout<<"T: terminate"<<endl;
     cout<<"Q: quit"<<endl;
@@ -84,44 +102,77 @@ void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
     PrintMenu();
 }
 
+FILE* server_fopen(const char *path, const char *mode)
+{
+    (void) path;
+    return fopen(CRED_FILE, mode);
+}
+
+void EnableSecurity()
+{
+    cout << "Inside EnableSecurity API.." << endl;
+
+    gIsSecured = true;
+
+    // Initialize Persistent Storage for SVR database
+    ps = { server_fopen, fread, fwrite, fclose, unlink };
+    OCRegisterPersistentStorageHandler(&ps);
+}
 
 void StartEasySetup()
 {
     cout<<"StartEasySetup and onboarding started.."<<endl;
 
-    if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
+    if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
     {
         cout<<"StartEasySetup and onboarding Fail!!"<<endl;
         return;
     }
+}
+
+void StartOICStackAndStartResources()
+{
+    cout<<"Starting Enrollee Provisioning"<<endl;
+
+    // Initialize the OC Stack in Server mode
+    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
+    {
+        cout<<"OCStack init error!!"<<endl;
+        return;
+    }
+
+    if (InitProvisioning() == ES_ERROR)
+    {
+        cout<<"Init Provisioning Failed!!"<<endl;
+        return;
+    }
 
     pthread_t thread_handle;
     if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
     {
         cout<<"Thread creation failed"<<endl;
     }
+
+    cout<<"InitProvisioning Success"<<endl;
 }
 
-void StartProvisioning()
+void StopEasySetup()
 {
-    cout<<"Starting Enrollee Provisioning"<<endl;
+    cout<<"StopEasySetup IN"<<endl;
 
-    if(InitProvisioning()== ES_ERROR)
+    if (TerminateEasySetup() == ES_ERROR)
     {
-        cout<<"Init Provisioning Failed"<<endl;
+        cout<<"TerminateEasySetup Failed!!"<<endl;
         return;
     }
-    cout<<"InitProvisioning:Success"<<endl;
-}
 
-void StopEasySetup()
-{
-    cout<<"StopEasySetup IN"<<endl;
-    if(TerminateEasySetup()== ES_ERROR)
+    //stop OC Stack
+    if (OCStop() != OC_STACK_OK)
     {
-        cout<<"return value is: ES_ERROR"<<endl;
+        cout<<"OCStack stop failed!!"<<endl;
         return;
     }
+
     cout<<"StopEasySetup OUT"<<endl;
 }
 
@@ -129,17 +180,10 @@ int main()
 {
     cout<<"#########################"<<endl;
     cout<<"EasySetup Enrollee SAMPLE"<<endl;
-    cout<<"This is modified sample:1"<<endl;
     cout<<"#########################"<<endl;
     PrintMenu();
     char option;
 
-    // Initialize the OC Stack in Server mode
-    if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
-    {
-        return -1;
-    }
-
     while(true)
     {
         cin>>option;
@@ -155,14 +199,19 @@ int main()
                 cout<<"quit";
                 break;
 
-            case 'S': // start easy setup
+            case 'S': // Enable Security
             case 's':
+                EnableSecurity();
+                break;
+
+            case 'I': // Init EasySetup
+            case 'i':
                 StartEasySetup();
                 break;
 
             case 'P': // start provisioning
             case 'p':
-                StartProvisioning();
+                StartOICStackAndStartResources();
                 break;
 
             case 'T': // stop easy setup
@@ -174,7 +223,7 @@ int main()
                 cout<<"wrong option"<<endl;
                 break;
         }
-        if(option=='Q') break;
+        if (option == 'Q' || option == 'q') break;
     }
     return 0;
 }
old mode 100644 (file)
new mode 100755 (executable)
index 6b968d3..31b304f
@@ -43,10 +43,24 @@ static char ssid[] = "EasySetup123";
  */
 static char passwd[] = "EasySetup123";
 
+/**
+ * Secure Virtual Resource database for Iotivity Server
+ * It contains Server's Identity and the PSK credentials
+ * of other devices which the server trusts
+ */
+static char CRED_FILE[] = "oic_svr_db_server.json";
+
+/**
+ * @var gIsSecured
+ * @brief Variable to check if secure mode is enabled or not.
+ */
+static bool gIsSecured = false;
+
 void PrintMenu()
 {
     cout<<"============"<<endl;
-    cout<<"S: start easy setup"<<endl;
+    cout<<"I: Init easy setup"<<endl;
+    cout<<"S: Enabled Security"<<endl;
     cout<<"P: start provisioning resources"<<endl;
     cout<<"T: terminate"<<endl;
     cout<<"Q: quit"<<endl;
@@ -84,12 +98,28 @@ void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
     PrintMenu();
 }
 
+FILE* server_fopen(const char *path, const char *mode)
+{
+    (void) path;
+    return fopen(CRED_FILE, mode);
+}
+
+void EnableSecurity()
+{
+    cout << "Inside EnableSecurity API.." << endl;
+
+    gIsSecured = true;
+
+    // Initialize Persistent Storage for SVR database
+    OCPersistentStorage ps = { server_fopen, fread, fwrite, fclose, unlink };
+    OCRegisterPersistentStorageHandler(&ps);
+}
 
 void StartEasySetup()
 {
     cout<<"StartEasySetup and onboarding started.."<<endl;
 
-    if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, EventCallbackInApp) == ES_ERROR)
+    if(InitEasySetup(CT_ADAPTER_IP, ssid, passwd, gIsSecured, EventCallbackInApp) == ES_ERROR)
     {
         cout<<"StartEasySetup and onboarding Fail!!"<<endl;
         return;
@@ -102,7 +132,7 @@ void StartEasySetup()
     }
 }
 
-void StartProvisioning()
+void StartOICStackAndStartResources()
 {
     cout<<"Starting Enrollee Provisioning"<<endl;
 
@@ -165,14 +195,19 @@ int main()
                 cout<<"quit";
                 break;
 
-            case 'S': // start easy setup
-            case 's':
+            case 'I': // Init EasySetup
+            case 'i':
                 StartEasySetup();
                 break;
 
+            case 'S': // Enable Security
+            case 's':
+                EnableSecurity();
+                break;
+
             case 'P': // start provisioning
             case 'p':
-                StartProvisioning();
+                StartOICStackAndStartResources();
                 break;
 
             case 'T': // stop easy setup