Introduce certsvc_pkcs12_import_from_file_to_store_ret_list()
[platform/core/security/cert-svc.git] / src / vcore / pkcs12.cpp
index 5985005..3e0cc70 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd 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.
@@ -27,6 +27,7 @@
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <limits.h>
 #include <string>
 #include <memory>
 #include <functional>
@@ -97,6 +98,73 @@ inline CertStoreType nextStore(CertStoreType type)
        }
 }
 
+CertSvcStoreCertList *createStoreListNode(const std::string &gname, const std::string &title,
+                                                                                 CertStoreType storeType)
+{
+       CertSvcStoreCertList *node = (CertSvcStoreCertList *)malloc(sizeof(CertSvcStoreCertList));
+
+       if (node == NULL)
+               return NULL;
+
+       node->gname = strdup(gname.c_str());
+       node->title = strdup(title.c_str());
+       node->status = ENABLED;
+       node->storeType = storeType;
+       node->next = NULL;
+
+       if (node->gname == NULL || node->title == NULL) {
+               free(node->gname);
+               free(node->title);
+               free(node);
+               return NULL;
+       }
+
+       return node;
+}
+
+void destroyStoreList(CertSvcStoreCertList **certList, size_t *length)
+{
+       if (certList == NULL || length == NULL) {
+               return;
+       }
+
+       CertSvcStoreCertList *list = *certList;
+
+       while (list) {
+               CertSvcStoreCertList *next = list->next;
+               free(list->gname);
+               free(list->title);
+               free(list);
+               list = next;
+       }
+
+       *length = 0;
+}
+
+void addStoreListNode(CertSvcStoreCertList **list, CertSvcStoreCertList *node)
+{
+       node->next = *list;
+       *list = node;
+}
+
+int appendStoreListNode(CertSvcStoreCertList **certList, size_t *length,
+                                                const std::string &gname, const std::string &alias,
+                                                CertStoreType storeType)
+{
+       if (certList == NULL || length == NULL)
+               return CERTSVC_SUCCESS;
+
+       CertSvcStoreCertList *node = createStoreListNode(gname, alias, storeType);
+       if (node == NULL) {
+               return CERTSVC_BAD_ALLOC;
+       }
+
+       addStoreListNode(certList, node);
+       (*length)++;
+
+       return CERTSVC_SUCCESS;
+}
+
 std::string generateGname(void)
 {
        int generator;
@@ -243,13 +311,13 @@ int installChainCert(CertStoreType storeType,
 }
 int installCert(CertStoreType storeType,
                                const std::string &cert,
-                               const std::string &gname)
+                               const std::string &gname,
+                               const std::string &alias)
 {
-       std::string commonName = getCommonName(PEM_CRT, cert);
        return vcore_client_install_certificate_to_store(
                           storeType,
                           gname.c_str(),
-                          commonName.c_str(),
+                          alias.c_str(),
                           NULL,
                           NULL,
                           cert.c_str(),
@@ -270,8 +338,8 @@ std::string readFromFile(const std::string &path)
        fseek(fp, 0L, SEEK_END);
        int len = ftell(fp);
 
-       if (len <= 0) {
-               LogError("Fail to get certificate length.");
+       if (len <= 0 || len == INT_MAX) {
+               LogError("Fail to get proper certificate.");
                return std::string();
        }
 
@@ -609,7 +677,9 @@ int insertToStore(CertStoreType storeTypes,
                                  const std::string &endCertName,
                                  const std::string &endCertBuffer,
                                  const std::vector<std::string> &certChainName,
-                                 const std::vector<std::string> &certChainBuffer)
+                                 const std::vector<std::string> &certChainBuffer,
+                                 CertSvcStoreCertList **certList,
+                                 size_t *length)
 {
        size_t ncerts = certChainName.size();
 
@@ -633,18 +703,30 @@ int insertToStore(CertStoreType storeTypes,
                        return result;
                }
 
+               int res = appendStoreListNode(certList, length, endCertName, alias, storeType);
+               if (res != CERTSVC_SUCCESS) {
+                       LogError("Failed to append store list node.");
+                       return result;
+               }
+
                for (size_t i = 0; i < ncerts; i++) {
                        if (i == ncerts - 1)
                                result = installChainCert(storeType, certChainBuffer[i], certChainName[i], endCertName,
-                                                                                 P12_INTERMEDIATE);
+                                                                                 P12_TRUSTED);
                        else
                                result = installChainCert(storeType, certChainBuffer[i], certChainName[i], endCertName,
-                                                                                 P12_TRUSTED);
+                                                                                 P12_INTERMEDIATE);
 
                        if (result != CERTSVC_SUCCESS) {
                                LogError("Failed to install the ca certificates. result : " << result);
                                return result;
                        }
+
+                       int res = appendStoreListNode(certList, length, certChainName[i], alias, storeType);
+                       if (res != CERTSVC_SUCCESS) {
+                               LogError("Failed to append store list node.");
+                               return result;
+                       }
                }
        }
 
