Merge "Fix accessing freed memory in X509CertificateStore::Update()" into tizen
[platform/framework/native/appfw.git] / src / security / crypto / FSecCryptoSha1Hash.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                FSecCryptoSha1Hash.cpp
19  * @brief               This file contains the implementation of Sha1Hash class.
20  */
21 #include <unique_ptr.h>
22 #include <openssl/sha.h>
23 #include <openssl/evp.h>
24 #include <FBaseResult.h>
25 #include <FBaseErrors.h>
26 #include <FSecCryptoSha1Hash.h>
27 #include <FBaseSysLog.h>
28
29 using namespace Tizen::Base;
30
31
32 namespace Tizen { namespace Security { namespace Crypto
33 {
34
35 Sha1Hash::Sha1Hash(void)
36         : __pEvpMdCtx(null)
37         , __pSha1HashImpl(null)
38 {
39 }
40
41 Sha1Hash::~Sha1Hash(void)
42 {
43         delete __pEvpMdCtx;
44 }
45
46 result
47 Sha1Hash::SetAlgorithm(const Tizen::Base::String& algorithm)
48 {
49         // Unsupported in Sha1Hash
50         return E_UNSUPPORTED_ALGORITHM;
51 }
52
53 ByteBuffer*
54 Sha1Hash::GetHashN(const ByteBuffer& input) const
55 {
56         result r = E_SUCCESS;
57         int dataLen = 0;
58         unsigned int outLen = 0;
59         byte* pData = null;
60         byte hashOut[SHA_DIGEST_LENGTH] = {'\0'};
61
62         ClearLastResult();
63
64         pData = const_cast< byte* >(input.GetPointer());
65         SysTryReturn(NID_SEC_CRYPTO, pData != null, null, E_INVALID_ARG, "[E_INVALID_ARG] The input data value should be valid.");
66
67         dataLen = input.GetRemaining();
68         SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The input data length should be positive.");
69
70         //this function returns the SHA1 hash hashOut
71         EVP_Digest(pData, dataLen, hashOut, &outLen, EVP_sha1(), null);
72         SysTryReturn(NID_SEC_CRYPTO, outLen > 0, null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
73
74         //stores the hash in bytebuffer
75         std::unique_ptr<ByteBuffer> pOutput(new (std::nothrow) ByteBuffer());
76         SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
77
78         r = pOutput->Construct(outLen);
79         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
80
81         r = pOutput->SetArray(hashOut, 0, outLen);
82         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
83
84         pOutput->Flip();
85
86         return pOutput.release();
87 }
88
89 result
90 Sha1Hash::Initialize(void)
91 {
92         result r = E_SUCCESS;
93         int ret = 0;
94
95         SysAssertf(__pEvpMdCtx == null, "Already constructed. Calling Initialize() twice or more on a same instance is not allowed for this class.");
96
97         __pEvpMdCtx = new (std::nothrow) EVP_MD_CTX();
98         SysTryReturn(NID_SEC_CRYPTO, __pEvpMdCtx != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
99
100         //SHA1 init is called
101         ret = EVP_DigestInit(__pEvpMdCtx, EVP_sha1());
102         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
103
104 CATCH:
105
106         if (IsFailed(r))
107         {
108                 delete __pEvpMdCtx;
109                 __pEvpMdCtx = null;
110         }
111         return r;
112 }
113
114 result
115 Sha1Hash::Update(const ByteBuffer& input)
116 {
117         result r = E_SUCCESS;
118         int ret = 0;
119         int dataLen = 0;
120         byte* pData = null;
121
122         SysAssertf(__pEvpMdCtx != null, "Not yet constructed. Initialize() should be called before use.");
123
124         pData = const_cast< byte* >(input.GetPointer());
125         SysTryReturn(NID_SEC_CRYPTO, pData != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input data value should be valid.");
126
127         dataLen = input.GetRemaining();
128         SysTryReturn(NID_SEC_CRYPTO, dataLen > 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input data length should be positive.");
129
130         //SHA1_update is called
131         ret = EVP_DigestUpdate(__pEvpMdCtx, pData, dataLen);
132         SysTryReturn(NID_SEC_CRYPTO, ret == 1, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
133
134         return r;
135 }
136
137 ByteBuffer*
138 Sha1Hash::FinalizeN(void)
139 {
140         result r = E_SUCCESS;
141         int ret = 0;
142         int outLen = SHA_DIGEST_LENGTH;
143         std::unique_ptr<ByteBuffer> pOutput(null);
144
145         ClearLastResult();
146
147         SysAssertf(__pEvpMdCtx != null, "Not yet constructed. Initialize() should be called before use.");
148
149         std::unique_ptr<byte[]> pTempBuf(new (std::nothrow) byte[outLen]);
150         SysTryCatch(NID_SEC_CRYPTO, pTempBuf != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
151
152         //SHA1_final is called and is stored in pTempBuf
153         ret = EVP_DigestFinal(__pEvpMdCtx, pTempBuf.get(), null);
154         SysTryCatch(NID_SEC_CRYPTO, ret == 1, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
155
156         //hash is stored in bytebuffer
157         pOutput = std::unique_ptr<ByteBuffer>(new (std::nothrow) ByteBuffer());
158         SysTryCatch(NID_SEC_CRYPTO, pOutput != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
159
160         r = pOutput->Construct(outLen);
161         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory");
162
163         r = pOutput->SetArray(pTempBuf.get(), 0, outLen);
164         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] An unexpected system error occurred.");
165
166         pOutput->Flip();
167
168 CATCH:
169
170         EVP_MD_CTX_cleanup(__pEvpMdCtx);
171
172         if (IsFailed(r))
173         {
174                 pOutput.reset(null);
175         }
176
177         delete __pEvpMdCtx;
178         __pEvpMdCtx = null;
179
180         return pOutput.release();
181 }
182
183 } } } //Tizen::Security::Crypto