3a230b91ee2322c17ee86166ffddc5e4d7070df2
[strongswan.git] / src / libcharon / encoding / payloads / cert_payload.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2010 Martin Willi
4 * Copyright (C) 2010 revosec AG
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include <stddef.h>
20 #include <ctype.h>
21
22 #include <daemon.h>
23
24 #include "cert_payload.h"
25
26 ENUM(cert_encoding_names, ENC_PKCS7_WRAPPED_X509, ENC_OCSP_CONTENT,
27 "ENC_PKCS7_WRAPPED_X509",
28 "ENC_PGP",
29 "ENC_DNS_SIGNED_KEY",
30 "ENC_X509_SIGNATURE",
31 "ENC_X509_KEY_EXCHANGE",
32 "ENC_KERBEROS_TOKENS",
33 "ENC_CRL",
34 "ENC_ARL",
35 "ENC_SPKI",
36 "ENC_X509_ATTRIBUTE",
37 "ENC_RAW_RSA_KEY",
38 "ENC_X509_HASH_AND_URL",
39 "ENC_X509_HASH_AND_URL_BUNDLE",
40 "ENC_OCSP_CONTENT",
41 );
42
43 typedef struct private_cert_payload_t private_cert_payload_t;
44
45 /**
46 * Private data of an cert_payload_t object.
47 */
48 struct private_cert_payload_t {
49
50 /**
51 * Public cert_payload_t interface.
52 */
53 cert_payload_t public;
54
55 /**
56 * Next payload type.
57 */
58 u_int8_t next_payload;
59
60 /**
61 * Critical flag.
62 */
63 bool critical;
64
65 /**
66 * reserved bits
67 */
68 bool reserved[7];
69
70 /**
71 * Length of this payload.
72 */
73 u_int16_t payload_length;
74
75 /**
76 * Encoding of the CERT Data.
77 */
78 u_int8_t encoding;
79
80 /**
81 * The contained cert data value.
82 */
83 chunk_t data;
84
85 /**
86 * TRUE if the "Hash and URL" data is invalid
87 */
88 bool invalid_hash_and_url;
89
90 /**
91 * The payload type.
92 */
93 payload_type_t type;
94 };
95
96 /**
97 * Encoding rules to parse or generate a CERT payload
98 *
99 * The defined offsets are the positions in a object of type
100 * private_cert_payload_t.
101 *
102 */
103 static encoding_rule_t encodings[] = {
104 /* 1 Byte next payload type, stored in the field next_payload */
105 { U_INT_8, offsetof(private_cert_payload_t, next_payload) },
106 /* the critical bit */
107 { FLAG, offsetof(private_cert_payload_t, critical) },
108 /* 7 Bit reserved bits, nowhere stored */
109 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[0]) },
110 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[1]) },
111 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[2]) },
112 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[3]) },
113 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[4]) },
114 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[5]) },
115 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[6]) },
116 /* Length of the whole payload*/
117 { PAYLOAD_LENGTH, offsetof(private_cert_payload_t, payload_length)},
118 /* 1 Byte CERT type*/
119 { U_INT_8, offsetof(private_cert_payload_t, encoding) },
120 /* some cert data bytes, length is defined in PAYLOAD_LENGTH */
121 { CHUNK_DATA, offsetof(private_cert_payload_t, data) }
122 };
123
124 /*
125 1 2 3
126 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
127 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128 ! Next Payload !C! RESERVED ! Payload Length !
129 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 ! Cert Encoding ! !
131 +-+-+-+-+-+-+-+-+ !
132 ~ Certificate Data ~
133 ! !
134 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 */
136
137 METHOD(payload_t, verify, status_t,
138 private_cert_payload_t *this)
139 {
140 if (this->encoding == ENC_X509_HASH_AND_URL ||
141 this->encoding == ENC_X509_HASH_AND_URL_BUNDLE)
142 {
143 int i;
144
145 /* coarse verification of "Hash and URL" encoded certificates */
146 if (this->data.len <= 20)
147 {
148 DBG1(DBG_ENC, "invalid payload length for hash-and-url (%d), ignore",
149 this->data.len);
150 this->invalid_hash_and_url = TRUE;
151 return SUCCESS;
152 }
153 for (i = 20; i < this->data.len; ++i)
154 {
155 if (this->data.ptr[i] == '\0')
156 {
157 /* null terminated, fine */
158 return SUCCESS;
159 }
160 else if (!isprint(this->data.ptr[i]))
161 {
162 DBG1(DBG_ENC, "non printable characters in url of hash-and-url"
163 " encoded certificate payload, ignore");
164 this->invalid_hash_and_url = TRUE;
165 return SUCCESS;
166 }
167 }
168 /* URL is not null terminated, correct that */
169 this->data = chunk_cat("mc", this->data, chunk_from_chars(0));
170 }
171 return SUCCESS;
172 }
173
174 METHOD(payload_t, get_encoding_rules, int,
175 private_cert_payload_t *this, encoding_rule_t **rules)
176 {
177 *rules = encodings;
178 return countof(encodings);
179 }
180
181 METHOD(payload_t, get_header_length, int,
182 private_cert_payload_t *this)
183 {
184 return 5;
185 }
186
187 METHOD(payload_t, get_type, payload_type_t,
188 private_cert_payload_t *this)
189 {
190 return this->type;
191 }
192
193 METHOD(payload_t, get_next_type, payload_type_t,
194 private_cert_payload_t *this)
195 {
196 return this->next_payload;
197 }
198
199 METHOD(payload_t, set_next_type, void,
200 private_cert_payload_t *this, payload_type_t type)
201 {
202 this->next_payload = type;
203 }
204
205 METHOD(payload_t, get_length, size_t,
206 private_cert_payload_t *this)
207 {
208 return this->payload_length;
209 }
210
211 METHOD(cert_payload_t, get_cert_encoding, cert_encoding_t,
212 private_cert_payload_t *this)
213 {
214 return this->encoding;
215 }
216
217 METHOD(cert_payload_t, get_cert, certificate_t*,
218 private_cert_payload_t *this)
219 {
220 int type;
221
222 switch (this->encoding)
223 {
224 case ENC_X509_SIGNATURE:
225 type = CERT_X509;
226 break;
227 case ENC_CRL:
228 type = CERT_X509_CRL;
229 break;
230 default:
231 return NULL;
232 }
233 return lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
234 BUILD_BLOB_ASN1_DER, this->data, BUILD_END);
235 }
236
237 METHOD(cert_payload_t, get_hash, chunk_t,
238 private_cert_payload_t *this)
239 {
240 chunk_t hash = chunk_empty;
241
242 if ((this->encoding != ENC_X509_HASH_AND_URL &&
243 this->encoding != ENC_X509_HASH_AND_URL_BUNDLE) ||
244 this->invalid_hash_and_url)
245 {
246 return hash;
247 }
248 hash.ptr = this->data.ptr;
249 hash.len = 20;
250 return hash;
251 }
252
253 METHOD(cert_payload_t, get_url, char*,
254 private_cert_payload_t *this)
255 {
256 if ((this->encoding != ENC_X509_HASH_AND_URL &&
257 this->encoding != ENC_X509_HASH_AND_URL_BUNDLE) ||
258 this->invalid_hash_and_url)
259 {
260 return NULL;
261 }
262 return (char*)this->data.ptr + 20;
263 }
264
265 METHOD2(payload_t, cert_payload_t, destroy, void,
266 private_cert_payload_t *this)
267 {
268 free(this->data.ptr);
269 free(this);
270 }
271
272 /*
273 * Described in header
274 */
275 cert_payload_t *cert_payload_create(payload_type_t type)
276 {
277 private_cert_payload_t *this;
278
279 INIT(this,
280 .public = {
281 .payload_interface = {
282 .verify = _verify,
283 .get_encoding_rules = _get_encoding_rules,
284 .get_header_length = _get_header_length,
285 .get_length = _get_length,
286 .get_next_type = _get_next_type,
287 .set_next_type = _set_next_type,
288 .get_type = _get_type,
289 .destroy = _destroy,
290 },
291 .get_cert = _get_cert,
292 .get_cert_encoding = _get_cert_encoding,
293 .get_hash = _get_hash,
294 .get_url = _get_url,
295 .destroy = _destroy,
296 },
297 .next_payload = NO_PAYLOAD,
298 .payload_length = get_header_length(this),
299 .type = type,
300 );
301 return &this->public;
302 }
303
304 /*
305 * Described in header
306 */
307 cert_payload_t *cert_payload_create_from_cert(payload_type_t type,
308 certificate_t *cert)
309 {
310 private_cert_payload_t *this;
311
312 this = (private_cert_payload_t*)cert_payload_create(type);
313 switch (cert->get_type(cert))
314 {
315 case CERT_X509:
316 this->encoding = ENC_X509_SIGNATURE;
317 break;
318 default:
319 DBG1(DBG_ENC, "embedding %N certificate in payload failed",
320 certificate_type_names, cert->get_type(cert));
321 free(this);
322 return NULL;
323 }
324 if (!cert->get_encoding(cert, CERT_ASN1_DER, &this->data))
325 {
326 DBG1(DBG_ENC, "encoding certificate for cert payload failed");
327 free(this);
328 return NULL;
329 }
330 this->payload_length = get_header_length(this) + this->data.len;
331
332 return &this->public;
333 }
334
335 /*
336 * Described in header
337 */
338 cert_payload_t *cert_payload_create_from_hash_and_url(chunk_t hash, char *url)
339 {
340 private_cert_payload_t *this;
341
342 this = (private_cert_payload_t*)cert_payload_create(CERTIFICATE);
343 this->encoding = ENC_X509_HASH_AND_URL;
344 this->data = chunk_cat("cc", hash, chunk_create(url, strlen(url)));
345 this->payload_length = get_header_length(this) + this->data.len;
346
347 return &this->public;
348 }
349
350 /*
351 * Described in header
352 */
353 cert_payload_t *cert_payload_create_custom(payload_type_t type,
354 cert_encoding_t encoding, chunk_t data)
355 {
356 private_cert_payload_t *this;
357
358 this = (private_cert_payload_t*)cert_payload_create(type);
359 this->encoding = encoding;
360 this->data = data;
361 this->payload_length = get_header_length(this) + this->data.len;
362
363 return &this->public;
364 }
365