embedded IPsec source code documentation


ah_test.c

Go to the documentation of this file.
00001 /*
00002  * embedded IPsec
00003  * Copyright (c) 2003 Niklaus Schild and Christian Scheurer, HTI Biel/Bienne
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without modification,
00007  * are permitted provided that the following conditions are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright notice,
00010  *    this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  *    this list of conditions and the following disclaimer in the documentation
00013  *    and/or other materials provided with the distribution.
00014  * 3. The name of the author may not be used to endorse or promote products
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00018  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00020  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00021  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00022  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00025  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00026  * OF SUCH DAMAGE.
00027  *
00028  */
00029 
00051 #include <string.h>
00052 
00053 #include "ipsec/util.h"
00054 #include "ipsec/ah.h"
00055 #include "ipsec/sa.h"
00056 #include "ipsec/debug.h"
00057 #include "testing/structural/structural_test.h"
00058 
00059 #include "testing/structural/ah_test-sample_ah_packet.h"
00060 
00065 int ah_test_ipsec_ah_check(void) 
00066 {
00067         sad_entry packet1_sa =  {       SAD_ENTRY(      192,168,1,40, 255,255,255,255, 
00068                                                                 0x1010, 
00069                                                                 IPSEC_PROTO_AH, IPSEC_TUNNEL, 
00070                                                                 IPSEC_3DES, 
00071                                                                 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 
00072                                                                 IPSEC_HMAC_MD5,  
00073                                                                 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0, 0, 0, 0)
00074                                                         };
00075         int local_error_count   = 0;
00076         int payload_size                = 0;
00077         int payload_offset              = 0;
00078         int ret_val;
00079 
00080         // feed valid AH packet
00081         ret_val = ipsec_ah_check((ipsec_ip_header *)&ah_test_sample_ah_outer_packet, (int *)&payload_offset, (int *)&payload_size, (sad_entry *)&packet1_sa);
00082         if(ret_val != IPSEC_STATUS_SUCCESS) {
00083                 local_error_count++;
00084                 IPSEC_LOG_TST("ah_test_ipsec_ah_check", "FAILURE", ("ipsec_ah_check(ah_test_sample_ah_packet) failed")) ;
00085         }
00086 
00087         // feed invalid packet (offset + 1)
00088         ret_val = ipsec_ah_check(((ipsec_ip_header *)&ah_test_sample_ah_outer_packet[1]), (int *)&payload_offset, (int *)&payload_size, (sad_entry *)&packet1_sa);
00089         if(ret_val == IPSEC_STATUS_SUCCESS) {
00090                 local_error_count++;
00091                 IPSEC_LOG_TST("ah_test_ipsec_ah_check", "FAILURE", ("ipsec_ah_check(invalid_packet) was not rejected")) ;
00092         }
00093 
00094         return local_error_count;
00095 }
00096 
00101 int ah_test_ipsec_ah_encapsulate(void) 
00102 {
00103         sad_entry packet1_sa =  {       SAD_ENTRY(      192,168,1,5, 255,255,255,255, 
00104                                                                 0x1016, 
00105                                                                 IPSEC_PROTO_AH, IPSEC_TUNNEL, 
00106                                                                 IPSEC_3DES, 
00107                                                                 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 
00108                                                                 IPSEC_HMAC_MD5,  
00109                                                                 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0x01, 0x23, 0x45, 0x67, 0, 0, 0, 0)
00110                                                         };
00111 
00112         static unsigned char encapsulated_ah_packet[104] =
00113         {
00114             0x45, 0x00, 0x00, 0x68, 0xE8, 0x03, 0x00, 0x00, 0x40, 0x33, 0x0F, 0x07, 0xC0, 0xA8, 0x01, 0x03, 
00115             0xC0, 0xA8, 0x01, 0x05, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x10, 0x16, 0x00, 0x00, 0x00, 0x01, 
00116 
00117                 // ICV
00118                 0x6F, 0x1D, 0x8E, 0x94, 0x4F, 0x70, 0x23, 0xE8, 0x53, 0xB1, 0x51, 0xBF,
00119                 
00120                 0x45, 0x00, 0x00, 0x3C, 
00121             0xE7, 0x7A, 0x40, 0x00, 0x40, 0x06, 0xCF, 0xC5, 0xC0, 0xA8, 0x01, 0x28, 0xC0, 0xA8, 0x01, 0x03, 
00122             0x80, 0x1A, 0x00, 0x50, 0x84, 0xB9, 0xC5, 0x66, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x02, 0x7E, 0xB8, 
00123             0x1F, 0x75, 0x00, 0x00, 0x02, 0x04, 0x3F, 0x5C, 0x04, 0x02, 0x08, 0x0A, 0x00, 0x0F, 0x22, 0x1C, 
00124             0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x00, 
00125         } ;
00126 
00127         int local_error_count   = 0;
00128         int payload_size                = 0;
00129         int payload_offset              = 0;
00130         __u32 src;
00131         __u32 dst;
00132         int ret_val = 0;
00133         unsigned char buffer[sizeof (ah_test_sample_ah_inner_packet) + 100];
00134 
00135         local_error_count = 0;
00136 
00137         src = 0x0301A8C0;
00138         dst = 0x0501A8C0;
00139 
00140         /* copy packet in a buffer where space for the new headers is left */
00141         memcpy(buffer + 100, ah_test_sample_ah_inner_packet, sizeof(ah_test_sample_ah_inner_packet));
00142 
00143         ret_val = ipsec_ah_encapsulate((ipsec_ip_header *)(buffer + 100), 
00144                                               (int *)&payload_offset, (int *)&payload_size, 
00145                                                                                   (sad_entry *)&packet1_sa,
00146                                                                                   src, dst
00147                                                                                  );
00148         if(ret_val != 0) {
00149                 local_error_count++;
00150                 IPSEC_LOG_TST("ah_test_ipsec_ah_encapsulate", "FAILURE", ("ipsec_ah_encapsulate() failed (rev_val indicates no SUCCESS)")) ;
00151         } 
00152 
00153         if(payload_offset != -44)
00154         {
00155                 local_error_count++ ;
00156                 IPSEC_LOG_TST("ah_test_ipsec_ah_encapsulate", "FAILURE", ("offset was not calculated properly")) ;
00157         }
00158 
00159         if(payload_size != 104)
00160         {
00161                 local_error_count++ ;
00162                 IPSEC_LOG_TST("ah_test_ipsec_ah_encapsulate", "FAILURE", ("length was not calculated properly")) ;
00163         }
00164 
00165         payload_offset = -44;
00166         payload_size = 104;
00167 
00168         if(memcmp(((char*)(buffer + 100)) + payload_offset, encapsulated_ah_packet, payload_size) != 0)
00169         {
00170                 local_error_count++ ;
00171                 IPSEC_LOG_TST("ah_test_ipsec_ah_encapsulate", "FAILURE", ("packet was not properly encapsulated"));
00172         }
00173 
00174         return local_error_count;
00175 }
00176 
00181 void ah_test(test_result *global_results)
00182 {
00183         test_result     sub_results     = {
00184                                                   6,                    
00185                                                   2,                    
00186                                                   0,                    
00187                                                   0,                    
00188                                         };
00189 
00190         int retcode;
00191 
00192         retcode = ah_test_ipsec_ah_check();
00193         IPSEC_TESTING_EVALUATE(retcode, sub_results, "ah_test_ipsec_ah_check()", (""));
00194 
00195         retcode = ah_test_ipsec_ah_encapsulate();
00196         IPSEC_TESTING_EVALUATE(retcode, sub_results, "ah_test_ipsec_ah_encapsulate()", (""));
00197 
00198         global_results->tests += sub_results.tests;
00199         global_results->functions += sub_results.functions;
00200         global_results->errors += sub_results.errors;
00201         global_results->notimplemented += sub_results.notimplemented;
00202 }
00203 
00204 
00205 

Copyright 2003 by Christian Scheurer and Niklaus Schild