certificate handling for XAuth responder.
[strongswan.git] / src / libcharon / encoding / payloads / certreq_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include <daemon.h>
21 #include <crypto/hashers/hasher.h>
22 #include <encoding/payloads/cert_payload.h>
23
24 #include "certreq_payload.h"
25
26 typedef struct private_certreq_payload_t private_certreq_payload_t;
27
28 /**
29 * Private data of an certreq_payload_t object.
30 */
31 struct private_certreq_payload_t {
32
33 /**
34 * Public certreq_payload_t interface.
35 */
36 certreq_payload_t public;
37
38 /**
39 * Next payload type.
40 */
41 u_int8_t next_payload;
42
43 /**
44 * Critical flag.
45 */
46 bool critical;
47
48 /**
49 * Reserved bits
50 */
51 bool reserved[7];
52
53 /**
54 * Length of this payload.
55 */
56 u_int16_t payload_length;
57
58 /**
59 * Encoding of the CERT Data.
60 */
61 u_int8_t encoding;
62
63 /**
64 * The contained certreq data value.
65 */
66 chunk_t data;
67
68 /**
69 * Payload type for certificate request.
70 */
71 payload_type_t payload_type;
72 };
73
74 /**
75 * Encoding rules to parse or generate a CERTREQ payload
76 *
77 * The defined offsets are the positions in a object of type
78 * private_certreq_payload_t.
79 */
80 static encoding_rule_t encodings[] = {
81 /* 1 Byte next payload type, stored in the field next_payload */
82 { U_INT_8, offsetof(private_certreq_payload_t, next_payload) },
83 /* the critical bit */
84 { FLAG, offsetof(private_certreq_payload_t, critical) },
85 /* 7 Bit reserved bits */
86 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[0]) },
87 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[1]) },
88 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[2]) },
89 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[3]) },
90 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[4]) },
91 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[5]) },
92 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[6]) },
93 /* Length of the whole payload*/
94 { PAYLOAD_LENGTH, offsetof(private_certreq_payload_t, payload_length) },
95 /* 1 Byte CERTREQ type*/
96 { U_INT_8, offsetof(private_certreq_payload_t, encoding) },
97 /* some certreq data bytes, length is defined in PAYLOAD_LENGTH */
98 { CHUNK_DATA, offsetof(private_certreq_payload_t, data) }
99 };
100
101 /*
102 1 2 3
103 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
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 ! Next Payload !C! RESERVED ! Payload Length !
106 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 ! Cert Encoding ! !
108 +-+-+-+-+-+-+-+-+ !
109 ~ Certification Authority ~
110 ! !
111 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 */
113
114 METHOD(payload_t, verify, status_t,
115 private_certreq_payload_t *this)
116 {
117 if (this->encoding == ENC_X509_SIGNATURE)
118 {
119 if (this->data.len < HASH_SIZE_SHA1 ||
120 this->data.len % HASH_SIZE_SHA1)
121 {
122 DBG1(DBG_ENC, "invalid X509 hash length (%d) in certreq",
123 this->data.len);
124 return FAILED;
125 }
126 }
127 return SUCCESS;
128 }
129
130 METHOD(payload_t, verify_v1, status_t,
131 private_certreq_payload_t *this)
132 {
133 /*TODO: */
134 return SUCCESS;
135 }
136
137 METHOD(payload_t, get_encoding_rules, int,
138 private_certreq_payload_t *this, encoding_rule_t **rules)
139 {
140 *rules = encodings;
141 return countof(encodings);
142 }
143
144 METHOD(payload_t, get_header_length, int,
145 private_certreq_payload_t *this)
146 {
147 return 5;
148 }
149
150 METHOD(payload_t, get_type, payload_type_t,
151 private_certreq_payload_t *this)
152 {
153 return this->payload_type;
154 }
155
156 METHOD(payload_t, get_next_type, payload_type_t,
157 private_certreq_payload_t *this)
158 {
159 return this->next_payload;
160 }
161
162 METHOD(payload_t, set_next_type, void,
163 private_certreq_payload_t *this, payload_type_t type)
164 {
165 this->next_payload = type;
166 }
167
168 METHOD(payload_t, get_length, size_t,
169 private_certreq_payload_t *this)
170 {
171 return this->payload_length;
172 }
173
174 METHOD(certreq_payload_t, get_dn, chunk_t,
175 private_certreq_payload_t *this)
176 {
177 return this->data;
178 }
179
180 METHOD(certreq_payload_t, set_dn, void,
181 private_certreq_payload_t *this, chunk_t dn)
182 {
183 if (this->data.ptr)
184 {
185 free(this->data.ptr);
186 }
187 this->data = chunk_clone(dn);
188 this->payload_length = get_header_length(this) + this->data.len;
189 }
190
191 METHOD(certreq_payload_t, add_keyid, void,
192 private_certreq_payload_t *this, chunk_t keyid)
193 {
194 this->data = chunk_cat("mc", this->data, keyid);
195 this->payload_length += keyid.len;
196 }
197
198 typedef struct keyid_enumerator_t keyid_enumerator_t;
199
200 /**
201 * enumerator to enumerate keyids
202 */
203 struct keyid_enumerator_t {
204 enumerator_t public;
205 chunk_t full;
206 u_char *pos;
207 };
208
209 METHOD(enumerator_t, keyid_enumerate, bool,
210 keyid_enumerator_t *this, chunk_t *chunk)
211 {
212 if (this->pos == NULL)
213 {
214 this->pos = this->full.ptr;
215 }
216 else
217 {
218 this->pos += HASH_SIZE_SHA1;
219 if (this->pos > (this->full.ptr + this->full.len - HASH_SIZE_SHA1))
220 {
221 this->pos = NULL;
222 }
223 }
224 if (this->pos)
225 {
226 chunk->ptr = this->pos;
227 chunk->len = HASH_SIZE_SHA1;
228 return TRUE;
229 }
230 return FALSE;
231 }
232
233 METHOD(certreq_payload_t, create_keyid_enumerator, enumerator_t*,
234 private_certreq_payload_t *this)
235 {
236 keyid_enumerator_t *enumerator;
237
238 INIT(enumerator,
239 .public = {
240 .enumerate = (void*)_keyid_enumerate,
241 .destroy = (void*)free,
242 },
243 .full = this->data,
244 );
245 return &enumerator->public;
246 }
247
248 METHOD(certreq_payload_t, get_cert_type, certificate_type_t,
249 private_certreq_payload_t *this)
250 {
251 switch (this->encoding)
252 {
253 case ENC_X509_SIGNATURE:
254 return CERT_X509;
255 default:
256 return CERT_ANY;
257 }
258 }
259
260 METHOD2(payload_t, certreq_payload_t, destroy, void,
261 private_certreq_payload_t *this)
262 {
263 chunk_free(&this->data);
264 free(this);
265 }
266
267 /*
268 * Described in header
269 */
270 certreq_payload_t *certreq_payload_create(payload_type_t payload_type)
271 {
272 private_certreq_payload_t *this;
273
274 INIT(this,
275 .public = {
276 .payload_interface = {
277 .verify = _verify,
278 .get_encoding_rules = _get_encoding_rules,
279 .get_header_length = _get_header_length,
280 .get_length = _get_length,
281 .get_next_type = _get_next_type,
282 .set_next_type = _set_next_type,
283 .get_type = _get_type,
284 .destroy = _destroy,
285 },
286 .create_keyid_enumerator = _create_keyid_enumerator,
287 .get_cert_type = _get_cert_type,
288 .add_keyid = _add_keyid,
289 .destroy = _destroy,
290 .get_dn = _get_dn,
291 .set_dn = _set_dn,
292 },
293 .next_payload = NO_PAYLOAD,
294 .payload_length = get_header_length(this),
295 .payload_type = payload_type,
296 );
297
298 if (payload_type == CERTIFICATE_REQUEST_V1)
299 {
300 this->public.payload_interface.verify = _verify_v1;
301 }
302
303 return &this->public;
304 }
305
306 /*
307 * Described in header
308 */
309 certreq_payload_t *certreq_payload_create_type(payload_type_t payload_type, certificate_type_t type)
310 {
311 private_certreq_payload_t *this = (private_certreq_payload_t*)certreq_payload_create(payload_type);
312
313 switch (type)
314 {
315 case CERT_X509:
316 this->encoding = ENC_X509_SIGNATURE;
317 break;
318 default:
319 DBG1(DBG_ENC, "certificate type %N not supported in requests",
320 certificate_type_names, type);
321 free(this);
322 return NULL;
323 }
324 return &this->public;
325 }
326