bb867b7b9d43cee4af1072ae3a9bba7379b97019
[platform/core/security/key-manager.git] / src / manager / main / key-manager-main.cpp
1 /*
2  *  Copyright (c) 2014 - 2020 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 <fstream>
26
27 #include <openssl/rand.h>
28
29 #include <dpl/log/log.h>
30 #include <dpl/singleton.h>
31
32 #include <socket-manager.h>
33
34 #include <ckm-service.h>
35 #include <ocsp-service.h>
36 #include <encryption-service.h>
37 #include <glib-service.h>
38
39 #include <key-provider.h>
40 #include <file-system.h>
41
42 namespace {
43
44 const char *DEV_HW_RANDOM_FILE = "/dev/hwrng";
45 const char *DEV_URANDOM_FILE = "/dev/urandom";
46 const size_t RANDOM_BUFFER_LEN = 32;
47
48 void initializeEntropy() // entropy sources - /dev/random,/dev/urandom(Default)
49 {
50         int ret = 0;
51
52         std::ifstream ifile(DEV_HW_RANDOM_FILE);
53
54         if (ifile.is_open())
55                 ret = RAND_load_file(DEV_HW_RANDOM_FILE, RANDOM_BUFFER_LEN);
56
57         if (ret != RANDOM_BUFFER_LEN) {
58                 LogWarning("Error in HW_RAND file load");
59                 ret = RAND_load_file(DEV_URANDOM_FILE, RANDOM_BUFFER_LEN);
60
61                 if (ret != RANDOM_BUFFER_LEN)
62                         LogError("Error in U_RAND_file_load");
63         }
64 }
65
66 } // anonymous namespace
67
68 #define REGISTER_SOCKET_SERVICE(manager, service) \
69         registerSocketService<service>(manager, #service)
70
71 template<typename T>
72 void registerSocketService(CKM::SocketManager &manager,
73                                                    const std::string &serviceName)
74 {
75         T *service = NULL;
76
77         try {
78                 service = new T();
79                 service->Start();
80                 manager.RegisterSocketService(service);
81                 service = NULL;
82         } catch (const CKM::Exception &exception) {
83                 LogError("Error in creating service " << serviceName <<
84                                  ", details:\n" << exception.DumpToString());
85         } catch (const std::exception &e) {
86                 LogError("Error in creating service " << serviceName <<
87                                  ", details:\n" << e.what());
88         } catch (...) {
89                 LogError("Error in creating service " << serviceName <<
90                                  ", unknown exception occured");
91         }
92
93         if (service)
94                 delete service;
95 }
96
97 int main(void)
98 {
99         try {
100                 CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM");
101
102                 int retCode = CKM::FileSystem::init();
103
104                 if (retCode) {
105                         LogError("Fatal error in FileSystem::init()");
106                         return 1;
107                 }
108
109                 CKM::FileLock fl = CKM::FileSystem::lock();
110
111                 sigset_t mask;
112                 sigemptyset(&mask);
113                 sigaddset(&mask, SIGTERM);
114                 sigaddset(&mask, SIGPIPE);
115
116                 if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
117                         LogError("Error in pthread_sigmask");
118                         return 1;
119                 }
120
121                 LogInfo("Init external libraries SKMM and openssl");
122
123                 initializeEntropy();
124
125                 CKM::KeyProvider::initializeLibrary();
126
127                 {
128                         LogInfo("Start!");
129                         CKM::SocketManager manager;
130
131                         REGISTER_SOCKET_SERVICE(manager, CKM::CKMService);
132                         REGISTER_SOCKET_SERVICE(manager, CKM::OCSPService);
133                         REGISTER_SOCKET_SERVICE(manager, CKM::EncryptionService);
134                         REGISTER_SOCKET_SERVICE(manager, CKM::GLIBService);
135
136                         manager.MainLoop();
137                 }
138
139                 // Manager has been destroyed and we may close external libraries.
140                 LogInfo("Deinit SKMM and openssl");
141                 CKM::KeyProvider::closeLibrary();
142         } catch (const std::runtime_error &e) {
143                 LogError(e.what());
144         }
145
146         UNHANDLED_EXCEPTION_HANDLER_END
147
148         return 0;
149 }