Fix memory leaks in the Security namespace
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcsAlgorithmIdentifier.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                FSecPkcsAlgorithmIdentifier.cpp
20  * @brief               This is the implementation file for AlgorithmIdentifier class.
21  *
22  * This header file contains the implementation of AlgorithmIdentifier class.
23  *
24  */
25
26 #include <unique_ptr.h>
27 #include <FBaseByteBuffer.h>
28 #include <FBaseResult.h>
29 #include <FBaseSysLog.h>
30 #include <FSecPkcsAlgorithmIdentifier.h>
31 #include <FSecPkcsIAlgorithmParameters.h>
32
33 #include "FSecPkcs_AlgorithmIdentifierImpl.h"
34
35 using namespace Tizen::Base;
36
37 namespace Tizen { namespace Security { namespace Pkcs
38 {
39
40 const wchar_t OID_PKCS_05[] = L"1.2.840.113549.1.5";
41 const wchar_t OID_PBKDF2[] = L"1.2.840.113549.1.5.12";
42 const wchar_t OID_PBES2[] = L"1.2.840.113549.1.5.13";
43 const wchar_t OID_PBMAC1[] = L"1.2.840.113549.1.5.14";
44 const wchar_t OID_HMAC_SHA1[] = L"1.2.840.113549.2.7";
45 const wchar_t OID_HMAC_SHA2_224[] = L"1.2.840.113549.2.8";
46 const wchar_t OID_HMAC_SHA2_256[] = L"1.2.840.113549.2.9";
47 const wchar_t OID_HMAC_SHA2_384[] = L"1.2.840.113549.2.10";
48 const wchar_t OID_HMAC_SHA2_512[] = L"1.2.840.113549.2.11";
49 const wchar_t OID_DES_CBC[] = L"1.3.14.3.2.7";
50 const wchar_t OID_DES_CBC_EDE3[] = L"1.2.840.113549.3.7";
51 const wchar_t OID_AES_128_CBC[] = L"2.16.840.1.101.3.4.1.2";
52 const wchar_t OID_AES_192_CBC[] = L"2.16.840.1.101.3.4.1.22";
53 const wchar_t OID_AES_256_CBC[] = L"2.16.840.1.101.3.4.1.42";
54 const wchar_t OID_RC2_CBC[] = L"1.2.840.113549.3.2";
55 const wchar_t OID_PKCS_08[] = L"1.2.840.113549.1.8";
56 const wchar_t OID_RSA_ENCRYPTION[] = L"1.2.804.113549.1.1.1";
57 const wchar_t OID_ATTR_NAME[] = L"2.5.4.41";
58 const wchar_t OID_ATTR_SURNAME[] = L"2.5.4.4";
59 const wchar_t OID_ATTR_GIVEN_NAME[] = L"2.5.4.42";
60 const wchar_t OID_ATTR_INITIAL[] = L"2.5.4.43";
61 const wchar_t OID_ATTR_GEN_QUALIFIER[] = L"2.5.4.44";
62 const wchar_t OID_ATTR_COMMON_NAME[] = L"2.5.4.3";
63 const wchar_t OID_ATTR_LOCALITY_NAME[] = L"2.5.4.7";
64 const wchar_t OID_ATTR_STATE_OR_PROV_NAME[] = L"2.5.4.8";
65 const wchar_t OID_ATTR_ORG_NAME[] = L"2.5.4.10";
66 const wchar_t OID_ATTR_ORG_UNIT_NAME[] = L"2.5.4.11";
67 const wchar_t OID_ATTR_TITLE[] = L"2.5.4.12";
68 const wchar_t OID_ATTR_DN_QUALIFIER[] = L"2.5.4.46";
69 const wchar_t OID_ATTR_COUNTRY_NAME[] = L"2.5.4.6";
70 const wchar_t OID_ATTR_SERIAL_NUMBER[] = L"2.5.4.5";
71 const wchar_t OID_ATTR_PSEUDONYM[] = L"2.5.4.65";
72 const wchar_t OID_ATTR_DOMAIN_COMPONENT[] = L"0.9.2342.19200300.100.1.25";
73 const wchar_t OID_ATTR_EMAIL_ADDRESS[] = L"1.2.840.113549.1.9.1";
74
75
76 AlgorithmIdentifier::AlgorithmIdentifier(void)
77         : __pAlgorithmIdentifierImpl(null)
78 {
79
80 }
81 AlgorithmIdentifier::~AlgorithmIdentifier(void)
82 {
83         delete __pAlgorithmIdentifierImpl;
84 }
85
86 result
87 AlgorithmIdentifier::Construct(const Tizen::Base::String& algorithm, const IAlgorithmParameters* pAlgoParams)
88 {
89         result r = E_SUCCESS;
90
91         SysAssertf(__pAlgorithmIdentifierImpl == null,
92                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
93
94
95         __pAlgorithmIdentifierImpl = new (std::nothrow) _AlgorithmIdentifierImpl();
96         SysTryReturnResult(NID_SEC_CRYPTO, __pAlgorithmIdentifierImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
97
98         r = __pAlgorithmIdentifierImpl->Construct(algorithm, pAlgoParams);
99         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
100
101         return r;
102
103 CATCH:
104
105         delete __pAlgorithmIdentifierImpl;
106         __pAlgorithmIdentifierImpl = null;
107
108         return r;
109 }
110
111 result
112 AlgorithmIdentifier::Construct(const Tizen::Base::ByteBuffer& encodedData)
113 {
114         result r = E_SUCCESS;
115
116         SysAssertf(__pAlgorithmIdentifierImpl == null,
117                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
118
119         __pAlgorithmIdentifierImpl = new (std::nothrow) _AlgorithmIdentifierImpl();
120         SysTryReturnResult(NID_SEC_CRYPTO, __pAlgorithmIdentifierImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
121
122         r = __pAlgorithmIdentifierImpl->Construct(encodedData);
123         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
124
125         return r;
126
127 CATCH:
128
129         delete __pAlgorithmIdentifierImpl;
130         __pAlgorithmIdentifierImpl = null;
131
132         return r;
133 }
134
135 AlgorithmIdentifier*
136 AlgorithmIdentifier::CloneN(void) const
137 {
138         result r = E_SUCCESS;
139
140         ClearLastResult();
141
142         SysAssertf(__pAlgorithmIdentifierImpl != null, "Not yet constructed. Reconstructor the object.");
143         SysAssertf(__pAlgorithmIdentifierImpl->GetAlgorithmObjectId().GetLength() > 0, "Not yet constructed. Reconstructor the object.");
144
145         std::unique_ptr< AlgorithmIdentifier > pObj(new (std::nothrow) AlgorithmIdentifier());
146         SysTryReturn(NID_SEC_CRYPTO, pObj, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
147
148         std::unique_ptr< IAlgorithmParameters > pParam(__pAlgorithmIdentifierImpl->GetParametersN());
149         SysTryReturn(NID_SEC_CRYPTO, pParam, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
150
151         r = pObj->Construct(__pAlgorithmIdentifierImpl->GetAlgorithmObjectId(), pParam.get());
152         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
153
154         return pObj.release();
155 }
156
157 String
158 AlgorithmIdentifier::GetAlgorithmObjectId(void) const
159 {
160         ClearLastResult();
161
162         SysAssertf(__pAlgorithmIdentifierImpl != null, "Not yet constructed. Reconstructor the object.");
163
164         return __pAlgorithmIdentifierImpl->__algorithm;
165 }
166
167 IAlgorithmParameters*
168 AlgorithmIdentifier::GetParametersN(void) const
169 {
170         //Return the Algorithm Parameter
171         result r = E_SUCCESS;
172
173         ClearLastResult();
174
175         SysAssertf(__pAlgorithmIdentifierImpl != null, "Not yet constructed. Reconstructor the object.");
176
177         std::unique_ptr< IAlgorithmParameters > pObj(__pAlgorithmIdentifierImpl->GetParametersN());
178         SysTryReturn(NID_IO, pObj != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
179
180         return pObj.release();
181
182 }
183
184 ByteBuffer*
185 AlgorithmIdentifier::GetEncodedDataN(void) const
186 {
187         result r = E_SUCCESS;
188
189         ClearLastResult();
190
191         SysAssertf(__pAlgorithmIdentifierImpl != null, "Not yet constructed. Reconstructor the object.");
192
193         std::unique_ptr< ByteBuffer > pEncAlgoIdParam(__pAlgorithmIdentifierImpl->GetEncodedDataN());
194         SysTryReturn(NID_IO, pEncAlgoIdParam != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
195
196         return pEncAlgoIdParam.release();
197
198 }
199
200 bool
201 AlgorithmIdentifier::Equals(const Object& obj) const
202 {
203         ClearLastResult();
204
205         SysAssertf(__pAlgorithmIdentifierImpl != null, "Not yet constructed. Reconstructor the object.");
206
207         const AlgorithmIdentifier* pOther = dynamic_cast< const AlgorithmIdentifier* >(&obj);
208         if (pOther == null)
209         {
210                 return false;
211         }
212
213         return __pAlgorithmIdentifierImpl->Equals(*pOther->__pAlgorithmIdentifierImpl);
214 }
215
216 int
217 AlgorithmIdentifier::GetHashCode(void) const
218 {
219         ClearLastResult();
220         SysAssertf(__pAlgorithmIdentifierImpl != null, "Not yet constructed. Reconstructor the object.");
221
222         return __pAlgorithmIdentifierImpl->GetHashCode();
223
224 }
225
226 } }}