moved TSS into pts object
[strongswan.git] / src / libimcv / plugins / imv_attestation / imv_attestation_state.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
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 "imv_attestation_state.h"
17
18 #include <utils/lexparser.h>
19 #include <debug.h>
20
21 typedef struct private_imv_attestation_state_t private_imv_attestation_state_t;
22
23 /**
24 * Private data of an imv_attestation_state_t object.
25 */
26 struct private_imv_attestation_state_t {
27
28 /**
29 * Public members of imv_attestation_state_t
30 */
31 imv_attestation_state_t public;
32
33 /**
34 * TNCCS connection ID
35 */
36 TNC_ConnectionID connection_id;
37
38 /**
39 * TNCCS connection state
40 */
41 TNC_ConnectionState state;
42
43 /**
44 * IMV Attestation handshake state
45 */
46 imv_attestation_handshake_state_t handshake_state;
47
48 /**
49 * IMV action recommendation
50 */
51 TNC_IMV_Action_Recommendation rec;
52
53 /**
54 * IMV evaluation result
55 */
56 TNC_IMV_Evaluation_Result eval;
57
58 /**
59 * PTS object
60 */
61 pts_t *pts;
62
63 };
64
65 typedef struct entry_t entry_t;
66
67 /**
68 * Define an internal reason string entry
69 */
70 struct entry_t {
71 char *lang;
72 char *string;
73 };
74
75 /**
76 * Table of multi-lingual reason string entries
77 */
78 static entry_t reasons[] = {
79 { "en", "IMC Attestation ..." },
80 { "mn", "IMC Attestation ..." },
81 { "de", "IMC Attestation ..." },
82 };
83
84 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
85 private_imv_attestation_state_t *this)
86 {
87 return this->connection_id;
88 }
89
90 METHOD(imv_state_t, change_state, void,
91 private_imv_attestation_state_t *this, TNC_ConnectionState new_state)
92 {
93 this->state = new_state;
94 }
95
96 METHOD(imv_state_t, get_recommendation, void,
97 private_imv_attestation_state_t *this, TNC_IMV_Action_Recommendation *rec,
98 TNC_IMV_Evaluation_Result *eval)
99 {
100 *rec = this->rec;
101 *eval = this->eval;
102 }
103
104 METHOD(imv_state_t, set_recommendation, void,
105 private_imv_attestation_state_t *this, TNC_IMV_Action_Recommendation rec,
106 TNC_IMV_Evaluation_Result eval)
107 {
108 this->rec = rec;
109 this->eval = eval;
110 }
111
112 METHOD(imv_state_t, get_reason_string, bool,
113 private_imv_attestation_state_t *this, chunk_t preferred_language,
114 chunk_t *reason_string, chunk_t *reason_language)
115 {
116 chunk_t pref_lang, lang;
117 u_char *pos;
118 int i;
119
120 while (eat_whitespace(&preferred_language))
121 {
122 if (!extract_token(&pref_lang, ',', &preferred_language))
123 {
124 /* last entry in a comma-separated list or single entry */
125 pref_lang = preferred_language;
126 }
127
128 /* eat trailing whitespace */
129 pos = pref_lang.ptr + pref_lang.len - 1;
130 while (pref_lang.len && *pos-- == ' ')
131 {
132 pref_lang.len--;
133 }
134
135 for (i = 0 ; i < countof(reasons); i++)
136 {
137 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
138 if (chunk_equals(lang, pref_lang))
139 {
140 *reason_language = lang;
141 *reason_string = chunk_create(reasons[i].string,
142 strlen(reasons[i].string));
143 return TRUE;
144 }
145 }
146 }
147
148 /* no preferred language match found - use the default language */
149 *reason_string = chunk_create(reasons[0].string,
150 strlen(reasons[0].string));
151 *reason_language = chunk_create(reasons[0].lang,
152 strlen(reasons[0].lang));
153 return TRUE;
154 }
155
156 METHOD(imv_state_t, destroy, void,
157 private_imv_attestation_state_t *this)
158 {
159 this->pts->destroy(this->pts);
160 free(this);
161 }
162
163 METHOD(imv_attestation_state_t, get_handshake_state, imv_attestation_handshake_state_t,
164 private_imv_attestation_state_t *this)
165 {
166 return this->handshake_state;
167 }
168
169 METHOD(imv_attestation_state_t, set_handshake_state, void,
170 private_imv_attestation_state_t *this, imv_attestation_handshake_state_t new_state)
171 {
172 this->handshake_state = new_state;
173 }
174
175 METHOD(imv_attestation_state_t, get_pts, pts_t*,
176 private_imv_attestation_state_t *this)
177 {
178 return this->pts;
179 }
180
181 /**
182 * Described in header.
183 */
184 imv_state_t *imv_attestation_state_create(TNC_ConnectionID connection_id)
185 {
186 private_imv_attestation_state_t *this;
187
188 INIT(this,
189 .public = {
190 .interface = {
191 .get_connection_id = _get_connection_id,
192 .change_state = _change_state,
193 .get_recommendation = _get_recommendation,
194 .set_recommendation = _set_recommendation,
195 .get_reason_string = _get_reason_string,
196 .destroy = _destroy,
197 },
198 .get_handshake_state = _get_handshake_state,
199 .set_handshake_state = _set_handshake_state,
200 .get_pts = _get_pts,
201 },
202 .connection_id = connection_id,
203 .state = TNC_CONNECTION_STATE_CREATE,
204 .handshake_state = IMV_ATTESTATION_STATE_INIT,
205 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
206 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
207 .pts = pts_create(),
208 );
209
210 return &this->public.interface;
211 }