summaryrefslogtreecommitdiff
path: root/src/libcharon/encoding/message.c
blob: ee49a6686b417c46006684ba13e594672e461394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
/*
 * Copyright (C) 2006-2007 Tobias Brunner
 * Copyright (C) 2005-2009 Martin Willi
 * Copyright (C) 2006 Daniel Roethlisberger
 * Copyright (C) 2005 Jan Hutter
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <stdlib.h>
#include <string.h>

#include "message.h"

#include <library.h>
#include <daemon.h>
#include <sa/ike_sa_id.h>
#include <encoding/generator.h>
#include <encoding/parser.h>
#include <utils/linked_list.h>
#include <encoding/payloads/encodings.h>
#include <encoding/payloads/payload.h>
#include <encoding/payloads/encryption_payload.h>
#include <encoding/payloads/unknown_payload.h>
#include <encoding/payloads/cp_payload.h>

/**
 * Max number of notify payloads per IKEv2 Message
 */
#define MAX_NOTIFY_PAYLOADS 20

/**
 * Max number of delete payloads per IKEv2 Message
 */
#define MAX_DELETE_PAYLOADS 20


typedef struct payload_rule_t payload_rule_t;

/**
 * A payload rule defines the rules for a payload
 * in a specific message rule. It defines if and how
 * many times a payload must/can occur in a message
 * and if it must be encrypted.
 */
struct payload_rule_t {
	/**
	 * Payload type.
	 */
	 payload_type_t payload_type;

	 /**
	  * Minimal occurence of this payload.
	  */
	 size_t min_occurence;

	 /**
	  * Max occurence of this payload.
	  */
	 size_t max_occurence;

	 /**
	  * TRUE if payload must be encrypted
	  */
	 bool encrypted;

	 /**
	  * If this payload occurs, the message rule is
	  * fullfilled in any case. This applies e.g. to
	  * notify_payloads.
	  */
	 bool sufficient;
};

typedef struct payload_order_t payload_order_t;

/**
 * payload ordering structure allows us to reorder payloads according to RFC.
 */
struct payload_order_t {

	/**
	 * payload type
	 */
	payload_type_t type;

	/**
	 * notify type, if payload == NOTIFY
	 */
	notify_type_t notify;
};


typedef struct message_rule_t message_rule_t;

/**
 * A message rule defines the kind of a message,
 * if it has encrypted contents and a list
 * of payload ordering rules and payload parsing rules.
 */
struct message_rule_t {
	/**
	 * Type of message.
	 */
	exchange_type_t exchange_type;

	/**
	 * Is message a request or response.
	 */
	bool is_request;

	/**
	 * Message contains encrypted content.
	 */
	bool encrypted_content;

	/**
	 * Number of payload rules which will follow
	 */
	int payload_rule_count;

	/**
	 * Pointer to first payload rule
	 */
	payload_rule_t *payload_rules;

	/**
	 * Number of payload order rules
	 */
	int payload_order_count;

	/**
	 * payload ordering rules
	 */
	payload_order_t *payload_order;
};

/**
 * Message rule for IKE_SA_INIT from initiator.
 */
static payload_rule_t ike_sa_init_i_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	FALSE,	FALSE},
	{SECURITY_ASSOCIATION,			1,	1,						FALSE,	FALSE},
	{KEY_EXCHANGE,					1,	1,						FALSE,	FALSE},
	{NONCE,							1,	1,						FALSE,	FALSE},
	{VENDOR_ID,						0,	10,						FALSE,	FALSE},
};

/**
 * payload order for IKE_SA_INIT initiator
 */
static payload_order_t ike_sa_init_i_payload_order[] = {
/*	payload type					notify type */
	{NOTIFY,						COOKIE},
	{SECURITY_ASSOCIATION,			0},
	{KEY_EXCHANGE,					0},
	{NONCE,							0},
	{NOTIFY,						NAT_DETECTION_SOURCE_IP},
	{NOTIFY,						NAT_DETECTION_DESTINATION_IP},
	{NOTIFY,						0},
	{VENDOR_ID,						0},
};

/**
 * Message rule for IKE_SA_INIT from responder.
 */
static payload_rule_t ike_sa_init_r_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	FALSE,	TRUE},
	{SECURITY_ASSOCIATION,			1,	1,						FALSE,	FALSE},
	{KEY_EXCHANGE,					1,	1,						FALSE,	FALSE},
	{NONCE,							1,	1,						FALSE,	FALSE},
	{VENDOR_ID,						0,	10,						FALSE,	FALSE},
};

/**
 * payload order for IKE_SA_INIT responder
 */
static payload_order_t ike_sa_init_r_payload_order[] = {
/*	payload type					notify type */
	{SECURITY_ASSOCIATION,			0},
	{KEY_EXCHANGE,					0},
	{NONCE,							0},
	{NOTIFY,						NAT_DETECTION_SOURCE_IP},
	{NOTIFY,						NAT_DETECTION_DESTINATION_IP},
	{NOTIFY,						HTTP_CERT_LOOKUP_SUPPORTED},
	{CERTIFICATE_REQUEST,			0},
	{NOTIFY,						0},
	{VENDOR_ID,						0},
};

/**
 * Message rule for IKE_AUTH from initiator.
 */
static payload_rule_t ike_auth_i_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	TRUE,	FALSE},
	{EXTENSIBLE_AUTHENTICATION,		0,	1,						TRUE,	TRUE},
	{AUTHENTICATION,				0,	1,						TRUE,	TRUE},
	{ID_INITIATOR,					0,	1,						TRUE,	FALSE},
	{CERTIFICATE,					0,	4,						TRUE,	FALSE},
	{CERTIFICATE_REQUEST,			0,	1,						TRUE,	FALSE},
	{ID_RESPONDER,					0,	1,						TRUE,	FALSE},
#ifdef ME
	{SECURITY_ASSOCIATION,			0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_INITIATOR,	0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_RESPONDER,	0,	1,						TRUE,	FALSE},
#else
	{SECURITY_ASSOCIATION,			0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_INITIATOR,	0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_RESPONDER,	0,	1,						TRUE,	FALSE},
#endif /* ME */
	{CONFIGURATION,					0,	1,						TRUE,	FALSE},
	{VENDOR_ID,						0,	10,						TRUE,	FALSE},
};

/**
 * payload order for IKE_AUTH initiator
 */
