Commit e11d7991 authored by lankamadan's avatar lankamadan Committed by Madan Lanka

Easysetup - Enable Justworks security option in Linux Enrollee

- Enabled Justworks security option in Linux Enrollee

Change-Id: I8ed43278e27aad19b68dc9dc65bddf671df8fe9f
Signed-off-by: default avatarlankamadan <lanka.madan@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/5041Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
parent 8b7cd98a
...@@ -44,19 +44,20 @@ typedef void (*EventCallback)(ESResult esResult, EnrolleeState enrolleeState); ...@@ -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 * 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 networkType NetworkType on which OnBoarding has to be performed.
* @param ssid SSID of the target SoftAP network to which the Enrollee is connecting. * @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 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 * @param isSecured True if the Enrollee is operating in secured mode.
* result to the application * @param eventCallback EventCallback for for updating the Enrollee OnBoarding status result to
* the application
* @return ::ES_OK on success, some other value upon failure. * @return ::ES_OK on success, some other value upon failure.
*/ */
ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
const char *passwd, bool isSecured,
EventCallback eventCallback); 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. * @return ::ES_OK on success, some other value upon failure.
*/ */
ESResult InitProvisioning(); ESResult InitProvisioning();
......
...@@ -36,7 +36,7 @@ typedef void (*ResourceEventCallback)(ESResult); ...@@ -36,7 +36,7 @@ typedef void (*ResourceEventCallback)(ESResult);
typedef struct PROVRESOURCE typedef struct PROVRESOURCE
{ {
OCResourceHandle handle; 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, ... 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 tnn[MAXSSIDLEN]; // target network name, i.e. SSID for WLAN, MAC address for BT
char cd[MAXNETCREDLEN]; // credential information char cd[MAXNETCREDLEN]; // credential information
...@@ -52,7 +52,7 @@ typedef struct NETRESOURCE ...@@ -52,7 +52,7 @@ typedef struct NETRESOURCE
char cnn[MAXSSIDLEN]; // current network name char cnn[MAXSSIDLEN]; // current network name
} NetResource; } NetResource;
OCStackResult CreateProvisioningResource(); OCStackResult CreateProvisioningResource(bool isSecured);
OCStackResult DeleteProvisioningResource(); OCStackResult DeleteProvisioningResource();
OCStackResult DeleteNetworkResource(); OCStackResult DeleteNetworkResource();
......
...@@ -41,16 +41,16 @@ ...@@ -41,16 +41,16 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** /**
* @var targetSsid * @var gTargetSsid
* @brief Target SSID of the Soft Access point to which the device has to connect * @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 * @brief Password of the target access point to which the device has to connect
*/ */
static char *targetPass; static char gTargetPass[MAXNETCREDLEN];
/** /**
* @var gEnrolleeStatusCb * @var gEnrolleeStatusCb
...@@ -58,13 +58,19 @@ static char *targetPass; ...@@ -58,13 +58,19 @@ static char *targetPass;
*/ */
static EventCallback gEnrolleeStatusCb = NULL; 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 // Private internal function prototypes
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void OnboardingCallback(ESResult esResult); void OnboardingCallback(ESResult esResult);
void ProvisioningCallback(ESResult esResult); void ProvisioningCallback(ESResult esResult);
void OnboardingCallbackTargetNet(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); EventCallback cb);
...@@ -86,18 +92,15 @@ void OnboardingCallback(ESResult esResult) ...@@ -86,18 +92,15 @@ void OnboardingCallback(ESResult esResult)
void ProvisioningCallback(ESResult esResult) void ProvisioningCallback(ESResult esResult)
{ {
OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ProvisioningCallback with result = %d", esResult); OC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ProvisioningCallback with result = %d", esResult);
ESResult res = ES_OK;
if (esResult == ES_RECVTRIGGEROFPROVRES) if (esResult == ES_RECVTRIGGEROFPROVRES)
{ {
targetSsid = (char *) malloc(MAXSSIDLEN); GetTargetNetworkInfoFromProvResource(gTargetSsid, gTargetPass);
targetPass = (char *) malloc(MAXNETCREDLEN);
GetTargetNetworkInfoFromProvResource(targetSsid, targetPass);
gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE); gEnrolleeStatusCb(ES_OK, ES_PROVISIONED_STATE);
OC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network"); OC_LOG(DEBUG, ES_ENROLLEE_TAG, "Connecting with target network");
// Connecting/onboarding to target network // Connecting/onboarding to target network
ConnectToWiFiNetwork(targetSsid, targetPass, OnboardingCallbackTargetNet); ConnectToWiFiNetwork(gTargetSsid, gTargetPass, OnboardingCallbackTargetNet);
} }
else else
{ {
...@@ -125,22 +128,12 @@ void OnboardingCallbackTargetNet(ESResult esResult) ...@@ -125,22 +128,12 @@ void OnboardingCallbackTargetNet(ESResult esResult)
} }
} }
static FILE* server_fopen(const char* /*path*/, const char *mode) ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, const char *passwd,
{ bool isSecured,
OC_LOG_V(INFO,ES_ENROLLEE_TAG,"oic_svr_db_server open %s",mode); EventCallback cb)
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)
{ {
OC_LOG(INFO, ES_ENROLLEE_TAG, "InitEasySetup IN"); 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, OC_LOG(ERROR, ES_ENROLLEE_TAG,
"InitEasySetup::Stopping Easy setup due to invalid parameters"); "InitEasySetup::Stopping Easy setup due to invalid parameters");
...@@ -150,6 +143,8 @@ ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid, ...@@ -150,6 +143,8 @@ ESResult InitEasySetup(OCConnectivityType networkType, const char *ssid,
//Init callback //Init callback
gEnrolleeStatusCb = cb; gEnrolleeStatusCb = cb;
gIsSecured = isSecured;
// TODO : This onboarding state has to be set by lower layer, as they better // TODO : This onboarding state has to be set by lower layer, as they better
// knows when actually on-boarding started. // knows when actually on-boarding started.
cb(ES_ERROR,ES_ON_BOARDING_STATE); cb(ES_ERROR,ES_ON_BOARDING_STATE);
...@@ -196,7 +191,7 @@ ESResult InitProvisioning() ...@@ -196,7 +191,7 @@ ESResult InitProvisioning()
{ {
OC_LOG(INFO, ES_ENROLLEE_TAG, "InitProvisioning <<IN>>"); 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"); OC_LOG(ERROR, ES_ENROLLEE_TAG, "CreateProvisioningResource error");
return ES_ERROR; return ES_ERROR;
...@@ -216,12 +211,12 @@ ESResult InitProvisioning() ...@@ -216,12 +211,12 @@ ESResult InitProvisioning()
return ES_RESOURCECREATED; 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) EventCallback cb)
{ {
if (!ssid || !passwd || !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 false;
} }
return true; return true;
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
* @brief Logging tag for module name. * @brief Logging tag for module name.
*/ */
#define ES_RH_TAG "ES_RH" #define ES_RH_TAG "ES_RH"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Private variables // Private variables
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
...@@ -84,16 +83,34 @@ void GetTargetNetworkInfoFromProvResource(char *name, char *pass) ...@@ -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; gProvResource.tnt = CT_ADAPTER_IP;
sprintf(gProvResource.tnn, "Unknown"); sprintf(gProvResource.tnn, "Unknown");
sprintf(gProvResource.cd, "Unknown"); sprintf(gProvResource.cd, "Unknown");
OCStackResult res = OCCreateResource(&gProvResource.handle, "oic.r.prov", OC_RSRVD_INTERFACE_DEFAULT, OCStackResult res = OC_STACK_ERROR;
OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb, NULL, if (isSecured)
OC_DISCOVERABLE | OC_OBSERVABLE); {
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)); OC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
return res; return res;
} }
......
...@@ -53,6 +53,11 @@ using namespace std; ...@@ -53,6 +53,11 @@ using namespace std;
#define OC_RSRVD_ES_TR "tr" #define OC_RSRVD_ES_TR "tr"
#define OC_RSRVD_ES_TNT "tnt" #define OC_RSRVD_ES_TNT "tnt"
#define OC_RSRVD_ES_ANT "ant" #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_PROV "/oic/prov"
#define OC_RSRVD_ES_URI_NET "/oic/net" #define OC_RSRVD_ES_URI_NET "/oic/net"
......
...@@ -57,10 +57,7 @@ enrollee_env.PrependUnique(CPPPATH = [ ...@@ -57,10 +57,7 @@ enrollee_env.PrependUnique(CPPPATH = [
enrollee_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'ocsrm', 'pthread', 'connectivity_abstraction','coap', 'ESEnrolleeSDK']) enrollee_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'ocsrm', 'pthread', 'connectivity_abstraction','coap', 'ESEnrolleeSDK'])
if env.get('SECURED') == '1': enrollee = enrollee_env.Program('enrollee', 'enrolleewifi.cpp')
enrollee = enrollee_env.Program('enrollee', 'enrolleewifisecured.cpp')
else :
enrollee = enrollee_env.Program('enrollee', 'enrolleewifi.cpp')
i_enrollee = enrollee_env.Install(env.get('BUILD_DIR'), enrollee) i_enrollee = enrollee_env.Install(env.get('BUILD_DIR'), enrollee)
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "easysetup.h" #include "easysetup.h"
#include <unistd.h>
#include <string.h> #include <string.h>
#include <iostream> #include <iostream>
#include <pthread.h> #include <pthread.h>
...@@ -43,10 +44,27 @@ static char ssid[] = "EasySetup123"; ...@@ -43,10 +44,27 @@ static char ssid[] = "EasySetup123";
*/ */
static char passwd[] = "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() void PrintMenu()
{ {
cout<<"============"<<endl; 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<<"P: start provisioning resources"<<endl;
cout<<"T: terminate"<<endl; cout<<"T: terminate"<<endl;
cout<<"Q: quit"<<endl; cout<<"Q: quit"<<endl;
...@@ -84,44 +102,77 @@ void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState) ...@@ -84,44 +102,77 @@ void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
PrintMenu(); 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() void StartEasySetup()
{ {
cout<<"StartEasySetup and onboarding started.."<<endl; 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; cout<<"StartEasySetup and onboarding Fail!!"<<endl;
return; 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; pthread_t thread_handle;
if (pthread_create(&thread_handle, NULL, listeningFunc, NULL)) if (pthread_create(&thread_handle, NULL, listeningFunc, NULL))
{ {
cout<<"Thread creation failed"<<endl; 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; return;
} }
cout<<"InitProvisioning:Success"<<endl;
}
void StopEasySetup() //stop OC Stack
{ if (OCStop() != OC_STACK_OK)
cout<<"StopEasySetup IN"<<endl;
if(TerminateEasySetup()== ES_ERROR)
{ {
cout<<"return value is: ES_ERROR"<<endl; cout<<"OCStack stop failed!!"<<endl;
return; return;
} }
cout<<"StopEasySetup OUT"<<endl; cout<<"StopEasySetup OUT"<<endl;
} }
...@@ -129,17 +180,10 @@ int main() ...@@ -129,17 +180,10 @@ int main()
{ {
cout<<"#########################"<<endl; cout<<"#########################"<<endl;
cout<<"EasySetup Enrollee SAMPLE"<<endl; cout<<"EasySetup Enrollee SAMPLE"<<endl;
cout<<"This is modified sample:1"<<endl;
cout<<"#########################"<<endl; cout<<"#########################"<<endl;
PrintMenu(); PrintMenu();
char option; char option;
// Initialize the OC Stack in Server mode
if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
{
return -1;
}
while(true) while(true)
{ {
cin>>option; cin>>option;
...@@ -155,14 +199,19 @@ int main() ...@@ -155,14 +199,19 @@ int main()
cout<<"quit"; cout<<"quit";
break; break;
case 'S': // start easy setup case 'S': // Enable Security
case 's': case 's':
EnableSecurity();
break;
case 'I': // Init EasySetup
case 'i':
StartEasySetup(); StartEasySetup();
break; break;
case 'P': // start provisioning case 'P': // start provisioning
case 'p': case 'p':
StartProvisioning(); StartOICStackAndStartResources();
break; break;
case 'T': // stop easy setup case 'T': // stop easy setup
...@@ -174,7 +223,7 @@ int main() ...@@ -174,7 +223,7 @@ int main()
cout<<"wrong option"<<endl; cout<<"wrong option"<<endl;
break; break;
} }
if(option=='Q') break; if (option == 'Q' || option == 'q') break;
} }
return 0; return 0;
} }
......
...@@ -43,10 +43,24 @@ static char ssid[] = "EasySetup123"; ...@@ -43,10 +43,24 @@ static char ssid[] = "EasySetup123";
*/ */
static char passwd[] = "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() void PrintMenu()
{ {
cout<<"============"<<endl; 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<<"P: start provisioning resources"<<endl;
cout<<"T: terminate"<<endl; cout<<"T: terminate"<<endl;
cout<<"Q: quit"<<endl; cout<<"Q: quit"<<endl;
...@@ -84,12 +98,28 @@ void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState) ...@@ -84,12 +98,28 @@ void EventCallbackInApp(ESResult esResult, EnrolleeState enrolleeState)
PrintMenu(); 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() void StartEasySetup()
{ {