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