static payload_order_t ike_auth_i_payload_order[] = {
/*	payload type					notify type */
	{ID_INITIATOR,					0},
	{CERTIFICATE,					0},
	{NOTIFY,						INITIAL_CONTACT},
	{NOTIFY,						HTTP_CERT_LOOKUP_SUPPORTED},
	{CERTIFICATE_REQUEST,			0},
	{ID_RESPONDER,					0},
	{AUTHENTICATION,				0},
	{EXTENSIBLE_AUTHENTICATION,		0},
	{CONFIGURATION,					0},
	{NOTIFY,						IPCOMP_SUPPORTED},
	{NOTIFY,						USE_TRANSPORT_MODE},
	{NOTIFY,						ESP_TFC_PADDING_NOT_SUPPORTED},
	{NOTIFY,						NON_FIRST_FRAGMENTS_ALSO},
	{SECURITY_ASSOCIATION,			0},
	{TRAFFIC_SELECTOR_INITIATOR,	0},
	{TRAFFIC_SELECTOR_RESPONDER,	0},
	{NOTIFY,						MOBIKE_SUPPORTED},
	{NOTIFY,						ADDITIONAL_IP4_ADDRESS},
	{NOTIFY,						ADDITIONAL_IP6_ADDRESS},
	{NOTIFY,						NO_ADDITIONAL_ADDRESSES},
	{NOTIFY,						0},
	{VENDOR_ID,						0},
};

/**
 * Message rule for IKE_AUTH from responder.
 */
static payload_rule_t ike_auth_r_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	TRUE,	TRUE},
	{EXTENSIBLE_AUTHENTICATION,		0,	1,						TRUE,	TRUE},
	{AUTHENTICATION,				0,	1,						TRUE,	TRUE},
	{CERTIFICATE,					0,	4,						TRUE,	FALSE},
	{ID_RESPONDER,					0,	1,						TRUE,	FALSE},
	{SECURITY_ASSOCIATION,			0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_INITIATOR,	0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_RESPONDER,	0,	1,						TRUE,	FALSE},
	{CONFIGURATION,					0,	1,						TRUE,	FALSE},
	{VENDOR_ID,						0,	10,						TRUE,	FALSE},
};

/**
 * payload order for IKE_AUTH responder
 */
static payload_order_t ike_auth_r_payload_order[] = {
/*	payload type					notify type */
	{ID_RESPONDER,					0},
	{CERTIFICATE,					0},
	{AUTHENTICATION,				0},
	{EXTENSIBLE_AUTHENTICATION,		0},
	{CONFIGURATION,					0},
	{NOTIFY,						IPCOMP_SUPPORTED},
	{NOTIFY,						USE_TRANSPORT_MODE},
	{NOTIFY,						ESP_TFC_PADDING_NOT_SUPPORTED},
	{NOTIFY,						NON_FIRST_FRAGMENTS_ALSO},
	{SECURITY_ASSOCIATION,			0},
	{TRAFFIC_SELECTOR_INITIATOR,	0},
	{TRAFFIC_SELECTOR_RESPONDER,	0},
	{NOTIFY,						AUTH_LIFETIME},
	{NOTIFY,						MOBIKE_SUPPORTED},
	{NOTIFY,						ADDITIONAL_IP4_ADDRESS},
	{NOTIFY,						ADDITIONAL_IP6_ADDRESS},
	{NOTIFY,						NO_ADDITIONAL_ADDRESSES},
	{NOTIFY,						0},
	{VENDOR_ID,						0},
};

/**
 * Message rule for INFORMATIONAL from initiator.
 */
static payload_rule_t informational_i_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	TRUE,	FALSE},
	{CONFIGURATION,					0,	1,						TRUE,	FALSE},
	{DELETE,						0,	MAX_DELETE_PAYLOADS,	TRUE,	FALSE},
	{VENDOR_ID,						0,	10,						TRUE,	FALSE},
};

/**
 * payload order for INFORMATIONAL initiator
 */
static payload_order_t informational_i_payload_order[] = {
/*	payload type					notify type */
	{NOTIFY,						UPDATE_SA_ADDRESSES},
	{NOTIFY,						NAT_DETECTION_SOURCE_IP},
	{NOTIFY,						NAT_DETECTION_DESTINATION_IP},
	{NOTIFY,						COOKIE2},
	{NOTIFY,						0},
	{DELETE,						0},
	{CONFIGURATION,					0},
};

/**
 * Message rule for INFORMATIONAL from responder.
 */
static payload_rule_t informational_r_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	TRUE,	FALSE},
	{CONFIGURATION,					0,	1,						TRUE,	FALSE},
	{DELETE,						0,	MAX_DELETE_PAYLOADS,	TRUE,	FALSE},
	{VENDOR_ID,						0,	10,						TRUE,	FALSE},
};

/**
 * payload order for INFORMATIONAL responder
 */
static payload_order_t informational_r_payload_order[] = {
/*	payload type					notify type */
	{NOTIFY,						UPDATE_SA_ADDRESSES},
	{NOTIFY,						NAT_DETECTION_SOURCE_IP},
	{NOTIFY,						NAT_DETECTION_DESTINATION_IP},
	{NOTIFY,						COOKIE2},
	{NOTIFY,						0},
	{DELETE,						0},
	{CONFIGURATION,					0},
};

/**
 * Message rule for CREATE_CHILD_SA from initiator.
 */
static payload_rule_t create_child_sa_i_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	TRUE,	FALSE},
	{SECURITY_ASSOCIATION,			1,	1,						TRUE,	FALSE},
	{NONCE,							1,	1,						TRUE,	FALSE},
	{KEY_EXCHANGE,					0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_INITIATOR,	0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_RESPONDER,	0,	1,						TRUE,	FALSE},
	{CONFIGURATION,					0,	1,						TRUE,	FALSE},
	{VENDOR_ID,						0,	10,						TRUE,	FALSE},
};

/**
 * payload order for CREATE_CHILD_SA from initiator.
 */
static payload_order_t create_child_sa_i_payload_order[] = {
/*	payload type					notify type */
	{NOTIFY,						REKEY_SA},
	{NOTIFY,						IPCOMP_SUPPORTED},
	{NOTIFY,						USE_TRANSPORT_MODE},
	{NOTIFY,						ESP_TFC_PADDING_NOT_SUPPORTED},
	{NOTIFY,						NON_FIRST_FRAGMENTS_ALSO},
	{SECURITY_ASSOCIATION,			0},
	{NONCE,							0},
	{KEY_EXCHANGE,					0},
	{TRAFFIC_SELECTOR_INITIATOR,	0},
	{TRAFFIC_SELECTOR_RESPONDER,	0},
	{NOTIFY,						0},
};

/**
 * Message rule for CREATE_CHILD_SA from responder.
 */
static payload_rule_t create_child_sa_r_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	TRUE,	TRUE},
	{SECURITY_ASSOCIATION,			1,	1,						TRUE,	FALSE},
	{NONCE,							1,	1,						TRUE,	FALSE},
	{KEY_EXCHANGE,					0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_INITIATOR,	0,	1,						TRUE,	FALSE},
	{TRAFFIC_SELECTOR_RESPONDER,	0,	1,						TRUE,	FALSE},
	{CONFIGURATION,					0,	1,						TRUE,	FALSE},
	{VENDOR_ID,						0,	10,						TRUE,	FALSE},
};

