814ec272677e6c270658644bb998d9e6819bbaad
[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 /**
92 * Encoding rules to parse or generate a CERT payload
93 *
94 * The defined offsets are the positions in a object of type
95 * private_cert_payload_t.
96 *
97 */
98 encoding_rule_t cert_payload_encodings[] = {
99 /* 1 Byte next payload type, stored in the field next_payload */
100 { U_INT_8, offsetof(private_cert_payload_t, next_payload) },
101 /* the critical bit */
102 { FLAG, offsetof(private_cert_payload_t, critical) },
103 /* 7 Bit reserved bits, nowhere stored */
104 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[0]) },
105 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[1]) },
106 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[2]) },
107 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[3]) },
108 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[4]) },
109 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[5]) },
110 { RESERVED_BIT, offsetof(private_cert_payload_t, reserved[6]) },
111 /* Length of the whole payload*/
112 { PAYLOAD_LENGTH, offsetof(private_cert_payload_t, payload_length)},
113 /* 1 Byte CERT type*/
114 { U_INT_8, offsetof(private_cert_payload_t, encoding) },
115 /* some cert data bytes, length is defined in PAYLOAD_LENGTH */
116 { CERT_DATA, offsetof(private_cert_payload_t, data) }
117 };
118
119 /*
120 1 2 3
121 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
122 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 ! Next Payload !C! RESERVED ! Payload Length !
124 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 ! Cert Encoding ! !
126 +-+-+-+-+-+-+-+-+ !
127 ~ Certificate Data ~
128 ! !
129 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 */
131
132 METHOD(payload_t, verify, status_t,
133 private_cert_payload_t *this)
134 {
135 if (this->encoding == ENC_X509_HASH_AND_URL ||
136 this->encoding == ENC_X509_HASH_AND_URL_BUNDLE)
137 {
138 int i;
139
140 /* coarse verification of "Hash and URL" encoded certificates */
141 if (this->data.len <= 20)
142 {
143 DBG1(DBG_ENC, "invalid payload length for hash-and-url (%d), ignore",
144 this->data.len);
145 this->invalid_hash_and_url = TRUE;
146 return SUCCESS;
147 }
148 for (i = 20; i < this->data.len; ++i)
149 {
150 if (this->data.ptr[i] == '\0')
151 {
152 /* null terminated, fine */
153 return SUCCESS;
154 }
155 else if (!isprint(this->data.ptr[i]))
156 {
157 DBG1(DBG_ENC, "non printable characters in url of hash-and-url"
158 " encoded certificate payload, ignore");
159 this->invalid_hash_and_url = TRUE;
160 return SUCCESS;
161 }
162 }
163 /* URL is not null terminated, correct that */
164 this->data = chunk_cat("mc", this->data, chunk_from_chars(0));
165 }
166 return SUCCESS;
167 }
168
169 METHOD(payload_t, get_encoding_rules, void,
170 private_cert_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
171 {
172 *rules = cert_payload_encodings;
173 *rule_count = countof(cert_payload_encodings);
174 }
175
176 METHOD(payload_t, get_type, payload_type_t,
177 private_cert_payload_t *this)
178 {
179 return CERTIFICATE;
180 }
181
182 METHOD(payload_t, get_next_type, payload_type_t,
183 private_cert_payload_t *this)
184 {
185 return this->next_payload;
186 }
187
188 METHOD(payload_t, set_next_type, void,
189 private_cert_payload_t *this, payload_type_t type)
190 {
191 this->next_payload = type;
192 }
193
194 METHOD(payload_t, get_length, size_t,
195 private_cert_payload_t *this)
196 {
197 return this->payload_length;
198 }
199
200 METHOD(cert_payload_t, get_cert_encoding, cert_encoding_t,
201 private_cert_payload_t *this)
202 {
203 return this->encoding;
204 }
205
206 METHOD(cert_payload_t, get_cert, certificate_t*,
207 private_cert_payload_t *this)
208 {
209 if (this->encoding != ENC_X509_SIGNATURE)
210 {
211 return NULL;
212 }
213 return lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
214 BUILD_BLOB_ASN1_DER, this->data,
215 BUILD_END);
216 }
217
218 METHOD(cert_payload_t, get_hash, chunk_t,
219 private_cert_payload_t *this)
220 {
221 chunk_t hash = chunk_empty;
222
223 if ((this->encoding != ENC_X509_HASH_AND_URL &&
224 this->encoding != ENC_X509_HASH_AND_URL_BUNDLE) ||
225 this->invalid_hash_and_url)
226 {
227 return hash;
228 }
229 hash.ptr = this->data.ptr;
230 hash.len = 20;
231 return hash;
232 }
233
234 METHOD(cert_payload_t, get_url, char*,
235 private_cert_payload_t *this)
236 {
237 if ((this->encoding != ENC_X509_HASH_AND_URL &&
238 this->encoding != ENC_X509_HASH_AND_URL_BUNDLE) ||
239 this->invalid_hash_and_url)
240 {
241 return NULL;
242 }
243 return (char*)this->data.ptr + 20;
244 }
245
246 METHOD2(payload_t, cert_payload_t, destroy, void,
247 private_cert_payload_t *this)
248 {
249 free(this->data.ptr);
250 free(this);
251 }
252
253 /*
254 * Described in header
255 */
256 cert_payload_t *cert_payload_create()
257 {
258 private_cert_payload_t *this;
259
260 INIT(this,
261 .public = {
262 .payload_interface = {
263 .verify = _verify,
264 .get_encoding_rules = _get_encoding_rules,
265 .get_length = _get_length,
266 .get_next_type = _get_next_type,
267 .set_next_type = _set_next_type,
268 .get_type = _get_type,
269 .destroy = _destroy,
270 },
271 .get_cert = _get_cert,
272 .get_cert_encoding = _get_cert_encoding,
273 .get_hash = _get_hash,
274 .get_url = _get_url,
275 .destroy = _destroy,
276 },
277 .next_payload = NO_PAYLOAD,
278 .payload_length = CERT_PAYLOAD_HEADER_LENGTH,
279 );
280 return &this->public;
281 }
282
283 /*
284 * Described in header
285 */
286 cert_payload_t *cert_payload_create_from_cert(certificate_t *cert)
287 {
288 private_cert_payload_t *this = (private_cert_payload_t*)cert_payload_create();
289
290 switch (cert->get_type(cert))
291 {
292 case CERT_X509:
293 this->encoding = ENC_X509_SIGNATURE;
294 break;
295 default:
296 DBG1(DBG_ENC, "embedding %N certificate in payload failed",
297 certificate_type_names, cert->get_type(cert));
298 free(this);
299 return NULL;
300 }
301 if (!cert->get_encoding(cert, CERT_ASN1_DER, &this->data))
302 {
303 DBG1(DBG_ENC, "encoding certificate for cert payload failed");
304 free(this);
305 return NULL;
306 }
307 this->payload_length = CERT_PAYLOAD_HEADER_LENGTH + this->data.len;
308 return &this->public;
309 }
310
311 /*
312 * Described in header
313 */
314 cert_payload_t *cert_payload_create_from_hash_and_url(chunk_t hash, char *url)
315 {
316 private_cert_payload_t *this = (private_cert_payload_t*)cert_payload_create();
317
318 this->encoding = ENC_X509_HASH_AND_URL;
319 this->data = chunk_cat("cc", hash, chunk_create(url, strlen(url)));
320 this->payload_length = CERT_PAYLOAD_HEADER_LENGTH + this->data.len;
321 return &this->public;
322 }
323
324 /*
325 * Described in header
326 */
327 cert_payload_t *cert_payload_create_custom(cert_encoding_t type, chunk_t data)
328 {
329 private_cert_payload_t *this = (private_cert_payload_t*)cert_payload_create();
330
331 this->encoding = type;
332 this->data = data;
333 this->payload_length = CERT_PAYLOAD_HEADER_LENGTH + this->data.len;
334 return &this->public;
335 }