#!/usr/bin/perl
#
# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
# Yokogawa Electric Corporation.
# All rights reserved.
# 
# Redistribution and use of this software in source and binary
# forms, with or without modification, are permitted provided that
# the following conditions and disclaimer are agreed and accepted
# by the user:
# 
# 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.
# 
# 3. Neither the names of the copyrighters, the name of the project
#    which is related to this software (hereinafter referred to as
#    "project") nor the names of the contributors may be used to
#    endorse or promote products derived from this software without
#    specific prior written permission.
# 
# 4. No merchantable use may be permitted without prior written
#    notification to the copyrighters.
# 
# 5. The copyrighters, the project and the contributors may prohibit
#    the use of this software at any time.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHTERS, THE PROJECT AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING
# BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.  IN NO EVENT SHALL THE
# COPYRIGHTERS, THE PROJECT OR CONTRIBUTORS 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.
#
# $Id: IKEv2-EN-R-1-1-2-1-A.seq,v 1.3 2008/11/28 10:16:21 doo Exp $
#
################################################################

BEGIN
{
	push(@INC, '..');
}

END { }

use IKEv2;
use strict;


my $remote	= 'common_remote_index';


my $session = undef;
my $str = undef;

my $ike_sa_init_req_param = undef;
my $ike_sa_init_resp_param = undef;


my @local_ike_sa_init_req = undef;


kLogHTML('<FONT COLOR="#FF0000" SIZE="+1"><U><B>TEST SETUP</B></U></FONT>');

#------------------------------#
# register IKEv2.pm            #
#------------------------------#
unless (IKEv2initialize('EN', 'EN')) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">IKEv2 module initializing failure</FONT>');
}


#------------------------------#
# configure TN                 #
#------------------------------#
unless (IKEv2setupTN()) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">TN setup failure</FONT>');
}


#------------------------------#
# configure NUT                #
#------------------------------#
unless (IKEv2setupNUT()) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">NUT setup failure</FONT>');
}

kLogHTML('<FONT COLOR="#FF0000" SIZE="+1"><U><B>TEST PROCEDURE</B></U></FONT>');


#------------------------------#
# prepare session handler      #
#------------------------------#
$session = IKEv2create_session('common_remote_index');
unless (defined($session)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">IKEv2 session creation failure</FONT>');
}


#--------------------------------------#
# send IKE_SA_INIT request to NUT      #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |<--------------| IKE_SA_INIT request (HDR, SAi1, KEi, Ni)\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);


$ike_sa_init_req_param = devel_IKEv2send_IKE_SA_INIT_request($session, $remote);
unless (defined($ike_sa_init_req_param)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send IKE_SA_INIT request.</FONT>');
}


#--------------------------------------#
# receive IKE_SA_INIT response by NUT  #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |-------------->| IKE_SA_INIT response (HDR, SAr1, KEr, Nr)\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);


$ike_sa_init_resp_param = devel_IKEv2receive_IKE_SA_INIT_response($session,
                                                                  $remote,
                                                                  $ike_sa_init_req_param);
unless (defined($ike_sa_init_resp_param)) {
	kLogHTML(kDump_Common_Error());
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe IKE_SA_INIT response.</FONT>');
	IKEv2exitFail();
}



#
# configure retransmitted IKE_SA_INIT request as same as the previous IKE_SA_INIT request
#
my $common_ike_sa_init_req = $gen_ike_sa_init_req->{'common_remote_index'};
@local_ike_sa_init_req = @{$common_ike_sa_init_req};

for (my $i = 0; $i < scalar(@local_ike_sa_init_req); $i++) {
	if ($local_ike_sa_init_req[$i]->{'self'} eq 'HDR') {
		$local_ike_sa_init_req[$i]->{'initSPI'} = $ike_sa_init_req_param->{'spi'};
		next;
	}
	if ($local_ike_sa_init_req[$i]->{'self'} eq 'KE') {
		$local_ike_sa_init_req[$i]->{'publicKey'} =
			kIKE::kHelpers::as_hex2($ike_sa_init_req_param->{'public'},
						256);
		next;
	}
	if ($local_ike_sa_init_req[$i]->{'self'} eq 'Ni, Nr') {
		$local_ike_sa_init_req[$i]->{'nonce'} =	kIKE::kHelpers::as_hex2($ike_sa_init_req_param->{'nonce'},
										$ike_sa_init_req_param->{'nonceLen'});
		next;
	}
}