/**
 * payload order for CREATE_CHILD_SA from responder.
 */
static payload_order_t create_child_sa_r_payload_order[] = {
/*	payload type					notify type */
	{NOTIFY,						IPCOMP_SUPPORTED},
	{NOTIFY,						USE_TRANSPORT_MODE},
	{NOTIFY,						ESP_TFC_PADDING_NOT_SUPPORTED},
	{NOTIFY,						NON_FIRST_FRAGMENTS_ALSO},
	{SECURITY_ASSOCIATION,			0},
	{NONCE,							0},
	{KEY_EXCHANGE,					0},
	{TRAFFIC_SELECTOR_INITIATOR,	0},
	{TRAFFIC_SELECTOR_RESPONDER,	0},
	{NOTIFY,						ADDITIONAL_TS_POSSIBLE},
	{NOTIFY,						0},
};

#ifdef ME
/**
 * Message rule for ME_CONNECT from initiator.
 */
static payload_rule_t me_connect_i_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	TRUE,	TRUE},
	{ID_PEER,						1,	1,						TRUE,	FALSE},
	{VENDOR_ID,						0,	10,						TRUE,	FALSE}
};

/**
 * payload order for ME_CONNECT from initiator.
 */
static payload_order_t me_connect_i_payload_order[] = {
/*	payload type					notify type */
	{NOTIFY,						0},
	{ID_PEER,						0},
	{VENDOR_ID,						0},
};

/**
 * Message rule for ME_CONNECT from responder.
 */
static payload_rule_t me_connect_r_payload_rules[] = {
/*	payload type					min	max						encr	suff */
	{NOTIFY,						0,	MAX_NOTIFY_PAYLOADS,	TRUE,	TRUE},
	{VENDOR_ID,						0,	10,						TRUE,	FALSE}
};

/**
 * payload order for ME_CONNECT from responder.
 */
static payload_order_t me_connect_r_payload_order[] = {
/*	payload type					notify type */
	{NOTIFY,						0},
	{VENDOR_ID,						0},
};
#endif /* ME */

/**
 * Message rules, defines allowed payloads.
 */
static message_rule_t message_rules[] = {
	{IKE_SA_INIT,		TRUE,	FALSE,
		(sizeof(ike_sa_init_i_payload_rules)/sizeof(payload_rule_t)),
		ike_sa_init_i_payload_rules,
		(sizeof(ike_sa_init_i_payload_order)/sizeof(payload_order_t)),
		ike_sa_init_i_payload_order,
	},
	{IKE_SA_INIT,		FALSE,	FALSE,
		(sizeof(ike_sa_init_r_payload_rules)/sizeof(payload_rule_t)),
		ike_sa_init_r_payload_rules,
		(sizeof(ike_sa_init_r_payload_order)/sizeof(payload_order_t)),
		ike_sa_init_r_payload_order,
	},
	{IKE_AUTH,			TRUE,	TRUE,
		(sizeof(ike_auth_i_payload_rules)/sizeof(payload_rule_t)),
		ike_auth_i_payload_rules,
		(sizeof(ike_auth_i_payload_order)/sizeof(payload_order_t)),
		ike_auth_i_payload_order,
	},
	{IKE_AUTH,			FALSE,	TRUE,
		(sizeof(ike_auth_r_payload_rules)/sizeof(payload_rule_t)),
		ike_auth_r_payload_rules,
		(sizeof(ike_auth_r_payload_order)/sizeof(payload_order_t)),
		ike_auth_r_payload_order,
	},
	{INFORMATIONAL,		TRUE,	TRUE,
		(sizeof(informational_i_payload_rules)/sizeof(payload_rule_t)),
		informational_i_payload_rules,
		(sizeof(informational_i_payload_order)/sizeof(payload_order_t)),
		informational_i_payload_order,
	},
	{INFORMATIONAL,		FALSE,	TRUE,
		(sizeof(informational_r_payload_rules)/sizeof(payload_rule_t)),
		informational_r_payload_rules,
		(sizeof(informational_r_payload_order)/sizeof(payload_order_t)),
		informational_r_payload_order,
	},
	{CREATE_CHILD_SA,	TRUE,	TRUE,
		(sizeof(create_child_sa_i_payload_rules)/sizeof(payload_rule_t)),
		create_child_sa_i_payload_rules,
		(sizeof(create_child_sa_i_payload_order)/sizeof(payload_order_t)),
		create_child_sa_i_payload_order,
	},
	{CREATE_CHILD_SA,	FALSE,	TRUE,
		(sizeof(create_child_sa_r_payload_rules)/sizeof(payload_rule_t)),
		create_child_sa_r_payload_rules,
		(sizeof(create_child_sa_r_payload_order)/sizeof(payload_order_t)),
		create_child_sa_r_payload_order,
	},
#ifdef ME
	{ME_CONNECT,		TRUE,	TRUE,
		(sizeof(me_connect_i_payload_rules)/sizeof(payload_rule_t)),
		me_connect_i_payload_rules,
		(sizeof(me_connect_i_payload_order)/sizeof(payload_order_t)),
		me_connect_i_payload_order,
	},
	{ME_CONNECT,		FALSE,	TRUE,
		(sizeof(me_connect_r_payload_rules)/sizeof(payload_rule_t)),
		me_connect_r_payload_rules,
		(sizeof(me_connect_r_payload_order)/sizeof(payload_order_t)),
		me_connect_r_payload_order,
	},
#endif /* ME */
};


typedef struct private_message_t private_message_t;

/**
 * Private data of an message_t object.
 */
struct private_message_t {

	/**
	 * Public part of a message_t object.
	 */
	message_t public;

	/**
	 * Minor version of message.
	 */
	u_int8_t major_version;

	/**
	 * Major version of message.
	 */
	u_int8_t minor_version;

	/**
	 * First Payload in message.
	 */
	payload_type_t first_payload;

	/**
	 * Assigned exchange type.
	 */
	exchange_type_t exchange_type;

	/**
	 * TRUE if message is a request, FALSE if a reply.
	 */
	bool is_request;

	/**
	 * Message ID of this message.
	 */
	u_int32_t message_id;

	/**
	 * ID of assigned IKE_SA.
	 */
	ike_sa_id_t *ike_sa_id;

	/**
	 * Assigned UDP packet, stores incoming packet or last generated one.
	 */
	packet_t *packet;

	/**
	 * Linked List where payload data are stored in.
	 */
	linked_list_t *payloads;

	 /**
	  * Assigned parser to parse Header and Body of this message.
	  */
	parser_t *parser;

