2 * Copyright (c) 2014 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
17 * @file ckm-manager-main.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Implementation of central key manager
27 #include <openssl/rand.h>
29 #include <dpl/log/log.h>
30 #include <dpl/singleton.h>
32 #include <socket-manager.h>
34 #include <ckm-service.h>
35 #include <ocsp-service.h>
36 #include <encryption-service.h>
37 #include <glib-service.h>
39 #include <key-provider.h>
40 #include <file-system.h>
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;
48 void initializeEntropy() // entropy sources - /dev/random,/dev/urandom(Default)
52 std::ifstream ifile(DEV_HW_RANDOM_FILE);
55 ret = RAND_load_file(DEV_HW_RANDOM_FILE, RANDOM_BUFFER_LEN);
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);
61 if (ret != RANDOM_BUFFER_LEN)
62 LogError("Error in U_RAND_file_load");
66 } // anonymous namespace
68 #define REGISTER_SOCKET_SERVICE(manager, service) \
69 registerSocketService<service>(manager, #service)
72 void registerSocketService(CKM::SocketManager &manager,
73 const std::string &serviceName)
80 manager.RegisterSocketService(service);
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());
89 LogError("Error in creating service " << serviceName <<
90 ", unknown exception occured");
100 CKM::Singleton<CKM::Log::LogSystem>::Instance().SetTag("CKM");
102 int retCode = CKM::FileSystem::init();
105 LogError("Fatal error in FileSystem::init()");
109 CKM::FileLock fl = CKM::FileSystem::lock();
113 sigaddset(&mask, SIGTERM);
114 sigaddset(&mask, SIGPIPE);
116 if (-1 == pthread_sigmask(SIG_BLOCK, &mask, NULL)) {
117 LogError("Error in pthread_sigmask");
121 LogInfo("Init external libraries SKMM and openssl");
125 CKM::KeyProvider::initializeLibrary();
129 CKM::SocketManager manager;
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);
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) {
146 UNHANDLED_EXCEPTION_HANDLER_END