2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "ietf/ietf_attr.h"
17 #include "ita/ita_attr.h"
18 #include "tcg/tcg_attr.h"
19 #include "pts/components/pts_component.h"
20 #include "pts/components/pts_component_manager.h"
21 #include "pts/components/tcg/tcg_comp_func_name.h"
22 #include "pts/components/ita/ita_comp_func_name.h"
23 #include "pts/components/ita/ita_comp_ima.h"
24 #include "pts/components/ita/ita_comp_tboot.h"
25 #include "pts/components/ita/ita_comp_tgrub.h"
27 #include <utils/debug.h>
28 #include <utils/utils.h>
36 #define IPSEC_SCRIPT "ipsec"
39 #define IMCV_DEBUG_LEVEL 1
40 #define IMCV_DEFAULT_POLICY_SCRIPT IPSEC_SCRIPT " _imv_policy"
44 * PA-TNC attribute manager
46 pa_tnc_attr_manager_t
*imcv_pa_tnc_attributes
;
49 * Global list of IMV sessions
51 imv_session_manager_t
*imcv_sessions
;
56 imv_database_t
*imcv_db
;
59 * PTS Functional Component manager
61 pts_component_manager_t
*imcv_pts_components
;
64 * Reference count for libimcv
66 static refcount_t libimcv_ref
= 0;
69 * Reference count for libstrongswan
71 static refcount_t libstrongswan_ref
= 0;
74 * Global configuration of imcv dbg function
76 static int imcv_debug_level
;
77 static bool imcv_stderr_quiet
;
82 static void imcv_dbg(debug_t group
, level_t level
, char *fmt
, ...)
86 if (level
<= imcv_debug_level
)
88 if (!imcv_stderr_quiet
)
91 fprintf(stderr
, "[HSR] ");
92 vfprintf(stderr
, fmt
, args
);
93 fprintf(stderr
, "\n");
99 int priority
= LOG_INFO
;
101 char *current
= buffer
, *next
;
103 /* write in memory buffer first */
105 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
108 /* do a syslog with every line */
111 next
= strchr(current
, '\n');
116 syslog(priority
, "[HSR] %s\n", current
);
120 #endif /* HAVE_SYSLOG */
125 * Described in header.
127 bool libimcv_init(bool is_imv
)
129 /* initialize libstrongswan library only once */
132 /* did main program initialize libstrongswan? */
133 if (libstrongswan_ref
== 0)
135 ref_get(&libstrongswan_ref
);
140 /* we are the first to initialize libstrongswan */
141 if (!library_init(NULL
, "libimcv"))
146 /* set the debug level and stderr output */
147 imcv_debug_level
= lib
->settings
->get_int(lib
->settings
,
148 "libimcv.debug_level", IMCV_DEBUG_LEVEL
);
149 imcv_stderr_quiet
= lib
->settings
->get_int(lib
->settings
,
150 "libimcv.stderr_quiet", FALSE
);
152 /* activate the imcv debugging hook */
155 openlog("imcv", 0, LOG_DAEMON
);
158 if (!lib
->plugins
->load(lib
->plugins
,
159 lib
->settings
->get_str(lib
->settings
, "libimcv.load",
160 "random nonce gmp pubkey x509")))
166 ref_get(&libstrongswan_ref
);
168 lib
->settings
->add_fallback(lib
->settings
, "%s.imcv", "libimcv", lib
->ns
);
169 lib
->settings
->add_fallback(lib
->settings
, "%s.plugins", "libimcv.plugins",
172 if (libimcv_ref
== 0)
176 /* initialize the PA-TNC attribute manager */
177 imcv_pa_tnc_attributes
= pa_tnc_attr_manager_create();
178 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_IETF
,
179 ietf_attr_create_from_data
, ietf_attr_names
);
180 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_ITA
,
181 ita_attr_create_from_data
, ita_attr_names
);
182 imcv_pa_tnc_attributes
->add_vendor(imcv_pa_tnc_attributes
, PEN_TCG
,
183 tcg_attr_create_from_data
, tcg_attr_names
);
185 imcv_pts_components
= pts_component_manager_create();
186 imcv_pts_components
->add_vendor(imcv_pts_components
, PEN_TCG
,
187 pts_tcg_comp_func_names
, PTS_TCG_QUALIFIER_TYPE_SIZE
,
188 pts_tcg_qualifier_flag_names
, pts_tcg_qualifier_type_names
);
189 imcv_pts_components
->add_vendor(imcv_pts_components
, PEN_ITA
,
190 pts_ita_comp_func_names
, PTS_ITA_QUALIFIER_TYPE_SIZE
,
191 pts_ita_qualifier_flag_names
, pts_ita_qualifier_type_names
);
193 imcv_pts_components
->add_component(imcv_pts_components
, PEN_ITA
,
194 PTS_ITA_COMP_FUNC_NAME_TGRUB
,
195 pts_ita_comp_tgrub_create
);
196 imcv_pts_components
->add_component(imcv_pts_components
, PEN_ITA
,
197 PTS_ITA_COMP_FUNC_NAME_TBOOT
,
198 pts_ita_comp_tboot_create
);
199 imcv_pts_components
->add_component(imcv_pts_components
, PEN_ITA
,
200 PTS_ITA_COMP_FUNC_NAME_IMA
,
201 pts_ita_comp_ima_create
);
204 /* instantiate global IMV session manager */
205 imcv_sessions
= imv_session_manager_create();
207 /* instantiate and attach global IMV database if URI is valid */
208 uri
= lib
->settings
->get_str(lib
->settings
,
209 "%s.imcv.database", NULL
, lib
->ns
);
210 script
= lib
->settings
->get_str(lib
->settings
,
211 "%s.imcv.policy_script", IMCV_DEFAULT_POLICY_SCRIPT
,
215 imcv_db
= imv_database_create(uri
, script
);
218 DBG1(DBG_LIB
, "libimcv initialized");
220 ref_get(&libimcv_ref
);
226 * Described in header.
228 void libimcv_deinit(void)
230 if (ref_put(&libimcv_ref
))
232 imcv_pts_components
->remove_vendor(imcv_pts_components
, PEN_TCG
);
233 imcv_pts_components
->remove_vendor(imcv_pts_components
, PEN_ITA
);
234 imcv_pts_components
->destroy(imcv_pts_components
);
236 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_IETF
);
237 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_ITA
);
238 imcv_pa_tnc_attributes
->remove_vendor(imcv_pa_tnc_attributes
, PEN_TCG
);
239 DESTROY_IF(imcv_pa_tnc_attributes
);
240 imcv_pa_tnc_attributes
= NULL
;
242 DESTROY_IF(imcv_sessions
);
243 DBG1(DBG_LIB
, "libimcv terminated");
245 if (ref_put(&libstrongswan_ref
))