a2d43b062c06bb67aed1a6083910a6997f47ce50
[platform/core/security/key-manager.git] / src / manager / main / key-manager-main.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        ckm-manager-main.cpp
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Implementation of central key manager
21  */
22 #include <stdlib.h>
23 #include <signal.h>
24
25 #include <openssl/ssl.h>
26 #include <openssl/conf.h>
27 #include <openssl/err.h>
28 #include <openssl/evp.h>
29
30 #include <dpl/log/log.h>
31 #include <dpl/singleton.h>
32 #include <dpl/singleton_safe_impl.h>
33
34 #include <socket-manager.h>
35
36 #include <ckm-service.h>
37 #include <ocsp-service.h>
38
39 #include <key-provider.h>
40 #include <CryptoService.h>
41
42 IMPLEMENT_SAFE_SINGLETON(CKM::Log::LogSystem);
43
44 #define REGISTER_SOCKET_SERVICE(manager, service) \
45     registerSocketService<service>(manager, #service)
46
47 template<typename T>
48 void registerSocketService(CKM::SocketManager &manager, const std::string& serviceName)
49 {
50     T *service = NULL;
51     try {
52         service = new T();
53         service->Create();
54         manager.RegisterSocketService(service);
55         service = NULL;
56     } catch (const CKM::Exception &exception) {
57         LogError("Error in creating service " << serviceName <<
58                  ", details:\n" << exception.DumpToString());
59     } catch (const std::exception& e) {
60         LogError("Error in creating service " << serviceName <<
61                  ", details:\n" << e.what());
62     } catch (...) {
63         LogError("Error in creating service " << serviceName <<
64                  ", unknown exception occured");
65     }
66     if (service)
67         delete service;
68 }
69
70 int main(void) {
71     UNHANDLED_EXCEPTION_HANDLER_BEGIN
72     {
73         CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM");
74
75         sigset_t mask;
76         sigemptyset(&mask);
77         sigaddset(&mask, SIGTERM);
78         sigaddset(&mask, SIGPIPE);
79         if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
80             LogError("Error in pthread_sigmask");
81             return 1;
82         }
83         LogInfo("Init external liblaries SKMM and openssl");
84
85         SSL_load_error_strings();
86         SSL_library_init();
87         OpenSSL_add_all_ciphers();
88         OPENSSL_config(NULL);
89
90         CKM::KeyProvider::initializeLibrary();
91         CKM::CryptoService::initialize();
92
93         {
94             LogInfo("Start!");
95             CKM::SocketManager manager;
96
97             REGISTER_SOCKET_SERVICE(manager, CKM::CKMService);
98             REGISTER_SOCKET_SERVICE(manager, CKM::OCSPService);
99
100             manager.MainLoop();
101         }
102         // Manager has been destroyed and we may close external libraries.
103         LogInfo("Deinit SKMM and openssl");
104         CKM::KeyProvider::closeLibrary();
105         // Deinit OPENSSL ?
106         EVP_cleanup();
107         ERR_free_strings();
108     }
109     UNHANDLED_EXCEPTION_HANDLER_END
110     return 0;
111 }
112