Modified c++ provisioning to support cert provisioning
authorAshwini Kumar <k.ashwini@samsung.com>
Wed, 21 Sep 2016 06:15:19 +0000 (11:45 +0530)
committerRandeep Singh <randeep.s@samsung.com>
Thu, 22 Sep 2016 07:58:44 +0000 (07:58 +0000)
Change-Id: I98b41b2b65d4f3ee52e3cabdbe3fab35789f555b
Signed-off-by: Randeep Singh <randeep.s@samsung.com>
Signed-off-by: Ashwini Kumar <k.ashwini@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10999
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
(cherry picked from commit 1b4f12e1fdd602cd521b82754c907d630e6a0a67)
Reviewed-on: https://gerrit.iotivity.org/gerrit/12121

resource/provisioning/examples/provisioningclient.cpp

index b5b43d2..82a27be 100644 (file)
@@ -69,6 +69,7 @@ using namespace OC;
 DeviceList_t pUnownedDevList, pOwnedDevList;
 static int transferDevIdx, ask = 1;
 static OicSecPconf_t g_pconf;
+static uint16_t g_credId = 0;
 
 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
 {
@@ -91,7 +92,11 @@ void printMenu()
     std::cout << "  10. Get Linked Devices"<<std::endl;
     std::cout << "  11. Get Device Status"<<std::endl;
     std::cout << "  12. Provision Direct-Pairing Configuration"<<std::endl;
-    std::cout << "  13. Exit loop"<<std::endl;
+#if defined(__WITH_X509__) || defined(__WITH_TLS__)
+    std::cout << "  13. Save the Trust Cert. Chain into Cred of SVR"<<std::endl;
+    std::cout << "  14. Provision the Trust Cert. Chain"<<std::endl;
+#endif // __WITH_X509__ || __WITH_TLS__
+    std::cout << "  99. Exit loop"<<std::endl;
 }
 
 void moveTransferredDevice()
@@ -796,6 +801,49 @@ PVDP_ERROR:
     ask = 1;
 }
 
+#ifdef __WITH_TLS__
+static int saveTrustCert(void)
+{
+
+    // call |OCSaveTrustCertChainBin| API actually
+    printf("   Save Trust Cert. Chain into Cred of SVR.\n");
+
+    ByteArray trustCertChainArray = {0, 0};
+
+    FILE *fp = fopen("rootca.crt", "rb+");
+
+    if (fp)
+    {
+        size_t fsize;
+        if (fseeko(fp, 0, SEEK_END) == 0 && (fsize = ftello(fp)) >= 0)
+        {
+            trustCertChainArray.data = (uint8_t*)OICMalloc(fsize);
+            trustCertChainArray.len = fsize;
+            if (NULL == trustCertChainArray.data)
+            {
+                OIC_LOG(ERROR,TAG,"malloc");
+                fclose(fp);
+                return -1;
+            }
+            rewind(fp);
+            fsize = fread(trustCertChainArray.data, 1, fsize, fp);
+            fclose(fp);
+        }
+    }
+    OIC_LOG_BUFFER(DEBUG, TAG, trustCertChainArray.data, trustCertChainArray.len);
+
+    if(OC_STACK_OK != OCSecure::saveTrustCertChain(trustCertChainArray.data, trustCertChainArray.len,
+                        OIC_ENCODING_PEM,&g_credId))
+    {
+        OIC_LOG(ERROR, TAG, "OCSaveTrustCertChainBin API error");
+        return -1;
+    }
+    printf("CredId of Saved Trust Cert. Chain into Cred of SVR : %d.\n", g_credId);
+
+    return 0;
+}
+#endif //__WITH_TLS__
+
 int main(void)
 {
     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
@@ -1232,7 +1280,36 @@ int main(void)
 
                         break;
                     }
+#if defined(__WITH_X509__) || defined(__WITH_TLS__)
                 case 13:
+                    {
+                        if(saveTrustCert())
+                        {
+                            std::cout<<"Error in saving cert"<<std::endl;
+                        }
+                        break;
+                    }
+                case 14:
+                    {
+                        int index;
+
+                        if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
+
+                        std::cout << "Provision cert for : "<<
+                            pOwnedDevList[index]->getDeviceID()<< std::endl;
+
+                        ask = 0;
+
+                        if (pOwnedDevList[index]->provisionTrustCertChain(SIGNED_ASYMMETRIC_KEY,
+                                                                    g_credId,provisionCB ) != OC_STACK_OK)
+                        {
+                            ask = 1;
+                            std::cout <<"provision cert is failed"<< std::endl;
+                        }
+                        break;
+                    }
+#endif //__WITH_X509__ || __WITH_TLS__
+                case 99:
                 default:
                     out = 1;
                     break;