@@ -652,7 +734,8 @@ int insertToStore(CertStoreType storeTypes,
        return CERTSVC_SUCCESS;
 }
 
-int insertToStorePEM(CertStoreType storeTypes, const std::string &path, const std::string &gname)
+int insertToStorePEM(CertStoreType storeTypes, const std::string &path, const std::string &gname,
+                                        const std::string &alias, CertSvcStoreCertList **certList, size_t *length)
 {
        std::string content = readFromFile(path);
 
@@ -673,7 +756,7 @@ int insertToStorePEM(CertStoreType storeTypes, const std::string &path, const st
                if (!hasStore(storeTypes, storeType))
                        continue;
 
-               int result = installCert(storeType, parsed, gname);
+               int result = installCert(storeType, parsed, gname, alias);
 
                if (result != CERTSVC_SUCCESS) {
                        LogError("Failed to install PEM/CRT to db store : " << storeType << " result : " << result);
@@ -681,6 +764,13 @@ int insertToStorePEM(CertStoreType storeTypes, const std::string &path, const st
                        return result;
                }
 
+               int res = appendStoreListNode(certList, length, gname, alias, storeType);
+               if (res != CERTSVC_SUCCESS) {
+                       rollbackStore(storeTypes, gname);
+                       LogError("Failed to append store list node.");
+                       return result;
+               }
+
                LogDebug("Success to install PEM/CRT to db store : " << storeType);
        }
 
@@ -694,7 +784,9 @@ int insertToStorePEM(CertStoreType storeTypes, const std::string &path, const st
 int pkcs12_import_from_file_to_store(CertStoreType storeTypes,
                                                                         const char *_path,
                                                                         const char *_password,
-                                                                        const char *_alias)
+                                                                        const char *_alias,
+                                                                        CertSvcStoreCertList **certList,
+                                                                        size_t *length)
 {
        int result = 0;
 
@@ -729,10 +821,12 @@ int pkcs12_import_from_file_to_store(CertStoreType storeTypes,
 
        if (strcasecmp(suffix.c_str(), ".pem") == 0 || strcasecmp(suffix.c_str(), ".crt") == 0) {
                std::string gnamePEM = generateGname();
-               result = insertToStorePEM(storeTypes, path, gnamePEM);
+               result = insertToStorePEM(storeTypes, path, gnamePEM, alias, certList, length);
 
-               if (result != CERTSVC_SUCCESS)
+               if (result != CERTSVC_SUCCESS) {
+                       destroyStoreList(certList, length);
                        LogError("Failed to install PEM/CRT file to store. gname : " << gnamePEM << " result : " << result);
+               }
 
                return result;;
        }
@@ -804,10 +898,14 @@ int pkcs12_import_from_file_to_store(CertStoreType storeTypes,
                                                   endCertName,
                                                   endCertBuffer,
                                                   certChainName,
-                                                  certChainBuffer);
+                                                  certChainBuffer,
+                                                  certList,
+                                                  length);
 
-       if (result != CERTSVC_SUCCESS)
+       if (result != CERTSVC_SUCCESS) {
+               destroyStoreList(certList, length);
                rollbackStore(storeTypes, endCertName);
+       }
 
        LogDebug("Success to import pkcs12 to store");
        return result;