Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcs_Pkcs05PbMacParametersImpl.cpp
1 //
2 // Copyright (c) 2013 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                FSecPkcsPkcs05PbMacParameters.cpp
19  * @brief               This is the implementation file for Pkcs05PbMacParameters class.
20  *
21  * This header file contains the implementation of Pkcs05PbMacParameters class.
22  *
23  */
24
25 #include <new>
26 #include <openssl/evp.h>
27 #include <openssl/crypto.h>
28 #include <openssl/x509.h>
29 #include <openssl/hmac.h>
30 #include <openssl/objects.h>
31 #include <openssl/obj_mac.h>
32 #include <unique_ptr.h>
33 #include <FBaseSysLog.h>
34 #include <FBaseByteBuffer.h>
35 #include <FBaseResult.h>
36 #include <FSecPkcsIAlgorithmParameters.h>
37 #include <FSecPkcsPkcs05PbKdf2Parameters.h>
38 #include <FSecPkcsAlgorithmIdentifier.h>
39 #include <FSecPkcsTypes.h>
40 #include <FSecPkcsPkcs05PbMacParameters.h>
41 #include "FSecPkcs_PkcsUtility.h"
42 #include "FSecPkcs_Pkcs05PbMacParametersImpl.h"
43
44 using namespace Tizen::Base;
45
46 namespace Tizen { namespace Security { namespace Pkcs
47 {
48
49
50 _Pkcs05PbMacParametersImpl::_Pkcs05PbMacParametersImpl(void)
51 {
52
53 }
54
55
56
57 _Pkcs05PbMacParametersImpl::~_Pkcs05PbMacParametersImpl(void)
58 {
59         //do nothing
60 }
61
62 result
63 _Pkcs05PbMacParametersImpl::Construct(const Tizen::Base::ByteBuffer& encodedData)
64 {
65         result r = E_SUCCESS;
66         const byte* pBuffer = null;
67         const unsigned char* pBuf = null;
68         int bufferLen = 0;
69         int nidMacAlgo = 0;
70         int paramLen = 0;
71         PBE2PARAM* pMacObj = null;
72         std::unique_ptr< ByteBuffer > pKdfParam;
73         Tizen::Base::String macOid = null;
74         ASN1_TYPE* pParam = null;
75         std::unique_ptr< Pkcs05PbKdf2Parameters > pKdf2Parameters;
76
77         pBuffer = encodedData.GetPointer();
78         SysTryReturnResult(NID_SEC_CRYPTO, pBuffer != null, E_INVALID_ARG, "The specified input parameter is invalid.");
79
80         bufferLen = encodedData.GetRemaining();
81         SysTryReturnResult(NID_SEC_CRYPTO, bufferLen > 0, E_INVALID_ARG, "The specified input parameter is invalid.");
82
83         pMacObj = d2i_PBE2PARAM(null, reinterpret_cast< const unsigned char** >(&pBuffer), bufferLen);
84         SysTryReturnResult(NID_SEC_CRYPTO, pMacObj != null, E_INVALID_ARG, "The specified input parameter is invalid.");
85
86         SysTryCatch(NID_SEC_CRYPTO, OBJ_obj2nid(pMacObj->keyfunc->algorithm) == NID_id_pbkdf2, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
87
88         pParam = pMacObj->keyfunc->parameter;
89         SysTryReturn(NID_SEC_CRYPTO, !((!pParam) || (pParam->type != V_ASN1_SEQUENCE)), E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
90
91         pBuf = pParam->value.sequence->data;
92         SysTryCatch(NID_SEC_CRYPTO, pBuf != null, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
93
94         paramLen = pParam->value.sequence->length;
95         SysTryCatch(NID_SEC_CRYPTO, paramLen > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
96
97         pKdfParam = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
98         SysTryCatch(NID_SEC_CRYPTO, pKdfParam, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
99
100         r = pKdfParam->Construct(paramLen);
101         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
102
103         r = pKdfParam->SetArray(pBuf, 0, paramLen);
104         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
105
106         pKdfParam->Flip();
107
108         pKdf2Parameters = std::unique_ptr< Pkcs05PbKdf2Parameters >(new (std::nothrow) Pkcs05PbKdf2Parameters());
109         SysTryCatch(NID_SEC_CRYPTO, pKdf2Parameters, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
110
111         r = pKdf2Parameters->Construct(*pKdfParam);
112         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
113
114         __keyDerivationFunction.Construct(OID_PBKDF2, pKdf2Parameters.get());
115         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
116
117         nidMacAlgo = OBJ_obj2nid(pMacObj->encryption->algorithm);
118         macOid = _PkcsUtility::ConvertToOid(nidMacAlgo);
119
120         r = GetLastResult();
121         SysTryCatch(NID_SEC_CRYPTO, r != E_UNSUPPORTED_ALGORITHM, r = E_UNSUPPORTED_ALGORITHM, E_UNSUPPORTED_ALGORITHM, "[E_UNSUPPORTED_ALGORITHM] The input algorithm is not supported.");
122
123         r = __messageAuthScheme.Construct(macOid, null);
124         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
125
126 CATCH:
127
128         PBE2PARAM_free(pMacObj);
129         return r;
130 }
131
132 result
133 _Pkcs05PbMacParametersImpl::Construct(const AlgorithmIdentifier& keyDerivationFunction, const AlgorithmIdentifier& messageAuthScheme)
134 {
135         result r = E_SUCCESS;
136
137         std::unique_ptr< IAlgorithmParameters > pParam(keyDerivationFunction.GetParametersN());
138         SysTryReturn(NID_SEC_CRYPTO, pParam != null, r, r, "[%s] Failed to get the parameters.", GetErrorMessage(r));
139
140         r = __keyDerivationFunction.Construct(keyDerivationFunction.GetAlgorithmObjectId(), pParam.get());
141         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to construct the algorithm identifier.", GetErrorMessage(r));
142
143         std::unique_ptr< IAlgorithmParameters > pAuthParam(messageAuthScheme.GetParametersN());
144         SysTryReturn(NID_SEC_CRYPTO, pParam != null, r, r, "[%s] Failed to get the parameters.", GetErrorMessage(r));
145
146         r = __messageAuthScheme.Construct(messageAuthScheme.GetAlgorithmObjectId(), pAuthParam.get());
147         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to construct the algorithm identifier.", GetErrorMessage(r));
148
149         return r;
150 }
151
152
153 ByteBuffer*
154 _Pkcs05PbMacParametersImpl::GetEncodedDataN(void) const
155 {
156
157         result r = E_SUCCESS;
158         PBE2PARAM* pMacObj = null;
159         byte* pTemp = {0, };
160         int value = 0;
161         int algoNid = 0;
162         int prfNid = 0;
163         int ret = 0;
164         Tizen::Base::String keyOid = null;
165         Tizen::Base::String macAlgo = null;
166         Tizen::Base::String prfOid = null;
167         std::unique_ptr< Pkcs05PbKdf2Parameters > pKeyParams;
168         std::unique_ptr< ByteBuffer > pSalt;
169         std::unique_ptr< ByteBuffer > pEncMacParam;
170
171         SysAssertf(__keyDerivationFunction.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
172         SysAssertf(__messageAuthScheme.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
173
174         keyOid = __keyDerivationFunction.GetAlgorithmObjectId();
175
176         macAlgo = __messageAuthScheme.GetAlgorithmObjectId();
177
178         pKeyParams = std::unique_ptr< Pkcs05PbKdf2Parameters >(dynamic_cast< Pkcs05PbKdf2Parameters* >(__keyDerivationFunction.GetParametersN()));
179         SysTryReturn(NID_SEC_CRYPTO, pKeyParams, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
180
181         pSalt = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
182         SysTryCatch(NID_SEC_CRYPTO, pSalt != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
183
184         SysTryCatch(NID_SEC_CRYPTO, pKeyParams->GetSaltValue().GetRemaining() > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
185
186         r = pSalt->Construct(pKeyParams->GetSaltValue());
187         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
188
189         pMacObj = PBE2PARAM_new();
190         SysTryReturn(NID_SEC_CRYPTO, pMacObj != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
191
192         prfOid = (pKeyParams->GetPrf()).GetAlgorithmObjectId();
193         prfNid = _PkcsUtility::ConvertToNid(prfOid);
194
195         pMacObj->keyfunc = _PkcsUtility::GenerateKdfParametersN(pKeyParams->GetIterationCount(), const_cast< unsigned char* >(pSalt->GetPointer()), pSalt->GetRemaining(), prfNid, pKeyParams->GetDerivedKeyLength());
196         SysTryCatch(NID_SEC_CRYPTO, pMacObj->keyfunc != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
197
198         algoNid = _PkcsUtility::ConvertToNid(macAlgo);
199
200         pMacObj->encryption = X509_ALGOR_new();
201         SysTryCatch(NID_SEC_CRYPTO, (pMacObj->encryption != null), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
202
203         ret = X509_ALGOR_set0(pMacObj->encryption, OBJ_nid2obj(algoNid), V_ASN1_NULL, NULL);
204         SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
205
206         // encode the PBE2PARAM structure
207
208         value = i2d_PBE2PARAM(pMacObj, &pTemp);
209         SysTryCatch(NID_SEC_CRYPTO, value > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
210
211         pEncMacParam = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
212         SysTryCatch(NID_SEC_CRYPTO, pEncMacParam, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
213
214         r = pEncMacParam->Construct(value);
215         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
216
217         r = pEncMacParam->SetArray(pTemp, 0, value);
218         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
219
220         pEncMacParam->Flip();
221
222 CATCH:
223
224         if (IsFailed(r))
225         {
226                 pEncMacParam.reset(null);
227         }
228         PBE2PARAM_free(pMacObj);
229         OPENSSL_free(pTemp);
230         SetLastResult(r);
231         return pEncMacParam.release();
232
233 }
234
235
236
237 const AlgorithmIdentifier&
238 _Pkcs05PbMacParametersImpl::GetKeyDerivationAlgorithm(void) const
239 {
240         ClearLastResult();
241
242         SysAssertf(__keyDerivationFunction.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
243         SysAssertf(__messageAuthScheme.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
244
245         return __keyDerivationFunction;
246 }
247
248 const AlgorithmIdentifier&
249 _Pkcs05PbMacParametersImpl::GetMacAlgorithm(void) const
250 {
251         ClearLastResult();
252
253         SysAssertf(__keyDerivationFunction.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
254         SysAssertf(__messageAuthScheme.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
255
256         return __messageAuthScheme;
257 }
258
259 PkcsAlgorithmParameterType
260 _Pkcs05PbMacParametersImpl::GetType(void) const
261 {
262         ClearLastResult();
263         return PKCS_ALGO_PARAM_TYPE_PKCS05_MAC;
264 }
265
266 bool
267 _Pkcs05PbMacParametersImpl::Equals(const Object& obj) const
268 {
269         bool value = false;
270
271         SysAssertf(__keyDerivationFunction.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
272         SysAssertf(__messageAuthScheme.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
273
274         const _Pkcs05PbMacParametersImpl* pOther = dynamic_cast< const _Pkcs05PbMacParametersImpl* >(&obj);
275
276         SysTryReturn(NID_SEC_CRYPTO, pOther != null, false, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
277
278         if (pOther == this)
279         {
280                 return true;
281         }
282
283         SysTryReturn(NID_SEC_CRYPTO, pOther->GetKeyDerivationAlgorithm().GetAlgorithmObjectId().GetLength() > 0, false, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
284         SysTryReturn(NID_SEC_CRYPTO, pOther->GetMacAlgorithm().GetAlgorithmObjectId().GetLength() > 0, false, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
285
286         value = (__keyDerivationFunction.Equals(pOther->GetKeyDerivationAlgorithm())) && (__messageAuthScheme.Equals(pOther->GetMacAlgorithm()));
287
288         return value;
289
290 }
291
292 int
293 _Pkcs05PbMacParametersImpl::GetHashCode(void) const
294 {
295         SysAssertf(__keyDerivationFunction.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
296         SysAssertf(__messageAuthScheme.GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Construct () should be called before use.");
297
298         return __keyDerivationFunction.GetHashCode() + __messageAuthScheme.GetHashCode();
299 }
300
301 _Pkcs05PbMacParametersImpl*
302 _Pkcs05PbMacParametersImpl::GetInstance(Pkcs05PbMacParameters& pkcs05PbMacParameters)
303 {
304         return pkcs05PbMacParameters.__pPkcs05PbMacParametersImpl;
305 }
306
307 const _Pkcs05PbMacParametersImpl*
308 _Pkcs05PbMacParametersImpl::GetInstance(const Pkcs05PbMacParameters& pkcs05PbMacParameters)
309 {
310         return pkcs05PbMacParameters.__pPkcs05PbMacParametersImpl;
311 }
312
313 } } } // end of namespace Pkcs