Merge "Fix accessing freed memory in X509CertificateStore::Update()" into tizen
[platform/framework/native/appfw.git] / src / security / crypto / FSecCryptoMd5Hash.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FSecCryptoMd5Hash.cpp
19  * @brief               This file contains the implementation of Md5Hash class.
20  *
21  */
22 #include <unique_ptr.h>
23 #include <openssl/md5.h>
24 #include <openssl/evp.h>
25 #include <FBaseResult.h>
26 #include <FBaseErrors.h>
27 #include <FSecCryptoMd5Hash.h>
28 #include <FBaseSysLog.h>
29
30 using namespace Tizen::Base;
31
32
33 namespace Tizen { namespace Security { namespace Crypto
34 {
35
36 Md5Hash::Md5Hash(void)
37         : __pEvpMdCtx(null)
38         , __pMd5HashImpl(null)
39 {
40 }
41
42 Md5Hash::~Md5Hash(void)
43 {
44         delete __pEvpMdCtx;
45 }
46
47 result
48 Md5Hash::SetAlgorithm(const Tizen::Base::String& algorithm)
49 {
50         // Unsupported in Md5Hash
51         return E_UNSUPPORTED_ALGORITHM;
52 }
53
54 ByteBuffer*
55 Md5Hash::GetHashN(const ByteBuffer& input) const
56 {
57         result r = E_SUCCESS;
58         int dataLen = 0;
59         unsigned int outLen = 0;
60         byte* pData = null;
61         byte hashOut[MD5_DIGEST_LENGTH] = {'\0'};
62
63         ClearLastResult();
64
65         pData = const_cast< byte* >(input.GetPointer());
66         SysTryReturn(NID_SEC_CRYPTO, pData != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The input data value should be valid.");
67
68         dataLen = input.GetRemaining();
69         SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The input data length should be positive.");
70
71         //this function returns the MD5 hash hashOut
72         EVP_Digest(pData, dataLen, hashOut, &outLen, EVP_md5(), null);
73         SysTryReturn(NID_SEC_CRYPTO, outLen > 0, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
74
75         //stores the hash in bytebuffer
76         std::unique_ptr<ByteBuffer> pOutput(new (std::nothrow) ByteBuffer());
77         SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
78
79         r = pOutput->Construct(outLen);
80         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
81
82         r = pOutput->SetArray(hashOut, 0, outLen);
83         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
84
85         pOutput->Flip();
86
87         return pOutput.release();
88 }
89
90 result
91 Md5Hash::Initialize(void)
92 {
93         result r = E_SUCCESS;
94         int ret = 0;
95
96         SysAssertf(__pEvpMdCtx == null, "Already constructed. Calling Initialize() twice or more on a same instance is not allowed for this class.");
97
98         //MD5 init is called
99         __pEvpMdCtx = new (std::nothrow) EVP_MD_CTX();
100         SysTryReturn(NID_SEC_CRYPTO, __pEvpMdCtx != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
101
102         ret = EVP_DigestInit(__pEvpMdCtx, EVP_md5());
103         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
104
105 CATCH:
106
107         if (IsFailed(r))
108         {
109                 delete __pEvpMdCtx;
110                 __pEvpMdCtx = null;
111         }
112         return r;
113 }
114
115 result
116 Md5Hash::Update(const ByteBuffer& input)
117 {
118         result r = E_SUCCESS;
119         int ret = 0;
120         int dataLen = 0;
121         byte* pData = null;
122
123         SysAssertf(__pEvpMdCtx != null, "Not yet constructed. Initialize() should be called before use.");
124
125         pData = const_cast< byte* >(input.GetPointer());
126         SysTryReturn(NID_SEC_CRYPTO, pData != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input data value should be valid.");
127
128         dataLen = input.GetRemaining();
129         SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input data length should be positive.");
130
131         //MD5_update is called
132         ret = EVP_DigestUpdate(__pEvpMdCtx, pData, dataLen);
133         SysTryReturn(NID_SEC_CRYPTO, ret == 1, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
134
135         return r;
136 }
137
138 ByteBuffer*
139 Md5Hash::FinalizeN(void)
140 {
141         result r = E_SUCCESS;
142         int ret = 0;
143         int outLen = MD5_DIGEST_LENGTH;
144         std::unique_ptr<ByteBuffer> pOutput(null);
145
146         ClearLastResult();
147
148         SysAssertf(__pEvpMdCtx != null, "Not yet constructed. Initialize() should be called before use.");
149
150         std::unique_ptr<byte[]> pTempBuf(new (std::nothrow) byte[outLen]);
151         SysTryCatch(NID_SEC_CRYPTO, pTempBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
152
153         //MD5_final is called and is stored in pTempBuf
154         ret = EVP_DigestFinal(__pEvpMdCtx, pTempBuf.get(), null);
155         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
156
157         //hash is stored in bytebuffer
158         pOutput = std::unique_ptr<ByteBuffer>(new (std::nothrow) ByteBuffer());
159         SysTryCatch(NID_SEC_CRYPTO, pOutput != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
160
161         r = pOutput->Construct(outLen);
162         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
163
164         r = pOutput->SetArray(pTempBuf.get(), 0, outLen);
165         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
166
167         pOutput->Flip();
168
169 CATCH:
170
171         EVP_MD_CTX_cleanup(__pEvpMdCtx);
172
173         if (IsFailed(r))
174         {
175                 pOutput.reset(null);
176         }
177
178         delete __pEvpMdCtx;
179         __pEvpMdCtx = null;
180
181         return pOutput.release();
182 }
183
184 } } } //Tizen::Security::Crypto