Merge "Update GetPackageAppInfoN() API" into tizen_2.1
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_CertManager.cpp
index 0ae2a73..25155f6 100644 (file)
@@ -83,7 +83,7 @@ _CertManager::CreateCrtFile(void)
        r = dir.Construct(dirName);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Failed to construct directory.", GetErrorMessage(r));
 
-       std::unique_ptr<DirEnumerator> pDirEnum(dir.ReadN());
+       std::unique_ptr< DirEnumerator > pDirEnum(dir.ReadN());
        SysTryReturn(NID_SEC_CERT, pDirEnum != null, GetLastResult(), GetLastResult(), "[%s] Failed to read directory.", GetErrorMessage(GetLastResult()));
 
        while (pDirEnum->MoveNext() == E_SUCCESS)
@@ -185,7 +185,7 @@ _CertManager::AddCertificate(CertChainCtx certCtx, byte* pCert, int certLen)
        SysTryReturnResult(NID_SEC_CERT, pCert != null, E_INVALID_ARG, "Initial parameters are invalid.");
 
        certFormat = _CertManager::GetEncodedCertBuffer(pCert, certLen, &pDerCert, &derCertBufferLength, &encodingType);
-       std::unique_ptr<byte[]> pDerCertBuffer(pDerCert);
+       std::unique_ptr< byte[] > pDerCertBuffer(pDerCert);
        pDerCert = null;
 
        SysTryReturnResult(NID_SEC_CERT, certFormat == _CERT_X509, E_INVALID_ARG, "Unsupported certificate format.");
