Inserted missing "!" mark
[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 #define PTS_AIK_FLAGS_NONE 0
40 #define PTS_AIK_FLAGS_NAKED_KEY (1<<7)
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 * AIK Certificate or Public Key
73 */
74 certificate_t *aik;
75 };
76
77 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
78 private_tcg_pts_attr_aik_t *this)
79 {
80 return this->vendor_id;
81 }
82
83 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
84 private_tcg_pts_attr_aik_t *this)
85 {
86 return this->type;
87 }
88
89 METHOD(pa_tnc_attr_t, get_value, chunk_t,
90 private_tcg_pts_attr_aik_t *this)
91 {
92 return this->value;
93 }
94
95 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
96 private_tcg_pts_attr_aik_t *this)
97 {
98 return this->noskip_flag;
99 }
100
101 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
102 private_tcg_pts_attr_aik_t *this, bool noskip)
103 {
104 this->noskip_flag = noskip;
105 }
106
107 METHOD(pa_tnc_attr_t, build, void,
108 private_tcg_pts_attr_aik_t *this)
109 {
110 bio_writer_t *writer;
111 u_int8_t flags = PTS_AIK_FLAGS_NONE;
112 chunk_t aik_blob;
113
114 if (this->aik->get_type(this->aik) == CERT_TRUSTED_PUBKEY)
115 {
116 flags |= PTS_AIK_FLAGS_NAKED_KEY;
117 }
118 if (!this->aik->get_encoding(this->aik, CERT_ASN1_DER, &aik_blob))
119 {
120 DBG1(DBG_TNC, "encoding of Attestation Identity Key failed");
121 aik_blob = chunk_empty;
122 }
123 writer = bio_writer_create(PTS_AIK_SIZE);
124 writer->write_uint8(writer, flags);
125 writer->write_data (writer, aik_blob);
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 certificate_type_t type;
136 chunk_t aik_blob;
137
138 if (this->value.len < PTS_AIK_SIZE)
139 {
140 DBG1(DBG_TNC, "insufficient data for Attestation Identity Key");
141 *offset = 0;
142 return FAILED;
143 }
144 reader = bio_reader_create(this->value);
145 reader->read_uint8(reader, &flags);
146 reader->read_data (reader, reader->remaining(reader), &aik_blob);
147
148 type = (flags & PTS_AIK_FLAGS_NAKED_KEY) ? CERT_TRUSTED_PUBKEY : CERT_X509;
149
150 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
151 BUILD_BLOB_PEM, aik_blob, BUILD_END);
152 reader->destroy(reader);
153
154 if (!this->aik)
155 {
156 DBG1(DBG_TNC, "parsing of Attestation Identity Key failed");
157 *offset = 0;
158 return FAILED;
159 }
160 return SUCCESS;
161 }
162
163 METHOD(pa_tnc_attr_t, destroy, void,
164 private_tcg_pts_attr_aik_t *this)
165 {
166 DESTROY_IF(this->aik);
167 free(this->value.ptr);
168 free(this);
169 }
170
171 METHOD(tcg_pts_attr_aik_t, get_aik, certificate_t*,
172 private_tcg_pts_attr_aik_t *this)
173 {
174 return this->aik;
175 }
176
177 /**
178 * Described in header.
179 */
180 pa_tnc_attr_t *tcg_pts_attr_aik_create(certificate_t *aik)
181 {
182 private_tcg_pts_attr_aik_t *this;
183
184 INIT(this,
185 .public = {
186 .pa_tnc_attribute = {
187 .get_vendor_id = _get_vendor_id,
188 .get_type = _get_type,
189 .get_value = _get_value,
190 .get_noskip_flag = _get_noskip_flag,
191 .set_noskip_flag = _set_noskip_flag,
192 .build = _build,
193 .process = _process,
194 .destroy = _destroy,
195 },
196 .get_aik = _get_aik,
197 },
198 .vendor_id = PEN_TCG,
199 .type = TCG_PTS_AIK,
200 .aik = aik->get_ref(aik),
201 );
202
203 return &this->public.pa_tnc_attribute;
204 }
205
206
207 /**
208 * Described in header.
209 */
210 pa_tnc_attr_t *tcg_pts_attr_aik_create_from_data(chunk_t data)
211 {
212 private_tcg_pts_attr_aik_t *this;
213
214 INIT(this,
215 .public = {
216 .pa_tnc_attribute = {
217 .get_vendor_id = _get_vendor_id,
218 .get_type = _get_type,
219 .get_value = _get_value,
220 .get_noskip_flag = _get_noskip_flag,
221 .set_noskip_flag = _set_noskip_flag,
222 .build = _build,
223 .process = _process,
224 .destroy = _destroy,
225 },
226 .get_aik = _get_aik,
227 },
228 .vendor_id = PEN_TCG,
229 .type = TCG_PTS_AIK,
230 .value = chunk_clone(data),
231 );
232
233 return &this->public.pa_tnc_attribute;
234 }