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