Applied sizeof operator on fixed sized array to calculate size.
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_CertDbManager.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 9df2375..a49bb1e
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
 using namespace Tizen::Base;
 using namespace Tizen::Io;
 
+namespace
+{
+struct ByteDeleter
+{
+       void operator ()(byte* c)
+       {
+               free(c);
+       }
+};
+}
+
 namespace Tizen { namespace Security { namespace Cert
 {
 _CertDbManager* _CertDbManager::__pCertDb = null;
@@ -85,7 +95,7 @@ _CertDbManager::CreateCertificateTables(void)
        result r = E_SUCCESS;
 
        r = __caCertDbStore.CreateCertificateTables();
-       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM,  "Failed to create certificate tables in database.");
+       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to create certificate tables in database.");
 
        return E_SUCCESS;
 }
@@ -96,14 +106,14 @@ _CertDbManager::IsCertificateTablesCreated(void)
        result r = E_SUCCESS;
 
        r = __caCertDbStore.IsRootCaCertTableCreated();
-       if( r != E_SUCCESS )
+       if (r != E_SUCCESS)
        {
                SetLastResult(E_SYSTEM);
                return false;
        }
 
        r = __userCertDbStore.IsUserCertTableCreated();
-       if( r != E_SUCCESS )
+       if (r != E_SUCCESS)
        {
                SetLastResult(E_SYSTEM);
                return false;
@@ -131,7 +141,7 @@ _CertDbManager::RemoveCertificateTables(void)
 {
        result r = E_SUCCESS;
        r = __caCertDbStore.DropCertificateTables();
-       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM,  "Failed to delete all the certificate tables in database.");
+       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to delete all the certificate tables in database.");
        return E_SUCCESS;
 }
 
@@ -142,7 +152,7 @@ _CertDbManager::RemoveCaCertificateByType(_CaCertType certType)
        char condition[_MAX_TYPE_CONST_SIZE] = {0, };
        char installed[_MAX_TYPE_RECORD_SIZE] = "T\0";
 
-       sprintf(condition, "certType = %d and installed = '%s'", certType, installed);
+       snprintf(condition, sizeof(condition), "certType = %d and installed = '%s'", certType, installed);
 
        r = __caCertDbStore.RemoveAllCertificateByCondition(reinterpret_cast< byte* >(condition));
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to delete all the certificate tables in database.");
@@ -157,7 +167,7 @@ _CertDbManager::RemoveUserCaCertificateByCertId(int certId)
        char condition[_MAX_TYPE_CONST_SIZE] = {0, };
        char installed[_MAX_TYPE_RECORD_SIZE] = "T\0";
 
-       sprintf(condition, "certId = %d and certType = %d and installed = '%s'", certId, _CERT_TYPE_ROOT_CA_BY_USER, installed);
+       snprintf(condition, sizeof(condition), "certId = %d and certType = %d and installed = '%s'", certId, _CERT_TYPE_ROOT_CA_BY_USER, installed);
        r = __caCertDbStore.RemoveAllCertificateByCondition(reinterpret_cast< byte* >(condition));
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to delete all the certificate tables in database.");
 
@@ -195,11 +205,11 @@ _CertDbManager::InsertCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        r = __caCertDbStore.IsRootCaCertTableCreated();
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Root certificate tables are not created in database.");
 
-       std::unique_ptr <_X509Certificate> pCert (new (std::nothrow) _X509Certificate());
+       std::unique_ptr< _X509Certificate > pCert(new (std::nothrow) _X509Certificate());
        SysTryReturnResult(NID_SEC_CERT, pCert != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        certBufFormat = _CertManager::GetEncodedCertBuffer(pCertBuf, certLen, &pDerCert, &derCertBufferLength, &encodingType);
-       std::unique_ptr <byte[]> pDerCertBuffer(pDerCert);
+       std::unique_ptr< byte, ByteDeleter > pDerCertBuffer(pDerCert);
        pDerCert = null;
        SysTryReturnResult(NID_SEC_CERT, pDerCertBuffer != null, E_INVALID_CONDITION, "Input certificate buffer.");
        SysTryReturnResult(NID_SEC_CERT, certBufFormat == _CERT_X509, E_INVALID_CONDITION, "Unsupported certificate format.");
@@ -209,16 +219,16 @@ _CertDbManager::InsertCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_DECODING_FAILED, "Decoding failed.");
 
        pTbsCert = pCert->GetTbsCertInstance();
-       SysTryReturnResult(NID_SEC_CERT, pTbsCert != null,E_SYSTEM, "Failed to get certificate to be signed instance.");
+       SysTryReturnResult(NID_SEC_CERT, pTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
        lenSubjectName = strlen(reinterpret_cast< const char* >(pTbsCert->GetSubjectName()));
        lenIssuerName = strlen(reinterpret_cast< const char* >(pTbsCert->GetIssuerName()));
 
-       SysTryReturnResult(NID_SEC_CERT, lenSubjectName < _MAX_ISSUER_SUBJECT_NAME_SIZE,E_SYSTEM, "Subject name is more then maximum specified length.");
+       SysTryReturnResult(NID_SEC_CERT, lenSubjectName < _MAX_ISSUER_SUBJECT_NAME_SIZE, E_SYSTEM, "Subject name is more then maximum specified length.");
        SysTryReturnResult(NID_SEC_CERT, lenIssuerName < _MAX_ISSUER_SUBJECT_NAME_SIZE, E_SYSTEM, "Subject name is more then maximum specified length.");
 
-       strcpy(subjectName, reinterpret_cast< const char* >(pTbsCert->GetSubjectName()));
-       strcpy(issuerName, reinterpret_cast< const char* >(pTbsCert->GetIssuerName()));
+       snprintf(subjectName, sizeof(subjectName), "%s", reinterpret_cast< const char* >(pTbsCert->GetSubjectName()));
+       snprintf(issuerName, sizeof(issuerName), "%s", reinterpret_cast< const char* >(pTbsCert->GetIssuerName()));
 
        pTbsCert->GetSerialNumber(pSerial, reinterpret_cast< int& >(lenSerialNo));
        if ((lenSerialNo <= 0) || (lenSerialNo > _MAX_SERIAL_NUMBER_SIZE))
@@ -242,7 +252,7 @@ _CertDbManager::InsertCaCertificateFromBuffer(_CaCertType certType, _CertFormat
                //Open this code - if u want to support installation of Intermediate CA Certificate with verification using this API.(ideally it should check if installing intermediate CA) (09082011)
                else if (pCert->IsCaCertificate())
                {
-                       std::unique_ptr <_CertChain> pCertChain (new (std::nothrow) _CertChain());
+                       std::unique_ptr< _CertChain > pCertChain(new (std::nothrow) _CertChain());
                        SysTryReturnResult(NID_SEC_CERT, pCertChain != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                        r = pCertChain->AddCertificate(certFormat, pDerCertBuffer.get(), derCertBufferLength);
@@ -270,10 +280,14 @@ _CertDbManager::InsertCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        }
 
        r = __caCertDbStore.CheckDuplicateCertificate(certType, reinterpret_cast< byte* >(subjectName), lenSubjectName);
-       if(r == E_SUCCESS)
+       if (!IsFailed(r))
        {
                return E_FILE_ALREADY_EXIST;
        }
+
+       SysTryReturnResult(NID_SEC_CERT, r == E_DATA_NOT_FOUND, r, "Failed to check duplicate");
+
+
        //Get the last installed certificate id from db table
        __caCertDbStore.GetCurrentCertId(certId);
        //Calculate the new (std::nothrow) certificate id for installation
@@ -284,10 +298,11 @@ _CertDbManager::InsertCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        certRecord.certType = static_cast< int >(certType);
        certRecord.certFormat = static_cast< int >(certFormat);
 
-       std::unique_ptr <char[]> pFileName(Tizen::Base::_StringConverter::CopyToCharArrayN(tempFileName));
+       std::unique_ptr< char[] > pFileName(Tizen::Base::_StringConverter::CopyToCharArrayN(tempFileName));
        SysTryReturnResult(NID_SEC_CERT, pFileName != null, E_SYSTEM, "Failed to get file attributes.");
 
-       strcpy(certRecord.fileName, pFileName.get());
+       int len = strlen(pFileName.get());
+       snprintf(certRecord.fileName, sizeof(certRecord.fileName), "%s", pFileName.get());
 
        certRecord.subjectNameLen = lenSubjectName;
        memcpy(certRecord.subjectName, subjectName, lenSubjectName);
@@ -338,7 +353,7 @@ _CertDbManager::UpdateCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        r = __caCertDbStore.IsRootCaCertTableCreated();
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Root certificate tables are not created in database.");
 
-       std::unique_ptr<_X509Certificate> pCert (new (std::nothrow) _X509Certificate());
+       std::unique_ptr< _X509Certificate > pCert(new (std::nothrow) _X509Certificate());
        SysTryReturnResult(NID_SEC_CERT, pCert != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        r = pCert->Parse(pCurCertBuf, curCertLen);
@@ -364,7 +379,7 @@ _CertDbManager::UpdateCaCertificateFromBuffer(_CaCertType certType, _CertFormat
                r = _Base64::Encode(pTbsCert->GetSubjectName(), lenSubjectName, subjectNameBase64, subjNameB64len);
                SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
 
-               sprintf(condition, "subjectName = '%s' and certType = %d and installed = '%s'", subjectNameBase64, certType, installed);
+               snprintf(condition, sizeof(condition), "subjectName = '%s' and certType = %d and installed = '%s'", subjectNameBase64, certType, installed);
                r = __caCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
                SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificates record.", GetErrorMessage(r));
 
@@ -375,7 +390,7 @@ _CertDbManager::UpdateCaCertificateFromBuffer(_CaCertType certType, _CertFormat
 
                if (certFormat == _CERT_X509)
                {
-                       std::unique_ptr <_X509Certificate> pNewCert(new (std::nothrow) _X509Certificate());
+                       std::unique_ptr< _X509Certificate > pNewCert(new (std::nothrow) _X509Certificate());
                        SysTryReturnResult(NID_SEC_CERT, pNewCert != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                        r = pNewCert->Parse(pNewCertBuf, newCertLen);
@@ -384,8 +399,8 @@ _CertDbManager::UpdateCaCertificateFromBuffer(_CaCertType certType, _CertFormat
                        pNewTbsCert = pNewCert->GetTbsCertInstance();
                        SysTryReturnResult(NID_SEC_CERT, pNewTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
-                       strcpy(newSubjectName, reinterpret_cast< const char* >(pNewTbsCert->GetSubjectName()));
-                       strcpy(newIssuerName, reinterpret_cast< const char* >((pNewTbsCert->GetIssuerName())));
+                       snprintf(newSubjectName, sizeof(newSubjectName), "%s", reinterpret_cast< const char* >(pNewTbsCert->GetSubjectName()));
+                       snprintf(newIssuerName, sizeof(newIssuerName), "%s", reinterpret_cast< const char* >(pNewTbsCert->GetIssuerName()));
 
                        lenNewSubjectName = strlen(newSubjectName);
                        lenNewIssuerName = strlen(newIssuerName);
@@ -408,10 +423,11 @@ _CertDbManager::UpdateCaCertificateFromBuffer(_CaCertType certType, _CertFormat
                certRecord1.certType = static_cast< int >(certType);
                certRecord1.certFormat = static_cast< int >(certFormat);
 
-               std::unique_ptr <char[]> pFileName(Tizen::Base::_StringConverter::CopyToCharArrayN(tempFileName));
+               std::unique_ptr< char[] > pFileName(Tizen::Base::_StringConverter::CopyToCharArrayN(tempFileName));
                SysTryReturnResult(NID_SEC_CERT, pFileName != null, E_OPERATION_FAILED, "Failed to get file name.");
 
-               strcpy(certRecord1.fileName, pFileName.get());
+               int len = strlen(pFileName.get());
+               snprintf(certRecord1.fileName, sizeof(certRecord1.fileName), "%s", pFileName.get());
 
                certRecord1.subjectNameLen = lenNewSubjectName;
                memcpy(certRecord1.subjectName, newSubjectName, lenNewSubjectName);
@@ -432,10 +448,6 @@ _CertDbManager::UpdateCaCertificateFromBuffer(_CaCertType certType, _CertFormat
                //No need to update record as only file data changed.
                return E_SUCCESS;
        }
-       else
-       {
-               return E_FILE_ALREADY_EXIST;
-       }
 
        return r;
 }
@@ -454,7 +466,7 @@ _CertDbManager::RemoveCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        r = __caCertDbStore.IsRootCaCertTableCreated();
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_OBJ_NOT_FOUND, "No root certificate tables are create in databased.");
 
-       std::unique_ptr <_X509Certificate> pCert (new (std::nothrow) _X509Certificate());
+       std::unique_ptr< _X509Certificate > pCert(new (std::nothrow) _X509Certificate());
        SysTryReturnResult(NID_SEC_CERT, pCert != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        r = pCert->Parse(pCertBuf, certLen);
@@ -464,11 +476,12 @@ _CertDbManager::RemoveCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        SysTryReturnResult(NID_SEC_CERT, pTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
        r = __caCertDbStore.CheckDuplicateCertificate(certType, pTbsCert->GetSubjectName(), strlen(reinterpret_cast< char* >(pTbsCert->GetSubjectName())));
-       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_OBJ_NOT_FOUND, "Certificate not found in db.");
+       SysTryReturnResult(NID_SEC_CERT, r != E_DATA_NOT_FOUND, E_OBJ_NOT_FOUND, "Certificate not found in db.");
+       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), r, "Propagated.");
 
        r = GetCaCertificateId(pTbsCert->GetSubjectName(), strlen(reinterpret_cast< char* >(pTbsCert->GetSubjectName())),
-                                                 pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
-                                                 certId, certType);
+                                                  pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
+                                                  certId, certType);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed get ca certificate id.", GetErrorMessage(r));
 
 
@@ -493,7 +506,7 @@ _CertDbManager::RemoveCertificateChainByCertId(int certId)
        memset(&userCertRecord, 0, sizeof(userCertRecord));
        memset(condition, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE + _MAX_SUBJECT_OFFSET_SIZE);
 
-       sprintf(condition, "certId = %d", certId);
+       snprintf(condition, sizeof(condition), "certId = %d", certId);
        r = __userCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &userCertRecord);
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r) || r == E_DATA_NOT_FOUND, E_SYSTEM, "Failed to get certificate record.");
        SysTryReturnResult(NID_SEC_CERT, r != E_DATA_NOT_FOUND, E_SUCCESS, "No such record found.");
@@ -538,22 +551,22 @@ _CertDbManager::GetCaCertificateId(byte* pSubjectName, int subjectNameSize, byte
 
                if (certType == _CERT_TYPE_NOT_BOUNDED)
                {
-                       sprintf(condition, "subjectName = '%s' and issuerName = '%s' and installed = '%s'", subjectNameBase64, issuerNameBase64, installed);
+                       snprintf(condition, sizeof(condition), "subjectName = '%s' and issuerName = '%s' and installed = '%s'", subjectNameBase64, issuerNameBase64, installed);
                }
                else
                {
-                       sprintf(condition, "subjectName = '%s' and issuerName = '%s' and certType = %d and installed = '%s'", subjectNameBase64, issuerNameBase64, certType, installed);
+                       snprintf(condition, sizeof(condition), "subjectName = '%s' and issuerName = '%s' and certType = %d and installed = '%s'", subjectNameBase64, issuerNameBase64, certType, installed);
                }
        }
        else
        {
                if (certType == _CERT_TYPE_NOT_BOUNDED)
                {
-                       sprintf(condition, "subjectName = '%s' and installed = '%s'", subjectNameBase64, installed);
+                       snprintf(condition, sizeof(condition), "subjectName = '%s' and installed = '%s'", subjectNameBase64, installed);
                }
                else
                {
-                       sprintf(condition, "subjectName = '%s' and certType = %d and installed = '%s'", subjectNameBase64, certType, installed);
+                       snprintf(condition, sizeof(condition), "subjectName = '%s' and certType = %d and installed = '%s'", subjectNameBase64, certType, installed);
                }
        }
 
@@ -596,11 +609,11 @@ _CertDbManager::GetUserCertificateId(byte* pSubjectName, int subjectNameSize, by
                r = _Base64::Encode(reinterpret_cast< byte* >(pIssuerName), issuerNameSize, issuerNameBase64, issuerB64len);
                SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
 
-               sprintf(condition, "subjectName = '%s' and issuerName = '%s' and installed = '%s'", subjectNameBase64, issuerNameBase64, installed);
+               snprintf(condition, sizeof(condition), "subjectName = '%s' and issuerName = '%s' and installed = '%s'", subjectNameBase64, issuerNameBase64, installed);
        }
        else
        {
-               sprintf(condition, "subjectName = '%s' and installed = '%s'", subjectNameBase64, installed);
+               snprintf(condition, sizeof(condition), "subjectName = '%s' and installed = '%s'", subjectNameBase64, installed);
        }
 
        r = __userCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &userCertRecord);
@@ -636,7 +649,8 @@ _CertDbManager::DeleteCertificateChain(int devCertId, int devParentCA)
 
        memset(condition, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE + _MAX_SUBJECT_OFFSET_SIZE);
 
-       sprintf(condition, "parentCa = %d and installed = '%s'", devParentCA, installed);
+       snprintf(condition, sizeof(condition), "parentCa = %d and installed = '%s'", devParentCA, installed);
+
        //Check if any other device certificate has same parent as of referred device certificare. If it is yes then we
        //delete only device certificate and return. We cannot disturb another chain.
        __userCertDbStore.GetCountByCondition(reinterpret_cast< byte* >(&condition), recCount);
@@ -692,7 +706,7 @@ _CertDbManager::DeleteCertificateChain(int devCertId, int devParentCA)
                        }
                        caCertId = caParentCa; // Now look for next certificate in chain
                        memset(condition, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE + _MAX_SUBJECT_OFFSET_SIZE);
-                       sprintf(condition, "certId = %d and installed = '%s'", devParentCA, installed);
+                       snprintf(condition, sizeof(condition), "certId = %d and installed = '%s'", devParentCA, installed);
                        memset(&certRecord, 0, sizeof(certRecord));
                        r = __caCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
                        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r) || r == E_DATA_NOT_FOUND, E_SYSTEM, "Failed to get certificate record.");
@@ -718,13 +732,13 @@ _CertDbManager::GetCertificateListByFormat(_CertFormat certFormat, _CertificateL
        char installed[_MAX_TYPE_RECORD_SIZE] = "T\0";
        char condition[_MAX_TYPE_CONST_SIZE] = {0, };
 
-       sprintf(condition, "certFormat = %d and certType != %d and installed = '%s'", certFormat, _CERT_TYPE_INTERMIDIATE_CA, installed);
+       snprintf(condition, sizeof(condition), "certFormat = %d and certType != %d and installed = '%s'", certFormat, _CERT_TYPE_INTERMIDIATE_CA, installed);
 
        r = __caCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r) || r == E_DATA_NOT_FOUND, E_SYSTEM, "Failed to get certificate record.");
        SysTryReturnResult(NID_SEC_CERT, r != E_DATA_NOT_FOUND, E_SUCCESS, "No such record found.");
 
