2 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file CertificateService.cpp
19 * @brief This is the implementation file for CertificateService class.
29 #include <unique_ptr.h>
32 #include <FOspConfig.h>
33 #include <FBaseString.h>
34 #include <FBaseByteBuffer.h>
35 #include <FBaseResult.h>
36 #include <FBaseSysLog.h>
37 #include <FBaseRt_EventDispatcher.h>
38 #include <FIo_IpcServer.h>
39 #include <CertificateServiceStub.h>
40 #include "FSecCert_CertServer.h"
41 #include <FIo_DirectoryImpl.h>
43 #include "CertificateService.h"
45 using namespace Tizen::App;
46 using namespace Tizen::Base;
47 using namespace Tizen::Io;
48 using namespace Tizen::Security::Cert;
50 const String __CERT_MGR_DIR_CERTS__(L"/opt/share/cert-svc/certs/");
51 const String __CERT_MGR_DIR_CERTS_SIM__(L"/opt/share/cert-svc/certs/sim/");
52 const String __CERT_MGR_DIR_CERTS_SIM_OPERATOR__(L"/opt/share/cert-svc/certs/sim/operator/");
53 const String __CERT_MGR_DIR_CERTS_SIM_THIRDPARTY__(L"/opt/share/cert-svc/certs/sim/thirdparty/");
54 const String __CERT_MGR_DIR_CERTS_SSL__(L"/opt/share/cert-svc/certs/ssl/");
55 const String __CERT_MGR_DIR_CA_CERTS__(L"/usr/share/cert-svc/ca-certs/");
57 CertificateService::CertificateService()
58 : __pCertMgrServer(null)
59 , __pFileEventListenerCerts(null)
60 , __pFileEventListenerCertsSim(null)
61 , __pFileEventListenerCertsSimOperator(null)
62 , __pFileEventListenerCertsSimThirdparty(null)
63 , __pFileEventListenerCertsSsl(null)
64 , __pFileEventListenerUsrCaCerts(null)
68 CertificateService::Construct()
71 String certsDir(__CERT_MGR_DIR_CERTS__);
72 String certsSimDir(__CERT_MGR_DIR_CERTS_SIM__);
73 String certsSimOperatorDir(__CERT_MGR_DIR_CERTS_SIM_OPERATOR__);
74 String certsSimThirdPartyDir(__CERT_MGR_DIR_CERTS_SIM_THIRDPARTY__);
75 String certsSslDir(__CERT_MGR_DIR_CERTS_SSL__);
76 String caCertsDir(__CERT_MGR_DIR_CA_CERTS__);
78 __pCertMgrServer = new (std::nothrow) Tizen::Security::Cert::_CertServiceStub();
79 TryReturnResult(__pCertMgrServer != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to initialize cert mgr server.");
81 r = __pCertMgrServer->Construct();
84 delete __pCertMgrServer;
85 __pCertMgrServer = null;
87 TryReturnResult(false, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed constructed certificate manager service.");
90 __pFileEventListenerCerts = new (std::nothrow) FileEventListenerCerts();
91 TryCatchResult(__pFileEventListenerCerts != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
93 __pFileEventListenerCertsSim = new (std::nothrow) FileEventListenerCertsSim();
94 TryCatchResult(__pFileEventListenerCertsSim != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
96 __pFileEventListenerCertsSimOperator = new (std::nothrow) FileEventListenerCertsSimOperator();
97 TryCatchResult(__pFileEventListenerCertsSimOperator != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
99 __pFileEventListenerCertsSimThirdparty = new (std::nothrow) FileEventListenerCertsSimThirdparty();
100 TryCatchResult(__pFileEventListenerCertsSimThirdparty != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
102 __pFileEventListenerCertsSsl = new (std::nothrow) FileEventListenerCertsSsl();
103 TryCatchResult(__pFileEventListenerCertsSsl != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
105 __pFileEventListenerUsrCaCerts = new (std::nothrow) FileEventListenerUsrCaCerts();
106 TryCatchResult(__pFileEventListenerUsrCaCerts != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
108 r = __fileEventManagerCerts.Construct(*__pFileEventListenerCerts);
109 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed constructed file event manager.");
111 r = __fileEventManagerCerts.AddPath(certsDir, FILE_EVENT_TYPE_CLOSE_WRITE);
112 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed add path in file event manager.");
114 r = __fileEventManagerCertsSim.Construct(*__pFileEventListenerCertsSim);
115 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed constructed file event manager.");
117 r = __fileEventManagerCertsSim.AddPath(certsSimDir, FILE_EVENT_TYPE_CLOSE_WRITE);
118 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed add path in file event manager.");
120 r = __fileEventManagerCertsSimOperator.Construct(*__pFileEventListenerCertsSimOperator);
121 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed constructed file event manager.");
123 r = __fileEventManagerCertsSimOperator.AddPath(certsSimOperatorDir, FILE_EVENT_TYPE_CLOSE_WRITE);
124 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed add path in file event manager.");
126 r = __fileEventManagerCertsSimThirdparty.Construct(*__pFileEventListenerCertsSimThirdparty);
127 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed constructed file event manager.");
129 r = __fileEventManagerCertsSimThirdparty.AddPath(certsSimThirdPartyDir, FILE_EVENT_TYPE_CLOSE_WRITE);
130 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed add path in file event manager.");
132 r = __fileEventManagerCertsSsl.Construct(*__pFileEventListenerCertsSsl);
133 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed constructed file event manager.");
135 r = __fileEventManagerCertsSsl.AddPath(certsSslDir, FILE_EVENT_TYPE_CLOSE_WRITE);
136 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed add path in file event manager.");
138 r = __fileEventManagerUsrCaCerts.Construct(*__pFileEventListenerUsrCaCerts);
139 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed constructed file event manager.");
141 r = __fileEventManagerUsrCaCerts.AddPath(caCertsDir, FILE_EVENT_TYPE_CLOSE_WRITE);
142 TryCatchResult(!(IsFailed(r) && r != E_FILE_NOT_FOUND), r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed add path in file event manager.");
147 CertificateService::~CertificateService()
149 delete __pFileEventListenerCerts;
150 delete __pFileEventListenerCertsSim;
151 delete __pFileEventListenerCertsSimOperator;
152 delete __pFileEventListenerCertsSimThirdparty;
153 delete __pFileEventListenerCertsSsl;
154 delete __pFileEventListenerUsrCaCerts;
155 delete __pCertMgrServer;
160 FileEventListenerCerts::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
162 result r = E_SUCCESS;
163 String fileName(path);
169 int certBuffLength = 0;
171 dirPath.Append(__CERT_MGR_DIR_CERTS__);
172 dirPath.Append(fileName);
174 r = file.Construct(dirPath, L"r");
175 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
177 r = File::GetAttributes(dirPath, attr);
178 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
180 size = attr.GetFileSize();
182 std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
183 TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
185 readCnt = file.Read(pCertBuffer.get(), size);
187 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
189 certBuffLength = static_cast< int >(readCnt);
190 TryReturnVoidResult(certBuffLength > 0, E_SYSTEM, "[E_SYSTEM] Failed to read file.");
194 case FILE_EVENT_TYPE_CLOSE_WRITE:
195 r = _CertServer::InsertCaCertificate(_CERT_TYPE_ROOT_CA, _CERT_X509, pCertBuffer.get(), certBuffLength);
196 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to insert root certificate.");
205 FileEventListenerCertsSim::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
207 result r = E_SUCCESS;
208 String fileName(path);
214 int certBuffLength = 0;
216 dirPath.Append(__CERT_MGR_DIR_CERTS_SIM__);
217 dirPath.Append(fileName);
219 r = file.Construct(dirPath, L"r");
220 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
222 r = File::GetAttributes(dirPath, attr);
223 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
225 size = attr.GetFileSize();
227 std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
228 TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
230 readCnt = file.Read(pCertBuffer.get(), size);
232 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
234 certBuffLength = static_cast< int >(readCnt);
238 case FILE_EVENT_TYPE_CLOSE_WRITE:
239 r = _CertServer::InsertCaCertificate(_CERT_TYPE_ROOT_CA, _CERT_X509, pCertBuffer.get(), certBuffLength);
240 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to insert root certificate.");
249 FileEventListenerCertsSimOperator::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
251 result r = E_SUCCESS;
252 String fileName(path);
258 int certBuffLength = 0;
260 dirPath.Append(__CERT_MGR_DIR_CERTS_SIM_OPERATOR__);
261 dirPath.Append(fileName);
263 r = file.Construct(dirPath, L"r");
264 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
266 r = File::GetAttributes(dirPath, attr);
267 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
269 size = attr.GetFileSize();
271 std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
272 TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
274 readCnt = file.Read(pCertBuffer.get(), size);
276 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
278 certBuffLength = static_cast< int >(readCnt);
282 case FILE_EVENT_TYPE_CLOSE_WRITE:
283 r = _CertServer::InsertCaCertificate(_CERT_TYPE_ROOT_CA, _CERT_X509, pCertBuffer.get(), certBuffLength);
284 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to insert root certificate.");
293 FileEventListenerCertsSimThirdparty::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
295 result r = E_SUCCESS;
296 String fileName(path);
302 int certBuffLength = 0;
304 dirPath.Append(__CERT_MGR_DIR_CERTS_SIM_THIRDPARTY__);
305 dirPath.Append(fileName);
307 r = file.Construct(dirPath, L"r");
308 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
310 r = File::GetAttributes(dirPath, attr);
311 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
313 size = attr.GetFileSize();
315 std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
316 TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
318 readCnt = file.Read(pCertBuffer.get(), size);
320 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
322 certBuffLength = static_cast< int >(readCnt);
326 case FILE_EVENT_TYPE_CLOSE_WRITE:
327 r = _CertServer::InsertCaCertificate(_CERT_TYPE_ROOT_CA, _CERT_X509, pCertBuffer.get(), certBuffLength);
328 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to insert root certificate.");
337 FileEventListenerCertsSsl::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
339 result r = E_SUCCESS;
340 String fileName(path);
346 int certBuffLength = 0;
348 dirPath.Append(__CERT_MGR_DIR_CERTS_SSL__);
349 dirPath.Append(fileName);
351 r = file.Construct(dirPath, L"r");
352 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
354 r = File::GetAttributes(dirPath, attr);
355 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
357 size = attr.GetFileSize();
359 std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
360 TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
362 readCnt = file.Read(pCertBuffer.get(), size);
364 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
366 certBuffLength = static_cast< int >(readCnt);
370 case FILE_EVENT_TYPE_CLOSE_WRITE:
371 r = _CertServer::InsertCaCertificate(_CERT_TYPE_ROOT_CA, _CERT_X509, pCertBuffer.get(), certBuffLength);
372 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to insert root certificate.");
381 FileEventListenerUsrCaCerts::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
383 result r = E_SUCCESS;
384 String fileName(path);
390 int certBuffLength = 0;
392 dirPath.Append(__CERT_MGR_DIR_CA_CERTS__);
393 dirPath.Append(fileName);
395 r = file.Construct(dirPath, L"r");
396 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
398 r = File::GetAttributes(dirPath, attr);
399 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
401 size = attr.GetFileSize();
403 std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
404 TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
406 readCnt = file.Read(pCertBuffer.get(), size);
408 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
410 certBuffLength = static_cast< int >(readCnt);
414 case FILE_EVENT_TYPE_CLOSE_WRITE:
415 r = _CertServer::InsertCaCertificate(_CERT_TYPE_ROOT_CA, _CERT_X509, pCertBuffer.get(), certBuffLength);
416 TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to insert root certificate.");