Removed strayed code fragment
[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
17 #include <stddef.h>
18
19 #include <daemon.h>
20 #include <crypto/hashers/hasher.h>
21 #include <encoding/payloads/cert_payload.h>
22
23 #include "certreq_payload.h"
24
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 */
32 struct private_certreq_payload_t {
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 * Length of this payload.
50 */
51 u_int16_t payload_length;
52
53 /**
54 * Encoding of the CERT Data.
55 */
56 u_int8_t encoding;
57
58 /**
59 * The contained certreq data value.
60 */
61 chunk_t data;
62 };
63
64 /**
65 * Encoding rules to parse or generate a CERTREQ payload
66 *
67 * The defined offsets are the positions in a object of type
68 * private_certreq_payload_t.
69 *
70 */
71 encoding_rule_t certreq_payload_encodings[] = {
72 /* 1 Byte next payload type, stored in the field next_payload */
73 { U_INT_8, offsetof(private_certreq_payload_t, next_payload) },
74 /* the critical bit */
75 { FLAG, offsetof(private_certreq_payload_t, critical) },
76 /* 7 Bit reserved bits, nowhere stored */
77 { RESERVED_BIT, 0 },
78 { RESERVED_BIT, 0 },
79 { RESERVED_BIT, 0 },
80 { RESERVED_BIT, 0 },
81 { RESERVED_BIT, 0 },
82 { RESERVED_BIT, 0 },
83 { RESERVED_BIT, 0 },
84 /* Length of the whole payload*/
85 { PAYLOAD_LENGTH, offsetof(private_certreq_payload_t, payload_length) },
86 /* 1 Byte CERTREQ type*/
87 { U_INT_8, offsetof(private_certreq_payload_t, encoding) },
88 /* some certreq data bytes, length is defined in PAYLOAD_LENGTH */
89 { CERTREQ_DATA, offsetof(private_certreq_payload_t, data) }
90 };
91
92 /*
93 1 2 3
94 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
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 ! Next Payload !C! RESERVED ! Payload Length !
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 ! Cert Encoding ! !
99 +-+-+-+-+-+-+-+-+ !
100 ~ Certification Authority ~
101 ! !
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 */
104
105 /**
106 * Implementation of payload_t.verify.
107 */
108 static status_t verify(private_certreq_payload_t *this)
109 {
110 if (this->encoding == ENC_X509_SIGNATURE)
111 {
112 if (this->data.len < HASH_SIZE_SHA1 ||
113 this->data.len % HASH_SIZE_SHA1)
114 {
115 DBG1(DBG_ENC, "invalid X509 hash length (%d) in certreq",
116 this->data.len);
117 return FAILED;
118 }
119 }
120 return SUCCESS;
121 }
122
123 /**
124 * Implementation of certreq_payload_t.get_encoding_rules.
125 */
126 static void get_encoding_rules(private_certreq_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
127 {
128 *rules = certreq_payload_encodings;
129 *rule_count = sizeof(certreq_payload_encodings) / sizeof(encoding_rule_t);
130 }
131
132 /**
133 * Implementation of payload_t.get_type.
134 */
135 static payload_type_t get_payload_type(private_certreq_payload_t *this)
136 {
137 return CERTIFICATE_REQUEST;
138 }
139
140 /**
141 * Implementation of payload_t.get_next_type.
142 */
143 static payload_type_t get_next_type(private_certreq_payload_t *this)
144 {
145 return (this->next_payload);
146 }
147
148 /**
149 * Implementation of payload_t.set_next_type.
150 */
151 static void set_next_type(private_certreq_payload_t *this,payload_type_t type)
152 {
153 this->next_payload = type;
154 }
155
156 /**
157 * Implementation of payload_t.get_length.
158 */
159 static size_t get_length(private_certreq_payload_t *this)
160 {
161 return this->payload_length;
162 }
163
164 /**
165 * Implementation of certreq_payload_t.add_keyid.
166 */
167 static void add_keyid(private_certreq_payload_t *this, chunk_t keyid)
168 {
169 this->data = chunk_cat("mc", this->data, keyid);
170 this->payload_length += keyid.len;
171 }
172
173 typedef struct keyid_enumerator_t keyid_enumerator_t;
174
175 /**
176 * enumerator to enumerate keyids
177 */
178 struct keyid_enumerator_t {
179 enumerator_t public;
180 chunk_t full;
181 u_char *pos;
182 };
183
184 /**
185 * enumerate function for keyid_enumerator
186 */
187 static bool keyid_enumerate(keyid_enumerator_t *this, chunk_t *chunk)
188 {
189 if (this->pos == NULL)
190 {
191 this->pos = this->full.ptr;
192 }
193 else
194 {
195 this->pos += HASH_SIZE_SHA1;
196 if (this->pos > (this->full.ptr + this->full.len - HASH_SIZE_SHA1))
197 {
198 this->pos = NULL;
199 }
200 }
201 if (this->pos)
202 {
203 chunk->ptr = this->pos;
204 chunk->len = HASH_SIZE_SHA1;
205 return TRUE;
206 }
207 return FALSE;
208 }
209
210 /**
211 * Implementation of certreq_payload_t.create_keyid_enumerator.
212 */
213 static enumerator_t* create_keyid_enumerator(private_certreq_payload_t *this)
214 {
215 keyid_enumerator_t *enumerator = malloc_thing(keyid_enumerator_t);
216 enumerator->public.enumerate = (void*)keyid_enumerate;
217 enumerator->public.destroy = (void*)free;
218 enumerator->full = this->data;
219 enumerator->pos = NULL;
220 return &enumerator->public;
221 }
222
223 /**
224 * Implementation of certreq_payload_t.get_cert_type.
225 */
226 static certificate_type_t get_cert_type(private_certreq_payload_t *this)
227 {
228 switch (this->encoding)
229 {
230 case ENC_X509_SIGNATURE:
231 return CERT_X509;
232 default:
233 return CERT_ANY;
234 }
235 }
236
237 /**
238 * Implementation of payload_t.destroy and certreq_payload_t.destroy.
239 */
240 static void destroy(private_certreq_payload_t *this)
241 {
242 chunk_free(&this->data);
243 free(this);
244 }
245
246 /*
247 * Described in header
248 */
249 certreq_payload_t *certreq_payload_create()
250 {
251 private_certreq_payload_t *this = malloc_thing(private_certreq_payload_t);
252
253 /* interface functions */
254 this->public.payload_interface.verify = (status_t (*) (payload_t*))verify;
255 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t*,encoding_rule_t**,size_t*))get_encoding_rules;
256 this->public.payload_interface.get_length = (size_t (*) (payload_t*))get_length;
257 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t*))get_next_type;
258 this->public.payload_interface.set_next_type = (void (*) (payload_t*,payload_type_t))set_next_type;
259 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t*))get_payload_type;
260 this->public.payload_interface.destroy = (void (*) (payload_t*))destroy;
261
262 /* public functions */
263 this->public.destroy = (void (*) (certreq_payload_t*)) destroy;
264 this->public.create_keyid_enumerator = (enumerator_t*(*)(certreq_payload_t*))create_keyid_enumerator;
265 this->public.get_cert_type = (certificate_type_t(*)(certreq_payload_t*))get_cert_type;
266 this->public.add_keyid = (void(*)(certreq_payload_t*, chunk_t keyid))add_keyid;
267
268 /* private variables */
269 this->critical = FALSE;
270 this->next_payload = NO_PAYLOAD;
271 this->payload_length = CERTREQ_PAYLOAD_HEADER_LENGTH;
272 this->data = chunk_empty;
273 this->encoding = 0;
274
275 return &this->public;
276 }
277
278 /*
279 * Described in header
280 */
281 certreq_payload_t *certreq_payload_create_type(certificate_type_t type)
282 {
283 private_certreq_payload_t *this = (private_certreq_payload_t*)certreq_payload_create();
284
285 switch (type)
286 {
287 case CERT_X509:
288 this->encoding = ENC_X509_SIGNATURE;
289 break;
290 default:
291 DBG1(DBG_ENC, "certificate type %N not supported in requests",
292 certificate_type_names, type);
293 free(this);
294 return NULL;
295 }
296 return &this->public;
297 }
298