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