-       std::unique_ptr <_CertificateListInfo> pCertList (new (std::nothrow) _CertificateListInfo);
+       std::unique_ptr< _CertificateListInfo > pCertList(new (std::nothrow) _CertificateListInfo);
        SysTryReturnResult(NID_SEC_CERT, pCertList != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        memset(pCertList.get(), 0, sizeof(*pCertList.get()));
@@ -746,7 +760,7 @@ _CertDbManager::GetCertificateListByFormat(_CertFormat certFormat, _CertificateL
 
        while (__caCertDbStore.GetNextRecordByCondition(reinterpret_cast< byte* >(condition), &certRecord, certRecord.certId) == E_SUCCESS)
        {
-               std::unique_ptr <_CertificateListInfo> pCertList (new (std::nothrow) _CertificateListInfo);
+               std::unique_ptr< _CertificateListInfo > pCertList(new (std::nothrow) _CertificateListInfo);
                SysTryReturnResult(NID_SEC_CERT, pCertList != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                memset(pCertList.get(), 0, sizeof(*pCertList.get()));
@@ -789,13 +803,13 @@ _CertDbManager::GetUserCertificateListByFormat(_CertFormat certFormat, _Certific
 
        *ppCertList = null;
 
-       sprintf(condition, "certFormat = %d and installed = '%s'", certFormat, installed);
+       snprintf(condition, sizeof(condition), "certFormat = %d and installed = '%s'", certFormat, installed);
 
        r = __userCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r) || r == E_DATA_NOT_FOUND, E_SYSTEM, "Failed to get certificate record.");
        SysTryReturnResult(NID_SEC_CERT, r != E_DATA_NOT_FOUND, E_SUCCESS, "No such record found.");
 
-       std::unique_ptr<_CertificateListInfo> pCertList (new (std::nothrow) _CertificateListInfo);
+       std::unique_ptr< _CertificateListInfo > pCertList(new (std::nothrow) _CertificateListInfo);
        SysTryReturnResult(NID_SEC_CERT, pCertList != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        memset(pCertList.get(), 0, sizeof(*pCertList.get()));
@@ -818,7 +832,7 @@ _CertDbManager::GetUserCertificateListByFormat(_CertFormat certFormat, _Certific
 
        while (__userCertDbStore.GetNextRecordByCondition(reinterpret_cast< byte* >(condition), &certRecord, certRecord.certId) == E_SUCCESS)
        {
-               std::unique_ptr<_CertificateListInfo> pCertList (new (std::nothrow) _CertificateListInfo);
+               std::unique_ptr< _CertificateListInfo > pCertList(new (std::nothrow) _CertificateListInfo);
                SysTryReturnResult(NID_SEC_CERT, pCertList != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                memset(pCertList.get(), 0, sizeof(*pCertList.get()));
@@ -857,12 +871,12 @@ _CertDbManager::GetCaCertificateListByCertId(int certId, _CertificateListInfo**
        char condition[_MAX_TYPE_CONST_SIZE] = {0, };
 
        *ppCertList = null;
-       sprintf(condition, "certId = %d and certType != %d and installed = '%s'", certId, _CERT_TYPE_INTERMIDIATE_CA, installed);
+       snprintf(condition, sizeof(condition), "certId = %d and certType != %d and installed = '%s'", certId, _CERT_TYPE_INTERMIDIATE_CA, installed);
 
        r = __caCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificate record.", GetErrorMessage(r));
 
-       std::unique_ptr <_CertificateListInfo> pCertList (new (std::nothrow) _CertificateListInfo);
+       std::unique_ptr< _CertificateListInfo > pCertList(new (std::nothrow) _CertificateListInfo);
        SysTryReturnResult(NID_SEC_CERT, pCertList != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        memset(pCertList.get(), 0, sizeof(*pCertList.get()));
@@ -900,11 +914,11 @@ _CertDbManager::GetUserCertificateListByCertId(int certId, _CertificateListInfo*
        SysTryReturnResult(NID_SEC_CERT, ppCertList != null, E_INVALID_ARG, "Invalid input arguments.");
        SysTryReturnResult(NID_SEC_CERT, certId > 0, E_INVALID_ARG, "Invalid input arguments.");
 
-       sprintf(condition, "certId = %d and installed = '%s'", certId, installed);
+       snprintf(condition, sizeof(condition), "certId = %d and installed = '%s'", certId, installed);
        r = __userCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificate record.", GetErrorMessage(r));
 
-       std::unique_ptr <_CertificateListInfo> pCertList (new (std::nothrow) _CertificateListInfo);
+       std::unique_ptr< _CertificateListInfo > pCertList(new (std::nothrow) _CertificateListInfo);
        SysTryReturnResult(NID_SEC_CERT, pCertList != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        memset(pCertList.get(), 0, sizeof(*pCertList.get()));
@@ -922,10 +936,10 @@ _CertDbManager::GetUserCertificateListByCertId(int certId, _CertificateListInfo*
        pCertList->format = static_cast< _CertFormat >(certRecord.certFormat);
        pCertList->certType = _CERT_TYPE_USER_CERT;
 
-       std::unique_ptr <_CertPrivateKeyInfo> pPriKey (new (std::nothrow) _CertPrivateKeyInfo());
+       std::unique_ptr< _CertPrivateKeyInfo > pPriKey(new (std::nothrow) _CertPrivateKeyInfo());
        SysTryReturnResult(NID_SEC_CERT, pPriKey != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
-       std::unique_ptr <byte[]> pPrivateKey (new (std::nothrow) byte[_MAX_CERT_PRIVATE_KEY_SIZE]);
+       std::unique_ptr< byte[] > pPrivateKey(new (std::nothrow) byte[_MAX_CERT_PRIVATE_KEY_SIZE]);
        SysTryReturnResult(NID_SEC_CERT, pPrivateKey != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        memset(pPrivateKey.get(), 0, _MAX_CERT_PRIVATE_KEY_SIZE);
@@ -944,7 +958,7 @@ _CertDbManager::GetUserCertificateListByCertId(int certId, _CertificateListInfo*
                pPriKey->GetPkcs8EncDecKeyN(priKeyLen, &pPrivateTempKey, 0);
                SysTryReturnResult(NID_SEC_CERT, pPrivateTempKey != null, E_SYSTEM, "Failed to get private key buffer.");
 
-               std::unique_ptr<byte[]> pPrivateKeyAuto(pPrivateTempKey);
+               std::unique_ptr< byte[] > pPrivateKeyAuto(pPrivateTempKey);
 
                memcpy(pCertList->privatekey, pPrivateTempKey, priKeyLen);
        }
@@ -979,7 +993,7 @@ _CertDbManager::FindIssuerCertificateAndTypeN(_CertFormat certFormat, char* pIss
        r = _Base64::Encode(reinterpret_cast< byte* >(pIssuerName), strlen(pIssuerName), issuerNameBase64, issuerNameB64len);
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
 
-       sprintf(condition, "subjectName = '%s' and certFormat = %d and installed = '%s'", issuerNameBase64, certFormat, installed);
+       snprintf(condition, sizeof(condition), "subjectName = '%s' and certFormat = %d and installed = '%s'", issuerNameBase64, certFormat, installed);
 
        r = __caCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificate record.", GetErrorMessage(r));
@@ -1025,7 +1039,7 @@ _CertDbManager::FindIssuerCertificateByTypeN(_CertFormat certFormat, _CaCertType
        r = _Base64::Encode(reinterpret_cast< byte* >(pIssuerName), strlen(pIssuerName), issuerNameBase64, issuerNameB64len);
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
 
-       sprintf(condition, "subjectName = '%s' and certFormat = %d and certType = %d and installed = '%s'", issuerNameBase64, certFormat, certType, installed);
+       snprintf(condition, sizeof(condition), "subjectName = '%s' and certFormat = %d and certType = %d and installed = '%s'", issuerNameBase64, certFormat, certType, installed);
        r = __caCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificate record.", GetErrorMessage(r));
 
@@ -1037,7 +1051,7 @@ _CertDbManager::FindIssuerCertificateByTypeN(_CertFormat certFormat, _CaCertType
        filePath = static_cast< char* >(certRecord.fileName);
        fileStore.SetFilePath(filePath);
 
-       std::unique_ptr <byte[]> pCert (new (std::nothrow) byte[_MAX_CERTIFICATE_SIZE]);
+       std::unique_ptr< byte[] > pCert(new (std::nothrow) byte[_MAX_CERTIFICATE_SIZE]);
        SysTryReturnResult(NID_SEC_CERT, pCert != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        r = fileStore.ReadFromFile(pCert.get(), certLen);
@@ -1083,7 +1097,7 @@ _CertDbManager::FindCertType(_CertFormat certFormat, char* pIssuerName, char* pS
        r = _Base64::Encode(reinterpret_cast< byte* >(pSubjectName), strlen(pSubjectName), subjectNameBase64, subjectNameB64len);
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
 
-       sprintf(condition, "certFormat = %d and issuerName = '%s' and subjectName = '%s' and installed = '%s'", certFormat, issuerNameBase64, subjectNameBase64, installed);
+       snprintf(condition, sizeof(condition), "certFormat = %d and issuerName = '%s' and subjectName = '%s' and installed = '%s'", certFormat, issuerNameBase64, subjectNameBase64, installed);
        r = __caCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificate record.", GetErrorMessage(r));
 
@@ -1158,7 +1172,7 @@ _CertDbManager::GetHashOfCertFile(byte* pFilePath, int* pLen, char* pBuf)
        r = file.Construct(fileName, L"r");
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to construct file.");
 
-       std::unique_ptr <byte[]> pCertBuf (new (std::nothrow) byte[fileSize + 1]);
+       std::unique_ptr< byte[] > pCertBuf(new (std::nothrow) byte[fileSize + 1]);
        SysTryReturnResult(NID_SEC_CERT, pCertBuf != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        memset(pCertBuf.get(), 0, fileSize + 1);
@@ -1167,7 +1181,7 @@ _CertDbManager::GetHashOfCertFile(byte* pFilePath, int* pLen, char* pBuf)
        SysTryReturn(NID_SEC_CERT, (readCnt == fileSize) || (!IsFailed(r)), r, r, "[%s] Failed to read file.", GetErrorMessage(r));
 
        certLen = readCnt;
-       std::unique_ptr <byte[]> pOutBuf (new (std::nothrow) byte[outLen]);
+       std::unique_ptr< byte[] > pOutBuf(new (std::nothrow) byte[outLen]);
        SysTryReturnResult(NID_SEC_CERT, pOutBuf != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        memset(pOutBuf.get(), 0, outLen);
@@ -1255,8 +1269,11 @@ _CertDbManager::InsertCertChain(_CertFormat certFormat, _CertChain* pCertChain)
                        pTbsCert = pCurCert->GetTbsCertInstance();
                        SysTryReturnResult(NID_SEC_CERT, pTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
-                       if (E_SUCCESS != __caCertDbStore.CheckDuplicateCertificate(certType, reinterpret_cast< byte* >(pTbsCert->GetSubjectName()), strlen(reinterpret_cast< char* >(pTbsCert->GetSubjectName()))))
+                       r = __caCertDbStore.CheckDuplicateCertificate(certType, reinterpret_cast< byte* >(pTbsCert->GetSubjectName()), strlen(reinterpret_cast< char* >(pTbsCert->GetSubjectName())));
+                       if (r != E_SUCCESS)
                        {
+                               SysTryReturn(NID_SEC_CERT, r == E_DATA_NOT_FOUND, r, r, "[%s] Failed to check duplicate.", GetErrorMessage(r));
+
                                pX509Buff = null;
                                x509BuffSize = 0;
 
@@ -1266,11 +1283,12 @@ _CertDbManager::InsertCertChain(_CertFormat certFormat, _CertChain* pCertChain)
                                r = InsertCaCertificateFromBuffer(certType, certFormat, pX509Buff, x509BuffSize);
                                SysTryReturn(NID_SEC_CERT, !(IsFailed(r) && r != E_FILE_ALREADY_EXIST), r, r, "[E_SYSTEM] Failed to insert ca certificate.");
                        }
+
                        // CA certificate already present or properly installed in CA certificate store,
                        // get the certificate id of certificate
                        r = GetCaCertificateId(pTbsCert->GetSubjectName(), strlen(reinterpret_cast< char* >(pTbsCert->GetSubjectName())),
-                                                                 pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
-                                                                 curCACertId, certType);
+                                                                  pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
+                                                                  curCACertId, certType);
                        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed get Ca certificate id.", GetErrorMessage(r));
 
                        if (updateUserParentCa)
@@ -1305,7 +1323,7 @@ _CertDbManager::InsertCertChain(_CertFormat certFormat, _CertChain* pCertChain)
                        SysTryReturnResult(NID_SEC_CERT, pTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
                        r = GetCaCertificateId(pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
-                                                                 null, 0, curCACertId);
+                                                                  null, 0, curCACertId);
                        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed get Ca certificate id.", GetErrorMessage(r));
 
                        __userCertDbStore.UpdateParentCa(curDevCertId, curCACertId);
@@ -1318,7 +1336,7 @@ _CertDbManager::InsertCertChain(_CertFormat certFormat, _CertChain* pCertChain)
                        SysTryReturnResult(NID_SEC_CERT, pTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
                        r = GetCaCertificateId(pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
-                                                                 null, 0, curCACertId);
+                                                                  null, 0, curCACertId);
                        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed get Ca certificate id.", GetErrorMessage(r));
 
                        __caCertDbStore.UpdateParentCa(lastCACertId, curCACertId);
@@ -1341,7 +1359,7 @@ _CertDbManager::InsertCertificateChain(_CertFormat certFormat, _CertChain* pCert
 
        bool updateUserParentCa = false;
        byte* pPrvKey = null;
-       std::unique_ptr <byte[]> pPrvKeyBuffer;
+       std::unique_ptr< byte[] > pPrvKeyBuffer;
 
        SysTryReturnResult(NID_SEC_CERT, pCertChain != null, E_INVALID_ARG, "Invalid input parameter.");
        SysTryReturnResult(NID_SEC_CERT, certFormat == _CERT_X509, E_INVALID_ARG, "Invalid certificate format.");
@@ -1380,14 +1398,14 @@ _CertDbManager::InsertCertificateChain(_CertFormat certFormat, _CertChain* pCert
                int subjectNameLen = strlen(reinterpret_cast< char* >(pTbsCert->GetSubjectName()));
 
                r = __userCertDbStore.CheckDuplicateCertificate(pSubjectName, subjectNameLen);
-               if (IsFailed(r))
+               if (r == E_DATA_NOT_FOUND)
                {
                        if (pPrivateKeyInfo != null)
                        {
                                pPrivateKeyInfo->GetPkcs8EncDecKeyN(prvKeyLen, &pPrvKey, 1);
                                SysTryReturnResult(NID_SEC_CERT, prvKeyLen > 0, E_INVALID_KEY, "Invalid key length .");
 
-                               pPrvKeyBuffer = std::unique_ptr <byte[]> (pPrvKey);
+                               pPrvKeyBuffer = std::unique_ptr< byte[] >(pPrvKey);
                                pPrvKey = null;
 
                        }
@@ -1455,8 +1473,10 @@ _CertDbManager::InsertCertificateChain(_CertFormat certFormat, _CertChain* pCert
                        SysTryReturnResult(NID_SEC_CERT, pTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
                        r = __caCertDbStore.CheckDuplicateCertificate(certType, pTbsCert->GetSubjectName(), strlen(reinterpret_cast< char* >(pTbsCert->GetSubjectName())));
-                       if (IsFailed(r))
+                       if (r != E_SUCCESS)
                        {
+                               SysTryReturnResult(NID_SEC_CERT, r == E_DATA_NOT_FOUND, r, "Failed to check duplicate.");
+
                                pX509Buff = null;
                                x509BuffSize = 0;
 
@@ -1471,8 +1491,8 @@ _CertDbManager::InsertCertificateChain(_CertFormat certFormat, _CertChain* pCert
                        // get the certificate id of certificate
 
                        r = GetCaCertificateId(pTbsCert->GetSubjectName(), strlen(reinterpret_cast< char* >(pTbsCert->GetSubjectName())),
-                                                                 pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
-                                                                 curCACertId, certType);
+                                                                  pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
+                                                                  curCACertId, certType);
                        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed get Ca certificate id.", GetErrorMessage(r));
 
                        if (updateUserParentCa)
@@ -1506,7 +1526,7 @@ _CertDbManager::InsertCertificateChain(_CertFormat certFormat, _CertChain* pCert
                        SysTryReturnResult(NID_SEC_CERT, pTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
                        r = GetCaCertificateId(pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
-                                                                 null, 0, curCACertId);
+                                                                  null, 0, curCACertId);
                        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed get Ca certificate id.", GetErrorMessage(r));
 
                        __userCertDbStore.UpdateParentCa(curDevCertId, curCACertId);
@@ -1519,7 +1539,7 @@ _CertDbManager::InsertCertificateChain(_CertFormat certFormat, _CertChain* pCert
                        SysTryReturnResult(NID_SEC_CERT, pTbsCert != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
                        r = GetCaCertificateId(pTbsCert->GetIssuerName(), strlen(reinterpret_cast< char* >(pTbsCert->GetIssuerName())),
-                                                                null, 0, curCACertId);
+                                                                  null, 0, curCACertId);
                        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed get Ca certificate id.", GetErrorMessage(r));
 
                        __caCertDbStore.UpdateParentCa(lastCACertId, curCACertId);
@@ -1544,8 +1564,6 @@ _CertDbManager::InsertUserCertificateFromBuffer(_CertFormat certFormat, byte* pC
        int lenSerialNo = 0;
        int certId = 0;
        int keyIdB64Length = 0;
-       char* pFileName = null;
-       char* pPriKeyFileName = null;
        char subjectNameBuffer[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
        char szIssuerName[_MAX_ISSUER_SUBJECT_NAME_SIZE] = {0, };
        char serialName[_MAX_SERIAL_NUMBER_SIZE] = {0, };
@@ -1561,7 +1579,7 @@ _CertDbManager::InsertUserCertificateFromBuffer(_CertFormat certFormat, byte* pC
        r = __userCertDbStore.IsUserCertTableCreated();
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to create user certificate.");
 
-       std::unique_ptr <_X509Certificate> pCert (new (std::nothrow) _X509Certificate());
+       std::unique_ptr< _X509Certificate > pCert(new (std::nothrow) _X509Certificate());
        SysTryReturnResult(NID_SEC_CERT, pCert != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        r = pCert->Parse(pCertBuffer, certLength);
@@ -1576,8 +1594,8 @@ _CertDbManager::InsertUserCertificateFromBuffer(_CertFormat certFormat, byte* pC
        SysTryReturnResult(NID_SEC_CERT, lenSubjectName < _MAX_ISSUER_SUBJECT_NAME_SIZE, E_DATABASE, "Length is greater than maximum allowed length.");
        SysTryReturnResult(NID_SEC_CERT, lenIssuerName < _MAX_ISSUER_SUBJECT_NAME_SIZE, E_DATABASE, "Length is greater than maximum allowed length.");
 
-       strcpy(subjectNameBuffer, reinterpret_cast< char* >(pTbsCert->GetSubjectName()));
-       strcpy(szIssuerName, reinterpret_cast< char* >(pTbsCert->GetIssuerName()));
+       snprintf(subjectNameBuffer, sizeof(subjectNameBuffer), "%s", reinterpret_cast< char* >(pTbsCert->GetSubjectName()));
+       snprintf(szIssuerName, sizeof(szIssuerName), "%s", reinterpret_cast< char* >(pTbsCert->GetIssuerName()));
 
        pTbsCert->GetSerialNumber(pSerial, static_cast< int& >(lenSerialNo));
        if ((lenSerialNo <= 0) || (lenSerialNo > _MAX_SERIAL_NUMBER_SIZE))
@@ -1597,12 +1615,12 @@ _CertDbManager::InsertUserCertificateFromBuffer(_CertFormat certFormat, byte* pC
        r = pCert->GetKeyIdN(&pKeyId);
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_DECODING_FAILED, "Failed to get key Id.");
 
-       std::unique_ptr <byte[]> pKeyIdBuffer (pKeyId);
+       std::unique_ptr< byte[] > pKeyIdBuffer(pKeyId);
 
        keyIdB64Length = _Base64::GetEncodedSize(_MAX_CERT_SHA1_DIGEST_SIZE);
        SysTryReturnResult(NID_SEC_CERT, keyIdB64Length >= 0, E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
 
-       std::unique_ptr <char[]> pId64 (new (std::nothrow) char[keyIdB64Length]);
+       std::unique_ptr< char[] > pId64(new (std::nothrow) char[keyIdB64Length]);
        SysTryReturnResult(NID_SEC_CERT, pId64 != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
        memset(pId64.get(), 0, keyIdB64Length);
@@ -1611,6 +1629,7 @@ _CertDbManager::InsertUserCertificateFromBuffer(_CertFormat certFormat, byte* pC
 
        r = __userCertDbStore.CheckDuplicateCertificate(reinterpret_cast< byte* >(subjectNameBuffer), lenSubjectName);
        SysTryReturnResult(NID_SEC_CERT, IsFailed(r), E_FILE_ALREADY_EXIST, "File already exists.");
+       SysTryReturnResult(NID_SEC_CERT, r == E_DATA_NOT_FOUND, r, "Failed to check duplicate");
 
        //Get the last installed certificate id from db table
        __userCertDbStore.GetCurrentCertId(certId);
@@ -1625,7 +1644,6 @@ _CertDbManager::InsertUserCertificateFromBuffer(_CertFormat certFormat, byte* pC
        }
        else
        {
-               pPriKeyFileName = null;
                privateKeyLen = 0;
        }
 
@@ -1639,20 +1657,23 @@ _CertDbManager::InsertUserCertificateFromBuffer(_CertFormat certFormat, byte* pC
        memcpy(certRecord.certPubKeyHash, pId64.get(), keyIdB64Length); //Base64 encoded device id
        certRecord.certFormat = static_cast< int >(certFormat);
 
-       pFileName = Tizen::Base::_StringConverter::CopyToCharArrayN(tempFileName);
+       std::unique_ptr< char[] > pFileName(Tizen::Base::_StringConverter::CopyToCharArrayN(tempFileName));
        SysTryReturnResult(NID_SEC_CERT, pFileName != null, E_SYSTEM, "Failed to get attributes.");
 
-       strcpy(certRecord.fileName, pFileName);
+       int len = strlen(pFileName.get());
+       snprintf(certRecord.fileName, sizeof(certRecord.fileName), "%s", pFileName.get());
+
        certRecord.subjectNameLen = lenSubjectName;
 
        memcpy(certRecord.subjectName, subjectNameBuffer, lenSubjectName);
        certRecord.issuerNameLen = lenIssuerName;
        memcpy(certRecord.issuerName, szIssuerName, lenIssuerName);
 
-       pPriKeyFileName = Tizen::Base::_StringConverter::CopyToCharArrayN(privateKeyFile);
+       std::unique_ptr< char[] > pPriKeyFileName(Tizen::Base::_StringConverter::CopyToCharArrayN(privateKeyFile));
        SysTryReturnResult(NID_SEC_CERT, pPriKeyFileName != null, E_SYSTEM, "Failed to get attributes.");
 
-       strcpy(certRecord.prvKeyPath, pPriKeyFileName);
+       len = strlen(pPriKeyFileName.get());
+       snprintf(certRecord.prvKeyPath, sizeof(certRecord.prvKeyPath), "%s", pPriKeyFileName.get());
        certRecord.prvKeyLen = privateKeyLen;
        certRecord.parentCa = certId;
        strcpy(certRecord.installed, installedRecord);
@@ -1709,36 +1730,36 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
        subjectNameBase64Len = _Base64::GetEncodedSize(issuerNameLen);
        memset(issuerNameBase64, 0, sizeof(issuerNameBase64));
        r = _Base64::Encode(reinterpret_cast< byte* >(pIssuerName), issuerNameLen, reinterpret_cast< char* >(issuerNameBase64), subjectNameBase64Len);
-       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r),E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
+       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
 
        if ((pSubjectName != null) && (subjectNameLen > 0))
        {
                subjectNameBase64Len = _Base64::GetEncodedSize(subjectNameLen);
                memset(subjectNameBase64, 0, sizeof(subjectNameBase64));
                r = _Base64::Encode(reinterpret_cast< byte* >(pSubjectName), subjectNameLen, reinterpret_cast< char* >(subjectNameBase64), subjectNameBase64Len);
-               SysTryReturnResult(NID_SEC_CERT, !IsFailed(r),E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
-               sprintf(condition, "subjectName = '%s' and installed = '%s'", subjectNameBase64, installedRecord);
+               SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
+               snprintf(condition, sizeof(condition), "subjectName = '%s' and installed = '%s'", subjectNameBase64, installedRecord);
        }
        else
        {
                r = __userCertDbStore.GetNumberOfCertificates(recordCount);
                SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificates.", GetErrorMessage(r));
                SysTryReturnResult(NID_SEC_CERT, recordCount > 0, E_OBJ_NOT_FOUND, "Failed to get certificate records.");
-               sprintf(condition, "installed = '%s'", installedRecord);
+               snprintf(condition, sizeof(condition), "installed = '%s'", installedRecord);
        }
 
        memset(&userCertRecord, 0, sizeof(userCertRecord));
        r = __userCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &userCertRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificates record.", GetErrorMessage(r));
 
-       std::unique_ptr <_CertPrivateKeyInfo> pPriKey (new (std::nothrow) _CertPrivateKeyInfo());
+       std::unique_ptr< _CertPrivateKeyInfo > pPriKey(new (std::nothrow) _CertPrivateKeyInfo());
        SysTryReturnResult(NID_SEC_CERT, pPriKey != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
        do
        {
-               std::unique_ptr <_CertFileStore> pFileStore (new (std::nothrow) _CertFileStore());
+               std::unique_ptr< _CertFileStore > pFileStore(new (std::nothrow) _CertFileStore());
                SysTryReturnResult(NID_SEC_CERT, pFileStore != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
-               std::unique_ptr<_CertificateListInfo> pCertList(new (std::nothrow) _CertificateListInfo);
+               std::unique_ptr< _CertificateListInfo > pCertList(new (std::nothrow) _CertificateListInfo);
                SysTryReturnResult(NID_SEC_CERT, pCertList != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                memset(pCertList.get(), 0, sizeof(*pCertList.get()));
@@ -1755,10 +1776,10 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
                if (encodingType == _CERT_ENC_TYPE_PEM)
                {
                        const byte* pCertBuffer = pCertList->certificate;
-               
+
                        pBio = BIO_new(BIO_s_mem());
                        SysTryReturnResult(NID_SEC_CERT, pBio != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
-       
+
                        pCert = d2i_X509(null, &pCertBuffer, pCertList->length);
                        SysTryCatch(NID_SEC_CERT, pCert != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Certificate convertion failed.");
 
@@ -1774,7 +1795,7 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
 
                        BIO_free(pBio);
                        pBio = null;
-                       
+
                        X509_free(pCert);
                        pCert = null;
 
@@ -1782,6 +1803,7 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
                else if (encodingType == _CERT_ENC_TYPE_BASE64)
                {
                        int certLen = _Base64::GetEncodedSize(pCertList->length);
+                       SysTryReturnResult(NID_SEC_CERT, certLen > 0, E_SYSTEM, "Certificate length is invalid.");
                        memset(pCertList->certificate + pCertList->length, 0, sizeof(pCertList->certificate) - pCertList->length);
                        r = _Base64::Encode(reinterpret_cast< byte* >(pCertList->certificate), pCertList->length, reinterpret_cast< char* >(pCertList->certificate), certLen);
                        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
@@ -1789,7 +1811,7 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
                        pCertList->length = certLen;
                }
 
-               std::unique_ptr <byte[]> pPrivateKey (new (std::nothrow) byte[_MAX_CERT_PRIVATE_KEY_SIZE]);
+               std::unique_ptr< byte[] > pPrivateKey(new (std::nothrow) byte[_MAX_CERT_PRIVATE_KEY_SIZE]);
                SysTryReturnResult(NID_SEC_CERT, pPrivateKey != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                memset(pPrivateKey.get(), 0, _MAX_CERT_PRIVATE_KEY_SIZE);
@@ -1813,8 +1835,8 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
 
                        pPriKey->GetPkcs8EncDecKeyN(priKeyLen, &pPrivateTempKey, 0);
                        SysTryReturnResult(NID_SEC_CERT, pPrivateTempKey != null, E_SYSTEM, "Failed to get private key buffer.");
-                       
-                       pPrivateKey = std::unique_ptr<byte[]>(pPrivateTempKey);
+
+                       pPrivateKey = std::unique_ptr< byte[] >(pPrivateTempKey);
 
                        if (encodingType == _CERT_ENC_TYPE_PEM)
                        {
@@ -1872,7 +1894,7 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
                        memset(subjectNameBase64, 0, sizeof(subjectNameBase64));
                        r = _Base64::Encode(reinterpret_cast< byte* >(subName), subNameLen, reinterpret_cast< char* >(subjectNameBase64), subjectNameBase64Len);
                        SysTryCatch(NID_SEC_CERT, !IsFailed(r), , r, "[%s] Failed to encode data in base 64 encoding.", GetErrorMessage(r));
-                       sprintf(condition, "subjectName = '%s' and installed = '%s'", subjectNameBase64, installedRecord);
+                       snprintf(condition, sizeof(condition), "subjectName = '%s' and installed = '%s'", subjectNameBase64, installedRecord);
 
                        if (strcmp(reinterpret_cast< char* >(issuerNameBase64), reinterpret_cast< char* >(subjectNameBase64)) == 0)
                        {
@@ -1885,7 +1907,7 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
 
                        if (strcmp(certRecord.issuerName, certRecord.subjectName) != 0)
                        {
-                               std::unique_ptr<_CertificateListInfo> pCertList(new (std::nothrow) _CertificateListInfo());
+                               std::unique_ptr< _CertificateListInfo > pCertList(new (std::nothrow) _CertificateListInfo());
                                SysTryReturnResult(NID_SEC_CERT, pCertList != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                                memset(pCertList.get(), 0, sizeof(*pCertList.get()));
@@ -1929,7 +1951,7 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
                                        int certLen = _Base64::GetEncodedSize(pCertList->length);
                                        memset(pCertList->certificate + pCertList->length, 0, sizeof(pCertList->certificate) - pCertList->length);
                                        r = _Base64::Encode(reinterpret_cast< byte* >(pCertList->certificate), pCertList->length, reinterpret_cast< char* >(pCertList->certificate), certLen);
-                                       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r),E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
+                                       SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
                                        pCertList->length = certLen;
                                }
                                pCertList->format = (_CertFormat) certRecord.certFormat;
@@ -1958,7 +1980,7 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
                        }
 
                        memset(condition, 0, sizeof(condition));
-                       sprintf(condition, "installed = '%s'", installedRecord);
+                       snprintf(condition, sizeof(condition), "installed = '%s'", installedRecord);
 
                        count++;
 
@@ -2014,7 +2036,7 @@ _CertDbManager::GetUserCertificateChain(_CertFormat certFormat, _CertChain* pCer
        memset(subjectNameBase64, 0, sizeof(subjectNameBase64));
        r = _Base64::Encode(reinterpret_cast< byte* >(pSubjectName), strlen(pSubjectName), subjectNameBase64, subjNameB64len);
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_ENCODING_FAILED, "Failed to encode data in base 64 encoding.");
-       sprintf(conditonRecord, "subjectName = '%s' and installed = '%s'", subjectNameBase64, installedRecord);
+       snprintf(conditonRecord, sizeof(conditonRecord), "subjectName = '%s' and installed = '%s'", subjectNameBase64, installedRecord);
 
        r = __userCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(conditonRecord), &userCertRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificates record.", GetErrorMessage(r));
@@ -2033,7 +2055,7 @@ _CertDbManager::GetUserCertificateChain(_CertFormat certFormat, _CertChain* pCer
        {
                memset(&caCertRecord, 0, sizeof(caCertRecord));
                memset(conditonRecord, 0, _MAX_ISSUER_SUBJECT_NAME_SIZE + _MAX_SUBJECT_OFFSET_SIZE);
-               sprintf(conditonRecord, "certId = %d and installed = '%s'", parentCa, installedRecord);
+               snprintf(conditonRecord, sizeof(conditonRecord), "certId = %d and installed = '%s'", parentCa, installedRecord);
 
                r = __caCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(conditonRecord), &caCertRecord);
                SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificates record.", GetErrorMessage(r));
@@ -2059,7 +2081,7 @@ _CertDbManager::GetUserCertificateInfoByCertId(int certId, int* pSubjectLength,
        char installedRecord[_MAX_TYPE_RECORD_SIZE] = "T\0";
        char condition[_MAX_TYPE_CONST_SIZE] = {0, };
 
-       sprintf(condition, "certId = %d and installed = '%s'", certId, installedRecord);
+       snprintf(condition, sizeof(condition), "certId = %d and installed = '%s'", certId, installedRecord);
 
        r = __userCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &userCertRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificates record.", GetErrorMessage(r));
@@ -2089,7 +2111,7 @@ _CertDbManager::GetUserCertificateInfoByCertId(int certId, _CertEncodingType enc
        byte* pPrivateTempKey = null;
        UserCertRecord certRecord = {0, };
        _CertFileStore fileStore;
-       std::unique_ptr <_CertPrivateKeyInfo> pPriKey;
+       std::unique_ptr< _CertPrivateKeyInfo > pPriKey;
        BIO* pBio = null;
        X509* pCert = null;
        EVP_PKEY* pKey = null;
@@ -2097,12 +2119,12 @@ _CertDbManager::GetUserCertificateInfoByCertId(int certId, _CertEncodingType enc
        *ppUserCertInfo = null;
 
        SysTryReturnResult(NID_SEC_CERT, certId > 0, E_INVALID_ARG, "Invalid input parameter.");
-       sprintf(condition, "certId = %d and installed = '%s'", certId, installedRecord);
+       snprintf(condition, sizeof(condition), "certId = %d and installed = '%s'", certId, installedRecord);
 
        r = __userCertDbStore.GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to get certificates record.", GetErrorMessage(r));
 
-       std::unique_ptr <_CertInfo> pCertInfo (new (std::nothrow) _CertInfo);
+       std::unique_ptr< _CertInfo > pCertInfo(new (std::nothrow) _CertInfo);
        SysTryCatch(NID_SEC_CERT, pCertInfo != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
        memset(pCertInfo.get(), 0, sizeof(*pCertInfo.get()));
@@ -2151,10 +2173,10 @@ _CertDbManager::GetUserCertificateInfoByCertId(int certId, _CertEncodingType enc
 
        if (certRecord.prvKeyLen > 0)
        {
-               pPriKey = std::unique_ptr <_CertPrivateKeyInfo> (new (std::nothrow) _CertPrivateKeyInfo());
+               pPriKey = std::unique_ptr< _CertPrivateKeyInfo >(new (std::nothrow) _CertPrivateKeyInfo());
                SysTryReturnResult(NID_SEC_CERT, pPriKey != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
-               std::unique_ptr <byte[]> pPrivateKey (new (std::nothrow) byte[_MAX_CERT_PRIVATE_KEY_SIZE]);
+               std::unique_ptr< byte[] > pPrivateKey(new (std::nothrow) byte[_MAX_CERT_PRIVATE_KEY_SIZE]);
                SysTryReturnResult(NID_SEC_CERT, pPrivateKey != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                memset(pPrivateKey.get(), 0, _MAX_CERT_PRIVATE_KEY_SIZE);
@@ -2175,7 +2197,7 @@ _CertDbManager::GetUserCertificateInfoByCertId(int certId, _CertEncodingType enc
                        pPriKey->GetPkcs8EncDecKeyN(priKeyLen, &pPrivateTempKey, 0);
                        SysTryReturnResult(NID_SEC_CERT, pPrivateTempKey != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
-                       pPrivateKey = std::unique_ptr<byte[]>(pPrivateTempKey);
+                       pPrivateKey = std::unique_ptr< byte[] >(pPrivateTempKey);
 
                        if (encodingType == _CERT_ENC_TYPE_PEM)
                        {
@@ -2227,8 +2249,6 @@ CATCH:
                EVP_PKEY_free(pKey);
        }
 
-       {
-       }
        return r;
 }