Fix memory leaks in the Security namespace
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcsPkcs05PbKdf2Parameters.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSecPkcsPkcs05PbKdf2Parameters.cpp
20  * @brief               This is the implementation file for Pkcs05PbKdf2Parameters class.
21  *
22  * This header file contains the implementation of Pkcs05PbKdf2Parameters class.
23  *
24  */
25
26 #include <unique_ptr.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseByteBuffer.h>
29 #include <FBaseResult.h>
30 #include <FBaseInteger.h>
31 #include <FSecPkcsPkcs05PbKdf2Parameters.h>
32 #include <FSecPkcsIAlgorithmParameters.h>
33 #include <FSecPkcsAlgorithmIdentifier.h>
34
35 #include "FSecPkcs_Pkcs05PbKdf2ParametersImpl.h"
36
37 using namespace Tizen::Base;
38
39 namespace Tizen { namespace Security { namespace Pkcs
40 {
41
42 Pkcs05PbKdf2Parameters::Pkcs05PbKdf2Parameters(void)
43         : __pPkcs05PbKdf2ParametersImpl(null)
44 {
45
46 }
47
48 Pkcs05PbKdf2Parameters::~Pkcs05PbKdf2Parameters(void)
49 {
50         delete __pPkcs05PbKdf2ParametersImpl;
51 }
52
53 result
54 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& encodedData)
55 {
56         result r = E_SUCCESS;
57
58         SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
59                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
60
61         __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
62         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
63
64         r = __pPkcs05PbKdf2ParametersImpl->Construct(encodedData);
65         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
66
67         return r;
68
69 CATCH:
70
71         delete __pPkcs05PbKdf2ParametersImpl;
72         __pPkcs05PbKdf2ParametersImpl = null;
73
74         return r;
75 }
76
77 result
78 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& saltBuffer, int iterationCount, int derivedKeyLength)
79 {
80         result r = E_SUCCESS;
81
82         SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
83                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
84
85         __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
86         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
87
88         r = __pPkcs05PbKdf2ParametersImpl->Construct(saltBuffer, iterationCount, derivedKeyLength);
89         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
90
91         return r;
92
93 CATCH:
94
95         delete __pPkcs05PbKdf2ParametersImpl;
96         __pPkcs05PbKdf2ParametersImpl = null;
97
98         return r;
99 }
100
101
102 result
103 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& saltBuffer, int iterationCount, const AlgorithmIdentifier& prf, int derivedKeyLength)
104 {
105         result r = E_SUCCESS;
106
107         SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
108                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
109
110         __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
111         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
112
113         r = __pPkcs05PbKdf2ParametersImpl->Construct(saltBuffer, iterationCount, prf, derivedKeyLength);
114         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
115
116         return r;
117
118 CATCH:
119
120         delete __pPkcs05PbKdf2ParametersImpl;
121         __pPkcs05PbKdf2ParametersImpl = null;
122
123         return r;
124 }
125
126 ByteBuffer*
127 Pkcs05PbKdf2Parameters::GetEncodedDataN(void) const
128 {
129         result r = E_SUCCESS;
130
131         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
132         std::unique_ptr< ByteBuffer > pEncKdfParam(__pPkcs05PbKdf2ParametersImpl->GetEncodedDataN());
133         SysTryReturn(NID_SEC_CRYPTO, pEncKdfParam != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
134
135         return pEncKdfParam.release();
136
137 }
138
139 const ByteBuffer&
140 Pkcs05PbKdf2Parameters::GetSaltValue(void) const
141 {
142         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
143
144         return __pPkcs05PbKdf2ParametersImpl->GetSaltValue();
145 }
146
147 int
148 Pkcs05PbKdf2Parameters::GetIterationCount(void) const
149 {
150         ClearLastResult();
151
152         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
153
154         return __pPkcs05PbKdf2ParametersImpl->__iterationCount;
155 }
156 int
157 Pkcs05PbKdf2Parameters::GetDerivedKeyLength(void) const
158 {
159         ClearLastResult();
160
161         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
162
163         return __pPkcs05PbKdf2ParametersImpl->__derivedKeyLength;
164 }
165
166 const AlgorithmIdentifier&
167 Pkcs05PbKdf2Parameters::GetPrf(void) const
168 {
169         ClearLastResult();
170
171         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
172
173         return __pPkcs05PbKdf2ParametersImpl->GetPrf();
174 }
175
176 IAlgorithmParameters*
177 Pkcs05PbKdf2Parameters::CloneN(void) const
178 {
179         result r = E_SUCCESS;
180
181         ClearLastResult();
182
183         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
184         SysAssertf(__pPkcs05PbKdf2ParametersImpl->GetSaltValue().GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
185
186         std::unique_ptr< Pkcs05PbKdf2Parameters > pKdf2Parameters(new (std::nothrow) Pkcs05PbKdf2Parameters());
187         SysTryReturn(NID_SEC_CRYPTO, pKdf2Parameters, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
188
189         if (__pPkcs05PbKdf2ParametersImpl->GetPrf().GetAlgorithmObjectId().GetLength() > 0)
190         {
191                 r = pKdf2Parameters->Construct(__pPkcs05PbKdf2ParametersImpl->GetSaltValue(), __pPkcs05PbKdf2ParametersImpl->GetIterationCount(), __pPkcs05PbKdf2ParametersImpl->GetPrf(), __pPkcs05PbKdf2ParametersImpl->GetDerivedKeyLength());
192         }
193         else
194         {
195                 r = pKdf2Parameters->Construct(__pPkcs05PbKdf2ParametersImpl->GetSaltValue(), __pPkcs05PbKdf2ParametersImpl->GetIterationCount(), __pPkcs05PbKdf2ParametersImpl->GetDerivedKeyLength());
196         }
197
198         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, r, "[%s] Failed to construct key derivation parameters.", GetErrorMessage(r));
199
200         return pKdf2Parameters.release();
201 }
202
203 PkcsAlgorithmParameterType
204 Pkcs05PbKdf2Parameters::GetType(void) const
205 {
206         ClearLastResult();
207
208         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
209
210         return __pPkcs05PbKdf2ParametersImpl->GetType();
211 }
212
213
214 bool
215 Pkcs05PbKdf2Parameters::Equals(const Object& obj) const
216 {
217         ClearLastResult();
218
219         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
220         const Pkcs05PbKdf2Parameters* pOther = dynamic_cast< const Pkcs05PbKdf2Parameters* >(&obj);
221         if (pOther == null)
222         {
223                 return false;
224         }
225
226         return __pPkcs05PbKdf2ParametersImpl->Equals(*pOther->__pPkcs05PbKdf2ParametersImpl);
227
228 }
229
230 int
231 Pkcs05PbKdf2Parameters::GetHashCode(void) const
232 {
233         ClearLastResult();
234
235         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
236
237         return __pPkcs05PbKdf2ParametersImpl->GetHashCode();
238 }
239
240 } } } // end of namespace Pkcs