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 racoon_cert_validity_test(void)
225 int result
= racoon_test_pass
;
228 * Below tests are applicable only for embedded
229 * because the crypto_cssm_check_x509cert_dates()
230 * does nothing on osx.
232 cert_status_t cert_status
;
234 fprintf(stdout
, "[TEST] RacoonCertValidity\n");
236 // For certificate info, look at past_cert.der
237 fprintf(stdout
, "\t[BEGIN] ExpiredCertTest\n");
238 CFDataRef past_cert_data
= CFDataCreate(kCFAllocatorDefault
, past_cert_der
, sizeof(past_cert_der
));
239 SecCertificateRef past_cert_ref
= SecCertificateCreateWithData(NULL
, past_cert_data
);
240 cert_status
= crypto_cssm_check_x509cert_dates (past_cert_ref
);
241 if (cert_status
!= CERT_STATUS_EXPIRED
) {
242 fprintf(stdout
, "\t[FAIL] ExpiredCertTest\n");
243 result
= racoon_test_failure
;
245 fprintf(stdout
, "\t[PASS] ExpiredCertTest\n");
248 // For certificate info, look at future_cert.der
249 fprintf(stdout
, "\t[BEGIN] PrematureCertTest\n");
250 CFDataRef future_cert_data
= CFDataCreate(kCFAllocatorDefault
, future_cert_der
, sizeof(future_cert_der
));
251 SecCertificateRef future_cert_ref
= SecCertificateCreateWithData(NULL
, future_cert_data
);
252 cert_status
= crypto_cssm_check_x509cert_dates (future_cert_ref
);
253 if (cert_status
!= CERT_STATUS_PREMATURE
) {
254 fprintf(stdout
, "\t[FAIL] PrematureCertTest\n");
255 result
= racoon_test_failure
;
257 fprintf(stdout
, "\t[PASS] PrematureCertTest\n");
261 // For certificate info, look at valid_cert.der
262 fprintf(stdout
, "\t[BEGIN] ValidCertTest\n");
263 CFDataRef valid_cert_data
= CFDataCreate(kCFAllocatorDefault
, valid_cert_der
, sizeof(valid_cert_der
));
264 SecCertificateRef valid_cert_ref
= SecCertificateCreateWithData(NULL
, valid_cert_data
);
265 cert_status
= crypto_cssm_check_x509cert_dates (valid_cert_ref
);
266 if (cert_status
!= CERT_STATUS_OK
) {
267 fprintf(stdout
, "\t[FAIL] ValidCertTest\n");
268 result
= racoon_test_failure
;
270 fprintf(stdout
, "\t[PASS] ValidCertTest\n");
272 #endif // HAVE_OPENSSL
277 racoon_test_overflow_transform_attributes(void)
279 #define TEST_GEN_PAYLOAD_LEN 4
280 void *payload
= NULL
;
282 // Test ISAKMP overflow
283 payload
= malloc(2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
));
284 if (payload
== NULL
) {
285 fprintf(stdout
, "malloc failed");
286 return racoon_test_failure
;
288 memset(payload
, 0, (2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
)));
290 fprintf(stdout
, "\t[BEGIN] TransformPayloadTest\n");
292 struct isakmp_pl_t
*trans
= (struct isakmp_pl_t
*)payload
;
293 struct isakmp_data
*data
= (struct isakmp_data
*)(trans
+ 1);
294 uint32_t *gen1
= (uint32_t *)(data
+ 1);
295 struct isakmpsa sa
= {0};
297 data
->type
= htons(OAKLEY_ATTR_GRP_GEN_ONE
);
298 data
->lorv
= htons(TEST_GEN_PAYLOAD_LEN
);
299 *gen1
= htonl(0x11111111);
301 // Transform header total length shorter than data header
302 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
303 if (t2isakmpsa(trans
, &sa
) == 0) {
304 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
305 return racoon_test_failure
;
308 // Transform header total length shorter than data header + payload without flag
309 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
310 if (t2isakmpsa(trans
, &sa
) == 0) {
311 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
312 return racoon_test_failure
;
315 // Transform header total length equal to data header + payload without flag
316 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
);
317 if (t2isakmpsa(trans
, &sa
) < 0) {
318 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
319 return racoon_test_failure
;
322 // Transform header total length shorter than data header + payload with flag set
323 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
324 data
->type
= htons(OAKLEY_ATTR_GRP_GEN_ONE
| ISAKMP_GEN_MASK
);
325 if (t2isakmpsa(trans
, &sa
) == 0) {
326 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
327 return racoon_test_failure
;
330 // Transform header total length shorter than data header + payload with flag set for Gen 2
331 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
332 data
->type
= htons(OAKLEY_ATTR_GRP_GEN_TWO
| ISAKMP_GEN_MASK
);
333 if (t2isakmpsa(trans
, &sa
) == 0) {
334 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
335 return racoon_test_failure
;
338 // Transform header total length shorter than data header + payload with flag set for Encryption
339 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) );
340 data
->type
= htons(OAKLEY_ATTR_ENC_ALG
);
341 if (t2isakmpsa(trans
, &sa
) == 0) {
342 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
343 return racoon_test_failure
;
346 // Transform header total length equal to data header + payload with flag set for Encryption
347 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
348 data
->type
= htons(OAKLEY_ATTR_ENC_ALG
| ISAKMP_GEN_MASK
);
349 if (t2isakmpsa(trans
, &sa
) < 0) {
350 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
351 return racoon_test_failure
;
354 // Transform header total length shorter than 2 * data header + payload with flag set for Encryption
355 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + sizeof(struct isakmp_data
) - 1);
356 data
->type
= htons(OAKLEY_ATTR_ENC_ALG
| ISAKMP_GEN_MASK
);
357 if (t2isakmpsa(trans
, &sa
) == 0) {
358 fprintf(stdout
, "\t[FAIL] TransformPayloadTest\n");
359 return racoon_test_failure
;
362 fprintf(stdout
, "\t[PASS] TransformPayloadTest\n");
363 return racoon_test_pass
;
367 racoon_test_overflow_attribute(void)
369 void *payload
= NULL
;
371 #define TEST_GEN_PAYLOAD_LEN 4
373 payload
= malloc(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
374 if (payload
== NULL
) {
375 fprintf(stdout
, "malloc failed");
376 return racoon_test_failure
;
378 memset(payload
, 0, sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
380 fprintf(stdout
, "\t[BEGIN] AttributeTest\n");
382 struct isakmp_pl_t
*trans
= (struct isakmp_pl_t
*)payload
;
383 struct isakmp_data
*data
= (struct isakmp_data
*)(trans
+ 1);
385 data
->type
= htons(OAKLEY_ATTR_GRP_GEN_ONE
);
386 data
->lorv
= htons(TEST_GEN_PAYLOAD_LEN
);
388 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
389 if (check_attr_isakmp(trans
) == 0) {
390 fprintf(stdout
, "\t[FAIL] AttributeTest\n");
391 return racoon_test_failure
;
394 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
- 1);
395 if (check_attr_isakmp(trans
) == 0) {
396 fprintf(stdout
, "\t[FAIL] AttributeTest\n");
397 return racoon_test_failure
;
400 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + TEST_GEN_PAYLOAD_LEN
);
401 if (check_attr_isakmp(trans
) < 0) {
402 fprintf(stdout
, "\t[FAIL] AttributeTest\n");
403 return racoon_test_failure
;
406 fprintf(stdout
, "\t[PASS] AttributeTest\n");
407 return racoon_test_pass
;
411 racoon_test_overflow_ipsec_attribute(void)
413 void *payload
= NULL
;
414 #define LA_PAYLOAD_LEN 4
416 payload
= malloc(2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
));
417 if (payload
== NULL
) {
418 fprintf(stdout
, "malloc failed");
419 return racoon_test_failure
;
421 memset(payload
, 0, (2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
)));
423 fprintf(stdout
, "\t[BEGIN] AttributeIPsecTest\n");
425 struct isakmp_pl_t
*trans
= (struct isakmp_pl_t
*)payload
;
426 struct isakmp_data
*data
= (struct isakmp_data
*)(trans
+ 1);
428 data
->type
= htons(IPSECDOI_ATTR_SA_LD
);
429 data
->lorv
= htons(LA_PAYLOAD_LEN
);
431 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
432 if (check_attr_ipsec(IPSECDOI_PROTO_IPSEC_AH
, trans
) == 0) {
433 fprintf(stdout
, "\t[FAIL] AttributeIPsecTest\n");
434 return racoon_test_failure
;
437 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
- 1);
438 if (check_attr_ipsec(IPSECDOI_PROTO_IPSEC_AH
, trans
) == 0) {
439 fprintf(stdout
, "\t[FAIL] AttributeIPsecTest\n");
440 return racoon_test_failure
;
443 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
+ sizeof(struct isakmp_data
));
444 struct isakmp_data
*auth_data
= (struct isakmp_data
*)((uint8_t *)data
+ sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
);
445 auth_data
->type
= htons(IPSECDOI_ATTR_AUTH
| ISAKMP_GEN_MASK
);
446 auth_data
->lorv
= htons(IPSECDOI_ATTR_AUTH_HMAC_MD5
);
447 trans
->t_id
= IPSECDOI_AH_MD5
;
448 if (check_attr_ipsec(IPSECDOI_PROTO_IPSEC_AH
, trans
) < 0) {
449 fprintf(stdout
, "\t[FAIL] AttributeIPsecTest\n");
450 return racoon_test_failure
;
453 fprintf(stdout
, "\t[PASS] AttributeIPsecTest\n");
454 return racoon_test_pass
;
458 racoon_test_overflow_ipcomp_attribute(void)
460 void *payload
= NULL
;
461 #define LA_PAYLOAD_LEN 4
463 payload
= malloc(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
464 if (payload
== NULL
) {
465 fprintf(stdout
, "malloc failed");
466 return racoon_test_failure
;
468 memset(payload
, 0, sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
));
470 fprintf(stdout
, "\t[BEGIN] AttributeIPCOMPTest\n");
472 struct isakmp_pl_t
*trans
= (struct isakmp_pl_t
*)payload
;
473 struct isakmp_data
*data
= (struct isakmp_data
*)(trans
+ 1);
475 data
->type
= htons(IPSECDOI_ATTR_SA_LD
);
476 data
->lorv
= htons(LA_PAYLOAD_LEN
);
478 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
479 if (check_attr_ipcomp(trans
) == 0) {
480 fprintf(stdout
, "\t[FAIL] AttributeIPCOMPTest\n");
481 return racoon_test_failure
;
484 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
- 1);
485 if (check_attr_ipcomp(trans
) == 0) {
486 fprintf(stdout
, "\t[FAIL] AttributeIPCOMPTest\n");
487 return racoon_test_failure
;
490 trans
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LA_PAYLOAD_LEN
);
491 if (check_attr_ipcomp(trans
) < 0) {
492 fprintf(stdout
, "\t[FAIL] AttributeIPCOMPTest\n");
493 return racoon_test_failure
;
496 fprintf(stdout
, "\t[PASS] AttributeIPCOMPTest\n");
497 return racoon_test_pass
;
501 racoon_test_overflow_proposal(void)
503 void *payload
= NULL
;
505 #define LD_PAYLOAD_LEN 4
507 payload
= malloc(2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LD_PAYLOAD_LEN
));
508 if (payload
== NULL
) {
509 fprintf(stdout
, "malloc failed");
510 return racoon_test_failure
;
512 memset(payload
, 0, (2 * (sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + LD_PAYLOAD_LEN
)));
514 fprintf(stdout
, "\t[BEGIN] ProposalTest\n");
516 struct isakmp_pl_t
*ik_payload
= (struct isakmp_pl_t
*)payload
;
517 struct isakmp_data
*data
= (struct isakmp_data
*)(ik_payload
+ 1);
518 struct saprop pp
= {0};
519 struct saproto pr
= {0};
520 struct satrns tr
= {0};
522 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) - 1);
523 if (ipsecdoi_t2satrns(ik_payload
, &pp
, &pr
, &tr
) == 0) {
524 fprintf(stdout
, "\t[FAIL] ProposalTest\n");
525 return racoon_test_failure
;
528 data
->type
= htons(IPSECDOI_ATTR_SA_LD_TYPE
| ISAKMP_GEN_MASK
);
529 data
->lorv
= htons(IPSECDOI_ATTR_SA_LD_TYPE_SEC
);
531 struct isakmp_data
*ld_data
= data
+ 1;
532 ld_data
->type
= htons(IPSECDOI_ATTR_SA_LD
);
533 ld_data
->lorv
= htons(LD_PAYLOAD_LEN
);
534 uint32_t *ld_payload
= (uint32_t *)(ld_data
+ 1);
535 *ld_payload
= 0x1111;
536 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + sizeof(struct isakmp_data
) + LD_PAYLOAD_LEN
- 1);
537 if (ipsecdoi_t2satrns(ik_payload
, &pp
, &pr
, &tr
) == 0) {
538 fprintf(stdout
, "\t[FAIL] ProposalTest\n");
539 return racoon_test_failure
;
542 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + sizeof(struct isakmp_data
) + LD_PAYLOAD_LEN
);
543 if (ipsecdoi_t2satrns(ik_payload
, &pp
, &pr
, &tr
) < 0) {
544 fprintf(stdout
, "\t[FAIL] ProposalTest\n");
545 return racoon_test_failure
;
548 data
->type
= htons(IPSECDOI_ATTR_AUTH
);
549 data
->lorv
= htons(IPSECDOI_ATTR_AUTH_HMAC_SHA1_96
);
551 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_t
) + sizeof(struct isakmp_data
) + sizeof(struct isakmp_data
));
552 if (ipsecdoi_t2satrns(ik_payload
, &pp
, &pr
, &tr
) == 0) {
553 fprintf(stdout
, "\t[FAIL] ProposalTest\n");
554 return racoon_test_failure
;
557 fprintf(stdout
, "\t[PASS] ProposalTest\n");
558 return racoon_test_pass
;
562 racoon_test_overflow_config_reply(void)
564 void *payload
= NULL
;
566 #define DUMMY_PAYLOAD_LEN 20
568 payload
= malloc(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
569 if (payload
== NULL
) {
570 fprintf(stdout
, "malloc failed");
571 return racoon_test_failure
;
573 memset(payload
, 0, sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
575 fprintf(stdout
, "\t[BEGIN] ConfigReplyTest\n");
577 struct isakmp_pl_attr
*ik_payload
= (struct isakmp_pl_attr
*)payload
;
578 struct isakmp_data
*data
= (struct isakmp_data
*)(ik_payload
+ 1);
579 phase1_handle_t iph1
= {0};
580 struct isakmp_cfg_state mode_cfg
= {0};
581 iph1
.mode_cfg
= &mode_cfg
;
583 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) - 1);
584 if (isakmp_cfg_reply(&iph1
, ik_payload
) == 0) {
585 fprintf(stdout
, "\t[FAIL] ConfigReplyTest\n");
586 return racoon_test_failure
;
589 data
->type
= htons(INTERNAL_IP4_SUBNET
);
590 data
->lorv
= htons(DUMMY_PAYLOAD_LEN
);
591 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
));
592 if (isakmp_cfg_reply(&iph1
, ik_payload
) == 0) {
593 fprintf(stdout
, "\t[FAIL] ConfigReplyTest\n");
594 return racoon_test_failure
;
597 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
598 if (isakmp_cfg_reply(&iph1
, ik_payload
) < 0) {
599 fprintf(stdout
, "\t[FAIL] ConfigReplyTest\n");
600 return racoon_test_failure
;
603 fprintf(stdout
, "\t[PASS] ConfigReplyTest\n");
604 return racoon_test_pass
;
608 racoon_test_overflow_config_request(void)
610 void *payload
= NULL
;
612 #define DUMMY_PAYLOAD_LEN 20
614 payload
= malloc(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
615 if (payload
== NULL
) {
616 fprintf(stdout
, "malloc failed");
617 return racoon_test_failure
;
619 memset(payload
, 0, sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
621 fprintf(stdout
, "\t[BEGIN] ConfigRequestTest\n");
623 struct isakmp_pl_attr
*ik_payload
= (struct isakmp_pl_attr
*)payload
;
624 struct isakmp_data
*data
= (struct isakmp_data
*)(ik_payload
+ 1);
625 phase1_handle_t iph1
= {0};
626 struct isakmp_cfg_state mode_cfg
= {0};
628 iph1
.mode_cfg
= &mode_cfg
;
630 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) - 1);
631 if (isakmp_cfg_request(&iph1
, ik_payload
, &msg
) == 0) {
632 fprintf(stdout
, "\t[FAIL] ConfigRequestTest\n");
633 return racoon_test_failure
;
636 data
->type
= htons(INTERNAL_ADDRESS_EXPIRY
);
637 data
->lorv
= htons(DUMMY_PAYLOAD_LEN
);
638 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
));
639 if (isakmp_cfg_request(&iph1
, ik_payload
, &msg
) == 0) {
640 fprintf(stdout
, "\t[FAIL] ConfigRequestTest\n");
641 return racoon_test_failure
;
644 fprintf(stdout
, "\t[PASS] ConfigRequestTest\n");
645 return racoon_test_pass
;
649 racoon_test_overflow_config_set(void)
651 void *payload
= NULL
;
653 #define DUMMY_PAYLOAD_LEN 20
655 payload
= malloc(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
656 if (payload
== NULL
) {
657 fprintf(stdout
, "malloc failed");
658 return racoon_test_failure
;
660 memset(payload
, 0, sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) + DUMMY_PAYLOAD_LEN
);
662 fprintf(stdout
, "\t[BEGIN] ConfigRequestSetTest\n");
664 struct isakmp_pl_attr
*ik_payload
= (struct isakmp_pl_attr
*)payload
;
665 struct isakmp_data
*data
= (struct isakmp_data
*)(ik_payload
+ 1);
666 phase1_handle_t iph1
= {0};
667 struct isakmp_cfg_state mode_cfg
= {0};
669 iph1
.mode_cfg
= &mode_cfg
;
671 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
) - 1);
672 if (isakmp_cfg_set(&iph1
, ik_payload
, &msg
) == 0) {
673 fprintf(stdout
, "\t[FAIL] ConfigRequestSetTest\n");
674 return racoon_test_failure
;
677 data
->type
= htons(XAUTH_CHALLENGE
);
678 data
->lorv
= htons(DUMMY_PAYLOAD_LEN
);
679 ik_payload
->h
.len
= htons(sizeof(struct isakmp_pl_attr
) + sizeof(struct isakmp_data
));
680 if (isakmp_cfg_set(&iph1
, ik_payload
, &msg
) == 0) {
681 fprintf(stdout
, "\t[FAIL] ConfigRequestSetTest\n");
682 return racoon_test_failure
;
685 fprintf(stdout
, "\t[PASS] ConfigRequestSetTest\n");
686 return racoon_test_pass
;
690 racoon_test_overflow_pfkey_add_sp(void)
692 void *mhp
[SADB_EXT_MAX
];
694 memset(mhp
, 0, sizeof(mhp
));
696 fprintf(stdout
, "\t[BEGIN] PFKeyAddSPTest\n");
698 struct sadb_address saddr
= {0};
699 struct sadb_address daddr
= {0};
700 struct sadb_x_policy
*xpl
= (struct sadb_x_policy
*)malloc(sizeof(*xpl
) + sizeof(struct sadb_x_ipsecrequest
) + 20 + 20);
702 fprintf(stdout
, "malloc failed");
703 return racoon_test_failure
;
705 memset(xpl
, 0, sizeof(*xpl
) + sizeof(struct sadb_x_ipsecrequest
));
707 mhp
[SADB_EXT_ADDRESS_SRC
] = (void *)&saddr
;
708 mhp
[SADB_EXT_ADDRESS_DST
] = (void *)&daddr
;
709 mhp
[SADB_X_EXT_POLICY
] = (void *)xpl
;
711 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) - 1);
712 if (addnewsp(&mhp
) == 0) {
713 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
714 return racoon_test_failure
;
717 xpl
->sadb_x_policy_type
= IPSEC_POLICY_IPSEC
;
718 struct sadb_x_ipsecrequest
*xisr
= xpl
+ 1;
719 xisr
->sadb_x_ipsecrequest_len
= sizeof(*xisr
) + 20;
720 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) + sizeof(*xisr
));
721 if (addnewsp(&mhp
) == 0) {
722 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
723 return racoon_test_failure
;
726 struct sockaddr
*srcaddr
= (struct sockaddr
*)(xisr
+ 1);
727 srcaddr
->sa_len
= 20;
728 struct sockaddr
*dstaddr
= ((uint8_t *)(srcaddr
) + 20);
729 dstaddr
->sa_len
= 20;
731 xisr
->sadb_x_ipsecrequest_proto
= IPPROTO_ESP
;
732 xisr
->sadb_x_ipsecrequest_mode
= IPSEC_MODE_TRANSPORT
;
733 xisr
->sadb_x_ipsecrequest_level
= IPSEC_LEVEL_DEFAULT
;
735 xisr
->sadb_x_ipsecrequest_len
= sizeof(*xisr
) + sizeof(struct sockaddr
) - 1;
736 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) + xisr
->sadb_x_ipsecrequest_len
+ 1);
737 if (addnewsp(&mhp
) == 0) {
738 fprintf(stdout
, "\t[FAIL] PFKeyAddSPTest\n");
739 return racoon_test_failure
;
742 xisr
->sadb_x_ipsecrequest_len
= sizeof(*xisr
) + srcaddr
->sa_len
- 1;
743 xpl
->sadb_x_policy_len
= PFKEY_UNIT64(sizeof(*xpl
) + xisr
->sadb_x_ipsecrequest_len
+ 8);
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
+ sizeof(struct sockaddr
) - 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
+ dstaddr
->sa_len
- 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 fprintf(stdout
, "\t[PASS] PFKeyAddSPTest\n");
764 return racoon_test_pass
;
768 racoon_test_overflow(void)
770 fprintf(stdout
, "[TEST] Racoon Overflow\n");
772 if (racoon_test_overflow_transform_attributes() == racoon_test_failure
) {
773 return racoon_test_failure
;
776 if (racoon_test_overflow_attribute() == racoon_test_failure
) {
777 return racoon_test_failure
;
780 if (racoon_test_overflow_ipsec_attribute() == racoon_test_failure
) {
781 return racoon_test_failure
;
784 if (racoon_test_overflow_ipcomp_attribute() == racoon_test_failure
) {
785 return racoon_test_failure
;
788 if (racoon_test_overflow_proposal() == racoon_test_failure
) {
789 return racoon_test_failure
;
792 if (racoon_test_overflow_config_reply() == racoon_test_failure
) {
793 return racoon_test_failure
;
796 if (racoon_test_overflow_config_request() == racoon_test_failure
) {
797 return racoon_test_failure
;
800 if (racoon_test_overflow_config_set() == racoon_test_failure
) {
801 return racoon_test_failure
;
804 if (racoon_test_overflow_pfkey_add_sp() == racoon_test_failure
) {
805 return racoon_test_failure
;
808 return racoon_test_pass
;
812 racoon_unit_test(void)
814 int result
= racoon_test_pass
;
816 if (racoon_cert_validity_test() == racoon_test_failure
) {
817 result
= racoon_test_failure
;
818 } else if (racoon_test_overflow() == racoon_test_failure
) {
819 result
= racoon_test_failure
;
822 if (result
== racoon_test_pass
) {
823 fprintf(stdout
, "\nAll Tests Passed\n\n");
828 main(int argc
, char *argv
[])
833 plog_ne_log_enabled
= 1;
836 print_usage(argv
[0]);
840 while ((opt
= getopt_long_only(argc
, argv
, "", long_options
, &opt_index
)) != -1) {
850 print_usage(argv
[0]);