improved logging on verify errors for some payloads
[strongswan.git] / src / charon / encoding / payloads / proposal_substructure.c
1 /**
2 * @file proposal_substructure.h
3 *
4 * @brief Implementation of proposal_substructure_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stddef.h>
25
26 #include "proposal_substructure.h"
27
28 #include <encoding/payloads/encodings.h>
29 #include <encoding/payloads/transform_substructure.h>
30 #include <types.h>
31 #include <utils/linked_list.h>
32 #include <utils/logger_manager.h>
33
34
35 /**
36 * IKEv1 Value for a proposal payload.
37 */
38 #define PROPOSAL_TYPE_VALUE 2
39
40
41 typedef struct private_proposal_substructure_t private_proposal_substructure_t;
42
43 /**
44 * Private data of an proposal_substructure_t object.
45 *
46 */
47 struct private_proposal_substructure_t {
48 /**
49 * Public proposal_substructure_t interface.
50 */
51 proposal_substructure_t public;
52
53 /**
54 * Next payload type.
55 */
56 u_int8_t next_payload;
57
58 /**
59 * Length of this payload.
60 */
61 u_int16_t proposal_length;
62
63 /**
64 * Proposal number.
65 */
66 u_int8_t proposal_number;
67
68 /**
69 * Protocol ID.
70 */
71 u_int8_t protocol_id;
72
73 /**
74 * SPI size of the following SPI.
75 */
76 u_int8_t spi_size;
77
78 /**
79 * Number of transforms.
80 */
81 u_int8_t transforms_count;
82
83 /**
84 * SPI is stored as chunk.
85 */
86 chunk_t spi;
87
88 /**
89 * Transforms are stored in a linked_list_t.
90 */
91 linked_list_t * transforms;
92
93 /**
94 * assigned logger
95 */
96 logger_t *logger;
97
98 /**
99 * @brief Computes the length of this substructure.
100 *
101 * @param this calling private_proposal_substructure_t object
102 */
103 void (*compute_length) (private_proposal_substructure_t *this);
104 };
105
106 /**
107 * Encoding rules to parse or generate a Proposal substructure.
108 *
109 * The defined offsets are the positions in a object of type
110 * private_proposal_substructure_t.
111 *
112 */
113 encoding_rule_t proposal_substructure_encodings[] = {
114 /* 1 Byte next payload type, stored in the field next_payload */
115 { U_INT_8, offsetof(private_proposal_substructure_t, next_payload) },
116 /* Reserved Byte is skipped */
117 { RESERVED_BYTE, 0 },
118 /* Length of the whole proposal substructure payload*/
119 { PAYLOAD_LENGTH, offsetof(private_proposal_substructure_t, proposal_length) },
120 /* proposal number is a number of 8 bit */
121 { U_INT_8, offsetof(private_proposal_substructure_t, proposal_number) },
122 /* protocol ID is a number of 8 bit */
123 { U_INT_8, offsetof(private_proposal_substructure_t, protocol_id) },
124 /* SPI Size has its own type */
125 { SPI_SIZE, offsetof(private_proposal_substructure_t, spi_size) },
126 /* Number of transforms is a number of 8 bit */
127 { U_INT_8, offsetof(private_proposal_substructure_t, transforms_count) },
128 /* SPI is a chunk of variable size*/
129 { SPI, offsetof(private_proposal_substructure_t, spi) },
130 /* Transforms are stored in a transform substructure,
131 offset points to a linked_list_t pointer */
132 { TRANSFORMS, offsetof(private_proposal_substructure_t, transforms) }
133 };
134
135 /*
136 1 2 3
137 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
138 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 ! 0 (last) or 2 ! RESERVED ! Proposal Length !
140 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 ! Proposal # ! Protocol ID ! SPI Size !# of Transforms!
142 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 ~ SPI (variable) ~
144 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 ! !
146 ~ <Transforms> ~
147 ! !
148 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 */
150
151 /**
152 * Implementation of payload_t.verify.
153 */
154 static status_t verify(private_proposal_substructure_t *this)
155 {
156 status_t status = SUCCESS;
157 iterator_t *iterator;
158
159 if ((this->next_payload != NO_PAYLOAD) && (this->next_payload != 2))
160 {
161 /* must be 0 or 2 */
162 this->logger->log(this->logger, ERROR, "inconsistent next payload");
163 return FAILED;
164 }
165 if (this->transforms_count != this->transforms->get_count(this->transforms))
166 {
167 /* must be the same! */
168 this->logger->log(this->logger, ERROR, "transform count invalid");
169 return FAILED;
170 }
171
172 if ((this->protocol_id == 0) || (this->protocol_id >= 4))
173 {
174 /* reserved are not supported */
175 this->logger->log(this->logger, ERROR, "invalid protocol");
176 return FAILED;
177 }
178
179 iterator = this->transforms->create_iterator(this->transforms,TRUE);
180
181 while(iterator->has_next(iterator))
182 {
183 payload_t *current_transform;
184 iterator->current(iterator,(void **)&current_transform);
185
186 status = current_transform->verify(current_transform);
187 if (status != SUCCESS)
188 {
189 this->logger->log(this->logger, ERROR, "TRANSFORM_SUBSTRUCTURE verification failed");
190 break;
191 }
192 }
193
194 iterator->destroy(iterator);
195
196
197 /* proposal number is checked in SA payload */
198 return status;
199 }
200
201 /**
202 * Implementation of payload_t.get_encoding_rules.
203 */
204 static void get_encoding_rules(private_proposal_substructure_t *this, encoding_rule_t **rules, size_t *rule_count)
205 {
206 *rules = proposal_substructure_encodings;
207 *rule_count = sizeof(proposal_substructure_encodings) / sizeof(encoding_rule_t);
208 }
209
210 /**
211 * Implementation of payload_t.get_type.
212 */
213 static payload_type_t get_type(private_proposal_substructure_t *this)
214 {
215 return PROPOSAL_SUBSTRUCTURE;
216 }
217
218 /**
219 * Implementation of payload_t.get_next_type.
220 */
221 static payload_type_t get_next_type(private_proposal_substructure_t *this)
222 {
223 return (this->next_payload);
224 }
225
226 /**
227 * Implementation of payload_t.set_next_type.
228 */
229 static void set_next_type(private_proposal_substructure_t *this,payload_type_t type)
230 {
231 }
232
233 /**
234 * Implementation of payload_t.get_length.
235 */
236 static size_t get_length(private_proposal_substructure_t *this)
237 {
238 this->compute_length(this);
239 return this->proposal_length;
240 }
241
242 /**
243 * Implementation of proposal_substructure_t.create_transform_substructure_iterator.
244 */
245 static iterator_t *create_transform_substructure_iterator (private_proposal_substructure_t *this,bool forward)
246 {
247 return (this->transforms->create_iterator(this->transforms,forward));
248 }
249
250 /**
251 * Implementation of proposal_substructure_t.add_transform_substructure.
252 */
253 static void add_transform_substructure (private_proposal_substructure_t *this,transform_substructure_t *transform)
254 {
255 status_t status;
256 if (this->transforms->get_count(this->transforms) > 0)
257 {
258 transform_substructure_t *last_transform;
259 status = this->transforms->get_last(this->transforms,(void **) &last_transform);
260 /* last transform is now not anymore last one */
261 last_transform->set_is_last_transform(last_transform,FALSE);
262
263 }
264 transform->set_is_last_transform(transform,TRUE);
265
266 this->transforms->insert_last(this->transforms,(void *) transform);
267 this->compute_length(this);
268 }
269
270 /**
271 * Implementation of proposal_substructure_t.proposal_substructure_t.
272 */
273 static void set_is_last_proposal (private_proposal_substructure_t *this, bool is_last)
274 {
275 this->next_payload = (is_last) ? 0: PROPOSAL_TYPE_VALUE;
276 }
277
278
279 /**
280 * Implementation of proposal_substructure_t.set_proposal_number.
281 */
282 static void set_proposal_number(private_proposal_substructure_t *this,u_int8_t proposal_number)
283 {
284 this->proposal_number = proposal_number;
285 }
286
287 /**
288 * Implementation of proposal_substructure_t.get_proposal_number.
289 */
290 static u_int8_t get_proposal_number (private_proposal_substructure_t *this)
291 {
292 return (this->proposal_number);
293 }
294
295 /**
296 * Implementation of proposal_substructure_t.set_protocol_id.
297 */
298 static void set_protocol_id(private_proposal_substructure_t *this,u_int8_t protocol_id)
299 {
300 this->protocol_id = protocol_id;
301 }
302
303 /**
304 * Implementation of proposal_substructure_t.get_protocol_id.
305 */
306 static u_int8_t get_protocol_id (private_proposal_substructure_t *this)
307 {
308 return (this->protocol_id);
309 }
310
311 /**
312 * Implementation of proposal_substructure_t.set_spi.
313 */
314 static void set_spi (private_proposal_substructure_t *this, chunk_t spi)
315 {
316 /* first delete already set spi value */
317 if (this->spi.ptr != NULL)
318 {
319 free(this->spi.ptr);
320 this->spi.ptr = NULL;
321 this->spi.len = 0;
322 this->compute_length(this);
323 }
324
325 this->spi.ptr = clalloc(spi.ptr,spi.len);
326 this->spi.len = spi.len;
327 this->spi_size = spi.len;
328 this->compute_length(this);
329 }
330
331 /**
332 * Implementation of proposal_substructure_t.get_spi.
333 */
334 static chunk_t get_spi (private_proposal_substructure_t *this)
335 {
336 chunk_t spi;
337 spi.ptr = this->spi.ptr;
338 spi.len = this->spi.len;
339
340 return spi;
341 }
342
343 /**
344 * Implementation of proposal_substructure_t.get_info_for_transform_type.
345 */
346 static status_t get_info_for_transform_type (private_proposal_substructure_t *this,transform_type_t type, u_int16_t *transform_id, u_int16_t *key_length)
347 {
348 iterator_t *iterator;
349 status_t status;
350 u_int16_t found_transform_id;
351 u_int16_t found_key_length;
352
353 iterator = this->transforms->create_iterator(this->transforms,TRUE);
354
355 while (iterator->has_next(iterator))
356 {
357 transform_substructure_t *current_transform;
358 status = iterator->current(iterator,(void **) &current_transform);
359 if (status != SUCCESS)
360 {
361 break;
362 }
363 if (current_transform->get_transform_type(current_transform) == type)
364 {
365 /* now get data for specific type */
366 found_transform_id = current_transform->get_transform_id(current_transform);
367 status = current_transform->get_key_length(current_transform,&found_key_length);
368 *transform_id = found_transform_id;
369 *key_length = found_key_length;
370 iterator->destroy(iterator);
371 return status;
372 }
373 }
374 iterator->destroy(iterator);
375 return NOT_FOUND;
376 }
377
378 /**
379 * Implementation of private_proposal_substructure_t.compute_length.
380 */
381 static void compute_length (private_proposal_substructure_t *this)
382 {
383 iterator_t *iterator;
384 size_t transforms_count = 0;
385 size_t length = PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH;
386 iterator = this->transforms->create_iterator(this->transforms,TRUE);
387 while (iterator->has_next(iterator))
388 {
389 payload_t * current_transform;
390 iterator->current(iterator,(void **) &current_transform);
391 length += current_transform->get_length(current_transform);
392 transforms_count++;
393 }
394 iterator->destroy(iterator);
395
396 length += this->spi.len;
397 this->transforms_count = transforms_count;
398 this->proposal_length = length;
399 }
400
401 /**
402 * Implementation of proposal_substructure_t.get_transform_count.
403 */
404 static size_t get_transform_count (private_proposal_substructure_t *this)
405 {
406 return this->transforms->get_count(this->transforms);
407 }
408
409 /**
410 * Implementation of proposal_substructure_t.get_spi_size.
411 */
412 static size_t get_spi_size (private_proposal_substructure_t *this)
413 {
414 return this->spi.len;
415 }
416
417 /**
418 * Implementation of proposal_substructure_t.get_proposal.
419 */
420 proposal_t* get_proposal(private_proposal_substructure_t *this)
421 {
422 iterator_t *iterator;
423 proposal_t *proposal;
424 u_int64_t spi;
425
426 proposal = proposal_create(this->protocol_id);
427
428 iterator = this->transforms->create_iterator(this->transforms, TRUE);
429 while (iterator->has_next(iterator))
430 {
431 transform_substructure_t *transform;
432 transform_type_t transform_type;
433 u_int16_t transform_id;
434 u_int16_t key_length = 0;
435
436 iterator->current(iterator, (void**)&transform);
437
438 transform_type = transform->get_transform_type(transform);
439 transform_id = transform->get_transform_id(transform);
440 transform->get_key_length(transform, &key_length);
441
442 proposal->add_algorithm(proposal, transform_type, transform_id, key_length);
443 }
444 iterator->destroy(iterator);
445
446 switch (this->spi.len)
447 {
448 case 4:
449 spi = *((u_int32_t*)this->spi.ptr);
450 break;
451 case 8:
452 spi = *((u_int64_t*)this->spi.ptr);
453 break;
454 default:
455 spi = 0;
456 }
457 proposal->set_spi(proposal, spi);
458
459 return proposal;
460 }
461
462 /**
463 * Implementation of proposal_substructure_t.clone.
464 */
465 static private_proposal_substructure_t* clone_(private_proposal_substructure_t *this)
466 {
467 private_proposal_substructure_t * new_clone;
468 iterator_t *transforms;
469
470 new_clone = (private_proposal_substructure_t *) proposal_substructure_create();
471
472 new_clone->next_payload = this->next_payload;
473 new_clone->proposal_number = this->proposal_number;
474 new_clone->protocol_id = this->protocol_id;
475 new_clone->spi_size = this->spi_size;
476 if (this->spi.ptr != NULL)
477 {
478 new_clone->spi.ptr = clalloc(this->spi.ptr,this->spi.len);
479 new_clone->spi.len = this->spi.len;
480 }
481
482 transforms = this->transforms->create_iterator(this->transforms,FALSE);
483
484 while (transforms->has_next(transforms))
485 {
486 transform_substructure_t *current_transform;
487 transform_substructure_t *current_transform_clone;
488
489 transforms->current(transforms,(void **) &current_transform);
490
491 current_transform_clone = current_transform->clone(current_transform);
492
493 new_clone->public.add_transform_substructure(&(new_clone->public),current_transform_clone);
494 }
495
496 transforms->destroy(transforms);
497
498 return new_clone;
499 }
500
501 /**
502 * Implements payload_t's and proposal_substructure_t's destroy function.
503 * See #payload_s.destroy or proposal_substructure_s.destroy for description.
504 */
505 static status_t destroy(private_proposal_substructure_t *this)
506 {
507 /* all proposals are getting destroyed */
508 while (this->transforms->get_count(this->transforms) > 0)
509 {
510 transform_substructure_t *current_transform;
511 if (this->transforms->remove_last(this->transforms,(void **)&current_transform) != SUCCESS)
512 {
513 break;
514 }
515 current_transform->destroy(current_transform);
516 }
517 this->transforms->destroy(this->transforms);
518
519 if (this->spi.ptr != NULL)
520 {
521 free(this->spi.ptr);
522 }
523
524 free(this);
525
526 return SUCCESS;
527 }
528
529 /*
530 * Described in header.
531 */
532 proposal_substructure_t *proposal_substructure_create()
533 {
534 private_proposal_substructure_t *this = malloc_thing(private_proposal_substructure_t);
535
536 /* interface functions */
537 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
538 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
539 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
540 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
541 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
542 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
543 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
544
545
546 /* public functions */
547 this->public.create_transform_substructure_iterator = (iterator_t* (*) (proposal_substructure_t *,bool)) create_transform_substructure_iterator;
548 this->public.add_transform_substructure = (void (*) (proposal_substructure_t *,transform_substructure_t *)) add_transform_substructure;
549 this->public.set_proposal_number = (void (*) (proposal_substructure_t *,u_int8_t))set_proposal_number;
550 this->public.get_proposal_number = (u_int8_t (*) (proposal_substructure_t *)) get_proposal_number;
551 this->public.set_protocol_id = (void (*) (proposal_substructure_t *,u_int8_t))set_protocol_id;
552 this->public.get_protocol_id = (u_int8_t (*) (proposal_substructure_t *)) get_protocol_id;
553 this->public.get_info_for_transform_type = (status_t (*) (proposal_substructure_t *,transform_type_t,u_int16_t *, u_int16_t *))get_info_for_transform_type;
554 this->public.set_is_last_proposal = (void (*) (proposal_substructure_t *,bool)) set_is_last_proposal;
555 this->public.get_proposal = (proposal_t* (*) (proposal_substructure_t*))get_proposal;
556 this->public.set_spi = (void (*) (proposal_substructure_t *,chunk_t))set_spi;
557 this->public.get_spi = (chunk_t (*) (proposal_substructure_t *)) get_spi;
558 this->public.get_transform_count = (size_t (*) (proposal_substructure_t *)) get_transform_count;
559 this->public.get_spi_size = (size_t (*) (proposal_substructure_t *)) get_spi_size;
560 this->public.clone = (proposal_substructure_t * (*) (proposal_substructure_t *)) clone_;
561 this->public.destroy = (void (*) (proposal_substructure_t *)) destroy;
562
563 /* private functions */
564 this->compute_length = compute_length;
565
566 /* set default values of the fields */
567 this->next_payload = NO_PAYLOAD;
568 this->proposal_length = 0;
569 this->proposal_number = 0;
570 this->protocol_id = 0;
571 this->transforms_count = 0;
572 this->spi_size = 0;
573 this->spi.ptr = NULL;
574 this->spi.len = 0;
575 this->logger = logger_manager->get_logger(logger_manager, PAYLOAD);
576
577 this->transforms = linked_list_create();
578
579 return (&(this->public));
580 }
581
582 /*
583 * Described in header.
584 */
585 proposal_substructure_t *proposal_substructure_create_from_proposal(proposal_t *proposal)
586 {
587 private_proposal_substructure_t *this = (private_proposal_substructure_t*)proposal_substructure_create();
588 iterator_t *iterator;
589 algorithm_t *algo;
590 transform_substructure_t *transform;
591
592 /* encryption algorithm is only availble in ESP */
593 iterator = proposal->create_algorithm_iterator(proposal, ENCRYPTION_ALGORITHM);
594 while (iterator->has_next(iterator))
595 {
596 iterator->current(iterator, (void**)&algo);
597 transform = transform_substructure_create_type(ENCRYPTION_ALGORITHM, algo->algorithm, algo->key_size);
598 this->public.add_transform_substructure(&(this->public), transform);
599 }
600 iterator->destroy(iterator);
601
602 /* integrity algorithms */
603 iterator = proposal->create_algorithm_iterator(proposal, INTEGRITY_ALGORITHM);
604 while (iterator->has_next(iterator))
605 {
606 algorithm_t *algo;
607 iterator->current(iterator, (void**)&algo);
608 transform = transform_substructure_create_type(INTEGRITY_ALGORITHM, algo->algorithm, algo->key_size);
609 this->public.add_transform_substructure(&(this->public), transform);
610 }
611 iterator->destroy(iterator);
612
613 /* prf algorithms */
614 iterator = proposal->create_algorithm_iterator(proposal, PSEUDO_RANDOM_FUNCTION);
615 while (iterator->has_next(iterator))
616 {
617 algorithm_t *algo;
618 iterator->current(iterator, (void**)&algo);
619 transform = transform_substructure_create_type(PSEUDO_RANDOM_FUNCTION, algo->algorithm, algo->key_size);
620 this->public.add_transform_substructure(&(this->public), transform);
621 }
622 iterator->destroy(iterator);
623
624 /* dh groups */
625 iterator = proposal->create_algorithm_iterator(proposal, DIFFIE_HELLMAN_GROUP);
626 while (iterator->has_next(iterator))
627 {
628 algorithm_t *algo;
629 iterator->current(iterator, (void**)&algo);
630 transform = transform_substructure_create_type(DIFFIE_HELLMAN_GROUP, algo->algorithm, 0);
631 this->public.add_transform_substructure(&(this->public), transform);
632 }
633 iterator->destroy(iterator);
634
635 /* extended sequence numbers */
636 iterator = proposal->create_algorithm_iterator(proposal, EXTENDED_SEQUENCE_NUMBERS);
637 while (iterator->has_next(iterator))
638 {
639 algorithm_t *algo;
640 iterator->current(iterator, (void**)&algo);
641 transform = transform_substructure_create_type(EXTENDED_SEQUENCE_NUMBERS, algo->algorithm, 0);
642 this->public.add_transform_substructure(&(this->public), transform);
643 }
644 iterator->destroy(iterator);
645
646 /* take over general infos */
647 this->spi_size = proposal->get_protocol(proposal) == PROTO_IKE ? 0 : 4;
648 this->spi.len = this->spi_size;
649 if (this->spi_size == 4)
650 {
651 this->spi.ptr = malloc(this->spi_size);
652 *((u_int32_t*)this->spi.ptr) = proposal->get_spi(proposal);
653 }
654 this->proposal_number = 0;
655 this->protocol_id = proposal->get_protocol(proposal);
656
657 return &(this->public);
658 }