fixed prevent issue
authorKyungwooNoh <kw1004.noh@samsung.com>
Fri, 14 Jun 2013 11:31:39 +0000 (20:31 +0900)
committerKyungwooNoh <kw1004.noh@samsung.com>
Fri, 14 Jun 2013 11:40:30 +0000 (20:40 +0900)
Change-Id: I7784fadf52febd37646e617ae0ab27ff43618ab8
Signed-off-by: KyungwooNoh <kw1004.noh@samsung.com>
src/security/FSecRsaKeyConverter.cpp
src/security/FSec_Prng.cpp
src/security/cert/FSecCert_CertPrivateKeyInfo.cpp
src/security/cert/FSecCert_Certificate.cpp
src/security/crypto/FSecCryptoSha2Hash.cpp
src/security/crypto/FSecCryptoSha2Hmac.cpp
src/security/crypto/FSecCrypto_SymmetricCipher.cpp

index 32b1e92..4302318 100644 (file)
@@ -351,6 +351,7 @@ RsaKeyConverter::ConvertDerToPemN(RsaKeyFormat format, const IKey& key)
                if (pPrivKeyInfo != null)
                {
                        pEvpKey = EVP_PKCS82PKEY(pPrivKeyInfo);
+                       SysTryCatch(NID_SEC, pEvpKey != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid");
                        PKCS8_PRIV_KEY_INFO_free(pPrivKeyInfo);
                }
                pBio = BIO_new(BIO_s_mem());
index 63c1d36..1509e7a 100644 (file)
@@ -185,19 +185,19 @@ _Prng::GenerateRandomBytes(PrngContext& prng, ByteBuffer* pSeed)
        }
 
        pBlock = std::unique_ptr <byte[]> (new (std::nothrow) byte[blockSize]);
