5 // Copyright (c) 2017 Apple Inc. All rights reserved.
9 #include "crypto_cssm.h"
10 #include "racoon_certs_data.h"
11 #include "ipsec_doi.h"
12 #include "remoteconf.h"
17 #include "isakmp_cfg.h"
18 #include "racoon_types.h"
21 #include "vpn_control.h"
24 #include <TargetConditionals.h>
25 #include <Security/SecCertificate.h>
28 #include <net/pfkeyv2.h>
29 #include <netinet6/ipsec.h>
31 #define racoon_test_pass 0
32 #define racoon_test_failure 1
34 struct localconf
*lcconf
;
36 static struct option long_options
[] =
38 {"unit_test", no_argument
, 0, 'u'},
39 {"help" , no_argument
, 0, 'h'}
43 print_usage(char *name
)
45 printf("Usage: %s\n", name
);
46 printf(" -unit_test\n");
50 xauth_attr_reply(iph1
, attr
, id
)
51 phase1_handle_t
*iph1
;
52 struct isakmp_data
*attr
;
55 __builtin_unreachable();
60 isakmp_unity_reply(iph1
, attr
)
61 phase1_handle_t
*iph1
;
62 struct isakmp_data
*attr
;
64 __builtin_unreachable();
69 vpncontrol_notify_phase_change(int start
, u_int16_t from
, phase1_handle_t
*iph1
, phase2_handle_t
*iph2
)
71 __builtin_unreachable();
76 ike_session_newph2(unsigned int version
, int type
)
78 __builtin_unreachable();
83 ike_session_link_ph2_to_ph1 (phase1_handle_t
*iph1
, phase2_handle_t
*iph2
)
85 __builtin_unreachable();
90 isakmp_xauth_req(iph1
, attr
)
91 phase1_handle_t
*iph1
;
92 struct isakmp_data
*attr
;
94 __builtin_unreachable();
99 isakmp_ph2resend(iph2
)
100 phase2_handle_t
*iph2
;
102 __builtin_unreachable();
107 ike_session_start_xauth_timer (phase1_handle_t
*iph1
)
109 __builtin_unreachable();
114 isakmp_send(iph1
, sbuf
)
115 phase1_handle_t
*iph1
;
118 __builtin_unreachable();
123 ike_session_unlink_phase2 (phase2_handle_t
*iph2
)
125 __builtin_unreachable();
130 ike_session_add_recvdpkt(remote
, local
, sbuf
, rbuf
, non_esp
, frag_flags
)
131 struct sockaddr_storage
*remote
, *local
;
132 vchar_t
*sbuf
, *rbuf
;
134 u_int32_t frag_flags
;
136 __builtin_unreachable();
141 ike_session_is_client_ph1_rekey (phase1_handle_t
*iph1
)
147 vpncontrol_notify_need_authinfo(phase1_handle_t
*iph1
, void* attr_list
, size_t attr_len
)
149 __builtin_unreachable();
154 isakmp_newmsgid2(iph1
)
155 phase1_handle_t
*iph1
;
157 __builtin_unreachable();
162 fsm_set_state(int *var
, int state
)
164 __builtin_unreachable();
170 phase1_handle_t
*iph1
;
172 __builtin_unreachable();
177 ike_session_delph2(phase2_handle_t
*iph2
)
179 __builtin_unreachable();
184 isakmp_unity_req(iph1
, attr
)
185 phase1_handle_t
*iph1
;
186 struct isakmp_data
*attr
;
188 __builtin_unreachable();
193 isakmp_info_send_d1(phase1_handle_t
*iph1
)
195 __builtin_unreachable();
200 isakmp_xauth_set(iph1
, attr
)
201 phase1_handle_t
*iph1
;
202 struct isakmp_data
*attr
;
204 __builtin_unreachable();
209 isakmp_ph1expire(iph1
)
210 phase1_handle_t
*iph1
;
212 __builtin_unreachable();
217 check_auto_exit(void)
223 ipsecSessionTracerEvent (ike_session_t
*session
, ipsecSessionEventCode_t eventCode
, const char *event
, const char *failure_reason
)
225 __builtin_unreachable();
230 racoon_cert_validity_test(void)
232 int result
= racoon_test_pass
;
235 * Below tests are applicable only for embedded
236 * because the crypto_cssm_check_x509cert_dates()
237 * does nothing on osx.
239 cert_status_t cert_status
;
241 fprintf(stdout
, "[TEST] RacoonCertValidity\n");
243 // For certificate info, look at past_cert.der
244 fprintf(stdout
, "\t[BEGIN] ExpiredCertTest\n");
245 CFDataRef past_cert_data
= CFDataCreate(kCFAllocatorDefault
, past_cert_der
, sizeof(past_cert_der
));
246 SecCertificateRef past_cert_ref
= SecCertificateCreateWithData(NULL
, past_cert_data
);
247 cert_status
= crypto_cssm_check_x509cert_dates (past_cert_ref
);
248 if (cert_status
!= CERT_STATUS_EXPIRED
) {
249 fprintf(stdout
, "\t[FAIL] ExpiredCertTest\n");
250 result
= racoon_test_failure
;
252 fprintf(stdout
, "\t[PASS] ExpiredCertTest\n");
255 // For certificate info, look at future_cert.der
256 fprintf(stdout
, "\t[BEGIN] PrematureCertTest\n");
257 CFDataRef future_cert_data
= CFDataCreate(kCFAllocatorDefault
, future_cert_der
, sizeof(future_cert_der
));
258 SecCertificateRef future_cert_ref
= SecCertificateCreateWithData(NULL
, future_cert_data
);
259 cert_status
= crypto_cssm_check_x509cert_dates (future_cert_ref
);
260 if (cert_status
!= CERT_STATUS_PREMATURE
) {
261 fprintf(stdout
, "\t[FAIL] PrematureCertTest\n");
262 result
= racoon_test_failure
;
264 fprintf(stdout
, "\t[PASS] PrematureCertTest\n");
268 // For certificate info, look at valid_cert.der
269 fprintf(stdout
, "\t[BEGIN] ValidCertTest\n");
270 CFDataRef valid_cert_data
= CFDataCreate(kCFAllocatorDefault
, valid_cert_der
, sizeof(valid_cert_der
));
271 SecCertificateRef valid_cert_ref
= SecCertificateCreateWithData(NULL
, valid_cert_data
);
272 cert_status
= crypto_cssm_check_x509cert_dates (valid_cert_ref
);
273 if (cert_status
!= CERT_STATUS_OK
) {
274 fprintf(stdout
, "\t[FAIL] ValidCertTest\n");
275 result
= racoon_test_failure
;
277 fprintf(stdout
, "\t[PASS] ValidCertTest\n");
279 #endif // HAVE_OPENSSL
284 racoon_test_overflow_transform_attributes(void)
286 #define TEST_GEN_PAYLOAD_LEN 4
287 void *payload
= NULL
;
289 // Test ISAKMP overflow
290 payload
= malloc(2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
));
291 if (payload
== NULL
) {
292 fprintf(stdout
, "malloc failed");
293 return racoon_test_failure
;
295 memset(payload
, 0, (2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
)));
297 fprintf(stdout
, "\t[BEGIN] TransformPayloadTest\n");
299 struct isakmp_pl_t
*trans
= (struct isakmp_pl_t
*)payload
;
300 struct isakmp_data
*data
= (struct isakmp_data
*)(trans
+ 1);
301 uint32_t *gen1
= (uint32_t *)(data
+ 1);
302 struct isakmpsa sa
= {0};
304 data
->type
= htons(OAKLEY_ATTR_GRP_GEN_ONE
);
305 data
->lorv
= htons(TEST_GEN_PAYLOAD_LEN
);
306 *gen1
= htonl(0x11111111);
308 // Transform header total length shorter than data header
309 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
310 if (t2isakmpsa(trans
, &sa
) == 0) {
311 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
312 return racoon_test_failure
;
315 // Transform header total length shorter than data header + payload without flag
316 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
317 if (t2isakmpsa(trans
, &sa
) == 0) {
318 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
319 return racoon_test_failure
;
322 // Transform header total length equal to data header + payload without flag
323 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
);
324 if (t2isakmpsa(trans
, &sa
) < 0) {
325 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
326 return racoon_test_failure
;
329 // Transform header total length shorter than data header + payload with flag set
330 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
331 data
->type
= htons(OAKLEY_ATTR_GRP_GEN_ONE
| ISAKMP_GEN_MASK
);
332 if (t2isakmpsa(trans
, &sa
) == 0) {
333 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
334 return racoon_test_failure
;
337 // Transform header total length shorter than data header + payload with flag set for Gen 2
338 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
339 data
->type
= htons(OAKLEY_ATTR_GRP_GEN_TWO
| ISAKMP_GEN_MASK
);
340 if (t2isakmpsa(trans
, &sa
) == 0) {
341 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
342 return racoon_test_failure
;
345 // Transform header total length shorter than data header + payload with flag set for Encryption
346 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) );
347 data
->type
= htons(OAKLEY_ATTR_ENC_ALG
);
348 if (t2isakmpsa(trans
, &sa
) == 0) {
349 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
350 return racoon_test_failure
;
353 // Transform header total length equal to data header + payload with flag set for Encryption
354 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
355 data
->type
= htons(OAKLEY_ATTR_ENC_ALG
| ISAKMP_GEN_MASK
);
356 if (t2isakmpsa(trans
, &sa
) < 0) {
357 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
358 return racoon_test_failure
;
361 // Transform header total length shorter than 2 * data header + payload with flag set for Encryption
362 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + sizeof(struct isakmp_data
) - 1);
363 data
->type
= htons(OAKLEY_ATTR_ENC_ALG
| ISAKMP_GEN_MASK
);
364 if (t2isakmpsa(trans
, &sa
) == 0) {
365 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
366 return racoon_test_failure
;
369 fprintf(stdout
, "\t[PASS] TransformPayloadTest\n");
370 return racoon_test_pass
;
374 racoon_test_overflow_attribute(void)
376 void *payload
= NULL
;
378 #define TEST_GEN_PAYLOAD_LEN 4
380 payload
= malloc(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
381 if (payload
== NULL
) {
382 fprintf(stdout
, "malloc failed");
383 return racoon_test_failure
;
385 memset(payload
, 0, sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
387 fprintf(stdout
, "\t[BEGIN] AttributeTest\n");
389 struct isakmp_pl_t
*trans
= (struct isakmp_pl_t
*)payload
;
390 struct isakmp_data
*data
= (struct isakmp_data
*)(trans
+ 1);
392 data
->type
= htons(OAKLEY_ATTR_GRP_GEN_ONE
);
393 data
->lorv
= htons(TEST_GEN_PAYLOAD_LEN
);
395 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
396 if (check_attr_isakmp(trans
) == 0) {
397 fprintf(stdout
, "\t[FAIL] AttributeTest\n");
398 return racoon_test_failure
;
401 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
- 1);
402 if (check_attr_isakmp(trans
) == 0) {
403 fprintf(stdout
, "\t[FAIL] AttributeTest\n");
404 return racoon_test_failure
;
407 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
);
408 if (check_attr_isakmp(trans
) < 0) {
409 fprintf(stdout
, "\t[FAIL] AttributeTest\n");
410 return racoon_test_failure
;
413 fprintf(stdout
, "\t[PASS] AttributeTest\n");
414 return racoon_test_pass
;
418 racoon_test_overflow_ipsec_attribute(void)
420 void *payload
= NULL
;
421 #define LA_PAYLOAD_LEN 4
423 payload
= malloc(2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
));
424 if (payload
== NULL
) {
425 fprintf(stdout
, "malloc failed");
426 return racoon_test_failure
;
428 memset(payload
, 0, (2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
)));
430 fprintf(stdout
, "\t[BEGIN] AttributeIPsecTest\n");
432 struct isakmp_pl_t
*trans
= (struct isakmp_pl_t
*)payload
;
433 struct isakmp_data
*data
= (struct isakmp_data
*)(trans
+ 1);
435 data
->type
= htons(IPSECDOI_ATTR_SA_LD
);
436 data
->lorv
= htons(LA_PAYLOAD_LEN
);
438 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
439 if (check_attr_ipsec(IPSECDOI_PROTO_IPSEC_AH
, trans
) == 0) {
440 fprintf(stdout
, "\t[FAIL] AttributeIPsecTest\n");
441 return racoon_test_failure
;
444 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
- 1);
445 if (check_attr_ipsec(IPSECDOI_PROTO_IPSEC_AH
, trans
) == 0) {
446 fprintf(stdout
, "\t[FAIL] AttributeIPsecTest\n");
447 return racoon_test_failure
;
450 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
+ sizeof(struct isakmp_data
));
451 struct isakmp_data
*auth_data
= (struct isakmp_data
*)((uint8_t *)data
+ sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
);
452 auth_data
->type
= htons(IPSECDOI_ATTR_AUTH
| ISAKMP_GEN_MASK
);
453 auth_data
->lorv
= htons(IPSECDOI_ATTR_AUTH_HMAC_MD5
);
454 trans
->t_id
= IPSECDOI_AH_MD5
;
455 if (check_attr_ipsec(IPSECDOI_PROTO_IPSEC_AH
, trans
) < 0) {
456 fprintf(stdout
, "\t[FAIL] AttributeIPsecTest\n");
457 return racoon_test_failure
;
460 fprintf(stdout
, "\t[PASS] AttributeIPsecTest\n");
461 return racoon_test_pass
;
465 racoon_test_overflow_ipcomp_attribute(void)
467 void *payload
= NULL
;
468 #define LA_PAYLOAD_LEN 4
470 payload
= malloc(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
471 if (payload
== NULL
) {
472 fprintf(stdout
, "malloc failed");
473 return racoon_test_failure
;
475 memset(payload
, 0, sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
477 fprintf(stdout
, "\t[BEGIN] AttributeIPCOMPTest\n");
479 struct isakmp_pl_t
*trans
= (struct isakmp_pl_t
*)payload
;
480 struct isakmp_data
*data
= (struct isakmp_data
*)(trans
+ 1);
482 data
->type
= htons(IPSECDOI_ATTR_SA_LD
);
483 data
->lorv
= htons(LA_PAYLOAD_LEN
);
485 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
486 if (check_attr_ipcomp(trans
) == 0) {
487 fprintf(stdout
, "\t[FAIL] AttributeIPCOMPTest\n");
488 return racoon_test_failure
;
491 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
- 1);
492 if (check_attr_ipcomp(trans
) == 0) {
493 fprintf(stdout
, "\t[FAIL] AttributeIPCOMPTest\n");
494 return racoon_test_failure
;
497 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
);
498 if (check_attr_ipcomp(trans
) < 0) {
499 fprintf(stdout
, "\t[FAIL] AttributeIPCOMPTest\n");
500 return racoon_test_failure
;
503 fprintf(stdout
, "\t[PASS] AttributeIPCOMPTest\n");
504 return racoon_test_pass
;
508 racoon_test_overflow_proposal(void)
510 void *payload
= NULL
;
512 #define LD_PAYLOAD_LEN 4
514 payload
= malloc(2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LD_PAYLOAD_LEN
));
515 if (payload
== NULL
) {
516 fprintf(stdout
, "malloc failed");
517 return racoon_test_failure
;
519 memset(payload
, 0, (2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LD_PAYLOAD_LEN
)));
521 fprintf(stdout
, "\t[BEGIN] ProposalTest\n");
523 struct isakmp_pl_t
*ik_payload
= (struct isakmp_pl_t
*)payload
;
524 struct isakmp_data
*data
= (struct isakmp_data
*)(ik_payload
+ 1);
525 struct saprop pp
= {0};
526 struct saproto pr
= {0};
527 struct satrns tr
= {0};
529 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
530 if (ipsecdoi_t2satrns(ik_payload
, &pp
, &pr
, &tr
) == 0) {
531 fprintf(stdout
, "\t[FAIL] ProposalTest\n");
532 return racoon_test_failure
;
535 data
->type
= htons(IPSECDOI_ATTR_SA_LD_TYPE
| ISAKMP_GEN_MASK
);
536 data
->lorv
= htons(IPSECDOI_ATTR_SA_LD_TYPE_SEC
);
538 struct isakmp_data
*ld_data
= data
+ 1;
539 ld_data
->type
= htons(IPSECDOI_ATTR_SA_LD
);
540 ld_data
->lorv
= htons(LD_PAYLOAD_LEN
);
541 uint32_t *ld_payload
= (uint32_t *)(ld_data
+ 1);
542 *ld_payload
= 0x1111;
543 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + sizeof(struct isakmp_data
) + LD_PAYLOAD_LEN
- 1);
544 if (ipsecdoi_t2satrns(ik_payload
, &pp
, &pr
, &tr
) == 0) {
545 fprintf(stdout
, "\t[FAIL] ProposalTest\n");
546 return racoon_test_failure
;
549 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + sizeof(struct isakmp_data
) + LD_PAYLOAD_LEN
);
550 if (ipsecdoi_t2satrns(ik_payload
, &pp
, &pr
, &tr
) < 0) {
551 fprintf(stdout
, "\t[FAIL] ProposalTest\n");
552 return racoon_test_failure
;
555 data
->type
= htons(IPSECDOI_ATTR_AUTH
);
556 data
->lorv
= htons(IPSECDOI_ATTR_AUTH_HMAC_SHA1_96
);
558 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + sizeof(struct isakmp_data
));
559 if (ipsecdoi_t2satrns(ik_payload
, &pp
, &pr
, &tr
) == 0) {
560 fprintf(stdout
, "\t[FAIL] ProposalTest\n");
561 return racoon_test_failure
;
564 fprintf(stdout
, "\t[PASS] ProposalTest\n");
565 return racoon_test_pass
;
569 racoon_test_overflow_config_reply(void)
571 void *payload
= NULL
;
573 #define DUMMY_PAYLOAD_LEN 20
575 payload
= malloc(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
576 if (payload
== NULL
) {
577 fprintf(stdout
, "malloc failed");
578 return racoon_test_failure
;
580 memset(payload
, 0, sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
582 fprintf(stdout
, "\t[BEGIN] ConfigReplyTest\n");
584 struct isakmp_pl_attr
*ik_payload
= (struct isakmp_pl_attr
*)payload
;
585 struct isakmp_data
*data
= (struct isakmp_data
*)(ik_payload
+ 1);
586 phase1_handle_t iph1
= {0};
587 struct isakmp_cfg_state mode_cfg
= {0};
588 iph1
.mode_cfg
= &mode_cfg
;
590 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) - 1);
591 if (isakmp_cfg_reply(&iph1
, ik_payload
) == 0) {
592 fprintf(stdout
, "\t[FAIL] ConfigReplyTest\n");
593 return racoon_test_failure
;
596 data
->type
= htons(INTERNAL_IP4_SUBNET
);
597 data
->lorv
= htons(DUMMY_PAYLOAD_LEN
);
598 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
));
599 if (isakmp_cfg_reply(&iph1
, ik_payload
) == 0) {
600 fprintf(stdout
, "\t[FAIL] ConfigReplyTest\n");
601 return racoon_test_failure
;
604 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
605 if (isakmp_cfg_reply(&iph1
, ik_payload
) < 0) {
606 fprintf(stdout
, "\t[FAIL] ConfigReplyTest\n");
607 return racoon_test_failure
;
610 fprintf(stdout
, "\t[PASS] ConfigReplyTest\n");
611 return racoon_test_pass
;
615 racoon_test_overflow_config_request(void)
617 void *payload
= NULL
;
619 #define DUMMY_PAYLOAD_LEN 20
621 payload
= malloc(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
622 if (payload
== NULL
) {
623 fprintf(stdout
, "malloc failed");
624 return racoon_test_failure
;
626 memset(payload
, 0, sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
628 fprintf(stdout
, "\t[BEGIN] ConfigRequestTest\n");
630 struct isakmp_pl_attr
*ik_payload
= (struct isakmp_pl_attr
*)payload
;
631 struct isakmp_data
*data
= (struct isakmp_data
*)(ik_payload
+ 1);
632 phase1_handle_t iph1
= {0};
633 struct isakmp_cfg_state mode_cfg
= {0};
635 iph1
.mode_cfg
= &mode_cfg
;
637 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) - 1);
638 if (isakmp_cfg_request(&iph1
, ik_payload
, &msg
) == 0) {
639 fprintf(stdout
, "\t[FAIL] ConfigRequestTest\n");
640 return racoon_test_failure
;
643 data
->type
= htons(INTERNAL_ADDRESS_EXPIRY
);
644 data
->lorv
= htons(DUMMY_PAYLOAD_LEN
);
645 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
));
646 if (isakmp_cfg_request(&iph1
, ik_payload
, &msg
) == 0) {
647 fprintf(stdout
, "\t[FAIL] ConfigRequestTest\n");
648 return racoon_test_failure
;
651 fprintf(stdout
, "\t[PASS] ConfigRequestTest\n");
652 return racoon_test_pass
;
656 racoon_test_overflow_config_set(void)
658 void *payload
= NULL
;
660 #define DUMMY_PAYLOAD_LEN 20
662 payload
= malloc(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
663 if (payload
== NULL
) {
664 fprintf(stdout
, "malloc failed");
665 return racoon_test_failure
;
667 memset(payload
, 0, sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
669 fprintf(stdout
, "\t[BEGIN] ConfigRequestSetTest\n");
671 struct isakmp_pl_attr
*ik_payload
= (struct isakmp_pl_attr
*)payload
;
672 struct isakmp_data
*data
= (struct isakmp_data
*)(ik_payload
+ 1);
673 phase1_handle_t iph1
= {0};
674 struct isakmp_cfg_state mode_cfg
= {0};
676 iph1
.mode_cfg
= &mode_cfg
;
678 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) - 1);
679 if (isakmp_cfg_set(&iph1
, ik_payload
, &msg
) == 0) {
680 fprintf(stdout
, "\t[FAIL] ConfigRequestSetTest\n");
681 return racoon_test_failure
;
684 data
->type
= htons(XAUTH_CHALLENGE
);
685 data
->lorv
= htons(DUMMY_PAYLOAD_LEN
);
686 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
));
687 if (isakmp_cfg_set(&iph1
, ik_payload
, &msg
) == 0) {
688 fprintf(stdout
, "\t[FAIL] ConfigRequestSetTest\n");
689 return racoon_test_failure
;
692 fprintf(stdout
, "\t[PASS] ConfigRequestSetTest\n");
693 return racoon_test_pass
;
697 racoon_test_overflow_pfkey_add_sp(void)
699 void *mhp
[SADB_EXT_MAX
];
701 memset(mhp
, 0, sizeof(mhp
));
703 fprintf(stdout
, "\t[BEGIN] PFKeyAddSPTest\n");
705 struct sadb_address saddr
= {0};
706 struct sadb_address daddr
= {0};
707 struct sadb_x_policy
*xpl
= (struct sadb_x_policy
*)malloc(sizeof(*xpl
) + sizeof(struct sadb_x_ipsecrequest
) + 20 + 20);
709 fprintf(stdout
, "malloc failed");
710 return racoon_test_failure
;
712 memset(xpl
, 0, sizeof(*xpl
) + sizeof(struct sadb_x_ipsecrequest
));
714 mhp
[SADB_EXT_ADDRESS_SRC
] = (void *)&saddr
;
715 mhp
[SADB_EXT_ADDRESS_DST
] = (void *)&daddr
;
716 mhp
[SADB_X_EXT_POLICY
] = (void *)xpl
;
718 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) - 1);
719 if (addnewsp(&mhp
) == 0) {
720 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
721 return racoon_test_failure
;
724 xpl
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
725 struct sadb_x_ipsecrequest
*xisr
= xpl
+ 1;
726 xisr
->sadb_x_ipsecrequest_len
= sizeof(*xisr
) + 20;
727 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) + sizeof(*xisr
));
728 if (addnewsp(&mhp
) == 0) {
729 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
730 return racoon_test_failure
;
733 struct sockaddr
*srcaddr
= (struct sockaddr
*)(xisr
+ 1);
734 srcaddr
->sa_len
= 20;
735 struct sockaddr
*dstaddr
= ((uint8_t *)(srcaddr
) + 20);
736 dstaddr
->sa_len
= 20;
738 xisr
->sadb_x_ipsecrequest_proto
= IPPROTO_ESP
;
739 xisr
->sadb_x_ipsecrequest_mode
= IPSEC_MODE_TRANSPORT
;
740 xisr
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_DEFAULT
;
742 xisr
->sadb_x_ipsecrequest_len
= sizeof(*xisr
) + sizeof(struct sockaddr
) - 1;
743 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) + xisr
->sadb_x_ipsecrequest_len
+ 1);
744 if (addnewsp(&mhp
) == 0) {
745 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
746 return racoon_test_failure
;
749 xisr
->sadb_x_ipsecrequest_len
= sizeof(*xisr
) + srcaddr
->sa_len
- 1;
750 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) + xisr
->sadb_x_ipsecrequest_len
+ 8);
751 if (addnewsp(&mhp
) == 0) {
752 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
753 return racoon_test_failure
;
756 xisr
->sadb_x_ipsecrequest_len
= sizeof(*xisr
) + srcaddr
->sa_len
+ sizeof(struct sockaddr
) - 1;
757 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) + xisr
->sadb_x_ipsecrequest_len
+ 8);
758 if (addnewsp(&mhp
) == 0) {
759 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
760 return racoon_test_failure
;
763 xisr
->sadb_x_ipsecrequest_len
= sizeof(*xisr
) + srcaddr
->sa_len
+ dstaddr
->sa_len
- 1;
764 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) + xisr
->sadb_x_ipsecrequest_len
+ 8);
765 if (addnewsp(&mhp
) == 0) {
766 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
767 return racoon_test_failure
;
770 fprintf(stdout
, "\t[PASS] PFKeyAddSPTest\n");
771 return racoon_test_pass
;
775 racoon_test_overflow(void)
777 fprintf(stdout
, "[TEST] Racoon Overflow\n");
779 if (racoon_test_overflow_transform_attributes() == racoon_test_failure
) {
780 return racoon_test_failure
;
783 if (racoon_test_overflow_attribute() == racoon_test_failure
) {
784 return racoon_test_failure
;
787 if (racoon_test_overflow_ipsec_attribute() == racoon_test_failure
) {
788 return racoon_test_failure
;
791 if (racoon_test_overflow_ipcomp_attribute() == racoon_test_failure
) {
792 return racoon_test_failure
;
795 if (racoon_test_overflow_proposal() == racoon_test_failure
) {
796 return racoon_test_failure
;
799 if (racoon_test_overflow_config_reply() == racoon_test_failure
) {
800 return racoon_test_failure
;
803 if (racoon_test_overflow_config_request() == racoon_test_failure
) {
804 return racoon_test_failure
;
807 if (racoon_test_overflow_config_set() == racoon_test_failure
) {
808 return racoon_test_failure
;
811 if (racoon_test_overflow_pfkey_add_sp() == racoon_test_failure
) {
812 return racoon_test_failure
;
815 return racoon_test_pass
;
819 racoon_unit_test(void)
821 int result
= racoon_test_pass
;
823 if (racoon_cert_validity_test() == racoon_test_failure
) {
824 result
= racoon_test_failure
;
825 } else if (racoon_test_overflow() == racoon_test_failure
) {
826 result
= racoon_test_failure
;
829 if (result
== racoon_test_pass
) {
830 fprintf(stdout
, "\nAll Tests Passed\n\n");
835 main(int argc
, char *argv
[])
840 plog_ne_log_enabled
= 1;
843 print_usage(argv
[0]);
847 while ((opt
= getopt_long_only(argc
, argv
, "", long_options
, &opt_index
)) != -1) {
857 print_usage(argv
[0]);