	/**
	 * The message rule for this message instance
	 */
	message_rule_t *message_rule;
};

/**
 * Implementation of private_message_t.set_message_rule.
 */
static  status_t set_message_rule(private_message_t *this)
{
	int i;

	for (i = 0; i < (sizeof(message_rules) / sizeof(message_rule_t)); i++)
	{
		if ((this->exchange_type == message_rules[i].exchange_type) &&
			(this->is_request == message_rules[i].is_request))
		{
			/* found rule for given exchange_type*/
			this->message_rule = &(message_rules[i]);
			return SUCCESS;
		}
	}
	this->message_rule = NULL;
	return NOT_FOUND;
}

/**
 * Implementation of private_message_t.get_payload_rule.
 */
static status_t get_payload_rule(private_message_t *this,
					payload_type_t payload_type, payload_rule_t **payload_rule)
{
	int i;

	for (i = 0; i < this->message_rule->payload_rule_count;i++)
	{
		if (this->message_rule->payload_rules[i].payload_type == payload_type)
		{
			*payload_rule = &(this->message_rule->payload_rules[i]);
			return SUCCESS;
		}
	}

	*payload_rule = NULL;
	return NOT_FOUND;
}

/**
 * Implementation of message_t.set_ike_sa_id.
 */
static void set_ike_sa_id(private_message_t *this,ike_sa_id_t *ike_sa_id)
{
	DESTROY_IF(this->ike_sa_id);
	this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
}

/**
 * Implementation of message_t.get_ike_sa_id.
 */
static ike_sa_id_t* get_ike_sa_id(private_message_t *this)
{
	return this->ike_sa_id;
}

/**
 * Implementation of message_t.set_message_id.
 */
static void set_message_id(private_message_t *this,u_int32_t message_id)
{
	this->message_id = message_id;
}

/**
 * Implementation of message_t.get_message_id.
 */
static u_int32_t get_message_id(private_message_t *this)
{
	return this->message_id;
}

/**
 * Implementation of message_t.get_initiator_spi.
 */
