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