Asynchronous User Confirm
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / sample / sampleserver_mfg.cpp
index c347a42..88ff489 100644 (file)
 #endif //HAVE_WINDOWS_H
 #include "platform_features.h"
 #include "logger.h"
-
+#include "pkix_interface.h"
+#include "hw_emul/hw_interface.h"
+#include "oxmverifycommon.h"
+#include "casecurityinterface.h"
 
 #define TAG "SAMPLE_MANUFACTURER_CERT"
 
@@ -403,20 +406,111 @@ void handleSigInt(int signum)
     }
 }
 
+OCStackResult confirmCB_thread()
+{
+    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)
+        {
+            SendUserConfirm(true);
+            break;
+        }
+        else if (0 == userConfirm)
+        {
+            SendUserConfirm(false);
+            break;
+        }
+        printf("   Entered Wrong Number. Please Enter Again\n");
+    }
+    return OC_STACK_OK;
+}
+
+OCStackResult confirmCB(void * ctx)
+{
+    OC_UNUSED(ctx);
+    pthread_t threadId;
+    pthread_create (&threadId, NULL, &confirmCB_thread, NULL);
+    return OC_STACK_OK;
+}
+
+void confirmNoCertCB(CACertificateVerificationStatus_t status)
+{
+    if (CA_CERTIFICATE_VERIFY_SUCCESS_MUTUAL == status)
+    {
+        printf("   > Peer certificate verification successful");
+    }
+    else if (CA_CERTIFICATE_VERIFY_NO_CERT == status)
+    {
+        printf("   > Peer has not provided certificate\n");
+    }
+    else if (CA_CERTIFICATE_VERIFY_FAILED == status)
+    {
+        printf("   > Peer certificate verification failed\n");
+    }
+    return;
+}
+
+void informOxmSelCB(OicSecOxm_t oxmSel)\r
+{\r
+    printf("   > OXM selected: 0x%x\n", oxmSel);\r
+}\r
+\r
 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};
+
+    SetAsyncUserConfirmCB(NULL, confirmCB);
+    SetInformOxmSelCB(informOxmSelCB);
+    CAsetCertificateVerificationCallback(confirmNoCertCB);
 
     OCRegisterPersistentStorageHandler(&ps);
 
@@ -426,6 +520,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
      */