/**
- * 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.
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <limits.h>
#include <string>
#include <memory>
#include <functional>
}
}
+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;
}
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(),
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();
}
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();
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;
+ }
}
}
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);
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);
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);
}
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;
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;;
}
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;