Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcs_Rc2CbcParametersImpl.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                FSecPkcs_Rc2CbcParametersImpl.cpp
19  * @brief               This is the implementation file for _Rc2CbcParametersImpl class.
20  *
21  * This header file contains the implementation of _Rc2CbcParametersImpl class.
22  *
23  */
24
25 #include <new>
26 #include <openssl/x509.h>
27 #include <openssl/objects.h>
28 #include <openssl/obj_mac.h>
29 #include <openssl/evp.h>
30 #include <unique_ptr.h>
31 #include <FBaseErrors.h>
32 #include <FBaseByteBuffer.h>
33 #include <FBaseInteger.h>
34 #include <FBaseResult.h>
35 #include <FBaseSysLog.h>
36 #include <FSecPkcsIAlgorithmParameters.h>
37 #include <FSecPkcsRc2CbcParameters.h>
38
39 #include "FSecPkcs_Rc2CbcParametersImpl.h"
40
41 using namespace Tizen::Base;
42
43 namespace Tizen { namespace Security { namespace Pkcs
44 {
45
46 _Rc2CbcParametersImpl::_Rc2CbcParametersImpl(void)
47         : __rc2ParameterVersion(0)
48 {
49
50 }
51
52 _Rc2CbcParametersImpl::~_Rc2CbcParametersImpl(void)
53 {
54
55 }
56
57 result
58 _Rc2CbcParametersImpl::Construct(const Tizen::Base::ByteBuffer& initialVector, int version)
59 {
60         result r = E_SUCCESS;
61
62         SysAssertf(__initialVector.GetRemaining() <= 0, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
63
64         SysTryReturnResult(NID_SEC_CRYPTO, initialVector.GetRemaining() > 0, E_INVALID_ARG, "The specified input parameter is invalid.");
65
66
67         if (version == 160)
68         {
69                 __rc2ParameterVersion = 160;
70         }
71         else if (version == 120)
72         {
73                 __rc2ParameterVersion = 120;
74         }
75         else if (version == 58)
76         {
77                 __rc2ParameterVersion = 58;
78         }
79         else if (version >= 256)
80         {
81                 __rc2ParameterVersion = version;
82         }
83         else if (version == 0)
84         {
85                 __rc2ParameterVersion = 0;
86         }
87         else
88         {
89                 r = E_INVALID_ARG;
90                 return r;
91         }
92
93         r = __initialVector.Construct(initialVector);
94         SysTryReturnResult(NID_SEC_CRYPTO, !IsFailed(r), E_OUT_OF_MEMORY, "The memory is insufficient.");
95
96         return r;
97 }
98
99 const ByteBuffer&
100 _Rc2CbcParametersImpl::GetInitialVector(void) const
101 {
102
103         ClearLastResult();
104
105         SysAssertf(__initialVector.GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
106
107         return __initialVector;
108 }
109
110
111
112 PkcsAlgorithmParameterType
113 _Rc2CbcParametersImpl::GetType(void) const
114 {
115         //Return the Parameter type
116         ClearLastResult();
117         return PKCS_ALGO_PARAM_TYPE_RC2;
118 }
119
120 int
121 _Rc2CbcParametersImpl::GetVersion(void) const
122 {
123         return __rc2ParameterVersion;
124 }
125
126 bool
127 _Rc2CbcParametersImpl::Equals(const Object& obj) const
128 {
129         result r = E_SUCCESS;
130         ByteBuffer initialVector;
131         std::unique_ptr< Tizen::Base::ByteBuffer > pInitialVector;
132         bool value = false;
133
134         const _Rc2CbcParametersImpl* pOther = dynamic_cast< const _Rc2CbcParametersImpl* >(&obj);
135
136         SysTryReturn(NID_SEC_CRYPTO, pOther != null, false, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
137
138         if (pOther == this)
139         {
140                 return true;
141         }
142
143         r = initialVector.Construct(pOther->GetInitialVector());
144         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), false, E_OUT_OF_MEMORY, "The memory is insufficient.");
145
146         value = (__initialVector == initialVector) && (pOther->GetVersion() == __rc2ParameterVersion);
147
148         return value;
149 }
150
151 int
152 _Rc2CbcParametersImpl::GetHashCode(void) const
153 {
154         return __initialVector.GetHashCode() + Integer::GetHashCode(__rc2ParameterVersion);
155 }
156
157 ByteBuffer*
158 _Rc2CbcParametersImpl::GetEncodedDataN(void) const
159 {
160         result r = E_SUCCESS;
161         int ret = 0;
162         int value = 0;
163         ASN1_TYPE* pAsn1Type = null;
164         ASN1_OCTET_STRING* pInitialVectorStr = null;
165         std::unique_ptr< ByteBuffer > pEncRc2CbcParam;
166         byte* pTemp = {0, };
167
168         ClearLastResult();
169
170         SysAssertf(__initialVector.GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
171
172         if (__rc2ParameterVersion == 0)
173         {
174                 pInitialVectorStr = M_ASN1_OCTET_STRING_new();
175                 SysTryCatch(NID_SEC_CRYPTO, pInitialVectorStr != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
176
177                 pInitialVectorStr->data = static_cast< unsigned char* >(OPENSSL_malloc(__initialVector.GetRemaining()));
178                 SysTryCatch(NID_SEC_CRYPTO, pInitialVectorStr->data != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
179
180                 pInitialVectorStr->length = __initialVector.GetRemaining();
181
182                 memcpy(pInitialVectorStr->data, __initialVector.GetPointer(), __initialVector.GetRemaining());
183
184                 value = i2d_ASN1_OCTET_STRING(pInitialVectorStr, &pTemp);
185                 SysTryCatch(NID_SEC_CRYPTO, value > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
186                 SysTryCatch(NID_SEC_CRYPTO, pTemp != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
187
188         }
189         else
190         {
191                 pAsn1Type = ASN1_TYPE_new();
192                 SysTryCatch(NID_SEC_CRYPTO, pAsn1Type != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
193
194                 ret = ASN1_TYPE_set_int_octetstring(pAsn1Type, __rc2ParameterVersion, const_cast< unsigned char* >(__initialVector.GetPointer()), __initialVector.GetRemaining());
195                 SysTryCatch(NID_SEC_CRYPTO, ret > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
196
197                 value = i2d_ASN1_TYPE(pAsn1Type, &pTemp);
198                 SysTryCatch(NID_SEC_CRYPTO, value > 0, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
199                 SysTryCatch(NID_SEC_CRYPTO, pTemp != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
200
201         }
202
203         pEncRc2CbcParam = std::unique_ptr< ByteBuffer >(new (std::nothrow) ByteBuffer());
204         SysTryCatch(NID_SEC_CRYPTO, pEncRc2CbcParam, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
205
206         r = pEncRc2CbcParam->Construct(value);
207         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
208
209         r = pEncRc2CbcParam->SetArray(pTemp, 0, value);
210         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The method cannot proceed due to a severe system error.");
211
212         pEncRc2CbcParam->Flip();
213
214 CATCH:
215
216         if (pInitialVectorStr != null)
217         {
218                 ASN1_OCTET_STRING_free(pInitialVectorStr);
219         }
220         if (pTemp != null)
221         {
222                 OPENSSL_free(pTemp);
223         }
224
225         if (pAsn1Type != null)
226         {
227                 ASN1_TYPE_free(pAsn1Type);
228         }
229
230         if (IsFailed(r))
231         {
232                 pEncRc2CbcParam.reset(null);
233         }
234         SetLastResult(r);
235         return pEncRc2CbcParam.release();
236
237
238 }
239
240 _Rc2CbcParametersImpl*
241 _Rc2CbcParametersImpl::GetInstance(Rc2CbcParameters& rc2CbcParameters)
242 {
243         return rc2CbcParameters.__pRc2CbcParametersImpl;
244 }
245
246 const _Rc2CbcParametersImpl*
247 _Rc2CbcParametersImpl::GetInstance(const Rc2CbcParameters& rc2CbcParameters)
248 {
249         return rc2CbcParameters.__pRc2CbcParametersImpl;
250 }
251
252
253 } } }