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