Removed strayed code fragment
[strongswan.git] / src / charon / encoding / payloads / notify_payload.c
1 /*
2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2006 Martin Willi
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
21 #include "notify_payload.h"
22
23 #include <daemon.h>
24 #include <encoding/payloads/encodings.h>
25 #include <crypto/hashers/hasher.h>
26
27 ENUM_BEGIN(notify_type_names, UNSUPPORTED_CRITICAL_PAYLOAD, UNSUPPORTED_CRITICAL_PAYLOAD,
28 "UNSUPPORTED_CRITICAL_PAYLOAD");
29 ENUM_NEXT(notify_type_names, INVALID_IKE_SPI, INVALID_MAJOR_VERSION, UNSUPPORTED_CRITICAL_PAYLOAD,
30 "INVALID_IKE_SPI",
31 "INVALID_MAJOR_VERSION");
32 ENUM_NEXT(notify_type_names, INVALID_SYNTAX, INVALID_SYNTAX, INVALID_MAJOR_VERSION,
33 "INVALID_SYNTAX");
34 ENUM_NEXT(notify_type_names, INVALID_MESSAGE_ID, INVALID_MESSAGE_ID, INVALID_SYNTAX,
35 "INVALID_MESSAGE_ID");
36 ENUM_NEXT(notify_type_names, INVALID_SPI, INVALID_SPI, INVALID_MESSAGE_ID,
37 "INVALID_SPI");
38 ENUM_NEXT(notify_type_names, NO_PROPOSAL_CHOSEN, NO_PROPOSAL_CHOSEN, INVALID_SPI,
39 "NO_PROPOSAL_CHOSEN");
40 ENUM_NEXT(notify_type_names, INVALID_KE_PAYLOAD, INVALID_KE_PAYLOAD, NO_PROPOSAL_CHOSEN,
41 "INVALID_KE_PAYLOAD");
42 ENUM_NEXT(notify_type_names, AUTHENTICATION_FAILED, AUTHENTICATION_FAILED, INVALID_KE_PAYLOAD,
43 "AUTHENTICATION_FAILED");
44 ENUM_NEXT(notify_type_names, SINGLE_PAIR_REQUIRED, USE_ASSIGNED_HoA, AUTHENTICATION_FAILED,
45 "SINGLE_PAIR_REQUIRED",
46 "NO_ADDITIONAL_SAS",
47 "INTERNAL_ADDRESS_FAILURE",
48 "FAILED_CP_REQUIRED",
49 "TS_UNACCEPTABLE",
50 "INVALID_SELECTORS",
51 "UNACCEPTABLE_ADDRESSES",
52 "UNEXPECTED_NAT_DETECTED",
53 "USE_ASSIGNED_HoA");
54 ENUM_NEXT(notify_type_names, ME_CONNECT_FAILED, ME_CONNECT_FAILED, USE_ASSIGNED_HoA,
55 "ME_CONNECT_FAILED");
56 ENUM_NEXT(notify_type_names, INITIAL_CONTACT, LINK_ID, ME_CONNECT_FAILED,
57 "INITIAL_CONTACT",
58 "SET_WINDOW_SIZE",
59 "ADDITIONAL_TS_POSSIBLE",
60 "IPCOMP_SUPPORTED",
61 "NAT_DETECTION_SOURCE_IP",
62 "NAT_DETECTION_DESTINATION_IP",
63 "COOKIE",
64 "USE_TRANSPORT_MODE",
65 "HTTP_CERT_LOOKUP_SUPPORTED",
66 "REKEY_SA",
67 "ESP_TFC_PADDING_NOT_SUPPORTED",
68 "NON_FIRST_FRAGMENTS_ALSO",
69 "MOBIKE_SUPPORTED",
70 "ADDITIONAL_IP4_ADDRESS",
71 "ADDITIONAL_IP6_ADDRESS",
72 "NO_ADDITIONAL_ADDRESSES",
73 "UPDATE_SA_ADDRESSES",
74 "COOKIE2",
75 "NO_NATS_ALLOWED",
76 "AUTH_LIFETIME",
77 "MULTIPLE_AUTH_SUPPORTED",
78 "ANOTHER_AUTH_FOLLOWS",
79 "REDIRECT_SUPPORTED",
80 "REDIRECT",
81 "REDIRECTED_FROM",
82 "TICKET_LT_OPAQUE",
83 "TICKET_REQUEST",
84 "TICKET_ACK",
85 "TICKET_NACK",
86 "TICKET_OPAQUE",
87 "LINK_ID");
88 ENUM_NEXT(notify_type_names, EAP_ONLY_AUTHENTICATION, EAP_ONLY_AUTHENTICATION, LINK_ID,
89 "EAP_ONLY_AUTHENTICATION");
90 ENUM_NEXT(notify_type_names, USE_BEET_MODE, USE_BEET_MODE, EAP_ONLY_AUTHENTICATION,
91 "USE_BEET_MODE");
92 ENUM_NEXT(notify_type_names, ME_MEDIATION, ME_RESPONSE, USE_BEET_MODE,
93 "ME_MEDIATION",
94 "ME_ENDPOINT",
95 "ME_CALLBACK",
96 "ME_CONNECTID",
97 "ME_CONNECTKEY",
98 "ME_CONNECTAUTH",
99 "ME_RESPONSE");
100 ENUM_END(notify_type_names, ME_RESPONSE);
101
102
103 ENUM_BEGIN(notify_type_short_names, UNSUPPORTED_CRITICAL_PAYLOAD, UNSUPPORTED_CRITICAL_PAYLOAD,
104 "CRIT");
105 ENUM_NEXT(notify_type_short_names, INVALID_IKE_SPI, INVALID_MAJOR_VERSION, UNSUPPORTED_CRITICAL_PAYLOAD,
106 "INVAL_IKE_SPI",
107 "INVAL_MAJOR");
108 ENUM_NEXT(notify_type_short_names, INVALID_SYNTAX, INVALID_SYNTAX, INVALID_MAJOR_VERSION,
109 "INVAL_SYN");
110 ENUM_NEXT(notify_type_short_names, INVALID_MESSAGE_ID, INVALID_MESSAGE_ID, INVALID_SYNTAX,
111 "INVAL_MID");
112 ENUM_NEXT(notify_type_short_names, INVALID_SPI, INVALID_SPI, INVALID_MESSAGE_ID,
113 "INVAL_SPI");
114 ENUM_NEXT(notify_type_short_names, NO_PROPOSAL_CHOSEN, NO_PROPOSAL_CHOSEN, INVALID_SPI,
115 "NO_PROP");
116 ENUM_NEXT(notify_type_short_names, INVALID_KE_PAYLOAD, INVALID_KE_PAYLOAD, NO_PROPOSAL_CHOSEN,
117 "INVAL_KE");
118 ENUM_NEXT(notify_type_short_names, AUTHENTICATION_FAILED, AUTHENTICATION_FAILED, INVALID_KE_PAYLOAD,
119 "AUTH_FAILED");
120 ENUM_NEXT(notify_type_short_names, SINGLE_PAIR_REQUIRED, USE_ASSIGNED_HoA, AUTHENTICATION_FAILED,
121 "SINGLE_PAIR",
122 "NO_ADD_SAS",
123 "INT_ADDR_FAIL",
124 "FAIL_CP_REQ",
125 "TS_UNACCEPT",
126 "INVAL_SEL",
127 "UNACCEPT_ADDR",
128 "UNEXPECT_NAT",
129 "ASSIGNED_HoA");
130 ENUM_NEXT(notify_type_short_names, ME_CONNECT_FAILED, ME_CONNECT_FAILED, USE_ASSIGNED_HoA,
131 "ME_CONN_FAIL");
132 ENUM_NEXT(notify_type_short_names, INITIAL_CONTACT, LINK_ID, ME_CONNECT_FAILED,
133 "INIT_CONTACT",
134 "SET_WINSIZE",
135 "ADD_TS_POSS",
136 "IPCOMP_SUPP",
137 "NATD_S_IP",
138 "NATD_D_IP",
139 "COOKIE",
140 "USE_TRANSP",
141 "HTTP_CERT_LOOK",
142 "REKEY_SA",
143 "ESP_TFC_PAD_N",
144 "NON_FIRST_FRAG",
145 "MOBIKE_SUP",
146 "ADD_4_ADDR",
147 "ADD_6_ADDR",
148 "NO_ADD_ADDR",
149 "UPD_SA_ADDR",
150 "COOKIE2",
151 "NO_NATS",
152 "AUTH_LFT",
153 "MULT_AUTH",
154 "AUTH_FOLLOWS",
155 "REDIR_SUP",
156 "REDIR",
157 "REDIR_FROM",
158 "TKT_LT_OPAK",
159 "TKT_REQ",
160 "TKT_ACK",
161 "TKT_NACK",
162 "TKT_OPAK",
163 "LINK_ID");
164 ENUM_NEXT(notify_type_short_names, EAP_ONLY_AUTHENTICATION, EAP_ONLY_AUTHENTICATION, LINK_ID,
165 "EAP_ONLY");
166 ENUM_NEXT(notify_type_short_names, USE_BEET_MODE, USE_BEET_MODE, EAP_ONLY_AUTHENTICATION,
167 "BEET_MODE");
168 ENUM_NEXT(notify_type_short_names, ME_MEDIATION, ME_RESPONSE, USE_BEET_MODE,
169 "ME_MED",
170 "ME_EP",
171 "ME_CB",
172 "ME_CID",
173 "ME_CKEY",
174 "ME_CAUTH",
175 "ME_R");
176 ENUM_END(notify_type_short_names, ME_RESPONSE);
177
178
179 typedef struct private_notify_payload_t private_notify_payload_t;
180
181 /**
182 * Private data of an notify_payload_t object.
183 *
184 */
185 struct private_notify_payload_t {
186 /**
187 * Public notify_payload_t interface.
188 */
189 notify_payload_t public;
190
191 /**
192 * Next payload type.
193 */
194 u_int8_t next_payload;
195
196 /**
197 * Critical flag.
198 */
199 bool critical;
200
201 /**
202 * Length of this payload.
203 */
204 u_int16_t payload_length;
205
206 /**
207 * Protocol id.
208 */
209 u_int8_t protocol_id;
210
211 /**
212 * Spi size.
213 */
214 u_int8_t spi_size;
215
216 /**
217 * Notify message type.
218 */
219 u_int16_t notify_type;
220
221 /**
222 * Security parameter index (spi).
223 */
224 chunk_t spi;
225
226 /**
227 * Notification data.
228 */
229 chunk_t notification_data;
230 };
231
232 /**
233 * Encoding rules to parse or generate a IKEv2-Notify Payload.
234 *
235 * The defined offsets are the positions in a object of type
236 * private_notify_payload_t.
237 *
238 */
239 encoding_rule_t notify_payload_encodings[] = {
240 /* 1 Byte next payload type, stored in the field next_payload */
241 { U_INT_8, offsetof(private_notify_payload_t, next_payload) },
242 /* the critical bit */
243 { FLAG, offsetof(private_notify_payload_t, critical) },
244 /* 7 Bit reserved bits, nowhere stored */
245 { RESERVED_BIT, 0 },
246 { RESERVED_BIT, 0 },
247 { RESERVED_BIT, 0 },
248 { RESERVED_BIT, 0 },
249 { RESERVED_BIT, 0 },
250 { RESERVED_BIT, 0 },
251 { RESERVED_BIT, 0 },
252 /* Length of the whole payload*/
253 { PAYLOAD_LENGTH, offsetof(private_notify_payload_t, payload_length) },
254 /* Protocol ID as 8 bit field*/
255 { U_INT_8, offsetof(private_notify_payload_t, protocol_id) },
256 /* SPI Size as 8 bit field*/
257 { SPI_SIZE, offsetof(private_notify_payload_t, spi_size) },
258 /* Notify message type as 16 bit field*/
259 { U_INT_16, offsetof(private_notify_payload_t, notify_type) },
260 /* SPI as variable length field*/
261 { SPI, offsetof(private_notify_payload_t, spi) },
262 /* Key Exchange Data is from variable size */
263 { NOTIFICATION_DATA, offsetof(private_notify_payload_t, notification_data) }
264 };
265
266 /*
267 1 2 3
268 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
269 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
270 ! Next Payload !C! RESERVED ! Payload Length !
271 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
272 ! Protocol ID ! SPI Size ! Notify Message Type !
273 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
274 ! !
275 ~ Security Parameter Index (SPI) ~
276 ! !
277 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
278 ! !
279 ~ Notification Data ~
280 ! !
281 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
282 */
283
284 /**
285 * Implementation of payload_t.verify.
286 */
287 static status_t verify(private_notify_payload_t *this)
288 {
289 bool bad_length = FALSE;
290
291 switch (this->protocol_id)
292 {
293 case PROTO_NONE:
294 case PROTO_IKE:
295 case PROTO_AH:
296 case PROTO_ESP:
297 break;
298 default:
299 DBG1(DBG_ENC, "Unknown protocol (%d)", this->protocol_id);
300 return FAILED;
301 }
302
303 switch (this->notify_type)
304 {
305 case INVALID_KE_PAYLOAD:
306 {
307 if (this->notification_data.len != 2)
308 {
309 bad_length = TRUE;
310 }
311 break;
312 }
313 case NAT_DETECTION_SOURCE_IP:
314 case NAT_DETECTION_DESTINATION_IP:
315 case ME_CONNECTAUTH:
316 {
317 if (this->notification_data.len != HASH_SIZE_SHA1)
318 {
319 bad_length = TRUE;
320 }
321 break;
322 }
323 case INVALID_SYNTAX:
324 case INVALID_MAJOR_VERSION:
325 case NO_PROPOSAL_CHOSEN:
326 {
327 if (this->notification_data.len != 0)
328 {
329 bad_length = TRUE;
330 }
331 break;
332 }
333 case ADDITIONAL_IP4_ADDRESS:
334 {
335 if (this->notification_data.len != 4)
336 {
337 bad_length = TRUE;
338 }
339 break;
340 }
341 case ADDITIONAL_IP6_ADDRESS:
342 {
343 if (this->notification_data.len != 16)
344 {
345 bad_length = TRUE;
346 }
347 break;
348 }
349 case AUTH_LIFETIME:
350 {
351 if (this->notification_data.len != 4)
352 {
353 bad_length = TRUE;
354 }
355 break;
356 }
357 case IPCOMP_SUPPORTED:
358 {
359 if (this->notification_data.len != 3)
360 {
361 bad_length = TRUE;
362 }
363 break;
364 }
365 case ME_ENDPOINT:
366 if (this->notification_data.len != 8 &&
367 this->notification_data.len != 12 &&
368 this->notification_data.len != 24)
369 {
370 bad_length = TRUE;
371 }
372 break;
373 case ME_CONNECTID:
374 if (this->notification_data.len < 4 ||
375 this->notification_data.len > 16)
376 {
377 bad_length = TRUE;
378 }
379 break;
380 case ME_CONNECTKEY:
381 if (this->notification_data.len < 16 ||
382 this->notification_data.len > 32)
383 {
384 bad_length = TRUE;
385 }
386 break;
387 default:
388 /* TODO: verify */
389 break;
390 }
391 if (bad_length)
392 {
393 DBG1(DBG_ENC, "invalid notify data length for %N (%d)",
394 notify_type_names, this->notify_type,
395 this->notification_data.len);
396 return FAILED;
397 }
398 return SUCCESS;
399 }
400
401 /**
402 * Implementation of payload_t.get_encoding_rules.
403 */
404 static void get_encoding_rules(private_notify_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
405 {
406 *rules = notify_payload_encodings;
407 *rule_count = sizeof(notify_payload_encodings) / sizeof(encoding_rule_t);
408 }
409
410 /**
411 * Implementation of payload_t.get_type.
412 */
413 static payload_type_t get_type(private_notify_payload_t *this)
414 {
415 return NOTIFY;
416 }
417
418 /**
419 * Implementation of payload_t.get_next_type.
420 */
421 static payload_type_t get_next_type(private_notify_payload_t *this)
422 {
423 return (this->next_payload);
424 }
425
426 /**
427 * Implementation of payload_t.set_next_type.
428 */
429 static void set_next_type(private_notify_payload_t *this,payload_type_t type)
430 {
431 this->next_payload = type;
432 }
433
434 /**
435 * recompute the payloads length.
436 */
437 static void compute_length (private_notify_payload_t *this)
438 {
439 size_t length = NOTIFY_PAYLOAD_HEADER_LENGTH;
440 if (this->notification_data.ptr != NULL)
441 {
442 length += this->notification_data.len;
443 }
444 if (this->spi.ptr != NULL)
445 {
446 length += this->spi.len;
447 }
448 this->payload_length = length;
449 }
450
451 /**
452 * Implementation of payload_t.get_length.
453 */
454 static size_t get_length(private_notify_payload_t *this)
455 {
456 compute_length(this);
457 return this->payload_length;
458 }
459
460 /**
461 * Implementation of notify_payload_t.get_protocol_id.
462 */
463 static u_int8_t get_protocol_id(private_notify_payload_t *this)
464 {
465 return this->protocol_id;
466 }
467
468 /**
469 * Implementation of notify_payload_t.set_protocol_id.
470 */
471 static void set_protocol_id(private_notify_payload_t *this, u_int8_t protocol_id)
472 {
473 this->protocol_id = protocol_id;
474 }
475
476 /**
477 * Implementation of notify_payload_t.get_notify_type.
478 */
479 static notify_type_t get_notify_type(private_notify_payload_t *this)
480 {
481 return this->notify_type;
482 }
483
484 /**
485 * Implementation of notify_payload_t.set_notify_type.
486 */
487 static void set_notify_type(private_notify_payload_t *this, u_int16_t notify_type)
488 {
489 this->notify_type = notify_type;
490 }
491
492 /**
493 * Implementation of notify_payload_t.get_spi.
494 */
495 static u_int32_t get_spi(private_notify_payload_t *this)
496 {
497 switch (this->protocol_id)
498 {
499 case PROTO_AH:
500 case PROTO_ESP:
501 if (this->spi.len == 4)
502 {
503 return *((u_int32_t*)this->spi.ptr);
504 }
505 default:
506 break;
507 }
508 return 0;
509 }
510
511 /**
512 * Implementation of notify_payload_t.set_spi.
513 */
514 static void set_spi(private_notify_payload_t *this, u_int32_t spi)
515 {
516 chunk_free(&this->spi);
517 switch (this->protocol_id)
518 {
519 case PROTO_AH:
520 case PROTO_ESP:
521 this->spi = chunk_alloc(4);
522 *((u_int32_t*)this->spi.ptr) = spi;
523 break;
524 default:
525 break;
526 }
527 this->spi_size = this->spi.len;
528 compute_length(this);
529 }
530
531 /**
532 * Implementation of notify_payload_t.get_notification_data.
533 */
534 static chunk_t get_notification_data(private_notify_payload_t *this)
535 {
536 return (this->notification_data);
537 }
538
539 /**
540 * Implementation of notify_payload_t.set_notification_data.
541 */
542 static status_t set_notification_data(private_notify_payload_t *this, chunk_t notification_data)
543 {
544 chunk_free(&this->notification_data);
545 if (notification_data.len > 0)
546 {
547 this->notification_data = chunk_clone(notification_data);
548 }
549 compute_length(this);
550 return SUCCESS;
551 }
552
553 /**
554 * Implementation of notify_payload_t.destroy and notify_payload_t.destroy.
555 */
556 static status_t destroy(private_notify_payload_t *this)
557 {
558 chunk_free(&this->notification_data);
559 chunk_free(&this->spi);
560 free(this);
561 return SUCCESS;
562 }
563
564 /*
565 * Described in header
566 */
567 notify_payload_t *notify_payload_create()
568 {
569 private_notify_payload_t *this = malloc_thing(private_notify_payload_t);
570
571 /* interface functions */
572 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
573 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
574 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
575 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
576 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
577 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
578 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
579
580 /* public functions */
581 this->public.get_protocol_id = (u_int8_t (*) (notify_payload_t *)) get_protocol_id;
582 this->public.set_protocol_id = (void (*) (notify_payload_t *,u_int8_t)) set_protocol_id;
583 this->public.get_notify_type = (notify_type_t (*) (notify_payload_t *)) get_notify_type;
584 this->public.set_notify_type = (void (*) (notify_payload_t *,notify_type_t)) set_notify_type;
585 this->public.get_spi = (u_int32_t (*) (notify_payload_t *)) get_spi;
586 this->public.set_spi = (void (*) (notify_payload_t *,u_int32_t)) set_spi;
587 this->public.get_notification_data = (chunk_t (*) (notify_payload_t *)) get_notification_data;
588 this->public.set_notification_data = (void (*) (notify_payload_t *,chunk_t)) set_notification_data;
589 this->public.destroy = (void (*) (notify_payload_t *)) destroy;
590
591 /* set default values of the fields */
592 this->critical = FALSE;
593 this->next_payload = NO_PAYLOAD;
594 this->payload_length = NOTIFY_PAYLOAD_HEADER_LENGTH;
595 this->protocol_id = 0;
596 this->notify_type = 0;
597 this->spi.ptr = NULL;
598 this->spi.len = 0;
599 this->spi_size = 0;
600 this->notification_data.ptr = NULL;
601 this->notification_data.len = 0;
602
603 return &this->public;
604 }
605
606 /*
607 * Described in header.
608 */
609 notify_payload_t *notify_payload_create_from_protocol_and_type(protocol_id_t protocol_id, notify_type_t notify_type)
610 {
611 notify_payload_t *notify = notify_payload_create();
612
613 notify->set_notify_type(notify,notify_type);
614 notify->set_protocol_id(notify,protocol_id);
615
616 return notify;
617 }