2 * Copyright (C) 2011-2022 Andreas Steffen
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "ietf/ietf_attr.h"
19 #include "ita/ita_attr.h"
20 #include "pwg/pwg_attr.h"
21 #include "tcg/tcg_attr.h"
22 #include "pts/components/pts_component.h"
23 #include "pts/components/pts_component_manager.h"
24 #include "pts/components/tcg/tcg_comp_func_name.h"
25 #include "pts/components/ita/ita_comp_func_name.h"
26 #include "pts/components/ita/ita_comp_ima.h"
27 #include "pts/components/ita/ita_comp_tboot.h"
28 #include "pts/components/ita/ita_comp_tgrub.h"
30 #include <utils/debug.h>
31 #include <utils/utils.h>
39 #define IPSEC_SCRIPT "ipsec"
42 #define IMCV_DEBUG_LEVEL 1
43 #define IMCV_DEFAULT_POLICY_SCRIPT IPSEC_SCRIPT " _imv_policy"
47 * PA-TNC attribute manager
49 pa_tnc_attr_manager_t
*imcv_pa_tnc_attributes
;
52 * Global list of IMV sessions
54 imv_session_manager_t
*imcv_sessions
;
59 imv_database_t
*imcv_db
;
62 * PTS Functional Component manager
64 pts_component_manager_t
*imcv_pts_components
;
67 * Reference count for libimcv
69 static refcount_t libimcv_ref
= 0;
72 * Reference count for libstrongswan
74 static refcount_t libstrongswan_ref
= 0;
77 * Global configuration of imcv dbg function
79 static int imcv_debug_level
;
80 static bool imcv_stderr_quiet
;
83 * Described in header.
85 void imcv_list_pa_tnc_attribute_type(char *label
, pen_t vendor_id
, uint32_t type
)
87 enum_name_t
*pa_attr_names
;
89 pa_attr_names
= imcv_pa_tnc_attributes
->get_names(imcv_pa_tnc_attributes
,
93 DBG2(DBG_TNC
, "%s PA-TNC attribute type '%N/%N' 0x%06x/0x%08x",
94 label
, pen_names
, vendor_id
, pa_attr_names
, type
, vendor_id
, type
);
98 DBG2(DBG_TNC
, "%s PA-TNC attribute type '%N' 0x%06x/0x%08x",
99 label
, pen_names
, vendor_id
, vendor_id
, type
);
106 static void imcv_dbg(debug_t group
, level_t level
, char *fmt
, ...)
110 if (level
<= imcv_debug_level
)
112 if (!imcv_stderr_quiet
)
115 fprintf(stderr
, "[HSR] ");
116 vfprintf(stderr
, fmt
, args
);
117 fprintf(stderr
, "\n");
123 int priority
= LOG_INFO
;
125 char *current
= buffer
, *next
;
127 /* write in memory buffer first */
129 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
132 /* do a syslog with every line */
135 next
= strchr(current
, '\n');
140 syslog(priority
, "[HSR] %s\n", current
);
144 #endif /* HAVE_SYSLOG */
149 * Described in header.
151 bool libimcv_init(bool is_imv
)
153 /* initialize libstrongswan library only once */
156 /* did main program initialize libstrongswan? */
157 if (!ref_cur(&libstrongswan_ref
))
159 ref_get(&libstrongswan_ref
);
164 /* we are the first to initialize libstrongswan */
165 if (!library_init(NULL
, "libimcv"))
170 /* set the debug level and stderr output */
171 imcv_debug_level
= lib
->settings
->get_int(lib
->settings
,
172 "libimcv.debug_level", IMCV_DEBUG_LEVEL
);
173 imcv_stderr_quiet
= lib
->settings
->get_int(lib
->settings
,
174 "libimcv.stderr_quiet", FALSE
);
176 /* activate the imcv debugging hook */
179 openlog("imcv", 0, LOG_DAEMON
);
182 if (!lib
->plugins
->load(lib
->plugins
,
183 lib
->settings
->get_str(lib
->settings
, "libimcv.load",
184 "random nonce gmp pubkey x509")))
190 ref_get(&libstrongswan_ref
);
192 lib
->settings
->add_fallback(lib
->settings
, "%s.imcv", "libimcv", lib
->ns
);
193 lib
->settings
->add_fallback(lib
->settings
, "%s.plugins", "libimcv.plugins",
196 if (!ref_cur(&libimcv_ref
))
202 /* initialize the PA-TNC attribute manager */
203 imcv_pa_tnc_attributes
= pa_tnc_attr_manager_create();
204 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_IETF
,
205 ietf_attr_create_from_data
, ietf_attr_names
);
206 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_ITA
,
207 ita_attr_create_from_data
, ita_attr_names
);
208 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_PWG
,
209 pwg_attr_create_from_data
, pwg_attr_names
);
210 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_TCG
,
211 tcg_attr_create_from_data
, tcg_attr_names
);
213 imcv_pts_components
= pts_component_manager_create();
214 imcv_pts_components
->add_vendor(imcv_pts_components
, PEN_TCG
,
215 pts_tcg_comp_func_names
, PTS_TCG_QUALIFIER_TYPE_SIZE
,
216 pts_tcg_qualifier_flag_names
, pts_tcg_qualifier_type_names
);
217 imcv_pts_components
->add_vendor(imcv_pts_components
, PEN_ITA
,
218 pts_ita_comp_func_names
, PTS_ITA_QUALIFIER_TYPE_SIZE
,
219 pts_ita_qualifier_flag_names
, pts_ita_qualifier_type_names
);
221 imcv_pts_components
->add_component(imcv_pts_components
, PEN_ITA
,
222 PTS_ITA_COMP_FUNC_NAME_TGRUB
,
223 pts_ita_comp_tgrub_create
);
224 imcv_pts_components
->add_component(imcv_pts_components
, PEN_ITA
,
225 PTS_ITA_COMP_FUNC_NAME_TBOOT
,
226 pts_ita_comp_tboot_create
);
227 imcv_pts_components
->add_component(imcv_pts_components
, PEN_ITA
,
228 PTS_ITA_COMP_FUNC_NAME_IMA
,
229 pts_ita_comp_ima_create
);
232 /* instantiate global IMV session manager */
233 imcv_sessions
= imv_session_manager_create();
235 /* instantiate and attach global IMV database if URI is valid */
236 uri
= lib
->settings
->get_str(lib
->settings
,
237 "%s.imcv.database", NULL
, lib
->ns
);
238 script
= lib
->settings
->get_str(lib
->settings
,
239 "%s.imcv.policy_script", IMCV_DEFAULT_POLICY_SCRIPT
,
243 imcv_db
= imv_database_create(uri
, script
);
246 DBG1(DBG_LIB
, "libimcv initialized");
248 ref_get(&libimcv_ref
);
254 * Described in header.
256 void libimcv_deinit(void)
258 if (ref_put(&libimcv_ref
))
260 imcv_pts_components
->remove_vendor(imcv_pts_components
, PEN_TCG
);
261 imcv_pts_components
->remove_vendor(imcv_pts_components
, PEN_ITA
);
262 imcv_pts_components
->destroy(imcv_pts_components
);
264 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_IETF
);
265 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_ITA
);
266 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_PWG
);
267 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_TCG
);
268 DESTROY_IF(imcv_pa_tnc_attributes
);
269 imcv_pa_tnc_attributes
= NULL
;
271 DESTROY_IF(imcv_sessions
);
272 DBG1(DBG_LIB
, "libimcv terminated");
276 if (ref_put(&libstrongswan_ref
))