2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FSecPkcs_Rc2CbcParametersImpl.cpp
19 * @brief This is the implementation file for _Rc2CbcParametersImpl class.
21 * This header file contains the implementation of _Rc2CbcParametersImpl class.
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>
39 #include "FSecPkcs_Rc2CbcParametersImpl.h"
41 using namespace Tizen::Base;
43 namespace Tizen { namespace Security { namespace Pkcs
46 _Rc2CbcParametersImpl::_Rc2CbcParametersImpl(void)
47 : __rc2ParameterVersion(0)
52 _Rc2CbcParametersImpl::~_Rc2CbcParametersImpl(void)
58 _Rc2CbcParametersImpl::Construct(const Tizen::Base::ByteBuffer& initialVector, int version)
62 SysAssertf(__initialVector.GetRemaining() <= 0, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
64 SysTryReturnResult(NID_SEC_CRYPTO, initialVector.GetRemaining() > 0, E_INVALID_ARG, "The specified input parameter is invalid.");
69 __rc2ParameterVersion = 160;
71 else if (version == 120)
73 __rc2ParameterVersion = 120;
75 else if (version == 58)
77 __rc2ParameterVersion = 58;
79 else if (version >= 256)
81 __rc2ParameterVersion = version;
83 else if (version == 0)
85 __rc2ParameterVersion = 0;
93 r = __initialVector.Construct(initialVector);
94 SysTryReturnResult(NID_SEC_CRYPTO, !IsFailed(r), E_OUT_OF_MEMORY, "The memory is insufficient.");
100 _Rc2CbcParametersImpl::GetInitialVector(void) const
105 SysAssertf(__initialVector.GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
107 return __initialVector;
112 PkcsAlgorithmParameterType
113 _Rc2CbcParametersImpl::GetType(void) const
115 //Return the Parameter type
117 return PKCS_ALGO_PARAM_TYPE_RC2;
121 _Rc2CbcParametersImpl::GetVersion(void) const
123 return __rc2ParameterVersion;
127 _Rc2CbcParametersImpl::Equals(const Object& obj) const
129 result r = E_SUCCESS;
130 ByteBuffer initialVector;
131 std::unique_ptr< Tizen::Base::ByteBuffer > pInitialVector;
134 const _Rc2CbcParametersImpl* pOther = dynamic_cast< const _Rc2CbcParametersImpl* >(&obj);
136 SysTryReturn(NID_SEC_CRYPTO, pOther != null, false, E_INVALID_ARG, "[E_INVALID_ARG] The specified input parameter is invalid.");
143 r = initialVector.Construct(pOther->GetInitialVector());
144 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), false, E_OUT_OF_MEMORY, "The memory is insufficient.");
146 value = (__initialVector == initialVector) && (pOther->GetVersion() == __rc2ParameterVersion);
152 _Rc2CbcParametersImpl::GetHashCode(void) const
154 return __initialVector.GetHashCode() + Integer::GetHashCode(__rc2ParameterVersion);
158 _Rc2CbcParametersImpl::GetEncodedDataN(void) const
160 result r = E_SUCCESS;
163 ASN1_TYPE* pAsn1Type = null;
164 ASN1_OCTET_STRING* pInitialVectorStr = null;
165 std::unique_ptr< ByteBuffer > pEncRc2CbcParam;
170 SysAssertf(__initialVector.GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
172 if (__rc2ParameterVersion == 0)
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.");
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.");
180 pInitialVectorStr->length = __initialVector.GetRemaining();
182 memcpy(pInitialVectorStr->data, __initialVector.GetPointer(), __initialVector.GetRemaining());
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.");
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.");
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.");
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.");
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.");
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.");
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.");
212 pEncRc2CbcParam->Flip();
216 if (pInitialVectorStr != null)
218 ASN1_OCTET_STRING_free(pInitialVectorStr);
225 if (pAsn1Type != null)
227 ASN1_TYPE_free(pAsn1Type);
232 pEncRc2CbcParam.reset(null);
235 return pEncRc2CbcParam.release();
240 _Rc2CbcParametersImpl*
241 _Rc2CbcParametersImpl::GetInstance(Rc2CbcParameters& rc2CbcParameters)
243 return rc2CbcParameters.__pRc2CbcParametersImpl;
246 const _Rc2CbcParametersImpl*
247 _Rc2CbcParametersImpl::GetInstance(const Rc2CbcParameters& rc2CbcParameters)
249 return rc2CbcParameters.__pRc2CbcParametersImpl;