Freeing memory allocated for AIK after writing
[strongswan.git] / src / libpts / 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 cred_encoding_type_t encoding_type = CERT_ASN1_DER;
113 chunk_t aik_blob;
114
115 if (this->aik->get_type(this->aik) == CERT_TRUSTED_PUBKEY)
116 {
117 flags |= PTS_AIK_FLAGS_NAKED_KEY;
118 encoding_type = PUBKEY_SPKI_ASN1_DER;
119 }
120 if (!this->aik->get_encoding(this->aik, encoding_type, &aik_blob))
121 {
122 DBG1(DBG_TNC, "encoding of Attestation Identity Key failed");
123 aik_blob = chunk_empty;
124 }
125 writer = bio_writer_create(PTS_AIK_SIZE);
126 writer->write_uint8(writer, flags);
127 writer->write_data (writer, aik_blob);
128 this->value = chunk_clone(writer->get_buf(writer));
129 free(aik_blob.ptr);
130 writer->destroy(writer);
131 }
132
133 METHOD(pa_tnc_attr_t, process, status_t,
134 private_tcg_pts_attr_aik_t *this, u_int32_t *offset)
135 {
136 bio_reader_t *reader;
137 u_int8_t flags;
138 certificate_type_t type;
139 chunk_t aik_blob;
140
141 if (this->value.len < PTS_AIK_SIZE)
142 {
143 DBG1(DBG_TNC, "insufficient data for Attestation Identity Key");
144 *offset = 0;
145 return FAILED;
146 }
147 reader = bio_reader_create(this->value);
148 reader->read_uint8(reader, &flags);
149 reader->read_data (reader, reader->remaining(reader), &aik_blob);
150
151 type = (flags & PTS_AIK_FLAGS_NAKED_KEY) ? CERT_TRUSTED_PUBKEY : CERT_X509;
152
153 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
154 BUILD_BLOB_PEM, aik_blob, BUILD_END);
155 reader->destroy(reader);
156
157 if (!this->aik)
158 {
159 DBG1(DBG_TNC, "parsing of Attestation Identity Key failed");
160 *offset = 0;
161 return FAILED;
162 }
163 return SUCCESS;
164 }
165
166 METHOD(pa_tnc_attr_t, destroy, void,
167 private_tcg_pts_attr_aik_t *this)
168 {
169 DESTROY_IF(this->aik);
170 free(this->value.ptr);
171 free(this);
172 }
173
174 METHOD(tcg_pts_attr_aik_t, get_aik, certificate_t*,
175 private_tcg_pts_attr_aik_t *this)
176 {
177 return this->aik;
178 }
179
180 /**
181 * Described in header.
182 */
183 pa_tnc_attr_t *tcg_pts_attr_aik_create(certificate_t *aik)
184 {
185 private_tcg_pts_attr_aik_t *this;
186
187 INIT(this,
188 .public = {
189 .pa_tnc_attribute = {
190 .get_vendor_id = _get_vendor_id,
191 .get_type = _get_type,
192 .get_value = _get_value,
193 .get_noskip_flag = _get_noskip_flag,
194 .set_noskip_flag = _set_noskip_flag,
195 .build = _build,
196 .process = _process,
197 .destroy = _destroy,
198 },
199 .get_aik = _get_aik,
200 },
201 .vendor_id = PEN_TCG,
202 .type = TCG_PTS_AIK,
203 .aik = aik->get_ref(aik),
204 );
205
206 return &this->public.pa_tnc_attribute;
207 }
208
209
210 /**
211 * Described in header.
212 */
213 pa_tnc_attr_t *tcg_pts_attr_aik_create_from_data(chunk_t data)
214 {
215 private_tcg_pts_attr_aik_t *this;
216
217 INIT(this,
218 .public = {
219 .pa_tnc_attribute = {
220 .get_vendor_id = _get_vendor_id,
221 .get_type = _get_type,
222 .get_value = _get_value,
223 .get_noskip_flag = _get_noskip_flag,
224 .set_noskip_flag = _set_noskip_flag,
225 .build = _build,
226 .process = _process,
227 .destroy = _destroy,
228 },
229 .get_aik = _get_aik,
230 },
231 .vendor_id = PEN_TCG,
232 .type = TCG_PTS_AIK,
233 .value = chunk_clone(data),
234 );
235
236 return &this->public.pa_tnc_attribute;
237 }