Initialize Tizen 2.3
[framework/osp/security-service.git] / src / CertificateService.cpp
1 //
2 // Copyright (c) 2012-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                CertificateService.cpp
19  * @brief               This is the implementation file for CertificateService class.
20  */
21
22 #include <new>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <error.h>
27 #include <memory.h>
28 #include <malloc.h>
29 #include <unique_ptr.h>
30 #include <sys/stat.h>
31 #include <assert.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>
42
43 #include "CertificateService.h"
44
45 using namespace Tizen::App;
46 using namespace Tizen::Base;
47 using namespace Tizen::Io;
48 using namespace Tizen::Security::Cert;
49
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/");
56
57 CertificateService::CertificateService()
58         : __pCertMgrServer(null)
59         , __pFileEventListenerCerts(null)
60         , __pFileEventListenerCertsSim(null)
61         , __pFileEventListenerCertsSimOperator(null)
62         , __pFileEventListenerCertsSimThirdparty(null)
63         , __pFileEventListenerCertsSsl(null)
64         , __pFileEventListenerUsrCaCerts(null)
65 {
66 }
67 result
68 CertificateService::Construct()
69 {
70         result r = E_SUCCESS;
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__);
77
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.");
80
81         r = __pCertMgrServer->Construct();
82         if (IsFailed(r))
83         {
84                 delete __pCertMgrServer;
85                 __pCertMgrServer = null;
86
87                 TryReturnResult(false, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed constructed certificate manager service.");
88         }
89
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.");
92
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.");
95
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.");
98
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.");
101
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.");
104
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.");
107
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.");
110
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.");
113
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.");
116
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.");
119
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.");
122
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.");
125
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.");
128
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.");
131
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.");
134
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.");
137
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.");
140
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.");
143
144 CATCH:
145         return r;
146 }
147 CertificateService::~CertificateService()
148 {
149         delete __pFileEventListenerCerts;
150         delete __pFileEventListenerCertsSim;
151         delete __pFileEventListenerCertsSimOperator;
152         delete __pFileEventListenerCertsSimThirdparty;
153         delete __pFileEventListenerCertsSsl;
154         delete __pFileEventListenerUsrCaCerts;
155         delete __pCertMgrServer;
156 }
157
158
159 void
160 FileEventListenerCerts::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
161 {
162         result r = E_SUCCESS;
163         String fileName(path);
164         String dirPath;
165         FileAttributes attr;
166         File file;
167         long long size;
168         int readCnt;
169         int certBuffLength = 0;
170
171         dirPath.Append(__CERT_MGR_DIR_CERTS__);
172         dirPath.Append(fileName);
173
174         r = file.Construct(dirPath, L"r");
175         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
176
177         r = File::GetAttributes(dirPath, attr);
178         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
179
180         size = attr.GetFileSize();
181
182         std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
183         TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
184
185         readCnt = file.Read(pCertBuffer.get(), size);
186         r = GetLastResult();
187         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
188
189         certBuffLength = static_cast< int >(readCnt);
190         TryReturnVoidResult(certBuffLength > 0, E_SYSTEM, "[E_SYSTEM] Failed to read file.");
191
192         switch (events)
193         {
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.");
197                 break;
198
199         default:
200                 break;
201         }
202 }
203
204 void
205 FileEventListenerCertsSim::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
206 {
207         result r = E_SUCCESS;
208         String fileName(path);
209         String dirPath;
210         FileAttributes attr;
211         File file;
212         long long size;
213         int readCnt;
214         int certBuffLength = 0;
215
216         dirPath.Append(__CERT_MGR_DIR_CERTS_SIM__);
217         dirPath.Append(fileName);
218
219         r = file.Construct(dirPath, L"r");
220         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
221
222         r = File::GetAttributes(dirPath, attr);
223         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
224
225         size = attr.GetFileSize();
226
227         std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
228         TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
229
230         readCnt = file.Read(pCertBuffer.get(), size);
231         r = GetLastResult();
232         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
233
234         certBuffLength = static_cast< int >(readCnt);
235
236         switch (events)
237         {
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.");
241                 break;
242
243         default:
244                 break;
245         }
246 }
247
248 void
249 FileEventListenerCertsSimOperator::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
250 {
251         result r = E_SUCCESS;
252         String fileName(path);
253         String dirPath;
254         FileAttributes attr;
255         File file;
256         long long size;
257         int readCnt;
258         int certBuffLength = 0;
259
260         dirPath.Append(__CERT_MGR_DIR_CERTS_SIM_OPERATOR__);
261         dirPath.Append(fileName);
262
263         r = file.Construct(dirPath, L"r");
264         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
265
266         r = File::GetAttributes(dirPath, attr);
267         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
268
269         size = attr.GetFileSize();
270
271         std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
272         TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
273
274         readCnt = file.Read(pCertBuffer.get(), size);
275         r = GetLastResult();
276         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
277
278         certBuffLength = static_cast< int >(readCnt);
279
280         switch (events)
281         {
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.");
285                 break;
286
287         default:
288                 break;
289         }
290 }
291
292 void
293 FileEventListenerCertsSimThirdparty::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
294 {
295         result r = E_SUCCESS;
296         String fileName(path);
297         String dirPath;
298         FileAttributes attr;
299         File file;
300         long long size;
301         int readCnt;
302         int certBuffLength = 0;
303
304         dirPath.Append(__CERT_MGR_DIR_CERTS_SIM_THIRDPARTY__);
305         dirPath.Append(fileName);
306
307         r = file.Construct(dirPath, L"r");
308         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
309
310         r = File::GetAttributes(dirPath, attr);
311         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
312
313         size = attr.GetFileSize();
314
315         std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
316         TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
317
318         readCnt = file.Read(pCertBuffer.get(), size);
319         r = GetLastResult();
320         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
321
322         certBuffLength = static_cast< int >(readCnt);
323
324         switch (events)
325         {
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.");
329                 break;
330
331         default:
332                 break;
333         }
334 }
335
336 void
337 FileEventListenerCertsSsl::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
338 {
339         result r = E_SUCCESS;
340         String fileName(path);
341         String dirPath;
342         FileAttributes attr;
343         File file;
344         long long size;
345         int readCnt;
346         int certBuffLength = 0;
347
348         dirPath.Append(__CERT_MGR_DIR_CERTS_SSL__);
349         dirPath.Append(fileName);
350
351         r = file.Construct(dirPath, L"r");
352         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
353
354         r = File::GetAttributes(dirPath, attr);
355         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
356
357         size = attr.GetFileSize();
358
359         std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
360         TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
361
362         readCnt = file.Read(pCertBuffer.get(), size);
363         r = GetLastResult();
364         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
365
366         certBuffLength = static_cast< int >(readCnt);
367
368         switch (events)
369         {
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.");
373                 break;
374
375         default:
376                 break;
377         }
378 }
379
380 void
381 FileEventListenerUsrCaCerts::OnFileEventOccured(const unsigned long events, const Tizen::Base::String& path, const unsigned int eventId)
382 {
383         result r = E_SUCCESS;
384         String fileName(path);
385         String dirPath;
386         FileAttributes attr;
387         File file;
388         long long size;
389         int readCnt;
390         int certBuffLength = 0;
391
392         dirPath.Append(__CERT_MGR_DIR_CA_CERTS__);
393         dirPath.Append(fileName);
394
395         r = file.Construct(dirPath, L"r");
396         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to construct file.");
397
398         r = File::GetAttributes(dirPath, attr);
399         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to get file attributes.");
400
401         size = attr.GetFileSize();
402
403         std::unique_ptr< byte[] > pCertBuffer(new (std::nothrow) byte[size]);
404         TryReturnVoidResult(pCertBuffer != null, E_SYSTEM, "[E_SYSTEM] Failed to allocate buffer.");
405
406         readCnt = file.Read(pCertBuffer.get(), size);
407         r = GetLastResult();
408         TryReturnVoidResult(!IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to read file.");
409
410         certBuffLength = static_cast< int >(readCnt);
411
412         switch (events)
413         {
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.");
417                 break;
418
419         default:
420                 break;
421         }
422 }