Merge "Fix DataSet memory leak" into tizen_2.1
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcsPkcs08PrivateKeyInfo.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                FSecPkcsPkcs08PrivateKeyInfo.cpp
20  * @brief               This is the implementation file for Pkcs08PrivateKeyInfo class.
21  *
22  * This header file contains the implementation of Pkcs08PrivateKeyInfo class.
23  *
24  */
25
26 #include <unique_ptr.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseByteBuffer.h>
29 #include <FBaseResult.h>
30 #include <FSecPkcsPkcs08PrivateKeyInfo.h>
31 #include <FSecPkcsAlgorithmIdentifier.h>
32 #include "FSecPkcs_Pkcs08PrivateKeyInfoImpl.h"
33
34 using namespace Tizen::Base;
35
36 namespace Tizen { namespace Security { namespace Pkcs
37 {
38
39 Pkcs08PrivateKeyInfo::Pkcs08PrivateKeyInfo(void)
40         : __pPkcs08PrivateKeyInfoImpl(null)
41 {
42
43 }
44
45 //Default Destructor
46 Pkcs08PrivateKeyInfo::~Pkcs08PrivateKeyInfo(void)
47 {
48
49 }
50
51
52 result
53 Pkcs08PrivateKeyInfo::Construct(const Tizen::Base::ByteBuffer& encodedData)
54 {
55         result r = E_SUCCESS;
56
57         SysAssertf(__pPkcs08PrivateKeyInfoImpl == null,
58                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
59
60         __pPkcs08PrivateKeyInfoImpl = new (std::nothrow) _Pkcs08PrivateKeyInfoImpl();
61         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs08PrivateKeyInfoImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
62
63         r = __pPkcs08PrivateKeyInfoImpl->Construct(encodedData);
64         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
65
66         return r;
67
68 CATCH:
69
70         delete __pPkcs08PrivateKeyInfoImpl;
71         __pPkcs08PrivateKeyInfoImpl = null;
72
73         return r;
74 }
75
76 result
77 Pkcs08PrivateKeyInfo::Construct(const AlgorithmIdentifier& privAlgoId, const Tizen::Base::ByteBuffer& privKey)
78 {
79         result r = E_SUCCESS;
80
81         SysAssertf(__pPkcs08PrivateKeyInfoImpl == null,
82                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
83
84         __pPkcs08PrivateKeyInfoImpl = new (std::nothrow) _Pkcs08PrivateKeyInfoImpl();
85         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs08PrivateKeyInfoImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
86
87
88         r = __pPkcs08PrivateKeyInfoImpl->Construct(privAlgoId, privKey);
89         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
90
91         return r;
92
93 CATCH:
94
95         delete __pPkcs08PrivateKeyInfoImpl;
96         __pPkcs08PrivateKeyInfoImpl = null;
97
98         return r;
99 }
100
101 int
102 Pkcs08PrivateKeyInfo::GetVersion(void) const
103 {
104         ClearLastResult();
105
106         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
107
108         return __pPkcs08PrivateKeyInfoImpl->GetVersion();
109
110 }
111
112 const AlgorithmIdentifier&
113 Pkcs08PrivateKeyInfo::GetPrivateKeyAlgorithm(void) const
114 {
115         ClearLastResult();
116
117         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
118
119         return __pPkcs08PrivateKeyInfoImpl->GetPrivateKeyAlgorithm();
120 }
121
122 const ByteBuffer&
123 Pkcs08PrivateKeyInfo::GetPrivateKey(void) const
124 {
125         ClearLastResult();
126
127         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
128
129         return __pPkcs08PrivateKeyInfoImpl->GetPrivateKey();
130 }
131
132 const Tizen::Base::Collection::ArrayList&
133 Pkcs08PrivateKeyInfo::GetAttributes(void) const
134 {
135         ClearLastResult();
136
137         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
138         return __pPkcs08PrivateKeyInfoImpl->GetAttributes();
139
140 }
141
142 result
143 Pkcs08PrivateKeyInfo::AddAttribute(const Pkcs08Attribute& attribute)
144 {
145         result r = E_SUCCESS;
146
147         ClearLastResult();
148
149         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
150         r = __pPkcs08PrivateKeyInfoImpl->AddAttribute(attribute);
151         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
152
153         return r;
154 }
155
156 result
157 Pkcs08PrivateKeyInfo::RemoveAttribute(const Pkcs08Attribute& attribute)
158 {
159         result r = E_SUCCESS;
160
161         ClearLastResult();
162
163         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
164         r = __pPkcs08PrivateKeyInfoImpl->RemoveAttribute(attribute);
165         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
166
167         return r;
168 }
169
170 ByteBuffer*
171 Pkcs08PrivateKeyInfo::GetEncodedDataN(void) const
172 {
173         result r = E_SUCCESS;
174
175         ClearLastResult();
176
177         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
178         std::unique_ptr< ByteBuffer > pEncodedData(__pPkcs08PrivateKeyInfoImpl->GetEncodedDataN());
179         SysTryReturn(NID_SEC_CRYPTO, pEncodedData != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
180
181         return pEncodedData.release();
182 }
183
184 } } }