65662db2a1ffbc5df935cc452681334fbfbd2ebf
[strongswan.git] / src / libpts / plugins / imc_attestation / imc_attestation_state.c
1 /*
2 * Copyright (C) 2011-2012 Sansar Choinyambuu, 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_attestation_state.h"
17
18 #include <libpts.h>
19
20 #include <utils/linked_list.h>
21 #include <debug.h>
22
23 typedef struct private_imc_attestation_state_t private_imc_attestation_state_t;
24 typedef struct func_comp_t func_comp_t;
25
26 /**
27 * Private data of an imc_attestation_state_t object.
28 */
29 struct private_imc_attestation_state_t {
30
31 /**
32 * Public members of imc_attestation_state_t
33 */
34 imc_attestation_state_t public;
35
36 /**
37 * TNCCS connection ID
38 */
39 TNC_ConnectionID connection_id;
40
41 /**
42 * TNCCS connection state
43 */
44 TNC_ConnectionState state;
45
46 /**
47 * Does the TNCCS connection support long message types?
48 */
49 bool has_long;
50
51 /**
52 * Does the TNCCS connection support exclusive delivery?
53 */
54 bool has_excl;
55
56 /**
57 * Maximum PA-TNC message size for this TNCCS connection
58 */
59 u_int32_t max_msg_len;
60
61 /**
62 * PTS object
63 */
64 pts_t *pts;
65
66 /**
67 * List of Functional Components
68 */
69 linked_list_t *components;
70
71 /**
72 * Functional Component Evidence cache list
73 */
74 linked_list_t *list;
75
76 };
77
78 METHOD(imc_state_t, get_connection_id, TNC_ConnectionID,
79 private_imc_attestation_state_t *this)
80 {
81 return this->connection_id;
82 }
83
84 METHOD(imc_state_t, has_long, bool,
85 private_imc_attestation_state_t *this)
86 {
87 return this->has_long;
88 }
89
90 METHOD(imc_state_t, has_excl, bool,
91 private_imc_attestation_state_t *this)
92 {
93 return this->has_excl;
94 }
95
96 METHOD(imc_state_t, set_flags, void,
97 private_imc_attestation_state_t *this, bool has_long, bool has_excl)
98 {
99 this->has_long = has_long;
100 this->has_excl = has_excl;
101 }
102
103 METHOD(imc_state_t, set_max_msg_len, void,
104 private_imc_attestation_state_t *this, u_int32_t max_msg_len)
105 {
106 this->max_msg_len = max_msg_len;
107 }
108
109 METHOD(imc_state_t, get_max_msg_len, u_int32_t,
110 private_imc_attestation_state_t *this)
111 {
112 return this->max_msg_len;
113 }
114
115 METHOD(imc_state_t, change_state, void,
116 private_imc_attestation_state_t *this, TNC_ConnectionState new_state)
117 {
118 this->state = new_state;
119 }
120
121 METHOD(imc_state_t, destroy, void,
122 private_imc_attestation_state_t *this)
123 {
124 this->pts->destroy(this->pts);
125 this->components->destroy_offset(this->components,
126 offsetof(pts_component_t, destroy));
127 this->list->destroy_offset(this->list,
128 offsetof(pts_comp_evidence_t, destroy));
129 free(this);
130 }
131
132 METHOD(imc_attestation_state_t, get_pts, pts_t*,
133 private_imc_attestation_state_t *this)
134 {
135 return this->pts;
136 }
137
138 METHOD(imc_attestation_state_t, create_component, pts_component_t*,
139 private_imc_attestation_state_t *this, pts_comp_func_name_t *name,
140 u_int32_t depth)
141 {
142 enumerator_t *enumerator;
143 pts_component_t *component;
144 bool found = FALSE;
145
146 enumerator = this->components->create_enumerator(this->components);
147 while (enumerator->enumerate(enumerator, &component))
148 {
149 if (name->equals(name, component->get_comp_func_name(component)))
150 {
151 found = TRUE;
152 break;
153 }
154 }
155 enumerator->destroy(enumerator);
156
157 if (!found)
158 {
159 component = pts_components->create(pts_components, name, depth, NULL);
160 if (!component)
161 {
162 return NULL;
163 }
164 this->components->insert_last(this->components, component);
165
166 }
167 return component;
168 }
169
170 METHOD(imc_attestation_state_t, add_evidence, void,
171 private_imc_attestation_state_t *this, pts_comp_evidence_t *evid)
172 {
173 this->list->insert_last(this->list, evid);
174 }
175
176 METHOD(imc_attestation_state_t, next_evidence, bool,
177 private_imc_attestation_state_t *this, pts_comp_evidence_t **evid)
178 {
179 return this->list->remove_first(this->list, (void**)evid) == SUCCESS;
180 }
181
182 /**
183 * Described in header.
184 */
185 imc_state_t *imc_attestation_state_create(TNC_ConnectionID connection_id)
186 {
187 private_imc_attestation_state_t *this;
188 char *platform_info;
189
190 INIT(this,
191 .public = {
192 .interface = {
193 .get_connection_id = _get_connection_id,
194 .has_long = _has_long,
195 .has_excl = _has_excl,
196 .set_flags = _set_flags,
197 .set_max_msg_len = _set_max_msg_len,
198 .get_max_msg_len = _get_max_msg_len,
199 .change_state = _change_state,
200 .destroy = _destroy,
201 },
202 .get_pts = _get_pts,
203 .create_component = _create_component,
204 .add_evidence = _add_evidence,
205 .next_evidence = _next_evidence,
206 },
207 .connection_id = connection_id,
208 .state = TNC_CONNECTION_STATE_CREATE,
209 .pts = pts_create(TRUE),
210 .components = linked_list_create(),
211 .list = linked_list_create(),
212 );
213
214 platform_info = lib->settings->get_str(lib->settings,
215 "libimcv.plugins.imc-attestation.platform_info", NULL);
216 if (platform_info)
217 {
218 this->pts->set_platform_info(this->pts, platform_info);
219 }
220
221 return &this->public.interface;
222 }
223
224