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