Merge "Beautified source code of appfw/src/base/utility" into tizen_2.2
[platform/framework/native/appfw.git] / src / security / cert / FSecCertX509CertificateStore.cpp
1 //
2 // Copyright (c) 2012 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                FSecCertX509CertificateStore.cpp
19  * @brief               This is the implementation file for X509CertificateStore class.
20  */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <new>
24 #include <FBaseResult.h>
25 #include <FSecCertX509CertificateStore.h>
26 #include <FSecCertX509Certificate.h>
27 #include <FBaseSysLog.h>
28 #include <FSecCert_X509CertificateStoreImpl.h>
29 #include <FSec_AccessController.h>
30 #include <FSec_AccessControlTypes.h>
31 #include <FApp_AppInfo.h>
32
33 using namespace Tizen::Base;
34 using namespace Tizen::App;
35
36 namespace Tizen { namespace Security { namespace Cert
37 {
38
39 X509CertificateStore::X509CertificateStore(void)
40         : __pX509CertificateStoreImpl(null)
41 {
42         __pX509CertificateStoreImpl = new (std::nothrow) _X509CertificateStoreImpl();
43         SysTryReturnVoidResult(NID_SEC_CERT, __pX509CertificateStoreImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
44
45 }
46
47 X509CertificateStore::~X509CertificateStore(void)
48 {
49
50         delete __pX509CertificateStoreImpl;
51 }
52
53 result
54 X509CertificateStore::GetName(String& name) const
55 {
56         result r = E_SUCCESS;
57
58         SysAssertf(__pX509CertificateStoreImpl != null, "Not yet constructed. Reconstructor the object.");
59
60         r = _AccessController::CheckUserPrivilege(_PRV_CERTIFICATE_READ);
61         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
62
63         r = __pX509CertificateStoreImpl->GetName(name);
64         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] An internal system error occurred.", GetErrorMessage(r));
65
66         return r;
67 }
68
69 result
70 X509CertificateStore::SetCertificateSelector(const Tizen::Security::Cert::ICertificateSelector& selector)
71 {
72         result r = E_SUCCESS;
73
74         SysAssertf(__pX509CertificateStoreImpl != null, "Not yet constructed. Re-construct the object.");
75
76         if (selector.GetType() != USER_CERT)
77         {
78                 r = _AccessController::CheckUserPrivilege(_PRV_CERTIFICATE_READ);
79                 SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
80         }
81         r = __pX509CertificateStoreImpl->SetCertificateSelector(selector);
82         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] An internal system error occurred.", GetErrorMessage(r));
83
84         return r;
85
86 }
87
88 result
89 X509CertificateStore::GetCertificateCount(int& count) const
90 {
91         result r = E_SUCCESS;
92
93         SysAssertf(__pX509CertificateStoreImpl != null, "Not yet constructed. Reconstructor the object.");
94
95         r = __pX509CertificateStoreImpl->GetCertificateCount(count);
96         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] An internal system error occurred.", GetErrorMessage(r));
97
98         return r;
99
100 }
101
102
103 Tizen::Security::Cert::ICertificate*
104 X509CertificateStore::GetNextCertificateN(void) const
105 {
106         result r = E_SUCCESS;
107         ICertificate* pCert = null;
108         ClearLastResult();
109
110         SysAssertf(__pX509CertificateStoreImpl != null, "Not yet constructed. Reconstructor the object.");
111
112         pCert = __pX509CertificateStoreImpl->GetNextCertificateN();
113         r = GetLastResult();
114         SysTryReturn(NID_SEC_CERT, pCert != null, null, r, "[%s] An internal system error occurred.", GetErrorMessage(r));
115
116         return pCert;
117 }
118
119
120 result
121 X509CertificateStore::Insert(CertificateType certificateType, const Tizen::Security::Cert::ICertificate& certificate)
122 {
123         result r = E_SUCCESS;
124
125         SysAssertf(__pX509CertificateStoreImpl != null, "Not yet constructed. Reconstructor the object.");
126
127         r = __pX509CertificateStoreImpl->Insert(certificateType, certificate);
128         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] An internal system error occurred.", GetErrorMessage(r));
129
130         return r;
131 }
132
133
134 result
135 X509CertificateStore::InsertPkcs12(const String& filePath, const String& password)
136 {
137         result r = E_SUCCESS;
138
139         SysAssertf(__pX509CertificateStoreImpl != null, "Not yet constructed. Reconstructor the object.");
140
141         r = __pX509CertificateStoreImpl->InsertPkcs12(filePath, password);
142         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] An internal system error occurred.", GetErrorMessage(r));
143
144         return r;
145 }
146
147
148 result
149 X509CertificateStore::Update(CertificateType certificateType, const Tizen::Security::Cert::ICertificate& oldCert, const Tizen::Security::Cert::ICertificate& newCert)
150 {
151         result r = E_SUCCESS;
152
153         SysAssertf(__pX509CertificateStoreImpl != null, "Not yet constructed. Reconstructor the object.");
154
155         r = __pX509CertificateStoreImpl->Update(certificateType, oldCert, newCert);
156         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] An internal system error occurred.", GetErrorMessage(r));
157
158         return r;
159
160 }
161
162
163 result
164 X509CertificateStore::Remove(CertificateType certificateType, const Tizen::Security::Cert::ICertificate& certificate)
165 {
166         result r = E_SUCCESS;
167
168         SysAssertf(__pX509CertificateStoreImpl != null, "Not yet constructed. Reconstructor the object.");
169
170         r = __pX509CertificateStoreImpl->Remove(certificateType, certificate);
171         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] An internal system error occurred.", GetErrorMessage(r));
172
173         return r;
174 }
175
176 } } }       // Tizen::Security::Cert