static u_int64_t get_initiator_spi(private_message_t *this)
{
	return (this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
}

/**
 * Implementation of message_t.get_responder_spi.
 */
static u_int64_t get_responder_spi(private_message_t *this)
{
	return (this->ike_sa_id->get_responder_spi(this->ike_sa_id));
}

/**
 * Implementation of message_t.set_major_version.
 */
static void set_major_version(private_message_t *this,u_int8_t major_version)
{
	this->major_version = major_version;
}

/**
 * Implementation of message_t.set_major_version.
 */
static u_int8_t get_major_version(private_message_t *this)
{
	return this->major_version;
}

/**
 * Implementation of message_t.set_minor_version.
 */
static void set_minor_version(private_message_t *this,u_int8_t minor_version)
{
	this->minor_version = minor_version;
}

/**
 * Implementation of message_t.get_minor_version.
 */
static u_int8_t get_minor_version(private_message_t *this)
{
	return this->minor_version;
}

/**
 * Implementation of message_t.set_exchange_type.
 */
static void set_exchange_type(private_message_t *this,
							  exchange_type_t exchange_type)
{
	this->exchange_type = exchange_type;
}

/**
 * Implementation of message_t.get_exchange_type.
 */
static exchange_type_t get_exchange_type(private_message_t *this)
{
	return this->exchange_type;
}

/**
 * Implementation of message_t.get_first_payload_type.
 */
static payload_type_t get_first_payload_type(private_message_t *this)
{
	return this->first_payload;
}

/**
 * Implementation of message_t.set_request.
 */
static void set_request(private_message_t *this, bool request)
{
	this->is_request = request;
}

/**
 * Implementation of message_t.get_request.
 */
static exchange_type_t get_request(private_message_t *this)
{
	return this->is_request;
}

/**
 * Is this message in an encoded form?
 */
static bool is_encoded(private_message_t *this)
{
	chunk_t data = this->packet->get_data(this->packet);

	if (data.ptr == NULL)
	{
		return FALSE;
	}
	return TRUE;
}

/**
 * Implementation of message_t.add_payload.
 */
static void add_payload(private_message_t *this, payload_t *payload)
{
	payload_t *last_payload;

	if (this->payloads->get_count(this->payloads) > 0)
	{
		this->payloads->get_last(this->payloads, (void **)&last_payload);
		last_payload->set_next_type(last_payload, payload->get_type(payload));
	}
	else
	{
		this->first_payload = payload->get_type(payload);
	}
	payload->set_next_type(payload, NO_PAYLOAD);
	this->payloads->insert_last(this->payloads, payload);

	DBG2(DBG_ENC ,"added payload of type %N to message",
		 payload_type_names, payload->get_type(payload));
}

/**
 * Implementation of message_t.add_notify.
 */
static void add_notify(private_message_t *this, bool flush, notify_type_t type,
					   chunk_t data)
{
	notify_payload_t *notify;
	payload_t *payload;

	if (flush)
	{
		while (this->payloads->remove_last(this->payloads,
												(void**)&payload) == SUCCESS)
		{
			payload->destroy(payload);
		}
	}
	notify = notify_payload_create();
	notify->set_notify_type(notify, type);
	notify->set_notification_data(notify, data);
	add_payload(this, (payload_t*)notify);
}

/**
 * Implementation of message_t.set_source.
 */
static void set_source(private_message_t *this, host_t *host)
{
	this->packet->set_source(this->packet, host);
}

/**
 * Implementation of message_t.set_destination.
 */
static void set_destination(private_message_t *this, host_t *host)
{
	this->packet->set_destination(this->packet, host);
}

/**
 * Implementation of message_t.get_source.
 */
static host_t* get_source(private_message_t *this)
{
	return this->packet->get_source(this->packet);
}

/**
 * Implementation of message_t.get_destination.
 */
static host_t * get_destination(private_message_t *this)
{
	return this->packet->get_destination(this->packet);
}

/**
 * Implementation of message_t.create_payload_enumerator.
 */
static enumerator_t *create_payload_enumerator(private_message_t *this)
{
	return this->payloads->create_enumerator(this->payloads);
}

/**
 * Implementation of message_t.get_payload.
 */
static payload_t *get_payload(private_message_t *this, payload_type_t type)
{
	payload_t *current, *found = NULL;
	enumerator_t *enumerator;

	enumerator = create_payload_enumerator(this);
	while (enumerator->enumerate(enumerator, &current))
	{
		if (current->get_type(current) == type)
		{
			found = current;
			break;
		}
	}
	enumerator->destroy(enumerator);
	return found;
}

/**
 * Implementation of message_t.get_notify
 */
static notify_payload_t* get_notify(private_message_t *this, notify_type_t type)
{
	enumerator_t *enumerator;
	notify_payload_t *notify = NULL;
	payload_t *payload;

	enumerator = create_payload_enumerator(this);
	while (enumerator->enumerate(enumerator, &payload))
	{
		if (payload->get_type(payload) == NOTIFY)
		{
			notify = (notify_payload_t*)payload;
			if (notify->get_notify_type(notify) == type)
			{
				break;
			}
			notify = NULL;
		}
	}
	enumerator->destroy(enumerator);
	return notify;
}

/**
 * get a string representation of the message
 */
static char* get_string(private_message_t *this, char *buf, int len)
{
	enumerator_t *enumerator;
	payload_t *payload;
	int written;
	char *pos = buf;

	memset(buf, 0, len);
	len--;

	written = snprintf(pos, len, "%N %s %d [",
					   exchange_type_names, this->exchange_type,
					   this->is_request ? "request" : "response",
					   this->message_id);
	if (written >= len || written < 0)
	{
		return "";
	}
	pos += written;
	len -= written;

	enumerator = create_payload_enumerator(this);
	while (enumerator->enumerate(enumerator, &payload))
	{
		written = snprintf(pos, len, " %N", payload_type_short_names,
						   payload->get_type(payload));
		if (written >= len || written < 0)
		{
			return buf;
		}
		pos += written;
		len -= written;
		if (payload->get_type(payload) == NOTIFY)
		{
			notify_payload_t *notify = (notify_payload_t*)payload;
			written = snprintf(pos, len, "(%N)", notify_type_short_names,
							   notify->get_notify_type(notify));
			if (written >= len || written < 0)
			{
				return buf;
			}
			pos += written;
			len -= written;
		}
		if (payload->get_type(payload) == EXTENSIBLE_AUTHENTICATION)
		{
			eap_payload_t *eap = (eap_payload_t*)payload;
			u_int32_t vendor;
			eap_type_t type;
			char method[64] = "";

			type = eap->get_type(eap, &vendor);
			if (type)
			{
				if (vendor)
				{
					snprintf(method, sizeof(method), "/%d-%d", type, vendor);
				}
				else
				{
					snprintf(method, sizeof(method), "/%N",
							 eap_type_short_names, type);
				}
			}
			written = snprintf(pos, len, "/%N%s", eap_code_short_names,
							   eap->get_code(eap), method);
			if (written >= len || written < 0)
			{
				return buf;
			}
			pos += written;
			len -= written;
		}
		if (payload->get_type(payload) == CONFIGURATION)
		{
			cp_payload_t *cp = (cp_payload_t*)payload;
			enumerator_t *attributes;
			configuration_attribute_t *attribute;
			bool first = TRUE;

			attributes = cp->create_attribute_enumerator(cp);
			while (attributes->enumerate(attributes, &attribute))
			{
				written = snprintf(pos, len, "%s%N", first ? "(" : " ",
								   configuration_attribute_type_short_names,
								   attribute->get_type(attribute));
				if (written >= len || written < 0)
				{
					return buf;
				}
				pos += written;
				len -= written;
				first = FALSE;
			}
			attributes->destroy(attributes);
			if (!first)
			{
				written = snprintf(pos, len, ")");
				if (written >= len || written < 0)
				{
					return buf;
				}
				pos += written;
				len -= written;
			}
		}
	}
	enumerator->destroy(enumerator);

	/* remove last space */
	snprintf(pos, len, " ]");
	return buf;
}

/**
 * reorder payloads depending on reordering rules
 */
static void order_payloads(private_message_t *this)
{
	linked_list_t *list;
	payload_t *payload;
	int i;

	/* move to temp list */
	list = linked_list_create();
	while (this->payloads->remove_last(this->payloads,
									   (void**)&payload) == SUCCESS)
	{
		list->insert_first(list, payload);
	}
	/* for each rule, ... */
	for (i = 0; i < this->message_rule->payload_order_count; i++)
	{
		enumerator_t *enumerator;
		notify_payload_t *notify;
		payload_order_t order = this->message_rule->payload_order[i];

		/* ... find all payload ... */
		enumerator = list->create_enumerator(list);
		while (enumerator->enumerate(enumerator, &payload))
		{
			/* ... with that type ... */
			if (payload->get_type(payload) == order.type)
			{
				notify = (notify_payload_t*)payload;

				/**... and check notify for type. */
				if (order.type != NOTIFY || order.notify == 0 ||
					order.notify == notify->get_notify_type(notify))
				{
					list->remove_at(list, enumerator);
					add_payload(this, payload);
				}
			}
		}
		enumerator->destroy(enumerator);
	}
	/* append all payloads without a rule to the end */
	while (list->remove_last(list, (void**)&payload) == SUCCESS)
	{
		/* do not complain about payloads in private use space */
		if (payload->get_type(payload) < 128)
		{
			DBG1(DBG_ENC, "payload %N has no ordering rule in %N %s",
				 payload_type_names, payload->get_type(payload),
				 exchange_type_names, this->message_rule->exchange_type,
				 this->message_rule->is_request ? "request" : "response");
		}
		add_payload(this, payload);
	}
	list->destroy(list);
}

/**
 * Implementation of private_message_t.encrypt_payloads.
 */
static status_t encrypt_payloads(private_message_t *this,
								 crypter_t *crypter, signer_t* signer)
{
	encryption_payload_t *encryption;
	linked_list_t *payloads;
	payload_t *current;
	status_t status;

	if (!this->message_rule->encrypted_content)
	{
		DBG2(DBG_ENC, "message doesn't have to be encrypted");
		/* message contains no content to encrypt */
		return SUCCESS;
	}

	if (!crypter || !signer)
	{
		DBG2(DBG_ENC, "no crypter or signer specified, do not encrypt message");
		/* message contains no content to encrypt */
		return SUCCESS;
	}

	DBG2(DBG_ENC, "copy all payloads to a temporary list");
	payloads = linked_list_create();

	/* first copy all payloads in a temporary list */
	while (this->payloads->get_count(this->payloads) > 0)
	{
		this->payloads->remove_first(this->payloads, (void**)&current);
		payloads->insert_last(payloads, current);
	}

	encryption = encryption_payload_create();

	DBG2(DBG_ENC, "check each payloads if they have to get encrypted");
	while (payloads->get_count(payloads) > 0)
	{
		payload_rule_t *rule;
		payload_type_t type;
		bool to_encrypt = TRUE;

		payloads->remove_first(payloads, (void**)&current);

		type = current->get_type(current);
		if (get_payload_rule(this, type, &rule) == SUCCESS)
		{
			to_encrypt = rule->encrypted;
		}
		if (to_encrypt)
		{
			DBG2(DBG_ENC, "insert payload %N to encryption payload",
				 payload_type_names, current->get_type(current));
			encryption->add_payload(encryption, current);
		}
		else
		{
			DBG2(DBG_ENC, "insert payload %N unencrypted",
				 payload_type_names, current->get_type(current));
			add_payload(this, (payload_t*)current);
		}
	}

	DBG2(DBG_ENC, "encrypting encryption payload");
	encryption->set_transforms(encryption, crypter, signer);
	status = encryption->encrypt(encryption);
	DBG2(DBG_ENC, "add encrypted payload to payload list");
	add_payload(this, (payload_t*)encryption);

	payloads->destroy(payloads);

	return status;
}

/**
 * Implementation of message_t.generate.
 */
static status_t generate(private_message_t *this, crypter_t *crypter,
						 signer_t* signer, packet_t **packet)
{
	generator_t *generator;
	ike_header_t *ike_header;
	payload_t *payload, *next_payload;
	enumerator_t *enumerator;
	status_t status;
	chunk_t packet_data;
	char str[256];

	if (is_encoded(this))
	{
		/* already generated, return a new packet clone */
		*packet = this->packet->clone(this->packet);
		return SUCCESS;
	}

	if (this->exchange_type == EXCHANGE_TYPE_UNDEFINED)
	{
		DBG1(DBG_ENC, "exchange type is not defined");
		return INVALID_STATE;
	}

	if (this->packet->get_source(this->packet) == NULL ||
		this->packet->get_destination(this->packet) == NULL)
	{
		DBG1(DBG_ENC, "%s not defined",
			 !this->packet->get_source(this->packet) ? "source" : "destination");
		return INVALID_STATE;
	}

	/* set the rules for this messge */
	status = set_message_rule(this);
	if (status != SUCCESS)
	{
		DBG1(DBG_ENC, "no message rules specified for this message type");
		return NOT_SUPPORTED;
	}

	order_payloads(this);

	DBG1(DBG_ENC, "generating %s", get_string(this, str, sizeof(str)));

	/* going to encrypt all content which have to be encrypted */
	status = encrypt_payloads(this, crypter, signer);
	if (status != SUCCESS)
	{
		DBG1(DBG_ENC, "payload encryption failed");
		return status;
	}

	/* build ike header */
	ike_header = ike_header_create();

	ike_header->set_exchange_type(ike_header, this->exchange_type);
	ike_header->set_message_id(ike_header, this->message_id);
	ike_header->set_response_flag(ike_header, !this->is_request);
	ike_header->set_initiator_flag(ike_header,
						this->ike_sa_id->is_initiator(this->ike_sa_id));
	ike_header->set_initiator_spi(ike_header,
						this->ike_sa_id->get_initiator_spi(this->ike_sa_id));
	ike_header->set_responder_spi(ike_header,
						this->ike_sa_id->get_responder_spi(this->ike_sa_id));

	generator = generator_create();

	payload = (payload_t*)ike_header;

	/* generate every payload expect last one, this is done later*/
	enumerator = create_payload_enumerator(this);
	while (enumerator->enumerate(enumerator, &next_payload))
	{
		payload->set_next_type(payload, next_payload->get_type(next_payload));
		generator->generate_payload(generator, payload);
		payload = next_payload;
	}
	enumerator->destroy(enumerator);

	/* last payload has no next payload*/
	payload->set_next_type(payload, NO_PAYLOAD);

	generator->generate_payload(generator, payload);

	ike_header->destroy(ike_header);

	/* build packet */
	generator->write_to_chunk(generator, &packet_data);
	generator->destroy(generator);

	/* if last payload is of type encrypted, integrity checksum if necessary */
	if (payload->get_type(payload) == ENCRYPTED)
	{
		DBG2(DBG_ENC, "build signature on whole message");
		encryption_payload_t *encryption_payload = (encryption_payload_t*)payload;
		status = encryption_payload->build_signature(encryption_payload, packet_data);
		if (status != SUCCESS)
		{
			return status;
		}
	}

	this->packet->set_data(this->packet, packet_data);

	/* clone packet for caller */
	*packet = this->packet->clone(this->packet);

	DBG2(DBG_ENC, "message generated successfully");
	return SUCCESS;
}

/**
 * Implementation of message_t.get_packet.
 */
static packet_t *get_packet(private_message_t *this)
{
	if (this->packet == NULL)
	{
		return NULL;
	}
	return this->packet->clone(this->packet);
}

/**
 * Implementation of message_t.get_packet_data.
 */
static chunk_t get_packet_data(private_message_t *this)
{
	if (this->packet == NULL)
	{
		return chunk_empty;
	}
	return chunk_clone(this->packet->get_data(this->packet));
}

/**
 * Implementation of message_t.parse_header.
 */
static status_t parse_header(private_message_t *this)
{
	ike_header_t *ike_header;
	status_t status;

	DBG2(DBG_ENC, "parsing header of message");

	this->parser->reset_context(this->parser);
	status = this->parser->parse_payload(this->parser, HEADER,
										 (payload_t**)&ike_header);
	if (status != SUCCESS)
	{
		DBG1(DBG_ENC, "header could not be parsed");
		return status;

	}

	/* verify payload */
	status = ike_header->payload_interface.verify(
										&ike_header->payload_interface);
	if (status != SUCCESS)
	{
		DBG1(DBG_ENC, "header verification failed");
		ike_header->destroy(ike_header);
		return status;
	}

	if (this->ike_sa_id != NULL)
	{
		this->ike_sa_id->destroy(this->ike_sa_id);
	}

	this->ike_sa_id = ike_sa_id_create(ike_header->get_initiator_spi(ike_header),
									ike_header->get_responder_spi(ike_header),
									ike_header->get_initiator_flag(ike_header));

	this->exchange_type = ike_header->get_exchange_type(ike_header);
	this->message_id = ike_header->get_message_id(ike_header);
	this->is_request = (!(ike_header->get_response_flag(ike_header)));
	this->major_version = ike_header->get_maj_version(ike_header);
	this->minor_version = ike_header->get_min_version(ike_header);
	this->first_payload = ike_header->payload_interface.get_next_type(
												&ike_header->payload_interface);

	DBG2(DBG_ENC, "parsed a %N %s", exchange_type_names, this->exchange_type,
		 this->is_request ? "request" : "response");

	ike_header->destroy(ike_header);

	/* get the rules for this messge */
	status = set_message_rule(this);
	if (status != SUCCESS)
	{
		DBG1(DBG_ENC, "no message rules specified for a %N %s",
			 exchange_type_names, this->exchange_type,
			 this->is_request ? "request" : "response");
	}

	return status;
}

/**
 * Implementation of private_message_t.decrypt_and_verify_payloads.
 */
static status_t decrypt_payloads(private_message_t *this, crypter_t *crypter,
								 signer_t* signer)
{
	bool current_payload_was_encrypted = FALSE;
	payload_t *previous_payload = NULL;
	int payload_number = 1;
	iterator_t *iterator;
	payload_t *current_payload;
	status_t status;

	iterator = this->payloads->create_iterator(this->payloads,TRUE);

	/* process each payload and decrypt a encryption payload */
	while(iterator->iterate(iterator, (void**)&current_payload))
	{
		payload_rule_t *payload_rule;
		payload_type_t current_payload_type;

		/* needed to check */
		current_payload_type = current_payload->get_type(current_payload);

		DBG2(DBG_ENC, "process payload of type %N",
			 payload_type_names, current_payload_type);

		if (current_payload_type == ENCRYPTED)
		{
			encryption_payload_t *encryption_payload;
			payload_t *current_encrypted_payload;

			encryption_payload = (encryption_payload_t*)current_payload;

			DBG2(DBG_ENC, "found an encryption payload");

			if (payload_number != this->payloads->get_count(this->payloads))
			{
				/* encrypted payload is not last one */
				DBG1(DBG_ENC, "encrypted payload is not last payload");
				iterator->destroy(iterator);
				return VERIFY_ERROR;
			}
			/* decrypt */
			encryption_payload->set_transforms(encryption_payload,
											   crypter, signer);
			DBG2(DBG_ENC, "verify signature of encryption payload");
			status = encryption_payload->verify_signature(encryption_payload,
										this->packet->get_data(this->packet));
			if (status != SUCCESS)
			{
				DBG1(DBG_ENC, "encryption payload signature invalid");
				iterator->destroy(iterator);
				return FAILED;
			}
			DBG2(DBG_ENC, "decrypting content of encryption payload");
			status = encryption_payload->decrypt(encryption_payload);
			if (status != SUCCESS)
			{
				DBG1(DBG_ENC, "encrypted payload could not be decrypted and parsed");
				iterator->destroy(iterator);
				return PARSE_ERROR;
			}

			/* needed later to find out if a payload was encrypted */
			current_payload_was_encrypted = TRUE;

			/* check if there are payloads contained in the encryption payload */
			if (encryption_payload->get_payload_count(encryption_payload) == 0)
			{
				DBG2(DBG_ENC, "encrypted payload is empty");
				/* remove the encryption payload, is not needed anymore */
				iterator->remove(iterator);
				/* encrypted payload contains no other payload */
				current_payload_type = NO_PAYLOAD;
			}
			else
			{
				/* encryption_payload is replaced with first payload contained
				 * in encryption_payload */
				encryption_payload->remove_first_payload(encryption_payload,
													&current_encrypted_payload);
				iterator->replace(iterator, NULL,
								  (void *)current_encrypted_payload);
				current_payload_type = current_encrypted_payload->get_type(
													current_encrypted_payload);
			}

			/* is the current paylad the first in the message? */
			if (previous_payload == NULL)
			{
				/* yes, set the first payload type of the message to the
				 * current type */
				this->first_payload = current_payload_type;
			}
			else
			{
				/* no, set the next_type of the previous payload to the
				 * current type */
				previous_payload->set_next_type(previous_payload,
												current_payload_type);
			}

			/* all encrypted payloads are added to the payload list */
			while (encryption_payload->get_payload_count(encryption_payload) > 0)
			{
				encryption_payload->remove_first_payload(encryption_payload,
													&current_encrypted_payload);
				DBG2(DBG_ENC, "insert unencrypted payload of type "
					 "%N at end of list", payload_type_names,
					 current_encrypted_payload->get_type(
											current_encrypted_payload));
				this->payloads->insert_last(this->payloads,
											current_encrypted_payload);
			}

			/* encryption payload is processed, payloads are moved. Destroy it. */
			encryption_payload->destroy(encryption_payload);
		}

		/* we allow unknown payloads of any type and don't bother if it was
		 * encrypted. Not our problem. */
		if (current_payload_type != UNKNOWN_PAYLOAD &&
			current_payload_type != NO_PAYLOAD)
		{
			/* get the ruleset for found payload */
			status = get_payload_rule(this, current_payload_type, &payload_rule);
			if (status != SUCCESS)
			{
				/* payload is not allowed */
				DBG1(DBG_ENC, "payload type %N not allowed",
								  payload_type_names, current_payload_type);
				iterator->destroy(iterator);
				return VERIFY_ERROR;
			}

			/* check if the payload was encrypted, and if it should been have
			 * encrypted */
			if (payload_rule->encrypted != current_payload_was_encrypted)
			{
				/* payload was not encrypted, but should have been.
				 * or vice-versa */
				DBG1(DBG_ENC, "payload type %N should be %s!",
					 payload_type_names, current_payload_type,
					 (payload_rule->encrypted) ? "encrypted" : "not encrypted");
				iterator->destroy(iterator);
				return VERIFY_ERROR;
			}
		}
		/* advance to the next payload */
		payload_number++;
		/* is stored to set next payload in case of found encryption payload */
		previous_payload = current_payload;
	}
	iterator->destroy(iterator);
	return SUCCESS;
}

/**
 * Implementation of private_message_t.verify.
 */
static status_t verify(private_message_t *this)
{
	int i;
	enumerator_t *enumerator;
	payload_t *current_payload;
	size_t total_found_payloads = 0;

	DBG2(DBG_ENC, "verifying message structure");

	/* check for payloads with wrong count*/
	for (i = 0; i < this->message_rule->payload_rule_count; i++)
	{
		size_t found_payloads = 0;
		payload_rule_t *rule;

		rule = &this->message_rule->payload_rules[i];
		enumerator = create_payload_enumerator(this);

		/* check all payloads for specific rule */
		while (enumerator->enumerate(enumerator, &current_payload))
		{
			payload_type_t current_payload_type;
			unknown_payload_t *unknown_payload;

			current_payload_type = current_payload->get_type(current_payload);
			if (current_payload_type == UNKNOWN_PAYLOAD)
			{
				/* unknown payloads are ignored, IF they are not critical */
				unknown_payload = (unknown_payload_t*)current_payload;
				if (unknown_payload->is_critical(unknown_payload))
				{
					DBG1(DBG_ENC, "%N is not supported, but its critical!",
						 payload_type_names, current_payload_type);
					enumerator->destroy(enumerator);
					return NOT_SUPPORTED;
				}
			}
			else if (current_payload_type == rule->payload_type)
			{
				found_payloads++;
				total_found_payloads++;
				DBG2(DBG_ENC, "found payload of type %N", payload_type_names,
					 rule->payload_type);

				/* as soon as ohe payload occures more then specified,
				 * the verification fails */
				if (found_payloads >
					rule->max_occurence)
				{
					DBG1(DBG_ENC, "payload of type %N more than %d times (%d) "
						 "occured in current message", payload_type_names,
						 current_payload_type, rule->max_occurence,
						 found_payloads);
					enumerator->destroy(enumerator);
					return VERIFY_ERROR;
				}
			}
		}
		enumerator->destroy(enumerator);

		if (found_payloads < rule->min_occurence)
		{
			DBG1(DBG_ENC, "payload of type %N not occured %d times (%d)",
				 payload_type_names, rule->payload_type, rule->min_occurence,
				 found_payloads);
			return VERIFY_ERROR;
		}
		if (rule->sufficient)
		{
			return SUCCESS;
		}
	}
	return SUCCESS;
}

/**
 * Implementation of message_t.parse_body.
 */
static status_t parse_body(private_message_t *this, crypter_t *crypter,
						   signer_t *signer)
{
	status_t status = SUCCESS;
	payload_type_t current_payload_type;
	char str[256];

	current_payload_type = this->first_payload;

	DBG2(DBG_ENC, "parsing body of message, first payload is %N",
		 payload_type_names, current_payload_type);

	/* parse payload for payload, while there are more available */
	while ((current_payload_type != NO_PAYLOAD))
	{
		payload_t *current_payload;

		DBG2(DBG_ENC, "starting parsing a %N payload",
			 payload_type_names, current_payload_type);

		/* parse current payload */
		status = this->parser->parse_payload(this->parser, current_payload_type,
											 (payload_t**)&current_payload);
		if (status != SUCCESS)
		{
			DBG1(DBG_ENC, "payload type %N could not be parsed",
				 payload_type_names, current_payload_type);
			return PARSE_ERROR;
		}

		DBG2(DBG_ENC, "verifying payload of type %N",
			 payload_type_names, current_payload_type);

		/* verify it, stop parsig if its invalid */
		status = current_payload->verify(current_payload);
		if (status != SUCCESS)
		{
			DBG1(DBG_ENC, "%N payload verification failed",
				 payload_type_names, current_payload_type);
			current_payload->destroy(current_payload);
			return VERIFY_ERROR;
		}

		DBG2(DBG_ENC, "%N payload verified. Adding to payload list",
			 payload_type_names, current_payload_type);
		this->payloads->insert_last(this->payloads,current_payload);

		/* an encryption payload is the last one, so STOP here. decryption is
		 * done later */
		if (current_payload_type == ENCRYPTED)
		{
			DBG2(DBG_ENC, "%N payload found. Stop parsing",
				 payload_type_names, current_payload_type);
			break;
		}

		/* get next payload type */
		current_payload_type = current_payload->get_next_type(current_payload);
	}

	if (current_payload_type == ENCRYPTED)
	{
		status = decrypt_payloads(this,crypter,signer);
		if (status != SUCCESS)
		{
			DBG1(DBG_ENC, "could not decrypt payloads");
			return status;
		}
	}

	status = verify(this);
	if (status != SUCCESS)
	{
		return status;
	}

	DBG1(DBG_ENC, "parsed %s", get_string(this, str, sizeof(str)));

	return SUCCESS;
}

/**
 * Implementation of message_t.destroy.
 */
static void destroy (private_message_t *this)
{
	DESTROY_IF(this->ike_sa_id);
	this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
	this->packet->destroy(this->packet);
	this->parser->destroy(this->parser);
	free(this);
}

/*
 * Described in Header-File
 */
message_t *message_create_from_packet(packet_t *packet)
{
	private_message_t *this = malloc_thing(private_message_t);

	/* public functions */
	this->public.set_major_version = (void(*)(message_t*, u_int8_t))set_major_version;
	this->public.get_major_version = (u_int8_t(*)(message_t*))get_major_version;
	this->public.set_minor_version = (void(*)(message_t*, u_int8_t))set_minor_version;
	this->public.get_minor_version = (u_int8_t(*)(message_t*))get_minor_version;
	this->public.set_message_id = (void(*)(message_t*, u_int32_t))set_message_id;
	this->public.get_message_id = (u_int32_t(*)(message_t*))get_message_id;
	this->public.get_initiator_spi = (u_int64_t(*)(message_t*))get_initiator_spi;
	this->public.get_responder_spi = (u_int64_t(*)(message_t*))get_responder_spi;
	this->public.set_ike_sa_id = (void(*)(message_t*, ike_sa_id_t *))set_ike_sa_id;
	this->public.get_ike_sa_id = (ike_sa_id_t*(*)(message_t*))get_ike_sa_id;
	this->public.set_exchange_type = (void(*)(message_t*, exchange_type_t))set_exchange_type;
	this->public.get_exchange_type = (exchange_type_t(*)(message_t*))get_exchange_type;
	this->public.get_first_payload_type = (payload_type_t(*)(message_t*))get_first_payload_type;
	this->public.set_request = (void(*)(message_t*, bool))set_request;
	this->public.get_request = (bool(*)(message_t*))get_request;
	this->public.add_payload = (void(*)(message_t*,payload_t*))add_payload;
	this->public.add_notify = (void(*)(message_t*,bool,notify_type_t,chunk_t))add_notify;
	this->public.generate = (status_t (*) (message_t *,crypter_t*,signer_t*,packet_t**)) generate;
	this->public.set_source = (void (*) (message_t*,host_t*)) set_source;
	this->public.get_source = (host_t * (*) (message_t*)) get_source;
	this->public.set_destination = (void (*) (message_t*,host_t*)) set_destination;
	this->public.get_destination = (host_t * (*) (message_t*)) get_destination;
	this->public.create_payload_enumerator = (enumerator_t * (*) (message_t *)) create_payload_enumerator;
	this->public.get_payload = (payload_t * (*) (message_t *, payload_type_t)) get_payload;
	this->public.get_notify = (notify_payload_t*(*)(message_t*, notify_type_t type))get_notify;
	this->public.parse_header = (status_t (*) (message_t *)) parse_header;
	this->public.parse_body = (status_t (*) (message_t *,crypter_t*,signer_t*)) parse_body;
	this->public.get_packet = (packet_t * (*) (message_t*)) get_packet;
	this->public.get_packet_data = (chunk_t (*) (message_t *this)) get_packet_data;
	this->public.destroy = (void(*)(message_t*))destroy;

	/* private values */
	this->exchange_type = EXCHANGE_TYPE_UNDEFINED;
	this->is_request = TRUE;
	this->ike_sa_id = NULL;
	this->first_payload = NO_PAYLOAD;
	this->message_id = 0;

	/* private values */
	if (packet == NULL)
	{
		packet = packet_create();
	}
	this->message_rule = NULL;
	this->packet = packet;
	this->payloads = linked_list_create();

	/* parser is created from data of packet */
	this->parser = parser_create(this->packet->get_data(this->packet));

	return (&this->public);
}

/*
 * Described in Header.
 */
message_t *message_create()
{
	return message_create_from_packet(NULL);
}