Cleaned up certreq payload for IKEv2/IKEv1 use
[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 CERTIFICATE_REQUEST or CERTIFICATE_REQUEST_V1
70 */
71 payload_type_t type;
72 };
73
74 /**
75 * Encoding rules for CERTREQ payload.
76 */
77 static encoding_rule_t encodings[] = {
78 /* 1 Byte next payload type, stored in the field next_payload */
79 { U_INT_8, offsetof(private_certreq_payload_t, next_payload) },
80 /* the critical bit */
81 { FLAG, offsetof(private_certreq_payload_t, critical) },
82 /* 7 Bit reserved bits */
83 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[0]) },
84 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[1]) },
85 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[2]) },
86 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[3]) },
87 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[4]) },
88 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[5]) },
89 { RESERVED_BIT, offsetof(private_certreq_payload_t, reserved[6]) },
90 /* Length of the whole payload*/
91 { PAYLOAD_LENGTH, offsetof(private_certreq_payload_t, payload_length) },
92 /* 1 Byte CERTREQ type*/
93 { U_INT_8, offsetof(private_certreq_payload_t, encoding) },
94 /* some certreq data bytes, length is defined in PAYLOAD_LENGTH */
95 { CHUNK_DATA, offsetof(private_certreq_payload_t, data) }
96 };
97
98 /*
99 1 2 3
100 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
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 ! Next Payload !C! RESERVED ! Payload Length !
103 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 ! Cert Encoding ! !
105 +-+-+-+-+-+-+-+-+ !
106 ~ Certification Authority ~
107 ! !
108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 */
110
111 METHOD(payload_t, verify, status_t,
112 private_certreq_payload_t *this)
113 {
114 if (this->type == CERTIFICATE_REQUEST &&
115 this->encoding == ENC_X509_SIGNATURE)
116 {
117 if (this->data.len < HASH_SIZE_SHA1 ||
118 this->data.len % HASH_SIZE_SHA1)
119 {
120 DBG1(DBG_ENC, "invalid X509 hash length (%d) in certreq",
121 this->data.len);
122 return FAILED;
123 }
124 }
125 return SUCCESS;
126 }
127
128 METHOD(payload_t, get_encoding_rules, int,
129 private_certreq_payload_t *this, encoding_rule_t **rules)
130 {
131 *rules = encodings;
132 return countof(encodings);
133 }
134
135 METHOD(payload_t, get_header_length, int,
136 private_certreq_payload_t *this)
137 {
138 return 5;
139 }
140
141 METHOD(payload_t, get_type, payload_type_t,
142 private_certreq_payload_t *this)
143 {
144 return this->type;
145 }
146
147 METHOD(payload_t, get_next_type, payload_type_t,
148 private_certreq_payload_t *this)
149 {
150 return this->next_payload;
151 }
152
153 METHOD(payload_t, set_next_type, void,
154 private_certreq_payload_t *this, payload_type_t type)
155 {
156 this->next_payload = type;
157 }
158
159 METHOD(payload_t, get_length, size_t,
160 private_certreq_payload_t *this)
161 {
162 return this->payload_length;
163 }
164
165 METHOD(certreq_payload_t, get_dn, identification_t*,
166 private_certreq_payload_t *this)
167 {
168 if (this->data.len)
169 {
170 return identification_create_from_encoding(ID_DER_ASN1_DN, this->data);
171 }
172 return NULL;
173 }
174
175 METHOD(certreq_payload_t, add_keyid, void,
176 private_certreq_payload_t *this, chunk_t keyid)
177 {
178 this->data = chunk_cat("mc", this->data, keyid);
179 this->payload_length += keyid.len;
180 }
181
182 typedef struct keyid_enumerator_t keyid_enumerator_t;
183
184 /**
185 * enumerator to enumerate keyids
186 */
187 struct keyid_enumerator_t {
188 enumerator_t public;
189 chunk_t full;
190 u_char *pos;
191 };
192
193 METHOD(enumerator_t, keyid_enumerate, bool,
194 keyid_enumerator_t *this, chunk_t *chunk)
195 {
196 if (this->pos == NULL)
197 {
198 this->pos = this->full.ptr;
199 }
200 else
201 {
202 this->pos += HASH_SIZE_SHA1;
203 if (this->pos > (this->full.ptr + this->full.len - HASH_SIZE_SHA1))
204 {
205 this->pos = NULL;
206 }
207 }
208 if (this->pos)
209 {
210 chunk->ptr = this->pos;
211 chunk->len = HASH_SIZE_SHA1;
212 return TRUE;
213 }
214 return FALSE;
215 }
216
217 METHOD(certreq_payload_t, create_keyid_enumerator, enumerator_t*,
218 private_certreq_payload_t *this)
219 {
220 keyid_enumerator_t *enumerator;
221
222 if (this->type == CERTIFICATE_REQUEST_V1)
223 {
224 return enumerator_create_empty();
225 }
226 INIT(enumerator,
227 .public = {
228 .enumerate = (void*)_keyid_enumerate,
229 .destroy = (void*)free,
230 },
231 .full = this->data,
232 );
233 return &enumerator->public;
234 }
235
236 METHOD(certreq_payload_t, get_cert_type, certificate_type_t,
237 private_certreq_payload_t *this)
238 {
239 switch (this->encoding)
240 {
241 case ENC_X509_SIGNATURE:
242 return CERT_X509;
243 default:
244 return CERT_ANY;
245 }
246 }
247
248 METHOD2(payload_t, certreq_payload_t, destroy, void,
249 private_certreq_payload_t *this)
250 {
251 chunk_free(&this->data);
252 free(this);
253 }
254
255 /*
256 * Described in header
257 */
258 certreq_payload_t *certreq_payload_create(payload_type_t type)
259 {
260 private_certreq_payload_t *this;
261
262 INIT(this,
263 .public = {
264 .payload_interface = {
265 .verify = _verify,
266 .get_encoding_rules = _get_encoding_rules,
267 .get_header_length = _get_header_length,
268 .get_length = _get_length,
269 .get_next_type = _get_next_type,
270 .set_next_type = _set_next_type,
271 .get_type = _get_type,
272 .destroy = _destroy,
273 },
274 .create_keyid_enumerator = _create_keyid_enumerator,
275 .get_cert_type = _get_cert_type,
276 .add_keyid = _add_keyid,
277 .destroy = _destroy,
278 .get_dn = _get_dn,
279 },
280 .next_payload = NO_PAYLOAD,
281 .payload_length = get_header_length(this),
282 .type = type,
283 );
284 return &this->public;
285 }
286
287 /*
288 * Described in header
289 */
290 certreq_payload_t *certreq_payload_create_type(certificate_type_t type)
291 {
292 private_certreq_payload_t *this;
293
294 this = (private_certreq_payload_t*)
295 certreq_payload_create(CERTIFICATE_REQUEST);
296 switch (type)
297 {
298 case CERT_X509:
299 this->encoding = ENC_X509_SIGNATURE;
300 break;
301 default:
302 DBG1(DBG_ENC, "certificate type %N not supported in requests",
303 certificate_type_names, type);
304 free(this);
305 return NULL;
306 }
307 return &this->public;
308 }
309
310 /*
311 * Described in header
312 */
313 certreq_payload_t *certreq_payload_create_dn(identification_t *id)
314 {
315 private_certreq_payload_t *this;
316
317 this = (private_certreq_payload_t*)
318 certreq_payload_create(CERTIFICATE_REQUEST_V1);
319
320 this->encoding = ENC_X509_SIGNATURE;
321 this->data = chunk_clone(id->get_encoding(id));
322 this->payload_length = get_header_length(this) + this->data.len;
323
324 return &this->public;
325 }