my $index = 'EN-R-1-1-2-1.1';
$gen_ike_sa_init_req->{$index} = \@local_ike_sa_init_req;



#-----------------------------------------#
# RE-send IKE_SA_INIT request to NUT      #
#-----------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |<--------------| IKE_SA_INIT request (HDR, SAi1, KEi, Ni)\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);


$ike_sa_init_req_param = undef;
$ike_sa_init_req_param = devel_IKEv2send_IKE_SA_INIT_request($session, $index);
unless (defined($ike_sa_init_req_param)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send IKE_SA_INIT request.</FONT>');
}


#-----------------------------------------#
# RE-receive IKE_SA_INIT response by NUT  #
#-----------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |-------------->| IKE_SA_INIT response (HDR, SAr1, KEr, Nr)\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);


$ike_sa_init_resp_param = undef;
$ike_sa_init_resp_param = devel_IKEv2receive_IKE_SA_INIT_response($session,
                                                                  $remote,
                                                                  $ike_sa_init_req_param);
unless (defined($ike_sa_init_resp_param)) {
	kLogHTML(kDump_Common_Error());
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe IKE_SA_INIT response.</FONT>');
	IKEv2exitFail();
}


#--------------------------------------#
# exit with cleanup                    #
#--------------------------------------#
IKEv2exitPass();

#
# perldoc
#
########################################################################
__END__

=head1 Title

  Test IKEv2.EN.R.1.1.2.1: Receipt of retransmitted IKE_SA_INIT request
  Part A: (BASIC)

=head1 Purpose

  To verify an IKEv2 device transmits an IKE_SA_INIT response when the device received a
  retransmitted IKE_SA_INIT request.

=head1 References

  * [RFC 4306] - Sections 2.1, 2.2 and 2.4
  * [RFC 4718] - Sections 2.2 and 2.3

=head1 Test Setup

  * Network Topology
      Connect the devices according to the Common Topology.
  * Configuration
      In each part, configure the devices according to the Common Configuration.
  * Pre-Sequence and Cleanup Sequence
      IKEv2 on the NUT is disabled after each part.

=head1 Procedure

=begin html
    
<BLOCKQUOTE>
<PRE>
      
   NUT                  TN1
(End-Node)           (End-Node)
    |                    |
    |<-------------------| IKE_SA_INIT request (HDR, SAi1, KEi, Ni)
    |                    | (Packet #1)
    |------------------->| IKE_SA_INIT response (HDR, SAr1, KEr, Nr)   
    |                    | (Judgement #1)
    |                    |
    |                    * wait until retrans timer expires
    |---------X          | IKE_SA_INIT response (HDR, SAr1, KEr, Nr)
    |                    | (Judgement #2)
    |                    |
    |<-------------------| IKE_SA_INIT request (HDR, SAi1, KEi, Ni)
    |                    | (Packet #2)
    |------------------->| IKE_SA_INIT response (HDR, SAr1, KEr, Nr)
    |                    | (Judgement #3)
    |                    |
    V                    V<br>
<br>
</BLOCKQUOTE>

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD><TT>Packet #1</TT></TD>
<TD><TT>See Common Packet #1</TT></TD>
</TR>
<TR>
<TD><TT>Packet #2</TT></TD>
<TD><TT>See Common Packet #1<br>(The Message ID is the same as Packet #1)</TT></TD>
</TR>
</TABLE>   
</BLOCKQUOTE>

=end html

  Part A: (BASIC)
     1. TN starts to negotiate with TN1 by sending IKE_SA_INIT request.
     2. Observe the messages transmitted on Link A.
     3. Observe the messages transmitted on Link A.
     4. TN1 retransmits same IKE_SA_INIT request as the message transmitted in Step 1 to the
        NUT.
     5. Observe the messages transmitted on Link A.

=head1 Observable Result

  Part A
       Step 2: Judgment #1
       The NUT transmits an IKE_SA_INIT response including
       "ENCR_3DES","PRF_HMAC_SHA1", "AUTH_HMAC_SHA1_96" and "D-H group 2" as
       accepted algorithms.

       Step 3: Judgment #2
       The NUT never retransmits the same IKE_SA_INIT response as the response transmitted at
       Step 2.

       Step 5: Judgment #3
       The NUT transmits the same IKE_SA_INIT response as the response transmitted at Step 2.


=head1 Possible Problems

  * None.

=cut


