-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.c
2516 lines (2329 loc) · 72.4 KB
/
message.c
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
/* $OpenBSD: message.c,v 1.129 2016/04/04 17:35:07 yasuoka Exp $ */
/* $EOM: message.c,v 1.156 2000/10/10 12:36:39 provos Exp $ */
/*
* Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist. All rights reserved.
* Copyright (c) 1999 Angelos D. Keromytis. All rights reserved.
* Copyright (c) 1999, 2000, 2001, 2004 Håkan Olsson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This code was written under funding by Ericsson Radio Systems.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include "attribute.h"
#include "cert.h"
#include "constants.h"
#include "crypto.h"
#include "doi.h"
#include "dpd.h"
#include "exchange.h"
#include "field.h"
#include "hash.h"
#include "ipsec.h"
#include "ipsec_num.h"
#include "isakmp.h"
#include "log.h"
#include "message.h"
#include "nat_traversal.h"
#include "prf.h"
#include "sa.h"
#include "timer.h"
#include "transport.h"
#include "util.h"
#include "vendor.h"
#include "virtual.h"
/* A local set datatype, coincidentally fd_set suits our purpose fine. */
typedef fd_set set;
#define ISSET FD_ISSET
#define SET FD_SET
#define ZERO FD_ZERO
static int message_check_duplicate(struct message *);
static int message_encrypt(struct message *);
static int message_index_payload(struct message *, struct payload *,
u_int8_t ,u_int8_t *);
static int message_parse_transform(struct message *, struct payload *,
u_int8_t, u_int8_t *);
static struct field *message_get_field(u_int8_t);
static int message_validate_payload(struct message *, struct payload *,
u_int8_t);
static u_int16_t message_payload_sz(u_int8_t);
static int message_validate_attribute(struct message *, struct payload *);
static int message_validate_cert(struct message *, struct payload *);
static int message_validate_cert_req(struct message *, struct payload *);
static int message_validate_delete(struct message *, struct payload *);
static int message_validate_hash(struct message *, struct payload *);
static int message_validate_id(struct message *, struct payload *);
static int message_validate_key_exch(struct message *, struct payload *);
static int message_validate_nat_d(struct message *, struct payload *);
static int message_validate_nat_oa(struct message *, struct payload *);
static int message_validate_nonce(struct message *, struct payload *);
static int message_validate_notify(struct message *, struct payload *);
static int message_validate_proposal(struct message *, struct payload *);
static int message_validate_sa(struct message *, struct payload *);
static int message_validate_sig(struct message *, struct payload *);
static int message_validate_transform(struct message *, struct payload *);
static int message_validate_vendor(struct message *, struct payload *);
static void message_packet_log(struct message *);
/*
* Fields used for checking monotonic increasing of proposal and transform
* numbers.
*/
static u_int8_t *last_sa = 0;
static u_int32_t last_prop_no;
static u_int8_t *last_prop = 0;
static u_int32_t last_xf_no;
/*
* Allocate a message structure bound to transport T, and with a first
* segment buffer sized SZ, copied from BUF if given.
*/
struct message *
message_alloc(struct transport *t, u_int8_t *buf, size_t sz)
{
struct message *msg;
int i;
/*
* We use calloc(3) because it zeroes the structure which we rely on in
* message_free when determining what sub-allocations to free.
*/
msg = calloc(1, sizeof *msg);
if (!msg)
return 0;
msg->iov = calloc(1, sizeof *msg->iov);
if (!msg->iov) {
message_free(msg);
return 0;
}
msg->iov[0].iov_len = sz;
msg->iov[0].iov_base = malloc(sz);
if (!msg->iov[0].iov_base) {
message_free(msg);
return 0;
}
msg->iovlen = 1;
if (buf)
memcpy(msg->iov[0].iov_base, buf, sz);
msg->nextp = (u_int8_t *)msg->iov[0].iov_base +
ISAKMP_HDR_NEXT_PAYLOAD_OFF;
msg->transport = t;
transport_reference(t);
msg->payload = calloc(ISAKMP_PAYLOAD_MAX, sizeof *msg->payload);
if (!msg->payload) {
message_free(msg);
return 0;
}
for (i = 0; i < ISAKMP_PAYLOAD_MAX; i++)
TAILQ_INIT(&msg->payload[i]);
TAILQ_INIT(&msg->post_send);
LOG_DBG((LOG_MESSAGE, 90, "message_alloc: allocated %p", msg));
return msg;
}
/*
* Allocate a message suitable for a reply to MSG. Just allocate an empty
* ISAKMP header as the first segment.
*/
struct message *
message_alloc_reply(struct message *msg)
{
struct message *reply;
reply = message_alloc(msg->transport, 0, ISAKMP_HDR_SZ);
reply->exchange = msg->exchange;
reply->isakmp_sa = msg->isakmp_sa;
reply->flags = msg->flags;
if (msg->isakmp_sa)
sa_reference(msg->isakmp_sa);
return reply;
}
/* Free up all resources used by the MSG message. */
void
message_free(struct message *msg)
{
u_int32_t i;
struct payload *payload;
struct post_send *node;
LOG_DBG((LOG_MESSAGE, 20, "message_free: freeing %p", msg));
if (!msg)
return;
if (msg->iov) {
if (msg->orig && msg->orig != (u_int8_t *)msg->iov[0].iov_base)
free(msg->orig);
for (i = 0; i < msg->iovlen; i++)
free(msg->iov[i].iov_base);
free(msg->iov);
}
if (msg->retrans)
timer_remove_event(msg->retrans);
if (msg->payload) {
for (i = 0; i < ISAKMP_PAYLOAD_MAX; i++)
while ((payload = TAILQ_FIRST(&msg->payload[i]))) {
TAILQ_REMOVE(&msg->payload[i], payload, link);
free(payload);
}
free(msg->payload);
}
while ((node = TAILQ_FIRST(&msg->post_send)))
TAILQ_REMOVE(&msg->post_send, node, link);
if (msg->transport) {
/* If we are on the send queue, remove us from there. */
if (msg->flags & MSG_IN_TRANSIT)
TAILQ_REMOVE(msg->transport->vtbl->get_queue(msg),
msg, link);
transport_release(msg->transport);
}
if (msg->isakmp_sa)
sa_release(msg->isakmp_sa);
free(msg);
}
/*
* Generic ISAKMP parser.
* MSG is the ISAKMP message to be parsed. NEXT is the type of the first
* payload to be parsed, and it's pointed to by BUF. ACCEPTED_PAYLOADS
* tells what payloads are accepted and FUNC is a pointer to a function
* to be called for each payload found, which is also responsible for
* freeing the passed ISAKMP message in the failure case.
* Returns the total length of the parsed payloads.
*/
static int
message_parse_payloads(struct message *msg, struct payload *p, u_int8_t next,
u_int8_t *buf, set *accepted_payloads, int (*func)(struct message *,
struct payload *, u_int8_t, u_int8_t *))
{
u_int8_t payload;
u_int16_t len;
int sz = 0;
do {
LOG_DBG((LOG_MESSAGE, 50,
"message_parse_payloads: offset %ld payload %s",
(long)(buf - (u_int8_t *) msg->iov[0].iov_base),
constant_name(isakmp_payload_cst, next)));
/* Does this payload's header fit? */
if (buf + ISAKMP_GEN_SZ > (u_int8_t *)msg->iov[0].iov_base +
msg->iov[0].iov_len) {
log_print("message_parse_payloads: short message");
message_drop(msg,
ISAKMP_NOTIFY_UNEQUAL_PAYLOAD_LENGTHS, 0, 1, 1);
return -1;
}
/* Ponder on the payload that is at BUF... */
payload = next;
/* Look at the next payload's type. */
next = GET_ISAKMP_GEN_NEXT_PAYLOAD(buf);
if (next >= ISAKMP_PAYLOAD_RESERVED_MIN &&
next <= ISAKMP_PAYLOAD_RESERVED_MAX) {
log_print("message_parse_payloads: invalid next "
"payload type %s in payload of type %d",
constant_name(isakmp_payload_cst, next), payload);
message_drop(msg, ISAKMP_NOTIFY_INVALID_PAYLOAD_TYPE,
0, 1, 1);
return -1;
}
/* Reserved fields in ISAKMP messages should be zero. */
if (GET_ISAKMP_GEN_RESERVED(buf) != 0) {
log_print("message_parse_payloads: reserved field "
"non-zero: %x", GET_ISAKMP_GEN_RESERVED(buf));
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED,
0, 1, 1);
return -1;
}
/*
* Decode and validate the payload length field.
*/
len = GET_ISAKMP_GEN_LENGTH(buf);
if (message_payload_sz(payload) == 0) {
log_print("message_parse_payloads: unknown minimum "
"payload size for payload type %s",
constant_name(isakmp_payload_cst, payload));
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED,
0, 1, 1);
return -1;
}
if (len < message_payload_sz(payload)) {
log_print("message_parse_payloads: payload too "
"short: %u", len);
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED,
0, 1, 1);
return -1;
}
if (buf + len > (u_int8_t *)msg->iov[0].iov_base +
msg->iov[0].iov_len) {
log_print("message_parse_payloads: payload too "
"long: %u", len);
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED,
0, 1, 1);
return -1;
}
/* Ignore most private payloads. */
if (next >= ISAKMP_PAYLOAD_PRIVATE_MIN &&
next != ISAKMP_PAYLOAD_NAT_D_DRAFT &&
next != ISAKMP_PAYLOAD_NAT_OA_DRAFT) {
LOG_DBG((LOG_MESSAGE, 30, "message_parse_payloads: "
"private next payload type %s in payload of "
"type %d ignored",
constant_name(isakmp_payload_cst, next), payload));
goto next_payload;
}
/*
* Check if the current payload is one of the accepted ones at
* this stage.
*/
if (!ISSET(payload, accepted_payloads)) {
log_print("message_parse_payloads: payload type %s "
"unexpected", constant_name(isakmp_payload_cst,
payload));
message_drop(msg, ISAKMP_NOTIFY_INVALID_PAYLOAD_TYPE,
0, 1, 1);
return -1;
}
/* Call the payload handler specified by the caller. */
if (func(msg, p, payload, buf))
return -1;
next_payload:
/* Advance to next payload. */
buf += len;
sz += len;
} while (next != ISAKMP_PAYLOAD_NONE);
return sz;
}
/*
* Parse a proposal payload found in message MSG. PAYLOAD is always
* ISAKMP_PAYLOAD_PROPOSAL and ignored in here. It's needed as the API for
* message_parse_payloads requires it. BUF points to the proposal's
* generic payload header.
*/
static int
message_parse_proposal(struct message *msg, struct payload *p,
u_int8_t payload, u_int8_t *buf)
{
set payload_set;
/* Put the proposal into the proposal bucket. */
if (message_index_payload(msg, p, payload, buf) == -1)
return -1;
ZERO(&payload_set);
SET(ISAKMP_PAYLOAD_TRANSFORM, &payload_set);
if (message_parse_payloads(msg,
TAILQ_LAST(&msg->payload[ISAKMP_PAYLOAD_PROPOSAL], payload_head),
ISAKMP_PAYLOAD_TRANSFORM, buf + ISAKMP_PROP_SPI_OFF +
GET_ISAKMP_PROP_SPI_SZ(buf), &payload_set, message_parse_transform)
== -1)
return -1;
return 0;
}
static int
message_parse_transform(struct message *msg, struct payload *p,
u_int8_t payload, u_int8_t *buf)
{
/* Put the transform into the transform bucket. */
if (message_index_payload(msg, p, payload, buf) == -1)
return -1;
LOG_DBG((LOG_MESSAGE, 50, "Transform %d's attributes",
GET_ISAKMP_TRANSFORM_NO(buf)));
attribute_map(buf + ISAKMP_TRANSFORM_SA_ATTRS_OFF,
GET_ISAKMP_GEN_LENGTH(buf) - ISAKMP_TRANSFORM_SA_ATTRS_OFF,
msg->exchange->doi->debug_attribute, msg);
return 0;
}
static struct field *
message_get_field(u_int8_t payload)
{
switch (payload) {
case ISAKMP_PAYLOAD_SA:
return isakmp_sa_fld;
case ISAKMP_PAYLOAD_PROPOSAL:
return isakmp_prop_fld;
case ISAKMP_PAYLOAD_TRANSFORM:
return isakmp_transform_fld;
case ISAKMP_PAYLOAD_KEY_EXCH:
return isakmp_ke_fld;
case ISAKMP_PAYLOAD_ID:
return isakmp_id_fld;
case ISAKMP_PAYLOAD_CERT:
return isakmp_cert_fld;
case ISAKMP_PAYLOAD_CERT_REQ:
return isakmp_certreq_fld;
case ISAKMP_PAYLOAD_HASH:
return isakmp_hash_fld;
case ISAKMP_PAYLOAD_SIG:
return isakmp_sig_fld;
case ISAKMP_PAYLOAD_NONCE:
return isakmp_nonce_fld;
case ISAKMP_PAYLOAD_NOTIFY:
return isakmp_notify_fld;
case ISAKMP_PAYLOAD_DELETE:
return isakmp_delete_fld;
case ISAKMP_PAYLOAD_VENDOR:
return isakmp_vendor_fld;
case ISAKMP_PAYLOAD_ATTRIBUTE:
return isakmp_attribute_fld;
case ISAKMP_PAYLOAD_NAT_D:
case ISAKMP_PAYLOAD_NAT_D_DRAFT:
return isakmp_nat_d_fld;
case ISAKMP_PAYLOAD_NAT_OA:
case ISAKMP_PAYLOAD_NAT_OA_DRAFT:
return isakmp_nat_oa_fld;
/* Not yet supported and any other unknown payloads. */
case ISAKMP_PAYLOAD_SAK:
case ISAKMP_PAYLOAD_SAT:
case ISAKMP_PAYLOAD_KD:
case ISAKMP_PAYLOAD_SEQ:
case ISAKMP_PAYLOAD_POP:
default:
break;
}
return NULL;
}
static int
message_validate_payload(struct message *m, struct payload *p, u_int8_t payload)
{
switch (payload) {
case ISAKMP_PAYLOAD_SA:
return message_validate_sa(m, p);
case ISAKMP_PAYLOAD_PROPOSAL:
return message_validate_proposal(m, p);
case ISAKMP_PAYLOAD_TRANSFORM:
return message_validate_transform(m, p);
case ISAKMP_PAYLOAD_KEY_EXCH:
return message_validate_key_exch(m, p);
case ISAKMP_PAYLOAD_ID:
return message_validate_id(m, p);
case ISAKMP_PAYLOAD_CERT:
return message_validate_cert(m, p);
case ISAKMP_PAYLOAD_CERT_REQ:
return message_validate_cert_req(m, p);
case ISAKMP_PAYLOAD_HASH:
return message_validate_hash(m, p);
case ISAKMP_PAYLOAD_SIG:
return message_validate_sig(m, p);
case ISAKMP_PAYLOAD_NONCE:
return message_validate_nonce(m, p);
case ISAKMP_PAYLOAD_NOTIFY:
return message_validate_notify(m, p);
case ISAKMP_PAYLOAD_DELETE:
return message_validate_delete(m, p);
case ISAKMP_PAYLOAD_VENDOR:
return message_validate_vendor(m, p);
case ISAKMP_PAYLOAD_ATTRIBUTE:
return message_validate_attribute(m, p);
case ISAKMP_PAYLOAD_NAT_D:
case ISAKMP_PAYLOAD_NAT_D_DRAFT:
return message_validate_nat_d(m, p);
case ISAKMP_PAYLOAD_NAT_OA:
case ISAKMP_PAYLOAD_NAT_OA_DRAFT:
return message_validate_nat_oa(m, p);
/* Not yet supported and any other unknown payloads. */
case ISAKMP_PAYLOAD_SAK:
case ISAKMP_PAYLOAD_SAT:
case ISAKMP_PAYLOAD_KD:
case ISAKMP_PAYLOAD_SEQ:
case ISAKMP_PAYLOAD_POP:
default:
break;
}
message_drop(m, ISAKMP_NOTIFY_INVALID_PAYLOAD_TYPE, 0, 1, 1);
return -1;
}
/* Check payloads for their required minimum size. */
static u_int16_t
message_payload_sz(u_int8_t payload)
{
switch (payload) {
case ISAKMP_PAYLOAD_SA:
return ISAKMP_SA_SZ;
case ISAKMP_PAYLOAD_PROPOSAL:
return ISAKMP_PROP_SZ;
case ISAKMP_PAYLOAD_TRANSFORM:
return ISAKMP_TRANSFORM_SZ;
case ISAKMP_PAYLOAD_KEY_EXCH:
return ISAKMP_KE_SZ;
case ISAKMP_PAYLOAD_ID:
return ISAKMP_ID_SZ;
case ISAKMP_PAYLOAD_CERT:
return ISAKMP_CERT_SZ;
case ISAKMP_PAYLOAD_CERT_REQ:
return ISAKMP_CERTREQ_SZ;
case ISAKMP_PAYLOAD_HASH:
return ISAKMP_HASH_SZ;
case ISAKMP_PAYLOAD_SIG:
return ISAKMP_SIG_SZ;
case ISAKMP_PAYLOAD_NONCE:
return ISAKMP_NONCE_SZ;
case ISAKMP_PAYLOAD_NOTIFY:
return ISAKMP_NOTIFY_SZ;
case ISAKMP_PAYLOAD_DELETE:
return ISAKMP_DELETE_SZ;
case ISAKMP_PAYLOAD_VENDOR:
return ISAKMP_VENDOR_SZ;
case ISAKMP_PAYLOAD_ATTRIBUTE:
return ISAKMP_ATTRIBUTE_SZ;
case ISAKMP_PAYLOAD_NAT_D:
case ISAKMP_PAYLOAD_NAT_D_DRAFT:
return ISAKMP_NAT_D_SZ;
case ISAKMP_PAYLOAD_NAT_OA:
case ISAKMP_PAYLOAD_NAT_OA_DRAFT:
return ISAKMP_NAT_OA_SZ;
/* Not yet supported and any other unknown payloads. */
case ISAKMP_PAYLOAD_SAK:
case ISAKMP_PAYLOAD_SAT:
case ISAKMP_PAYLOAD_KD:
case ISAKMP_PAYLOAD_SEQ:
case ISAKMP_PAYLOAD_POP:
default:
return 0;
}
}
/* Validate the attribute payload P in message MSG. */
static int
message_validate_attribute(struct message *msg, struct payload *p)
{
/* If we don't have an exchange yet, create one. */
if (!msg->exchange) {
if (zero_test((u_int8_t *) msg->iov[0].iov_base +
ISAKMP_HDR_MESSAGE_ID_OFF, ISAKMP_HDR_MESSAGE_ID_LEN))
msg->exchange = exchange_setup_p1(msg,
IPSEC_DOI_IPSEC);
else
msg->exchange = exchange_setup_p2(msg,
IPSEC_DOI_IPSEC);
if (!msg->exchange) {
log_print("message_validate_attribute: can not "
"create exchange");
message_free(msg);
return -1;
}
}
return 0;
}
/* Validate the certificate payload P in message MSG. */
static int
message_validate_cert(struct message *msg, struct payload *p)
{
if (GET_ISAKMP_CERT_ENCODING(p->p) >= ISAKMP_CERTENC_RESERVED_MIN) {
message_drop(msg, ISAKMP_NOTIFY_INVALID_CERT_ENCODING, 0, 1,
1);
return -1;
}
return 0;
}
/* Validate the certificate request payload P in message MSG. */
static int
message_validate_cert_req(struct message *msg, struct payload *p)
{
struct cert_handler *cert;
size_t len =
GET_ISAKMP_GEN_LENGTH(p->p) - ISAKMP_CERTREQ_AUTHORITY_OFF;
if (GET_ISAKMP_CERTREQ_TYPE(p->p) >= ISAKMP_CERTENC_RESERVED_MIN) {
message_drop(msg, ISAKMP_NOTIFY_INVALID_CERT_ENCODING, 0, 1,
1);
return -1;
}
/*
* Check the certificate types we support and if an acceptable
* authority is included in the payload check if it can be decoded
*/
cert = cert_get(GET_ISAKMP_CERTREQ_TYPE(p->p));
if (!cert || (len && !cert->certreq_validate(p->p +
ISAKMP_CERTREQ_AUTHORITY_OFF, len))) {
message_drop(msg, ISAKMP_NOTIFY_CERT_TYPE_UNSUPPORTED, 0, 1,
1);
return -1;
}
return 0;
}
/*
* Validate the delete payload P in message MSG. As a side-effect, create
* an exchange if we do not have one already.
*/
static int
message_validate_delete(struct message *msg, struct payload *p)
{
u_int8_t proto = GET_ISAKMP_DELETE_PROTO(p->p);
struct doi *doi;
struct sa *sa, *isakmp_sa;
struct sockaddr *dst, *dst_isa;
u_int32_t nspis = GET_ISAKMP_DELETE_NSPIS(p->p);
u_int8_t *spis = (u_int8_t *)p->p + ISAKMP_DELETE_SPI_OFF;
u_int32_t i;
char *addr;
/* Only accept authenticated DELETEs. */
if ((msg->flags & MSG_AUTHENTICATED) == 0) {
log_print("message_validate_delete: "
"got unauthenticated DELETE");
message_free(msg);
return -1;
}
doi = doi_lookup(GET_ISAKMP_DELETE_DOI(p->p));
if (!doi) {
log_print("message_validate_delete: DOI not supported");
message_free(msg);
return -1;
}
/* If we don't have an exchange yet, create one. */
if (!msg->exchange) {
if (zero_test((u_int8_t *) msg->iov[0].iov_base
+ ISAKMP_HDR_MESSAGE_ID_OFF, ISAKMP_HDR_MESSAGE_ID_LEN))
msg->exchange = exchange_setup_p1(msg, doi->id);
else
msg->exchange = exchange_setup_p2(msg, doi->id);
if (!msg->exchange) {
log_print("message_validate_delete: can not create "
"exchange");
message_free(msg);
return -1;
}
}
/* Only accept DELETE as part of an INFORMATIONAL exchange. */
if (msg->exchange->type != ISAKMP_EXCH_INFO) {
log_print("message_validate_delete: delete in exchange other "
"than INFO: %s", constant_name(isakmp_exch_cst,
msg->exchange->type));
message_free(msg);
return -1;
}
if (proto != ISAKMP_PROTO_ISAKMP && doi->validate_proto(proto)) {
log_print("message_validate_delete: protocol not supported");
message_free(msg);
return -1;
}
/* Validate the SPIs. */
for (i = 0; i < nspis; i++) {
/* Get ISAKMP SA protecting this message. */
isakmp_sa = msg->isakmp_sa;
if (!isakmp_sa) {
/* XXX should not happen? */
log_print("message_validate_delete: invalid spi (no "
"valid ISAKMP SA found)");
message_free(msg);
return -1;
}
isakmp_sa->transport->vtbl->get_dst(isakmp_sa->transport,
&dst_isa);
/* Get SA to be deleted. */
msg->transport->vtbl->get_dst(msg->transport, &dst);
if (proto == ISAKMP_PROTO_ISAKMP)
sa = sa_lookup_isakmp_sa(dst, spis + i
* ISAKMP_HDR_COOKIES_LEN);
else
sa = ipsec_sa_lookup(dst, ((u_int32_t *) spis)[i],
proto);
if (!sa) {
LOG_DBG((LOG_MESSAGE, 50, "message_validate_delete: "
"invalid spi (no valid SA found)"));
message_free(msg);
return -1;
}
sa->transport->vtbl->get_dst(sa->transport, &dst);
/* Destination addresses must match. */
if (dst->sa_family != dst_isa->sa_family ||
memcmp(sockaddr_addrdata(dst_isa), sockaddr_addrdata(dst),
sockaddr_addrlen(dst))) {
sockaddr2text(dst_isa, &addr, 0);
log_print("message_validate_delete: invalid spi "
"(illegal delete request from %s)", addr);
free(addr);
message_free(msg);
return -1;
}
}
return 0;
}
/*
* Validate the hash payload P in message MSG.
*/
static int
message_validate_hash(struct message *msg, struct payload *p)
{
struct sa *isakmp_sa = msg->isakmp_sa;
struct ipsec_sa *isa;
struct hash *hash;
struct payload *hashp = payload_first(msg, ISAKMP_PAYLOAD_HASH);
struct prf *prf;
u_int8_t *rest;
u_int8_t message_id[ISAKMP_HDR_MESSAGE_ID_LEN];
size_t rest_len;
/* active exchanges other than INFORMATIONAL validates hash payload. */
if (msg->exchange && (msg->exchange->type != ISAKMP_EXCH_INFO))
return 0;
if (isakmp_sa == NULL)
goto invalid;
isa = isakmp_sa->data;
hash = hash_get(isa->hash);
if (hash == NULL)
goto invalid;
/* If no SKEYID_a, we can not do anything (should not happen). */
if (!isa->skeyid_a)
goto invalid;
/* Allocate the prf and start calculating our HASH(1). */
LOG_DBG_BUF((LOG_MISC, 90, "message_validate_hash: SKEYID_a",
isa->skeyid_a, isa->skeyid_len));
prf = prf_alloc(isa->prf_type, hash->type, isa->skeyid_a,
isa->skeyid_len);
if (!prf) {
message_free(msg);
return -1;
}
/* This is not an active exchange. */
GET_ISAKMP_HDR_MESSAGE_ID(msg->iov[0].iov_base, message_id);
prf->Init(prf->prfctx);
LOG_DBG_BUF((LOG_MISC, 90, "message_validate_hash: message_id",
message_id, ISAKMP_HDR_MESSAGE_ID_LEN));
prf->Update(prf->prfctx, message_id, ISAKMP_HDR_MESSAGE_ID_LEN);
rest = hashp->p + GET_ISAKMP_GEN_LENGTH(hashp->p);
rest_len = (GET_ISAKMP_HDR_LENGTH(msg->iov[0].iov_base) - (rest -
(u_int8_t *)msg->iov[0].iov_base));
LOG_DBG_BUF((LOG_MISC, 90,
"message_validate_hash: payloads after HASH(1)", rest, rest_len));
prf->Update(prf->prfctx, rest, rest_len);
prf->Final(hash->digest, prf->prfctx);
prf_free(prf);
if (memcmp(hashp->p + ISAKMP_HASH_DATA_OFF, hash->digest,
hash->hashsize))
goto invalid;
/* Mark the HASH as handled. */
hashp->flags |= PL_MARK;
/* Mark message as authenticated. */
msg->flags |= MSG_AUTHENTICATED;
return 0;
invalid:
log_print("message_validate_hash: invalid hash information");
message_drop(msg, ISAKMP_NOTIFY_INVALID_HASH_INFORMATION, 0, 1, 1);
return -1;
}
/* Validate the identification payload P in message MSG. */
static int
message_validate_id(struct message *msg, struct payload *p)
{
struct exchange *exchange = msg->exchange;
size_t len = GET_ISAKMP_GEN_LENGTH(p->p);
if (!exchange) {
/* We should have an exchange at this point. */
log_print("message_validate_id: payload out of sequence");
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 1);
return -1;
}
if (exchange->doi &&
exchange->doi->validate_id_information(GET_ISAKMP_ID_TYPE(p->p),
p->p + ISAKMP_ID_DOI_DATA_OFF, p->p + ISAKMP_ID_DATA_OFF,
len - ISAKMP_ID_DATA_OFF, exchange)) {
message_drop(msg, ISAKMP_NOTIFY_INVALID_ID_INFORMATION, 0, 1,
1);
return -1;
}
return 0;
}
/* Validate the key exchange payload P in message MSG. */
static int
message_validate_key_exch(struct message *msg, struct payload *p)
{
struct exchange *exchange = msg->exchange;
size_t len = GET_ISAKMP_GEN_LENGTH(p->p);
if (!exchange) {
/* We should have an exchange at this point. */
log_print("message_validate_key_exch: "
"payload out of sequence");
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 1);
return -1;
}
if (exchange->doi && exchange->doi->validate_key_information(p->p +
ISAKMP_KE_DATA_OFF, len - ISAKMP_KE_DATA_OFF)) {
message_drop(msg, ISAKMP_NOTIFY_INVALID_KEY_INFORMATION,
0, 1, 1);
return -1;
}
return 0;
}
/* Validate the NAT-D payload P in message MSG. */
static int
message_validate_nat_d(struct message *msg, struct payload *p)
{
struct exchange *exchange = msg->exchange;
if (!exchange) {
/* We should have an exchange at this point. */
log_print("message_validate_nat_d: payload out of sequence");
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 1);
return -1;
}
if (exchange->phase != 1) {
log_print("message_validate_nat_d: "
"NAT-D payload must be in phase 1");
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 1);
return -1;
}
/* Mark as handled. */
p->flags |= PL_MARK;
return 0;
}
/* Validate the NAT-OA payload P in message MSG. */
static int
message_validate_nat_oa(struct message *msg, struct payload *p)
{
struct exchange *exchange = msg->exchange;
if (!exchange) {
/* We should have an exchange at this point. */
log_print("message_validate_nat_d: payload out of sequence");
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 1);
return -1;
}
#ifdef notyet /* XXX Probably never, due to patent issues. */
/* Mark as handled. */
p->flags |= PL_MARK;
#endif
return 0;
}
/* Validate the nonce payload P in message MSG. */
static int
message_validate_nonce(struct message *msg, struct payload *p)
{
if (!msg->exchange) {
/* We should have an exchange at this point. */
log_print("message_validate_nonce: payload out of sequence");
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 1);
return -1;
}
/* Nonces require no specific validation. */
return 0;
}
/*
* Validate the notify payload P in message MSG. As a side-effect, create
* an exchange if we do not have one already.
*/
static int
message_validate_notify(struct message *msg, struct payload *p)
{
u_int8_t proto = GET_ISAKMP_NOTIFY_PROTO(p->p);
u_int16_t type = GET_ISAKMP_NOTIFY_MSG_TYPE(p->p);
struct doi *doi;
doi = doi_lookup(GET_ISAKMP_NOTIFY_DOI(p->p));
if (!doi) {
log_print("message_validate_notify: DOI not supported");
message_free(msg);
return -1;
}
/* If we don't have an exchange yet, create one. */
if (!msg->exchange) {
if (zero_test((u_int8_t *) msg->iov[0].iov_base +
ISAKMP_HDR_MESSAGE_ID_OFF, ISAKMP_HDR_MESSAGE_ID_LEN))
msg->exchange = exchange_setup_p1(msg, doi->id);
else
msg->exchange = exchange_setup_p2(msg, doi->id);
if (!msg->exchange) {
log_print("message_validate_notify: can not create "
"exchange");
message_free(msg);
return -1;
}
}
if (proto != ISAKMP_PROTO_ISAKMP && doi->validate_proto(proto)) {
log_print("message_validate_notify: protocol not supported");
message_free(msg);
return -1;
}
/* Validate the SPI. XXX Just ISAKMP for now. */
if (proto == ISAKMP_PROTO_ISAKMP &&
GET_ISAKMP_NOTIFY_SPI_SZ(p->p) == ISAKMP_HDR_COOKIES_LEN &&
msg->isakmp_sa &&
memcmp(p->p + ISAKMP_NOTIFY_SPI_OFF, msg->isakmp_sa->cookies,
ISAKMP_HDR_COOKIES_LEN) != 0) {
log_print("message_validate_notify: bad cookies");
message_drop(msg, ISAKMP_NOTIFY_INVALID_SPI, 0, 1, 1);
return -1;
}
if (type < ISAKMP_NOTIFY_INVALID_PAYLOAD_TYPE ||
(type >= ISAKMP_NOTIFY_RESERVED_MIN &&
type < ISAKMP_NOTIFY_PRIVATE_MIN) ||
(type >= ISAKMP_NOTIFY_STATUS_RESERVED1_MIN &&
type <= ISAKMP_NOTIFY_STATUS_RESERVED1_MAX) ||
(type >= ISAKMP_NOTIFY_STATUS_DOI_MIN &&
type <= ISAKMP_NOTIFY_STATUS_DOI_MAX &&
doi->validate_notification(type)) ||
type >= ISAKMP_NOTIFY_STATUS_RESERVED2_MIN) {
log_print("message_validate_notify: "
"message type not supported");
message_free(msg);
return -1;
}
return 0;
}
/* Validate the proposal payload P in message MSG. */
static int
message_validate_proposal(struct message *msg, struct payload *p)
{
u_int8_t proto = GET_ISAKMP_PROP_PROTO(p->p);
u_int8_t *sa = p->context->p;
if (!msg->exchange) {
/* We should have an exchange at this point. */
log_print("message_validate_proposal: "
"payload out of sequence");
message_drop(msg, ISAKMP_NOTIFY_PAYLOAD_MALFORMED, 0, 1, 1);
return -1;
}
if (proto != ISAKMP_PROTO_ISAKMP &&
msg->exchange->doi->validate_proto(proto)) {
message_drop(msg, ISAKMP_NOTIFY_INVALID_PROTOCOL_ID, 0, 1, 1);
return -1;
}
/* Check that we get monotonically increasing proposal IDs per SA. */
if (sa != last_sa)
last_sa = sa;
else if (GET_ISAKMP_PROP_NO(p->p) < last_prop_no) {
message_drop(msg, ISAKMP_NOTIFY_BAD_PROPOSAL_SYNTAX, 0, 1, 1);
return -1;
}
last_prop_no = GET_ISAKMP_PROP_NO(p->p);
/* XXX Validate the SPI, and other syntactic things. */
return 0;
}
/*
* Validate the SA payload P in message MSG.
* Aside from normal validation, note what DOI is in use for other
* validation routines to look at. Also index the proposal payloads
* on the fly.
* XXX This assumes PAYLOAD_SA is always the first payload
* to be validated, which is true for IKE, except for quick mode where
* a PAYLOAD_HASH comes first, but in that specific case it does not matter.
* XXX Make sure the above comment is relevant, isn't SA always checked
* first due to the IANA assigned payload number?
*/
static int
message_validate_sa(struct message *msg, struct payload *p)
{
set payload_set;
size_t len;
u_int32_t doi_id;
struct exchange *exchange = msg->exchange;
u_int8_t *pkt = msg->iov[0].iov_base;
doi_id = GET_ISAKMP_SA_DOI(p->p);
if (!doi_lookup(doi_id)) {
log_print("message_validate_sa: DOI not supported");
message_drop(msg, ISAKMP_NOTIFY_DOI_NOT_SUPPORTED, 0, 1, 1);