-       SysTryCatch(NID_SEC, pBlock != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
+       SysTryReturn(NID_SEC, pBlock != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
 
        pInterVal1 = std::unique_ptr <byte[]> (new (std::nothrow) byte[blockSize]);
-       SysTryCatch(NID_SEC, pInterVal1 != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
+       SysTryReturn(NID_SEC, pInterVal1 != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
 
        pInterVal2 = std::unique_ptr <byte[]> (new (std::nothrow) byte[blockSize]);
-       SysTryCatch(NID_SEC, pInterVal2 != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
+       SysTryReturn(NID_SEC, pInterVal2 != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
 
        pInterVal1XorBlock = std::unique_ptr <byte[]> (new (std::nothrow) byte[blockSize]);
-       SysTryCatch(NID_SEC, pInterVal1XorBlock != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
+       SysTryReturn(NID_SEC, pInterVal1XorBlock != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
 
        pInterVal2XorInterVal1 = std::unique_ptr <byte[]> (new (std::nothrow) byte[blockSize]);
-       SysTryCatch(NID_SEC, pInterVal2XorInterVal1 != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
+       SysTryReturn(NID_SEC, pInterVal2XorInterVal1 != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Allocating new byte array failed.");
 
        lenInterVal1 = blockSize;
        lenInterVal2 = blockSize;
@@ -236,7 +236,7 @@ _Prng::GenerateRandomBytes(PrngContext& prng, ByteBuffer* pSeed)
                        {
                                r = E_INVALID_ARG;
                                SysLogException(NID_SEC, r, "The seed length do not match the data block size.");
-                               goto CATCH;
+                               return r;
                        }
 
                        memcpy(pBlock.get(), pDt, dtLen);
index 6edf660..5dba0c3 100644 (file)
@@ -46,6 +46,18 @@ using namespace Tizen::Base;
 using namespace Tizen::Io;
 using namespace Tizen::Security;
 
+struct FreeCharPtr
+{
+       void operator ()(char* p)
+       {   
+               if (p != null)
+               {   
+                       free(p);
+               }   
+       }   
+};
+
+
 namespace Tizen { namespace Security { namespace Cert
 {
 
@@ -103,7 +115,7 @@ _CertPrivateKeyInfo::GetEncDecPrivateKeyN(int& keyLen, byte** ppPrivKey, int enc
        int err = 0;
        EVP_CIPHER_CTX ctx;
 
-       std::unique_ptr< char > pUniqueKey(GetDeviceUniqueKey(certMgrInfo, certMgrInfoLen, aesBlockLen));
+       std::unique_ptr< char, FreeCharPtr > pUniqueKey(GetDeviceUniqueKey(certMgrInfo, certMgrInfoLen, aesBlockLen));
        SysTryReturnResult(NID_SEC_CERT, pUniqueKey != null, E_SYSTEM, "Failed to generate unique key.");
        
        memset(uniqueKey, 0, aesBlockLen);
@@ -172,7 +184,7 @@ _CertPrivateKeyInfo::GetPkcs8EncDecKeyN(int& keyLen, byte** ppPrivKey, int encDe
        long tempkeyLen = 0;
        byte* pPrivKey = null;
 
-       std::unique_ptr< char > pUniqueKey(GetDeviceUniqueKey(certMgrInfo, certMgrInfoLen, aesBlockLen));
+       std::unique_ptr< char, FreeCharPtr > pUniqueKey(GetDeviceUniqueKey(certMgrInfo, certMgrInfoLen, aesBlockLen));
        SysTryReturnResult(NID_SEC_CERT, pUniqueKey != null, E_SYSTEM, "Failed to generate unique key.");
 
        memset(uniqueKey, 0, aesBlockLen);
index 131d520..553de1b 100644 (file)
@@ -850,6 +850,7 @@ _X509Certificate::ParseSubjectPublicKeyInfo(void)
        SysTryReturnResult(NID_SEC_CERT, pKey != null, E_PARSING_FAILED, "Failed to get certificate public key.");
 
        pubKeyLen = i2d_PublicKey(pKey, &pPubKey);
+       SysTryReturnResult(NID_SEC_CERT, pPubKey != null, E_PARSING_FAILED, "Failed to get certificate public key.");
 
        __tbsCert.SetPublicKeyInfo(pubKeyLen, pPubKey);
 
index 7fa1f2a..b04983a 100644 (file)
@@ -166,14 +166,16 @@ Sha2Hash::FinalizeN(void)
        int ret = 0;
        int outLen = 0;
        std::unique_ptr<ByteBuffer> pOutput(null);
+       std::unique_ptr<byte[]> pTempBuf(null);
 
        ClearLastResult();
 
        SysAssertf(__pEvpMdCtx != null && __pAlgorithm != null, "Not yet constructed. Initialize() and SetAlgorithm() should be called before use.");
 
        outLen = EVP_MD_size(__pAlgorithm);
+       SysTryCatch(NID_SEC_CRYPTO, outLen > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
 
-       std::unique_ptr<byte[]> pTempBuf(new (std::nothrow) byte[outLen]);
+       pTempBuf = std::unique_ptr<byte[]>(new (std::nothrow) byte[outLen]);
        SysTryCatch(NID_SEC_CRYPTO, pTempBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
 
        ret = EVP_DigestFinal(__pEvpMdCtx, pTempBuf.get(), null);
index 8c15534..c4ba38f 100644 (file)
@@ -121,6 +121,7 @@ Sha2Hmac::GetHmacN(const ByteBuffer& input) const
        SysTryReturn(NID_SEC_CRYPTO, keyLen > 0, null, E_KEY_NOT_FOUND, "[E_KEY_NOT_FOUND] Key length should be positive.");
 
        mdLen = EVP_MD_size(__pAlgorithm);
+       SysTryReturn(NID_SEC_CRYPTO, mdLen > 0, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
 
        std::unique_ptr<byte[]> pMd(new (std::nothrow) byte[mdLen]);
        SysTryReturn(NID_SEC_CRYPTO, pMd != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
index e1f8a1a..58e2163 100644 (file)
@@ -499,7 +499,8 @@ _SymmetricCipher::DoWrapN(const Tizen::Base::ByteBuffer& secretKey)
        {
                r = E_SYSTEM;
                SysLogException(NID_SEC_CRYPTO, r, "An Unexpected system error occurred.");
-               goto CATCH;
+               SetLastResult(r);
+               return null;
        }
 
 
@@ -520,7 +521,8 @@ _SymmetricCipher::DoWrapN(const Tizen::Base::ByteBuffer& secretKey)
        default:
                r = E_UNSUPPORTED_ALGORITHM;
                SysLogException(NID_SEC_CRYPTO, r, " The algorithm for input key length is not supported");
-               goto CATCH;
+               SetLastResult(r);
+               return null;
        }
 
        pOut = std::unique_ptr <byte[]> (new (std::nothrow) byte[dataLen + 8]);
@@ -587,7 +589,7 @@ _SymmetricCipher::DoWrapN(const Tizen::Base::ByteBuffer& secretKey)
        pOutBuf->Flip();
 
 CATCH:
-
+       
        EVP_CIPHER_CTX_cleanup(&cipherCtx);
 
        SetLastResult(r);
@@ -637,9 +639,10 @@ _SymmetricCipher::DoUnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
 
        if (numVal < _MIN_WRAP_KEY_LEN_REFACTOR || dataLen > _MAX_AES_WRAP_DATA_LENGTH)
        {
-               ret = E_SYSTEM;
+               r = E_SYSTEM;
                SysLogException(NID_SEC_CRYPTO, r, "An Unexpected system error occurred.");
-               goto CATCH;
+               SetLastResult(r);
+               return null;
        }
 
 
@@ -660,7 +663,8 @@ _SymmetricCipher::DoUnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
        default:
                r = E_UNSUPPORTED_ALGORITHM;
                SysLogException(NID_SEC_CRYPTO, r, " The algorithm for input key length is not supported");
-               goto CATCH;
+               SetLastResult(r);
+               return null;
        }
 
        pOut = std::unique_ptr <byte[]> (new (std::nothrow) byte[dataLen]);