2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FSecPkcs_Rc2CbcParametersImpl.cpp
20 * @brief This is the implementation file for _Rc2CbcParametersImpl class.
22 * This header file contains the implementation of _Rc2CbcParametersImpl class.
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>
40 #include "FSecPkcs_Rc2CbcParametersImpl.h"
42 using namespace Tizen::Base;
44 namespace Tizen { namespace Security { namespace Pkcs
47 _Rc2CbcParametersImpl::_Rc2CbcParametersImpl(void)
48 : __rc2ParameterVersion(0)
53 _Rc2CbcParametersImpl::~_Rc2CbcParametersImpl(void)
59 _Rc2CbcParametersImpl::Construct(const Tizen::Base::ByteBuffer& initialVector, int version)
63 SysAssertf(__initialVector.GetRemaining() <= 0, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
65 SysTryReturnResult(NID_SEC_CRYPTO, initialVector.GetRemaining() > 0, E_INVALID_ARG, "The specified input parameter is invalid.");
70 __rc2ParameterVersion = 160;
72 else if (version == 120)
74 __rc2ParameterVersion = 120;
76 else if (version == 58)
78 __rc2ParameterVersion = 58;
80 else if (version >= 256)
82 __rc2ParameterVersion = version;
84 else if (version == 0)
86 __rc2ParameterVersion = 0;
94 r = __initialVector.Construct(initialVector);
95 SysTryReturnResult(NID_SEC_CRYPTO, !IsFailed(r), E_OUT_OF_MEMORY, "The memory is insufficient.");
101 _Rc2CbcParametersImpl::GetInitialVector(void) const
106 SysAssertf(__initialVector.GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
108 return __initialVector;
113 PkcsAlgorithmParameterType
114 _Rc2CbcParametersImpl::GetType(void) const
116 //Return the Parameter type
118 return PKCS_ALGO_PARAM_TYPE_RC2;
122 _Rc2CbcParametersImpl::GetVersion(void) const
124 return __rc2ParameterVersion;
128 _Rc2CbcParametersImpl::Equals(const Object& obj) const
130 result r = E_SUCCESS;
131 ByteBuffer initialVector;
132 std::unique_ptr< Tizen::Base::ByteBuffer > pInitialVector;
135 const _Rc2CbcParametersImpl* pOther = dynamic_cast< const _Rc2CbcParametersImpl* >(&obj);
137 SysTryReturn(NID_SEC_CRYPTO, pOther != null, false, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
144 r = initialVector.Construct(pOther->GetInitialVector());
145 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), false, E_OUT_OF_MEMORY, "The memory is insufficient.");
147 value = (__initialVector == initialVector) && (pOther->GetVersion() == __rc2ParameterVersion);
153 _Rc2CbcParametersImpl::GetHashCode(void) const
155 return __initialVector.GetHashCode() + Integer::GetHashCode(__rc2ParameterVersion);
159 _Rc2CbcParametersImpl::GetEncodedDataN(void) const
161 result r = E_SUCCESS;
164 ASN1_TYPE* pAsn1Type = null;
165 ASN1_OCTET_STRING* pInitialVectorStr = null;
166 std::unique_ptr< ByteBuffer > pEncRc2CbcParam;
171 SysAssertf(__initialVector.GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
173 if (__rc2ParameterVersion == 0)
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.");
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.");
181 pInitialVectorStr->length = __initialVector.GetRemaining();
183 memcpy(pInitialVectorStr->data, __initialVector.GetPointer(), __initialVector.GetRemaining());
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);
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.");
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.");
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.");
204 ASN1_TYPE_free(pAsn1Type);
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.");
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.");
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.");
216 pEncRc2CbcParam->Flip();
220 if (pInitialVectorStr != null)
222 ASN1_OCTET_STRING_free(pInitialVectorStr);
229 if (pAsn1Type != null)
231 ASN1_TYPE_free(pAsn1Type);
236 pEncRc2CbcParam.reset(null);
239 return pEncRc2CbcParam.release();
244 _Rc2CbcParametersImpl*
245 _Rc2CbcParametersImpl::GetInstance(Rc2CbcParameters& rc2CbcParameters)
247 return rc2CbcParameters.__pRc2CbcParametersImpl;
250 const _Rc2CbcParametersImpl*
251 _Rc2CbcParametersImpl::GetInstance(const Rc2CbcParameters& rc2CbcParameters)
253 return rc2CbcParameters.__pRc2CbcParametersImpl;