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