replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / sample / sampleserver_mfg.cpp
index c347a42..90ed672 100644 (file)
@@ -43,7 +43,9 @@
 #endif //HAVE_WINDOWS_H
 #include "platform_features.h"
 #include "logger.h"
-
+#include "pkix_interface.h"
+#include "hw_emul/hw_interface.h"
+#include "oxmverifycommon.h"
 
 #define TAG "SAMPLE_MANUFACTURER_CERT"
 
@@ -403,20 +405,78 @@ void handleSigInt(int signum)
     }
 }
 
+OCStackResult confirmCB(void * ctx)
+{
+    OC_UNUSED(ctx);
+    for (;;)
+    {
+        int userConfirm;
+
+        printf("   > Press 1 for confirmation\n");
+        printf("   > Press 0 otherwise\n");
+
+        for (int ret=0; 1!=ret; )
+        {
+            ret = scanf("%d", &userConfirm);
+            for (; 0x20<=getchar(); );  // for removing overflow garbage
+                                        // '0x20<=code' is character region
+        }
+        if (1 == userConfirm)
+        {
+            break;
+        }
+        else if (0 == userConfirm)
+        {
+            return OC_STACK_USER_DENIED_REQ;
+        }
+        printf("   Entered Wrong Number. Please Enter Again\n");
+    }
+    return OC_STACK_OK;
+}
+
 FILE* server_fopen(const char *path, const char *mode)
 {
     (void)path;
     return fopen(CRED_FILE, mode);
 }
 
-int main()
+int main(int argc, char **argv)
 {
     struct timespec timeout;
 
     OIC_LOG(DEBUG, TAG, "OCServer is starting...");
 
+    int opt;
+    char cert_file[4096] = {0,};
+    char key_file[4096] = {0,};
+    char key_pass[32] = {0,};
+
+    // Set options
+    while ((opt = getopt(argc, argv, "c:k:p:")) != -1)
+    {
+        switch (opt)
+        {
+            case 'c':
+                strncpy(cert_file, optarg, sizeof(cert_file) - 1);
+                printf("Set own certificate file : %s\n", cert_file);
+                break;
+            case 'k':
+                strncpy(key_file, optarg, sizeof(key_file) - 1);
+                printf("Set private key file : %s\n", key_file);
+                break;
+            case 'p':
+                strncpy(key_pass, optarg, sizeof(key_pass) - 1);
+                printf("Set private key password : %s\n", key_pass);
+                break;
+            default:
+                printf("Not set any options\n");
+        }
+    }
+
     // Initialize Persistent Storage for SVR database
-    OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
+    OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink, NULL, NULL};
+
+    SetUserConfirmCB(NULL, confirmCB);
 
     OCRegisterPersistentStorageHandler(&ps);
 
@@ -426,6 +486,31 @@ int main()
         return 0;
     }
 
+    // Register HW secure storage callback
+    if (0 < strlen(cert_file) && 0 < strlen(key_file))
+    {
+        if (0 == SSemulSetCertkeyFilepath(cert_file, key_file, key_pass))
+        {
+            if (0 != SetHwPkixCallbacks(HWGetKeyContext,
+                                                          HWFreeKeyContext,
+                                                          HWGetOwnCertificateChain,
+                                                          HWSetupPkContext))
+            {
+                printf("Fail to regist HW Pkix Callbacks");
+            }
+        }
+        else
+        {
+            printf("Fail to set cert/key file path");
+        }
+    }
+    else
+    {
+        printf("\n    [ Not set any mfg cert options ]\n");
+        printf("    Possible options: %s [-c certificate file path]"
+                " [-k key file path] [-p key password]\n\n", argv[0]);
+    }
+
     /*
      * Declare and create the example resource: LED
      */