payload: Use common prefixes for all payload type identifiers
[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 PLV2_CERTREQ or PLV1_CERTREQ
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 == PLV2_CERTREQ &&
115 this->encoding == ENC_X509_SIGNATURE)
116 {
117 if (this->data.len % HASH_SIZE_SHA1)
118 {
119 DBG1(DBG_ENC, "invalid X509 hash length (%d) in certreq",
120 this->data.len);
121 return FAILED;
122 }
123 }
124 return SUCCESS;
125 }
126
127 METHOD(payload_t, get_encoding_rules, int,
128 private_certreq_payload_t *this, encoding_rule_t **rules)
129 {
130 *rules = encodings;
131 return countof(encodings);
132 }
133
134 METHOD(payload_t, get_header_length, int,
135 private_certreq_payload_t *this)
136 {
137 return 5;
138 }
139
140 METHOD(payload_t, get_type, payload_type_t,
141 private_certreq_payload_t *this)
142 {
143 return this->type;
144 }
145
146 METHOD(payload_t, get_next_type, payload_type_t,
147 private_certreq_payload_t *this)
148 {
149 return this->next_payload;
150 }
151
152 METHOD(payload_t, set_next_type, void,
153 private_certreq_payload_t *this, payload_type_t type)
154 {
155 this->next_payload = type;
156 }
157
158 METHOD(payload_t, get_length, size_t,
159 private_certreq_payload_t *this)
160 {
161 return this->payload_length;
162 }
163
164 METHOD(certreq_payload_t, get_dn, identification_t*,
165 private_certreq_payload_t *this)
166 {
167 if (this->data.len)
168 {
169 return identification_create_from_encoding(ID_DER_ASN1_DN, this->data);
170 }
171 return NULL;
172 }
173
174 METHOD(certreq_payload_t, add_keyid, void,
175 private_certreq_payload_t *this, chunk_t keyid)
176 {
177 this->data = chunk_cat("mc", this->data, keyid);
178 this->payload_length += keyid.len;
179 }
180
181 typedef struct keyid_enumerator_t keyid_enumerator_t;
182
183 /**
184 * enumerator to enumerate keyids
185 */
186 struct keyid_enumerator_t {
187 enumerator_t public;
188 chunk_t full;
189 u_char *pos;
190 };
191
192 METHOD(enumerator_t, keyid_enumerate, bool,
193 keyid_enumerator_t *this, chunk_t *chunk)
194 {
195 if (this->pos == NULL)
196 {
197 this->pos = this->full.ptr;
198 }
199 else
200 {
201 this->pos += HASH_SIZE_SHA1;
202 if (this->pos > (this->full.ptr + this->full.len - HASH_SIZE_SHA1))
203 {
204 this->pos = NULL;
205 }
206 }
207 if (this->pos)
208 {
209 chunk->ptr = this->pos;
210 chunk->len = HASH_SIZE_SHA1;
211 return TRUE;
212 }
213 return FALSE;
214 }
215
216 METHOD(certreq_payload_t, create_keyid_enumerator, enumerator_t*,
217 private_certreq_payload_t *this)
218 {
219 keyid_enumerator_t *enumerator;
220
221 if (this->type == PLV1_CERTREQ)
222 {
223 return enumerator_create_empty();
224 }
225 INIT(enumerator,
226 .public = {
227 .enumerate = (void*)_keyid_enumerate,
228 .destroy = (void*)free,
229 },
230 .full = this->data,
231 );
232 return &enumerator->public;
233 }
234
235 METHOD(certreq_payload_t, get_cert_type, certificate_type_t,
236 private_certreq_payload_t *this)
237 {
238 switch (this->encoding)
239 {
240 case ENC_X509_SIGNATURE:
241 return CERT_X509;
242 default:
243 return CERT_ANY;
244 }
245 }
246
247 METHOD2(payload_t, certreq_payload_t, destroy, void,
248 private_certreq_payload_t *this)
249 {
250 chunk_free(&this->data);
251 free(this);
252 }
253
254 /*
255 * Described in header
256 */
257 certreq_payload_t *certreq_payload_create(payload_type_t type)
258 {
259 private_certreq_payload_t *this;
260
261 INIT(this,
262 .public = {
263 .payload_interface = {
264 .verify = _verify,
265 .get_encoding_rules = _get_encoding_rules,
266 .get_header_length = _get_header_length,
267 .get_length = _get_length,
268 .get_next_type = _get_next_type,
269 .set_next_type = _set_next_type,
270 .get_type = _get_type,
271 .destroy = _destroy,
272 },
273 .create_keyid_enumerator = _create_keyid_enumerator,
274 .get_cert_type = _get_cert_type,
275 .add_keyid = _add_keyid,
276 .destroy = _destroy,
277 .get_dn = _get_dn,
278 },
279 .next_payload = PL_NONE,
280 .payload_length = get_header_length(this),
281 .type = type,
282 );
283 return &this->public;
284 }
285
286 /*
287 * Described in header
288 */
289 certreq_payload_t *certreq_payload_create_type(certificate_type_t type)
290 {
291 private_certreq_payload_t *this;
292
293 this = (private_certreq_payload_t*)
294 certreq_payload_create(PLV2_CERTREQ);
295 switch (type)
296 {
297 case CERT_X509:
298 this->encoding = ENC_X509_SIGNATURE;
299 break;
300 default:
301 DBG1(DBG_ENC, "certificate type %N not supported in requests",
302 certificate_type_names, type);
303 free(this);
304 return NULL;
305 }
306 return &this->public;
307 }
308
309 /*
310 * Described in header
311 */
312 certreq_payload_t *certreq_payload_create_dn(identification_t *id)
313 {
314 private_certreq_payload_t *this;
315
316 this = (private_certreq_payload_t*)
317 certreq_payload_create(PLV1_CERTREQ);
318
319 this->encoding = ENC_X509_SIGNATURE;
320 this->data = chunk_clone(id->get_encoding(id));
321 this->payload_length = get_header_length(this) + this->data.len;
322
323 return &this->public;
324 }