@@ -432,7 +432,7 @@ _CertManager::GetCertInfo(CertificateHandle certHandle, _CertFieldType field, _C
                pCert->GetCertBuffer(pX509Buff, x509BuffSize);
                SysTryReturnResult(NID_SEC_CERT, pX509Buff != null, E_SYSTEM, "Failed to get certificate buffer.");
 
-               std::unique_ptr<byte[]> pFingerPrint(new (std::nothrow) byte[SHA_DIGEST_LENGTH + 1]);
+               std::unique_ptr< byte[] > pFingerPrint(new (std::nothrow) byte[SHA_DIGEST_LENGTH + 1]);
                SysTryReturnResult(NID_SEC_CERT, pFingerPrint != null, E_OUT_OF_MEMORY, "Failed to allocate memory. ");
 
                memset(pFingerPrint.get(), 0, SHA_DIGEST_LENGTH + 1);
@@ -455,7 +455,7 @@ _CertManager::GetCertInfo(CertificateHandle certHandle, _CertFieldType field, _C
                pTbsCert->GetPublicKeyInfoN(publicKeyLen, &pPublicKeyBuffer);
                if (pPublicKeyBuffer != null)
                {
-                       std::unique_ptr<byte[]> pPublicKeyAuto(pPublicKeyBuffer);
+                       std::unique_ptr< byte[] > pPublicKeyAuto(pPublicKeyBuffer);
                        int iterVal = publicKeyLen;
                        int index = 0;
 
@@ -510,7 +510,7 @@ _CertManager::GetEncodedCertBuffer(byte* pCertBuffer, int certBufferLen, byte**
        SysTryReturn(NID_SEC_CERT, pCertBuffer != null, _CERT_UNKNOWN, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument passed.");
        SysTryReturn(NID_SEC_CERT, certBufferLen > 0, _CERT_UNKNOWN, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument passed.");
 
-       std::unique_ptr<_X509Certificate> pX509Cert(new (std::nothrow) _X509Certificate());
+       std::unique_ptr< _X509Certificate > pX509Cert(new (std::nothrow) _X509Certificate());
        SysTryReturn(NID_SEC_CERT, pX509Cert != null, _CERT_UNKNOWN, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
        pBase64Header = strstr(reinterpret_cast< char* >(pCertBuffer), _CERT_BASE64_HEADER);
@@ -522,7 +522,7 @@ _CertManager::GetEncodedCertBuffer(byte* pCertBuffer, int certBufferLen, byte**
                SysTryReturn(NID_SEC_CERT, pBio != null, _CERT_UNKNOWN, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
                pemCertSize = (int(pBase64Trailer - pBase64Header) + strlen(_CERT_BASE64_TRAILER));
-               readCount = BIO_write(pBio, (const void*) pBase64Header,  pemCertSize);
+               readCount = BIO_write(pBio, (const void*) pBase64Header, pemCertSize);
                SysTryCatch(NID_SEC_CERT, readCount > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Certificate conversion failed");
 
                pOpensslX509Cert = PEM_read_bio_X509(pBio, NULL, 0, NULL);
@@ -538,7 +538,7 @@ _CertManager::GetEncodedCertBuffer(byte* pCertBuffer, int certBufferLen, byte**
        }
        else if (pX509Cert->Parse(pCertBuffer, certBufferLen) == E_SUCCESS)
        {
-               std::unique_ptr<byte[]> pCertBuf(new (std::nothrow) byte[certBufferLen]);
+               std::unique_ptr< byte[] > pCertBuf(new (std::nothrow) byte[certBufferLen]);
                SysTryReturn(NID_SEC_CERT, pCertBuf != null, _CERT_UNKNOWN, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
                memcpy(pCertBuf.get(), pCertBuffer, certBufferLen);
@@ -555,7 +555,7 @@ _CertManager::GetEncodedCertBuffer(byte* pCertBuffer, int certBufferLen, byte**
                certBufLen = _Base64::GetDecodedSize(certBufferLen);
                SysTryReturn(NID_SEC_CERT, certBufLen > 0, _CERT_UNKNOWN, E_SYSTEM, "[E_SYSTEM] Invalid certificate length.");
 
-               std::unique_ptr<byte[]> pCertBuf(new (std::nothrow) byte[certBufLen]);
+               std::unique_ptr< byte[] > pCertBuf(new (std::nothrow) byte[certBufLen]);
                SysTryReturn(NID_SEC_CERT, pCertBuf != null, _CERT_UNKNOWN, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
                memset(pCertBuf.get(), 0, certBufLen);
@@ -612,7 +612,7 @@ _CertManager::GetPublicKey(CertificateHandle certificate, char* pBuffer, int* pB
        pTbsCert->GetPublicKeyInfoN(pubKeyLen, &pPuKey);
        SysTryReturnResult(NID_SEC_CERT, pPuKey != null, E_SYSTEM, "Failed to get certificate to be signed instance.");
 
-       std::unique_ptr<byte[]> pPubKeyAuto(pPuKey);
+       std::unique_ptr< byte[] > pPubKeyAuto(pPuKey);
 
        memcpy(pBuffer, pPubKeyAuto.get(), pubKeyLen);
 
@@ -768,20 +768,20 @@ _CertManager::ParseCertTitle(char subject[_MAX_ISSUER_SUBJECT_NAME_SIZE + 1], ch
        ClearLastResult();
 
        SysTryReturn(NID_SEC_CERT, subject[0] != '\0', E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input parameter.");
-       
-       SysAssertf(strlen(subject) <= _MAX_ISSUER_SUBJECT_NAME_SIZE, "The buffer size of source is too big.");
+
+       SysAssertf(strlen(subject) <= (size_t) _MAX_ISSUER_SUBJECT_NAME_SIZE, "The buffer size of source is too big.");
 
        bool done = false;
-    char* pPivotPtr = null;
-    char* pSavePtr = null;
-    char* pSubStr = null;
-    char tempSubject[_MAX_ISSUER_SUBJECT_NAME_SIZE + 1] = {0, };
+       char* pPivotPtr = null;
+       char* pSavePtr = null;
+       char* pSubStr = null;
+       char tempSubject[_MAX_ISSUER_SUBJECT_NAME_SIZE + 1] = {0, };
 
        strncpy(tempSubject, subject, strlen(subject));
        pPivotPtr = tempSubject;
-    for (int count = 0; ; pSubStr = null)
-    {
-        pSubStr = strtok_r(pPivotPtr, "/", &pSavePtr);
+       for (;; pSubStr = null)
+       {
+               pSubStr = strtok_r(pPivotPtr, "/", &pSavePtr);
 
                if (strncmp(pSubStr, _CERT_COMMON_NAME, strlen(_CERT_COMMON_NAME)) == 0)
                {
@@ -822,7 +822,7 @@ _CertManager::ParseCertTitle(char subject[_MAX_ISSUER_SUBJECT_NAME_SIZE + 1], ch
 
        // copy title into out param
        strncpy(title, pSubStr, strlen(pSubStr));
-               
+
        return E_SUCCESS;
 }
 
@@ -841,7 +841,7 @@ _CertManager::MakeParseAndVerifyCertChainBufferN(byte* pCertChainBuffer, int cer
        byte* pCertBuf = null;
        byte* pCurrCertBuf = null;
        _X509Certificate* pUserCert = null;
-       std::unique_ptr<_CertPrivateKeyInfo> pPKeyInfo(null);
+       std::unique_ptr< _CertPrivateKeyInfo > pPKeyInfo(null);
 
        pCertBuf = pCertChainBuffer;
        bufSize = certChainLength;
@@ -852,7 +852,7 @@ _CertManager::MakeParseAndVerifyCertChainBufferN(byte* pCertChainBuffer, int cer
        // Process Private Key
        if (pUserPrivateKeyBuffer != null && userPrivateKeyLength > 0)
        {
-               pPKeyInfo = std::unique_ptr<_CertPrivateKeyInfo> (new (std::nothrow) _CertPrivateKeyInfo(pUserPrivateKeyBuffer, userPrivateKeyLength));
+               pPKeyInfo = std::unique_ptr< _CertPrivateKeyInfo >(new (std::nothrow) _CertPrivateKeyInfo(pUserPrivateKeyBuffer, userPrivateKeyLength));
                SysTryReturnResult(NID_SEC_CERT, pPKeyInfo != null, E_OUT_OF_MEMORY, "Failed allocate memory.");
        }
 
@@ -867,7 +867,7 @@ _CertManager::MakeParseAndVerifyCertChainBufferN(byte* pCertChainBuffer, int cer
 
        dataOffset = 0;
 
-       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 parse and verify certificate chain.");
 
        if (pPKeyInfo != null)
@@ -878,7 +878,7 @@ _CertManager::MakeParseAndVerifyCertChainBufferN(byte* pCertChainBuffer, int cer
                        pCurrCertBuf = pCertBuf + dataOffset;
                        currCertBufLen = _CertManager::GetBlockSize(pCurrCertBuf);
 
-                       std::unique_ptr<_X509Certificate> pTmpCert(new (std::nothrow) _X509Certificate());
+                       std::unique_ptr< _X509Certificate > pTmpCert(new (std::nothrow) _X509Certificate());
                        SysTryReturnResult(NID_SEC_CERT, pTmpCert != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                        r = pTmpCert->Parse(pCurrCertBuf, currCertBufLen);
@@ -910,7 +910,7 @@ _CertManager::MakeParseAndVerifyCertChainBufferN(byte* pCertChainBuffer, int cer
                pCurrCertBuf = pCertBuf + dataOffset;
                currCertBufLen = _CertManager::GetBlockSize(pCurrCertBuf);
 
-               std::unique_ptr<_X509Certificate> pUserCertAuto(new (std::nothrow) _X509Certificate());
+               std::unique_ptr< _X509Certificate > pUserCertAuto(new (std::nothrow) _X509Certificate());
                SysTryReturnResult(NID_SEC_CERT, pUserCertAuto != null, E_OUT_OF_MEMORY, "Failed to allocate memory.");
 
                r = pUserCertAuto->Parse(pCurrCertBuf, currCertBufLen);
@@ -938,7 +938,7 @@ _CertManager::MakeParseAndVerifyCertChainBufferN(byte* pCertChainBuffer, int cer
 
                while (dataOffset < bufSize)
                {
-                       std::unique_ptr<_X509Certificate> pCurrentCert(new (std::nothrow) _X509Certificate());
+                       std::unique_ptr< _X509Certificate > pCurrentCert(new (std::nothrow) _X509Certificate());
                        SysTryReturnResult(NID_SEC_CERT, pCurrentCert != null, E_OUT_OF_MEMORY, "Failed to allocate memory. ");
 
                        pCurrCertBuf = pCertBuf + dataOffset;
@@ -998,7 +998,7 @@ _CertManager::CheckRsaPublicPrivateKeyPair(_X509Certificate* pX509Certificate, _
                pX509CertificatePrivateKey->GetPrivateKeyN(privateKeyLength, &pPriKey);
                SysTryReturnResult(NID_SEC_CERT, pPriKey != null, E_SYSTEM, "Unable to get certificate private key.");
 
-               std::unique_ptr<byte[]> pPriKeyBuf(pPriKey);
+               std::unique_ptr< byte[] > pPriKeyBuf(pPriKey);
 
                pPrivateKey = d2i_PrivateKey(EVP_PKEY_RSA, null, const_cast< const unsigned char** >(static_cast< unsigned char** >(&pPriKey)), privateKeyLength);
                if (pPrivateKey != null)
@@ -1092,16 +1092,16 @@ _CertManager::OpenUserCertificateStore(int& totalCount)
 
        sprintf(condition, "installed = '%s'", installedRecord);
 
-       std::unique_ptr<_UserCertDbStore> pUserCertDbStore(new (std::nothrow) _UserCertDbStore());
+       std::unique_ptr< _UserCertDbStore > pUserCertDbStore(new (std::nothrow) _UserCertDbStore());
        SysTryReturn(NID_SEC_CERT, pUserCertDbStore != null, certificateStoreCtx, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
        r = pUserCertDbStore->GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), certificateStoreCtx, r, "[%s] Failed to get first certificate record.", GetErrorMessage(r));
 
-       std::unique_ptr<_CertRootList> pCertListFirstNode(new (std::nothrow) _CertRootList());
+       std::unique_ptr< _CertRootList > pCertListFirstNode(new (std::nothrow) _CertRootList());
        SysTryReturn(NID_SEC_CERT, pCertListFirstNode != null, certificateStoreCtx, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
-       std::unique_ptr<_CertRootCaInfo> pRootCa(new (std::nothrow) _CertRootCaInfo());
+       std::unique_ptr< _CertRootCaInfo > pRootCa(new (std::nothrow) _CertRootCaInfo());
        SysTryReturn(NID_SEC_CERT, pRootCa != null, certificateStoreCtx, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
        memset(pRootCa.get(), 0, sizeof(*pRootCa.get()));
@@ -1124,7 +1124,7 @@ _CertManager::OpenUserCertificateStore(int& totalCount)
 
        while ((pUserCertDbStore->GetNextRecordByCondition(reinterpret_cast< byte* >(condition), &certRecord, certRecord.certId)) == E_SUCCESS)
        {
-               std::unique_ptr<_CertRootList> pCertList(new (std::nothrow) _CertRootList());
+               std::unique_ptr< _CertRootList > pCertList(new (std::nothrow) _CertRootList());
                SysTryReturn(NID_SEC_CERT, pCertList != null, certificateStoreCtx, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
                r = fileStore.SetFileHandle(certRecord.certId, _CERT_PATH_USER_CERT);
@@ -1170,16 +1170,16 @@ _CertManager::OpenRootCaStore(_CaCertType type, int& totalCount) // _CERT_TYPE_T
 
        sprintf(condition, "certType = %d and installed = '%s'", static_cast< int >(type), installedRecord);
 
-       std::unique_ptr<_CaCertDbStore> pCaCertDbStore(new (std::nothrow) _CaCertDbStore());
+       std::unique_ptr< _CaCertDbStore > pCaCertDbStore(new (std::nothrow) _CaCertDbStore());
        SysTryReturn(NID_SEC_CERT, pCaCertDbStore != null, certificateStoreCtx, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
        r = pCaCertDbStore->GetFirstRecordByConditions(reinterpret_cast< byte* >(condition), &certRecord);
        SysTryReturn(NID_SEC_CERT, !IsFailed(r), certificateStoreCtx, r, "[%s] Failed to get first certificate record.", GetErrorMessage(r));
 
-       std::unique_ptr<_CertRootList> pCertListFirstNode(new (std::nothrow) _CertRootList());
+       std::unique_ptr< _CertRootList > pCertListFirstNode(new (std::nothrow) _CertRootList());
        SysTryReturn(NID_SEC_CERT, pCertListFirstNode != null, certificateStoreCtx, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
-       std::unique_ptr<_CertRootCaInfo> pRootCa(new (std::nothrow) _CertRootCaInfo());
+       std::unique_ptr< _CertRootCaInfo > pRootCa(new (std::nothrow) _CertRootCaInfo());
        SysTryReturn(NID_SEC_CERT, pRootCa != null, certificateStoreCtx, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
        memset(pRootCa.get(), 0, sizeof(*pRootCa.get()));
@@ -1202,7 +1202,7 @@ _CertManager::OpenRootCaStore(_CaCertType type, int& totalCount) // _CERT_TYPE_T
 
        while ((pCaCertDbStore->GetNextRecordByCondition(reinterpret_cast< byte* >(condition), &certRecord, certRecord.certId)) == E_SUCCESS)
        {
-               std::unique_ptr<_CertRootList> pCertList(new (std::nothrow) _CertRootList());
+               std::unique_ptr< _CertRootList > pCertList(new (std::nothrow) _CertRootList());
                SysTryReturn(NID_SEC_CERT, pCertList != null, certificateStoreCtx, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
                r = fileStore.SetFileHandle(certRecord.certId, _CERT_PATH_CA_CERT);