Fix the prevent issue - #38349
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_CertDbManager.cpp
index 9df2375..2f9886a 100755 (executable)
@@ -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;
 }
 
@@ -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,12 +219,12 @@ _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()));
@@ -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,7 +280,7 @@ _CertDbManager::InsertCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        }
 
        r = __caCertDbStore.CheckDuplicateCertificate(certType, reinterpret_cast< byte* >(subjectName), lenSubjectName);
-       if(r == E_SUCCESS)
+       if (r == E_SUCCESS)
        {
                return E_FILE_ALREADY_EXIST;
        }
@@ -284,7 +294,7 @@ _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());
@@ -338,7 +348,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);
@@ -375,7 +385,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);
@@ -408,7 +418,7 @@ _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());
@@ -454,7 +464,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);
@@ -467,8 +477,8 @@ _CertDbManager::RemoveCaCertificateFromBuffer(_CaCertType certType, _CertFormat
        SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_OBJ_NOT_FOUND, "Certificate not found in db.");
 
        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));
 
 
@@ -724,7 +734,7 @@ _CertDbManager::GetCertificateListByFormat(_CertFormat certFormat, _CertificateL
        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 +756,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()));
@@ -795,7 +805,7 @@ _CertDbManager::GetUserCertificateListByFormat(_CertFormat certFormat, _Certific
        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 +828,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()));
@@ -862,7 +872,7 @@ _CertDbManager::GetCaCertificateListByCertId(int certId, _CertificateListInfo**
        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()));
@@ -904,7 +914,7 @@ _CertDbManager::GetUserCertificateListByCertId(int certId, _CertificateListInfo*
        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 +932,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 +954,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);
        }
@@ -1037,7 +1047,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);
@@ -1158,7 +1168,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 +1177,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);
@@ -1269,8 +1279,8 @@ _CertDbManager::InsertCertChain(_CertFormat certFormat, _CertChain* pCertChain)
                        // 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 +1315,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 +1328,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 +1351,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.");
@@ -1387,7 +1397,7 @@ _CertDbManager::InsertCertificateChain(_CertFormat certFormat, _CertChain* pCert
                                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;
 
                        }
@@ -1471,8 +1481,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 +1516,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 +1529,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);
@@ -1561,7 +1571,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);
@@ -1597,12 +1607,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);
@@ -1709,14 +1719,14 @@ _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.");
+               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);
        }
        else
@@ -1731,14 +1741,14 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
        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 +1765,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 +1784,7 @@ _CertDbManager::GetUserCertificateChain(char* pIssuerName, int issuerNameLen, ch
 
                        BIO_free(pBio);
                        pBio = null;
-                       
+
                        X509_free(pCert);
                        pCert = null;
 
@@ -1782,6 +1792,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 +1800,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 +1824,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)
                        {
@@ -1885,7 +1896,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 +1940,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;
@@ -2089,7 +2100,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;
@@ -2102,7 +2113,7 @@ _CertDbManager::GetUserCertificateInfoByCertId(int certId, _CertEncodingType enc
        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 +2162,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 +2186,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)
                        {