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