OS IMV proposes IF-M segmentation contract
[strongswan.git] / src / libimcv / plugins / imc_os / imc_os.c
1 /*
2 * Copyright (C) 2011-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imc_os_state.h"
17
18 #include <imc/imc_agent.h>
19 #include <imc/imc_msg.h>
20 #include <imc/imc_os_info.h>
21 #include <ietf/ietf_attr.h>
22 #include <ietf/ietf_attr_attr_request.h>
23 #include <ietf/ietf_attr_default_pwd_enabled.h>
24 #include <ietf/ietf_attr_fwd_enabled.h>
25 #include <ietf/ietf_attr_installed_packages.h>
26 #include <ietf/ietf_attr_numeric_version.h>
27 #include <ietf/ietf_attr_op_status.h>
28 #include <ietf/ietf_attr_product_info.h>
29 #include <ietf/ietf_attr_string_version.h>
30 #include <ita/ita_attr.h>
31 #include <ita/ita_attr_get_settings.h>
32 #include <ita/ita_attr_settings.h>
33 #include <ita/ita_attr_device_id.h>
34
35 #include <tncif_pa_subtypes.h>
36
37 #include <pen/pen.h>
38 #include <utils/debug.h>
39
40 /* IMC definitions */
41
42 static const char imc_name[] = "OS";
43
44 static pen_type_t msg_types[] = {
45 { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM }
46 };
47
48 static imc_agent_t *imc_os;
49 static imc_os_info_t *os;
50
51 /**
52 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
53 */
54 TNC_Result TNC_IMC_API TNC_IMC_Initialize(TNC_IMCID imc_id,
55 TNC_Version min_version,
56 TNC_Version max_version,
57 TNC_Version *actual_version)
58 {
59 if (imc_os)
60 {
61 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
62 return TNC_RESULT_ALREADY_INITIALIZED;
63 }
64 imc_os = imc_agent_create(imc_name, msg_types, countof(msg_types),
65 imc_id, actual_version);
66 if (!imc_os)
67 {
68 return TNC_RESULT_FATAL;
69 }
70
71 os = imc_os_info_create();
72 if (!os)
73 {
74 imc_os->destroy(imc_os);
75 imc_os = NULL;
76
77 return TNC_RESULT_FATAL;
78 }
79
80 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
81 {
82 DBG1(DBG_IMC, "no common IF-IMC version");
83 return TNC_RESULT_NO_COMMON_VERSION;
84 }
85 return TNC_RESULT_SUCCESS;
86 }
87
88 /**
89 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
90 */
91 TNC_Result TNC_IMC_API TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
92 TNC_ConnectionID connection_id, TNC_ConnectionState new_state)
93 {
94 imc_state_t *state;
95
96 if (!imc_os)
97 {
98 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
99 return TNC_RESULT_NOT_INITIALIZED;
100 }
101 switch (new_state)
102 {
103 case TNC_CONNECTION_STATE_CREATE:
104 state = imc_os_state_create(connection_id);
105 return imc_os->create_state(imc_os, state);
106 case TNC_CONNECTION_STATE_HANDSHAKE:
107 if (imc_os->change_state(imc_os, connection_id, new_state,
108 &state) != TNC_RESULT_SUCCESS)
109 {
110 return TNC_RESULT_FATAL;
111 }
112 state->set_result(state, imc_id,
113 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
114 return TNC_RESULT_SUCCESS;
115 case TNC_CONNECTION_STATE_DELETE:
116 return imc_os->delete_state(imc_os, connection_id);
117 default:
118 return imc_os->change_state(imc_os, connection_id,
119 new_state, NULL);
120 }
121 }
122
123 /**
124 * Add IETF Product Information attribute to the send queue
125 */
126 static void add_product_info(imc_msg_t *msg)
127 {
128 pa_tnc_attr_t *attr;
129 os_type_t os_type;
130 pen_t vendor_id = PEN_IETF;
131 int i;
132
133 typedef struct vendor_pen_t {
134 os_type_t os_type;
135 pen_t pen;
136 } vendor_pen_t;
137
138 vendor_pen_t vendor_pens[] = {
139 { OS_TYPE_DEBIAN, PEN_DEBIAN },
140 { OS_TYPE_UBUNTU, PEN_CANONICAL },
141 { OS_TYPE_FEDORA, PEN_FEDORA },
142 { OS_TYPE_REDHAT, PEN_REDHAT },
143 { OS_TYPE_ANDROID, PEN_GOOGLE }
144 };
145
146 os_type = os->get_type(os);
147 for (i = 0; i < countof(vendor_pens); i++)
148 {
149 if (os_type == vendor_pens[i].os_type)
150 {
151 vendor_id = vendor_pens[i].pen;
152 break;
153 }
154 }
155 attr = ietf_attr_product_info_create(vendor_id, 0, os->get_name(os));
156 msg->add_attribute(msg, attr);
157 }
158
159 /**
160 * Add IETF Numeric Version attribute to the send queue
161 */
162 static void add_numeric_version(imc_msg_t *msg)
163 {
164 pa_tnc_attr_t *attr;
165 u_int32_t major, minor;
166
167 os->get_numeric_version(os, &major, &minor);
168 DBG1(DBG_IMC, "operating system numeric version is %d.%d",
169 major, minor);
170
171 attr = ietf_attr_numeric_version_create(major, minor, 0, 0, 0);
172 msg->add_attribute(msg, attr);
173 }
174
175 /**
176 * Add IETF String Version attribute to the send queue
177 */
178 static void add_string_version(imc_msg_t *msg)
179 {
180 pa_tnc_attr_t *attr;
181
182 attr = ietf_attr_string_version_create(os->get_version(os),
183 chunk_empty, chunk_empty);
184 msg->add_attribute(msg, attr);
185 }
186
187 /**
188 * Add IETF Operational Status attribute to the send queue
189 */
190 static void add_op_status(imc_msg_t *msg)
191 {
192 pa_tnc_attr_t *attr;
193 time_t uptime, last_boot;
194
195 uptime = os->get_uptime(os);
196 last_boot = uptime ? time(NULL) - uptime : UNDEFINED_TIME;
197 if (last_boot != UNDEFINED_TIME)
198 {
199 DBG1(DBG_IMC, "last boot: %T, %u s ago", &last_boot, TRUE, uptime);
200 }
201 attr = ietf_attr_op_status_create(OP_STATUS_OPERATIONAL,
202 OP_RESULT_SUCCESSFUL, last_boot);
203 msg->add_attribute(msg, attr);
204 }
205
206 /**
207 * Add IETF Forwarding Enabled attribute to the send queue
208 */
209 static void add_fwd_enabled(imc_msg_t *msg)
210 {
211 pa_tnc_attr_t *attr;
212 os_fwd_status_t fwd_status;
213
214 fwd_status = os->get_fwd_status(os);
215 DBG1(DBG_IMC, "IPv4 forwarding is %N",
216 os_fwd_status_names, fwd_status);
217 attr = ietf_attr_fwd_enabled_create(fwd_status);
218 msg->add_attribute(msg, attr);
219 }
220
221 /**
222 * Add IETF Factory Default Password Enabled attribute to the send queue
223 */
224 static void add_default_pwd_enabled(imc_msg_t *msg)
225 {
226 pa_tnc_attr_t *attr;
227
228 DBG1(DBG_IMC, "factory default password is disabled");
229 attr = ietf_attr_default_pwd_enabled_create(FALSE);
230 msg->add_attribute(msg, attr);
231 }
232
233 /**
234 * Add ITA Device ID attribute to the send queue
235 */
236 static void add_device_id(imc_msg_t *msg)
237 {
238 pa_tnc_attr_t *attr;
239 chunk_t value = chunk_empty, keyid;
240 char *name, *device_id, *cert_path;
241 certificate_t *cert = NULL;
242 public_key_t *pubkey;
243
244 /* Get the device ID as a character string */
245 device_id = lib->settings->get_str(lib->settings,
246 "%s.plugins.imc-os.device_id", NULL, lib->ns);
247 if (device_id)
248 {
249 value = chunk_clone(chunk_from_str(device_id));
250 }
251
252 if (value.len == 0)
253 {
254 /* Derive the device ID from a raw public key */
255 cert_path = lib->settings->get_str(lib->settings,
256 "%s.plugins.imc-os.device_pubkey", NULL, lib->ns);
257 if (cert_path)
258 {
259 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
260 CERT_TRUSTED_PUBKEY, BUILD_FROM_FILE,
261 cert_path, BUILD_END);
262 if (cert)
263 {
264 DBG2(DBG_IMC, "loaded device public key from '%s'", cert_path);
265 }
266 else
267 {
268 DBG1(DBG_IMC, "loading device public key from '%s' failed",
269 cert_path);
270 }
271 }
272
273 if (!cert)
274 {
275 /* Derive the device ID from the public key contained in a certificate */
276 cert_path = lib->settings->get_str(lib->settings,
277 "%s.plugins.imc-os.device_cert", NULL, lib->ns);
278 if (cert_path)
279 {
280 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
281 CERT_X509, BUILD_FROM_FILE,
282 cert_path, BUILD_END);
283 if (cert)
284 {
285 DBG2(DBG_IMC, "loaded device certificate from '%s'", cert_path);
286 }
287 else
288 {
289 DBG1(DBG_IMC, "loading device certificate from '%s' failed",
290 cert_path);
291 }
292 }
293 }
294
295 /* Compute the SHA-1 keyid of the retrieved device public key */
296 if (cert)
297 {
298 pubkey = cert->get_public_key(cert);
299 if (pubkey)
300 {
301 if (pubkey->get_fingerprint(pubkey, KEYID_PUBKEY_INFO_SHA1,
302 &keyid))
303 {
304 value = chunk_to_hex(keyid, NULL, FALSE);
305 }
306 pubkey->destroy(pubkey);
307 }
308 cert->destroy(cert);
309 }
310 }
311
312 if (value.len == 0)
313 {
314 /* Derive the device ID from some unique OS settings */
315 name = os->get_type(os) == OS_TYPE_ANDROID ?
316 "android_id" : "/var/lib/dbus/machine-id";
317 value = os->get_setting(os, name);
318
319 /* Trim trailing newline character */
320 if (value.len > 0 && value.ptr[value.len - 1] == '\n')
321 {
322 value.len--;
323 }
324 }
325
326 if (value.len == 0)
327 {
328 DBG1(DBG_IMC, "no device ID available");
329 return;
330 }
331
332 DBG1(DBG_IMC, "device ID is %.*s", value.len, value.ptr);
333 attr = ita_attr_device_id_create(value);
334 msg->add_attribute(msg, attr);
335 free(value.ptr);
336 }
337
338 /**
339 * Add an IETF Installed Packages attribute to the send queue
340 */
341 static void add_installed_packages(imc_state_t *state, imc_msg_t *msg)
342 {
343 pa_tnc_attr_t *attr;
344 ietf_attr_installed_packages_t *attr_cast;
345 enumerator_t *enumerator;
346 chunk_t name, version;
347
348 attr = ietf_attr_installed_packages_create();
349
350 enumerator = os->create_package_enumerator(os);
351 while (enumerator->enumerate(enumerator, &name, &version))
352 {
353 DBG2(DBG_IMC, "package '%.*s' (%.*s)",
354 name.len, name.ptr, version.len, version.ptr);
355 attr_cast = (ietf_attr_installed_packages_t*)attr;
356 attr_cast->add(attr_cast, name, version);
357 }
358 enumerator->destroy(enumerator);
359
360 msg->add_attribute(msg, attr);
361 }
362
363 /**
364 * Add ITA Settings attribute to the send queue
365 */
366 static void add_settings(enumerator_t *enumerator, imc_msg_t *msg)
367 {
368 pa_tnc_attr_t *attr = NULL;
369 ita_attr_settings_t *attr_cast;
370 chunk_t value;
371 char *name;
372 bool first = TRUE;
373
374 while (enumerator->enumerate(enumerator, &name))
375 {
376 DBG1(DBG_IMC, "setting '%s'", name);
377
378 value = os->get_setting(os, name);
379 if (!value.ptr)
380 {
381 continue;
382 }
383 if (first)
384 {
385 attr = ita_attr_settings_create();
386 first = FALSE;
387 }
388 attr_cast = (ita_attr_settings_t*)attr;
389 attr_cast->add(attr_cast, name, value);
390 chunk_free(&value);
391 }
392
393 if (attr)
394 {
395 msg->add_attribute(msg, attr);
396 }
397 }
398
399 /**
400 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
401 */
402 TNC_Result TNC_IMC_API TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
403 TNC_ConnectionID connection_id)
404 {
405 imc_state_t *state;
406 imc_msg_t *out_msg;
407 TNC_Result result = TNC_RESULT_SUCCESS;
408
409 if (!imc_os)
410 {
411 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
412 return TNC_RESULT_NOT_INITIALIZED;
413 }
414 if (!imc_os->get_state(imc_os, connection_id, &state))
415 {
416 return TNC_RESULT_FATAL;
417 }
418 if (lib->settings->get_bool(lib->settings,
419 "%s.plugins.imc-os.push_info", TRUE, lib->ns))
420 {
421 out_msg = imc_msg_create(imc_os, state, connection_id, imc_id,
422 TNC_IMVID_ANY, msg_types[0]);
423 add_product_info(out_msg);
424 add_string_version(out_msg);
425 add_numeric_version(out_msg);
426 add_op_status(out_msg);
427 add_fwd_enabled(out_msg);
428 add_default_pwd_enabled(out_msg);
429 add_device_id(out_msg);
430
431 /* send PA-TNC message with the excl flag not set */
432 result = out_msg->send(out_msg, FALSE);
433 out_msg->destroy(out_msg);
434 }
435
436 return result;
437 }
438
439 static TNC_Result receive_message(imc_state_t *state, imc_msg_t *in_msg)
440 {
441 imc_msg_t *out_msg;
442 enumerator_t *enumerator;
443 pa_tnc_attr_t *attr;
444 pen_type_t type;
445 TNC_Result result;
446 bool fatal_error = FALSE;
447
448 /* generate an outgoing PA-TNC message - we might need it */
449 out_msg = imc_msg_create_as_reply(in_msg);
450
451 /* parse received PA-TNC message and handle local and remote errors */
452 result = in_msg->receive(in_msg, out_msg, &fatal_error);
453 if (result != TNC_RESULT_SUCCESS)
454 {
455 out_msg->destroy(out_msg);
456 return result;
457 }
458
459 /* analyze PA-TNC attributes */
460 enumerator = in_msg->create_attribute_enumerator(in_msg);
461 while (enumerator->enumerate(enumerator, &attr))
462 {
463 type = attr->get_type(attr);
464
465 if (type.vendor_id == PEN_IETF)
466 {
467 if (type.type == IETF_ATTR_ATTRIBUTE_REQUEST)
468 {
469 ietf_attr_attr_request_t *attr_cast;
470 pen_type_t *entry;
471 enumerator_t *e;
472
473 attr_cast = (ietf_attr_attr_request_t*)attr;
474
475 e = attr_cast->create_enumerator(attr_cast);
476 while (e->enumerate(e, &entry))
477 {
478 if (entry->vendor_id == PEN_IETF)
479 {
480 switch (entry->type)
481 {
482 case IETF_ATTR_PRODUCT_INFORMATION:
483 add_product_info(out_msg);
484 break;
485 case IETF_ATTR_STRING_VERSION:
486 add_string_version(out_msg);
487 break;
488 case IETF_ATTR_NUMERIC_VERSION:
489 add_numeric_version(out_msg);
490 break;
491 case IETF_ATTR_OPERATIONAL_STATUS:
492 add_op_status(out_msg);
493 break;
494 case IETF_ATTR_FORWARDING_ENABLED:
495 add_fwd_enabled(out_msg);
496 break;
497 case IETF_ATTR_FACTORY_DEFAULT_PWD_ENABLED:
498 add_default_pwd_enabled(out_msg);
499 break;
500 case IETF_ATTR_INSTALLED_PACKAGES:
501 add_installed_packages(state, out_msg);
502 break;
503 default:
504 break;
505 }
506 }
507 else if (entry->vendor_id == PEN_ITA)
508 {
509 switch (entry->type)
510 {
511 case ITA_ATTR_DEVICE_ID:
512 add_device_id(out_msg);
513 break;
514 default:
515 break;
516 }
517 }
518 }
519 e->destroy(e);
520 }
521 }
522 else if (type.vendor_id == PEN_ITA && type.type == ITA_ATTR_GET_SETTINGS)
523 {
524 ita_attr_get_settings_t *attr_cast;
525 enumerator_t *e;
526
527 attr_cast = (ita_attr_get_settings_t*)attr;
528
529 e = attr_cast->create_enumerator(attr_cast);
530 add_settings(e, out_msg);
531 e->destroy(e);
532 }
533 }
534 enumerator->destroy(enumerator);
535
536 if (fatal_error)
537 {
538 result = TNC_RESULT_FATAL;
539 }
540 else
541 {
542 /* send PA-TNC message with the EXCL flag set */
543 result = out_msg->send(out_msg, TRUE);
544 }
545 out_msg->destroy(out_msg);
546
547 return result;
548 }
549
550 /**
551 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
552
553 */
554 TNC_Result TNC_IMC_API TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
555 TNC_ConnectionID connection_id,
556 TNC_BufferReference msg,
557 TNC_UInt32 msg_len,
558 TNC_MessageType msg_type)
559 {
560 imc_state_t *state;
561 imc_msg_t *in_msg;
562 TNC_Result result;
563
564 if (!imc_os)
565 {
566 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
567 return TNC_RESULT_NOT_INITIALIZED;
568 }
569 if (!imc_os->get_state(imc_os, connection_id, &state))
570 {
571 return TNC_RESULT_FATAL;
572 }
573 in_msg = imc_msg_create_from_data(imc_os, state, connection_id, msg_type,
574 chunk_create(msg, msg_len));
575 result = receive_message(state, in_msg);
576 in_msg->destroy(in_msg);
577
578 return result;
579 }
580
581 /**
582 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
583 */
584 TNC_Result TNC_IMC_API TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
585 TNC_ConnectionID connection_id,
586 TNC_UInt32 msg_flags,
587 TNC_BufferReference msg,
588 TNC_UInt32 msg_len,
589 TNC_VendorID msg_vid,
590 TNC_MessageSubtype msg_subtype,
591 TNC_UInt32 src_imv_id,
592 TNC_UInt32 dst_imc_id)
593 {
594 imc_state_t *state;
595 imc_msg_t *in_msg;
596 TNC_Result result;
597
598 if (!imc_os)
599 {
600 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
601 return TNC_RESULT_NOT_INITIALIZED;
602 }
603 if (!imc_os->get_state(imc_os, connection_id, &state))
604 {
605 return TNC_RESULT_FATAL;
606 }
607 in_msg = imc_msg_create_from_long_data(imc_os, state, connection_id,
608 src_imv_id, dst_imc_id,msg_vid, msg_subtype,
609 chunk_create(msg, msg_len));
610 result =receive_message(state, in_msg);
611 in_msg->destroy(in_msg);
612
613 return result;
614 }
615
616 /**
617 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
618 */
619 TNC_Result TNC_IMC_API TNC_IMC_BatchEnding(TNC_IMCID imc_id,
620 TNC_ConnectionID connection_id)
621 {
622 if (!imc_os)
623 {
624 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
625 return TNC_RESULT_NOT_INITIALIZED;
626 }
627 return TNC_RESULT_SUCCESS;
628 }
629
630 /**
631 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
632 */
633 TNC_Result TNC_IMC_API TNC_IMC_Terminate(TNC_IMCID imc_id)
634 {
635 if (!imc_os)
636 {
637 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
638 return TNC_RESULT_NOT_INITIALIZED;
639 }
640 imc_os->destroy(imc_os);
641 imc_os = NULL;
642
643 os->destroy(os);
644 os = NULL;
645
646 return TNC_RESULT_SUCCESS;
647 }
648
649 /**
650 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
651 */
652 TNC_Result TNC_IMC_API TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
653 TNC_TNCC_BindFunctionPointer bind_function)
654 {
655 if (!imc_os)
656 {
657 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
658 return TNC_RESULT_NOT_INITIALIZED;
659 }
660 return imc_os->bind_functions(imc_os, bind_function);
661 }