replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / cloud / csr.c
index f95328d..048e0f5 100644 (file)
@@ -1,3 +1,22 @@
+/* *****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * *****************************************************************/
 #include "utils.h"
 
 #include "logger.h"
 #include <unistd.h>
 #include <fcntl.h>
 
-#define TAG "CLOUD-CSR"
+#define TAG "OIC_CLOUD_CSR"
 
 //TODO: is it required in CSR response?
-static OCByteString privateKey = {0, 0};
+static OCByteString g_privateKey = {0, 0};
 
 #define MAX_URI_QUERY MAX_URI_LENGTH + MAX_QUERY_LENGTH
 
@@ -280,15 +299,15 @@ static int GenerateCSR(char *subject, OCByteString *csr)
         OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
         return -1;
     }
-    privateKey.bytes = (uint8_t *)OICMalloc(ret * sizeof(char));
-    if (NULL == privateKey.bytes)
+    g_privateKey.bytes = (uint8_t *)OICMalloc(ret * sizeof(char));
+    if (NULL == g_privateKey.bytes)
     {
-        OIC_LOG(ERROR, TAG, "OICMalloc returned NULL on privateKey.bytes allocation");
+        OIC_LOG(ERROR, TAG, "OICMalloc returned NULL on g_privateKey.bytes allocation");
         OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
         return -1;
     }
-    memcpy(privateKey.bytes, buf + bufsize - ret, ret * sizeof(uint8_t));
-    privateKey.len = ret;
+    memcpy(g_privateKey.bytes, buf + bufsize - ret, ret * sizeof(uint8_t));
+    g_privateKey.len = ret;
     // Public key to output
     ret = mbedtls_pk_write_pubkey_der(key, buf, bufsize);
     if (ret < 0)
@@ -369,22 +388,16 @@ static OCStackResult HandleCertificateIssueRequest(void *ctx, void **data, OCCli
     {
         OicSecKey_t key =
         {
-            privateKey.bytes,
-            privateKey.len,
+            g_privateKey.bytes,
+            g_privateKey.len,
             OIC_ENCODING_DER
         };
 
-        OicSecCert_t cert1 =
-        {
-            cert.data,
-            cert.len,
-        };
-
         uint16_t credId;
-        result = SRPSaveOwnCertChain(&cert1, &key, &credId);
+        result = SRPSaveOwnCertChain(&cert, &key, &credId);
         if (result != OC_STACK_OK)
         {
-            OIC_LOG(ERROR, TAG, "Cann't add cert");
+            OIC_LOG(ERROR, TAG, "Can't add cert");
         }
     }
 
@@ -393,7 +406,7 @@ static OCStackResult HandleCertificateIssueRequest(void *ctx, void **data, OCCli
     if (!OCRepPayloadGetPropPubDataType((OCRepPayload *)response->payload,
                                    OC_RSRVD_CACERT, &caCert))
     {
-        OIC_LOG_V(ERROR, TAG, "Cann't get: %s", OC_RSRVD_CACERT);
+        OIC_LOG_V(ERROR, TAG, "Can't get: %s", OC_RSRVD_CACERT);
         result = OC_STACK_ERROR;
     }
     else
@@ -406,9 +419,10 @@ static OCStackResult HandleCertificateIssueRequest(void *ctx, void **data, OCCli
         }
     }
 
-    OICFree(privateKey.bytes);
-    privateKey.bytes = NULL;
-    privateKey.len   = 0;
+    OICClearMemory(g_privateKey.bytes, g_privateKey.len);
+    OICFree(g_privateKey.bytes);
+    g_privateKey.bytes = NULL;
+    g_privateKey.len   = 0;
 
     OIC_LOG_V(DEBUG, TAG, "OUT: %s", __func__);
 
@@ -442,7 +456,7 @@ OCStackResult OCCloudCertificateIssueRequest(void* ctx,
 
     OIC_LOG_V(DEBUG, TAG, "Certificate Request subject: %s", subject);
 
-    OCByteString request;
+    OCByteString request = {0};
     if (0 != GenerateCSR(subject, &request))
     {
         OIC_LOG(ERROR, TAG, "Cann't get the sertificate request");
@@ -453,7 +467,7 @@ OCStackResult OCCloudCertificateIssueRequest(void* ctx,
     OIC_LOG_BUFFER(DEBUG, TAG, request.bytes, request.len);
 
     OIC_LOG(DEBUG, TAG, "Private Key:");
-    OIC_LOG_BUFFER(DEBUG, TAG, privateKey.bytes, privateKey.len);
+    OIC_LOG_BUFFER(DEBUG, TAG, g_privateKey.bytes, g_privateKey.len);
 
     OCRepPayload* payload = OCRepPayloadCreate();
     if (!payload)
@@ -475,6 +489,7 @@ OCStackResult OCCloudCertificateIssueRequest(void* ctx,
     snprintf(uri, MAX_URI_QUERY, DEFAULT_QUERY,
              endPoint->addr, endPoint->port,
              OC_RSRVD_PROV_CERT_URI);
+    OIC_LOG_V(DEBUG, TAG, "Certificate Request Query: %s", uri);
 
     OCCallbackData cbData;
     fillCallbackData(&cbData, ctx, callback, HandleCertificateIssueRequest, NULL);