sync with master
[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         __pPkcs08PrivateKeyInfoImpl = new (std::nothrow) _Pkcs08PrivateKeyInfoImpl();
43         SysTryReturnVoidResult(NID_SEC_CRYPTO, __pPkcs08PrivateKeyInfoImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
44 }
45
46 //Default Destructor
47 Pkcs08PrivateKeyInfo::~Pkcs08PrivateKeyInfo(void)
48 {
49         delete __pPkcs08PrivateKeyInfoImpl;
50 }
51
52
53 result
54 Pkcs08PrivateKeyInfo::Construct(const Tizen::Base::ByteBuffer& encodedData)
55 {
56         result r = E_SUCCESS;
57
58         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
59         r = __pPkcs08PrivateKeyInfoImpl->Construct(encodedData);
60         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
61
62         return r;
63 }
64
65 result
66 Pkcs08PrivateKeyInfo::Construct(const AlgorithmIdentifier& privAlgoId, const Tizen::Base::ByteBuffer& privKey)
67 {
68         result r = E_SUCCESS;
69
70         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
71         r = __pPkcs08PrivateKeyInfoImpl->Construct(privAlgoId, privKey);
72         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
73
74         return r;
75 }
76
77 int
78 Pkcs08PrivateKeyInfo::GetVersion(void) const
79 {
80         ClearLastResult();
81
82         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
83
84         return __pPkcs08PrivateKeyInfoImpl->GetVersion();
85
86 }
87
88 const AlgorithmIdentifier&
89 Pkcs08PrivateKeyInfo::GetPrivateKeyAlgorithm(void) const
90 {
91         ClearLastResult();
92
93         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
94
95         return __pPkcs08PrivateKeyInfoImpl->GetPrivateKeyAlgorithm();
96 }
97
98 const ByteBuffer&
99 Pkcs08PrivateKeyInfo::GetPrivateKey(void) const
100 {
101         ClearLastResult();
102
103         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
104
105         return __pPkcs08PrivateKeyInfoImpl->GetPrivateKey();
106 }
107
108 const Tizen::Base::Collection::ArrayList&
109 Pkcs08PrivateKeyInfo::GetAttributes(void) const
110 {
111         ClearLastResult();
112
113         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
114         return __pPkcs08PrivateKeyInfoImpl->GetAttributes();
115
116 }
117
118 result
119 Pkcs08PrivateKeyInfo::AddAttribute(const Pkcs08Attribute& attribute)
120 {
121         result r = E_SUCCESS;
122
123         ClearLastResult();
124
125         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
126         r = __pPkcs08PrivateKeyInfoImpl->AddAttribute(attribute);
127         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
128
129         return r;
130 }
131
132 result
133 Pkcs08PrivateKeyInfo::RemoveAttribute(const Pkcs08Attribute& attribute)
134 {
135         result r = E_SUCCESS;
136
137         ClearLastResult();
138
139         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
140         r = __pPkcs08PrivateKeyInfoImpl->RemoveAttribute(attribute);
141         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
142
143         return r;
144 }
145
146 ByteBuffer*
147 Pkcs08PrivateKeyInfo::GetEncodedDataN(void) const
148 {
149         result r = E_SUCCESS;
150
151         ClearLastResult();
152
153         SysAssertf(__pPkcs08PrivateKeyInfoImpl != null, "Not yet constructed. Reconstructor the object.");
154         std::unique_ptr< ByteBuffer > pEncodedData(__pPkcs08PrivateKeyInfoImpl->GetEncodedDataN());
155         SysTryReturn(NID_SEC_CRYPTO, pEncodedData != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
156
157         return pEncodedData.release();
158 }
159
160 } } }