3c278a6ea04d5ef75895c15c24a54a0e00c9fd39
[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 action recommendation
45 */
46 TNC_IMV_Action_Recommendation rec;
47
48 /**
49 * IMV evaluation result
50 */
51 TNC_IMV_Evaluation_Result eval;
52 };
53
54 typedef struct entry_t entry_t;
55
56 /**
57 * Define an internal reason string entry
58 */
59 struct entry_t {
60 char *lang;
61 char *string;
62 };
63
64 /**
65 * Table of multi-lingual reason string entries
66 */
67 static entry_t reasons[] = {
68 { "en", "IMC Attestation ..." },
69 { "mn", "IMC Attestation ..." },
70 { "de", "IMC Attestation ..." },
71 };
72
73 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
74 private_imv_attestation_state_t *this)
75 {
76 return this->connection_id;
77 }
78
79 METHOD(imv_state_t, change_state, void,
80 private_imv_attestation_state_t *this, TNC_ConnectionState new_state)
81 {
82 this->state = new_state;
83 }
84
85 METHOD(imv_state_t, get_recommendation, void,
86 private_imv_attestation_state_t *this, TNC_IMV_Action_Recommendation *rec,
87 TNC_IMV_Evaluation_Result *eval)
88 {
89 *rec = this->rec;
90 *eval = this->eval;
91 }
92
93 METHOD(imv_state_t, set_recommendation, void,
94 private_imv_attestation_state_t *this, TNC_IMV_Action_Recommendation rec,
95 TNC_IMV_Evaluation_Result eval)
96 {
97 this->rec = rec;
98 this->eval = eval;
99 }
100
101 METHOD(imv_state_t, get_reason_string, bool,
102 private_imv_attestation_state_t *this, chunk_t preferred_language,
103 chunk_t *reason_string, chunk_t *reason_language)
104 {
105 chunk_t pref_lang, lang;
106 u_char *pos;
107 int i;
108
109 while (eat_whitespace(&preferred_language))
110 {
111 if (!extract_token(&pref_lang, ',', &preferred_language))
112 {
113 /* last entry in a comma-separated list or single entry */
114 pref_lang = preferred_language;
115 }
116
117 /* eat trailing whitespace */
118 pos = pref_lang.ptr + pref_lang.len - 1;
119 while (pref_lang.len && *pos-- == ' ')
120 {
121 pref_lang.len--;
122 }
123
124 for (i = 0 ; i < countof(reasons); i++)
125 {
126 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
127 if (chunk_equals(lang, pref_lang))
128 {
129 *reason_language = lang;
130 *reason_string = chunk_create(reasons[i].string,
131 strlen(reasons[i].string));
132 return TRUE;
133 }
134 }
135 }
136
137 /* no preferred language match found - use the default language */
138 *reason_string = chunk_create(reasons[0].string,
139 strlen(reasons[0].string));
140 *reason_language = chunk_create(reasons[0].lang,
141 strlen(reasons[0].lang));
142 return TRUE;
143 }
144
145 METHOD(imv_state_t, destroy, void,
146 private_imv_attestation_state_t *this)
147 {
148 free(this);
149 }
150
151 /**
152 * Described in header.
153 */
154 imv_state_t *imv_attestation_state_create(TNC_ConnectionID connection_id)
155 {
156 private_imv_attestation_state_t *this;
157
158 INIT(this,
159 .public = {
160 .interface = {
161 .get_connection_id = _get_connection_id,
162 .change_state = _change_state,
163 .get_recommendation = _get_recommendation,
164 .set_recommendation = _set_recommendation,
165 .get_reason_string = _get_reason_string,
166 .destroy = _destroy,
167 },
168 },
169 .state = TNC_CONNECTION_STATE_CREATE,
170 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
171 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
172 );
173
174 return &this->public.interface;
175 }
176
177