Commented compiler options Wall Werror for Makefile
[strongswan.git] / src / libimcv / tcg / tcg_pts_attr_aik.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 "tcg_pts_attr_aik.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <debug.h>
22
23 typedef struct private_tcg_pts_attr_aik_t private_tcg_pts_attr_aik_t;
24
25 /**
26 * Attestation Identity Key (see section 3.13 of PTS Protocol: Binding to TNC IF-M Specification)
27 *
28 * 1 2 3
29 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
30 *
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 * | Flags | Attestation Identity Key (Variable Lenght) ~
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | Attestation Identity Key (Variable Lenght) ~
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 *
37 */
38
39 #define PTS_AIK_SIZE 4
40
41 /**
42 * Private data of an tcg_pts_attr_aik_t object.
43 */
44 struct private_tcg_pts_attr_aik_t {
45
46 /**
47 * Public members of tcg_pts_attr_aik_t
48 */
49 tcg_pts_attr_aik_t public;
50
51 /**
52 * Attribute vendor ID
53 */
54 pen_t vendor_id;
55
56 /**
57 * Attribute type
58 */
59 u_int32_t type;
60
61 /**
62 * Attribute value
63 */
64 chunk_t value;
65
66 /**
67 * Noskip flag
68 */
69 bool noskip_flag;
70
71 /**
72 * Naked Public Key flag
73 */
74 bool naked_pub_aik;
75
76 /**
77 * Attestation Identity Key
78 */
79 chunk_t aik;
80 };
81
82 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
83 private_tcg_pts_attr_aik_t *this)
84 {
85 return this->vendor_id;
86 }
87
88 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
89 private_tcg_pts_attr_aik_t *this)
90 {
91 return this->type;
92 }
93
94 METHOD(pa_tnc_attr_t, get_value, chunk_t,
95 private_tcg_pts_attr_aik_t *this)
96 {
97 return this->value;
98 }
99
100 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
101 private_tcg_pts_attr_aik_t *this)
102 {
103 return this->noskip_flag;
104 }
105
106 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
107 private_tcg_pts_attr_aik_t *this, bool noskip)
108 {
109 this->noskip_flag = noskip;
110 }
111
112 METHOD(pa_tnc_attr_t, build, void,
113 private_tcg_pts_attr_aik_t *this)
114 {
115 bio_writer_t *writer;
116 u_int8_t flags = 0;
117
118 writer = bio_writer_create(PTS_AIK_SIZE);
119
120 if(this->naked_pub_aik) flags += 1;
121 writer->write_uint8 (writer, flags);
122 writer->write_data(writer, this->aik);
123
124 this->value = chunk_clone(writer->get_buf(writer));
125 writer->destroy(writer);
126 }
127
128 METHOD(pa_tnc_attr_t, process, status_t,
129 private_tcg_pts_attr_aik_t *this, u_int32_t *offset)
130 {
131 bio_reader_t *reader;
132 u_int8_t flags;
133
134 if (this->value.len < PTS_AIK_SIZE)
135 {
136 DBG1(DBG_TNC, "insufficient data for Attestation Identity Key");
137 *offset = 0;
138 return FAILED;
139 }
140 reader = bio_reader_create(this->value);
141
142 reader->read_uint8(reader, &flags);
143 if(flags) this->naked_pub_aik = true;
144
145 reader->read_data (reader, sizeof(this->value) - 1, &this->aik);
146 this->aik = chunk_clone(this->aik);
147 reader->destroy(reader);
148
149 return SUCCESS;
150 }
151
152 METHOD(pa_tnc_attr_t, destroy, void,
153 private_tcg_pts_attr_aik_t *this)
154 {
155 free(this->value.ptr);
156 free(this->aik.ptr);
157 free(this);
158 }
159
160 METHOD(tcg_pts_attr_aik_t, get_naked_flag, bool,
161 private_tcg_pts_attr_aik_t *this)
162 {
163 return this->naked_pub_aik;
164 }
165
166 METHOD(tcg_pts_attr_aik_t, set_naked_flag, void,
167 private_tcg_pts_attr_aik_t *this, bool naked_pub_aik)
168 {
169 this->naked_pub_aik = naked_pub_aik;
170 }
171
172 METHOD(tcg_pts_attr_aik_t, get_aik, chunk_t,
173 private_tcg_pts_attr_aik_t *this)
174 {
175 return this->aik;
176 }
177
178 METHOD(tcg_pts_attr_aik_t, set_aik, void,
179 private_tcg_pts_attr_aik_t *this,
180 chunk_t aik)
181 {
182 this->aik = aik;
183 }
184
185 /**
186 * Described in header.
187 */
188 pa_tnc_attr_t *tcg_pts_attr_aik_create(bool naked_pub_aik, chunk_t aik)
189 {
190 private_tcg_pts_attr_aik_t *this;
191
192 INIT(this,
193 .public = {
194 .pa_tnc_attribute = {
195 .get_vendor_id = _get_vendor_id,
196 .get_type = _get_type,
197 .get_value = _get_value,
198 .get_noskip_flag = _get_noskip_flag,
199 .set_noskip_flag = _set_noskip_flag,
200 .build = _build,
201 .process = _process,
202 .destroy = _destroy,
203 },
204 .get_naked_flag = _get_naked_flag,
205 .set_naked_flag = _set_naked_flag,
206 .get_aik = _get_aik,
207 .set_aik = _set_aik,
208 },
209 .vendor_id = PEN_TCG,
210 .type = TCG_PTS_AIK,
211 .naked_pub_aik = naked_pub_aik,
212 .aik = aik,
213 );
214
215 return &this->public.pa_tnc_attribute;
216 }
217
218
219 /**
220 * Described in header.
221 */
222 pa_tnc_attr_t *tcg_pts_attr_aik_create_from_data(chunk_t data)
223 {
224 private_tcg_pts_attr_aik_t *this;
225
226 INIT(this,
227 .public = {
228 .pa_tnc_attribute = {
229 .get_vendor_id = _get_vendor_id,
230 .get_type = _get_type,
231 .get_value = _get_value,
232 .get_noskip_flag = _get_noskip_flag,
233 .set_noskip_flag = _set_noskip_flag,
234 .build = _build,
235 .process = _process,
236 .destroy = _destroy,
237 },
238 .get_naked_flag = _get_naked_flag,
239 .set_naked_flag = _set_naked_flag,
240 .get_aik = _get_aik,
241 .set_aik = _set_aik,
242 },
243 .vendor_id = PEN_TCG,
244 .type = TCG_PTS_AIK,
245 .value = chunk_clone(data),
246 );
247
248 return &this->public.pa_tnc_attribute;
249 }