#!/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-SGW-R-2-1-2-4-A.seq,v 1.3 2008/11/28 10:16:30 doo Exp $
#
################################################################



BEGIN
{
	push(@INC, '..');
	$ENV{'V6EVAL_WITH_KOI'} = 'TRUE';
}

END { }



use V6evalTool;
use IKEv2v6eval;
use IKEv2;
use strict;

use Socket6;


my $remote	= 'common_remote_index';
my $ike_sa_init_req_param	= undef;
my $ike_sa_init_resp_param	= undef;
my $ike_auth_req_param	= undef;
my $ike_auth_resp_param	= undef;

my $session	= undef;
my $material	= undef;
my $child_sa_material	= undef;
my $str	= undef;
my $ret	= undef;

my @local_ike_auth_req = undef;
my @local_ike_auth_resp = undef;

$need_vpn	= $true;


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

my $tn = 'EN';
my $nut = 'SGW';

unless(IKEv2initialize($nut, $tn)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">IKEv2 module initializing failure</FONT>');
}



#---------------------------------#
# switch Support or Not Support   #
#---------------------------------#
unless ($config_pl->{'advanced_support_cfg_payload'}) {
	IKEv2exitNS('Not support ' . "'advanced_support_cfg_payload'");
}



#------------------------------#
# change configuration         #
#------------------------------#
my $conf = {
	'provide' => 'on',
	'addresspool' => {
		'saddr' => $addresses{'cfg_req_internal_addr'},
		'eaddr' => $addresses{'cfg_req_internal_addr'},
	},
};
unless (IKEv2customize($conf)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">IKEv2 configuration 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>');
}



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



$session = IKEv2create_session($remote);
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();
}



#--------------------------------------#
# generate keying material             #
#--------------------------------------#
$material = devel_IKEv2gen_keymat(1, $ike_sa_init_req_param, $ike_sa_init_resp_param);
unless(defined($material)) {
	kLogHTML(kDump_Common_Error());
	IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t generate keying material.</FONT>');
}



#--------------------------------------#
# send IKE_AUTH request to NUT         #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |<--------------| IKE_AUTH request (HDR, SK {IDi, AUTH, SAi2, TSi, TSr})\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);


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



#------------------------------------------------------#
# prepare IKE_AUTH Response (w/ N(FAILED_CP_REQUIRED)  #
#------------------------------------------------------#
my $notify =
{	# Notify Payload
	'self'		=> 'N',		    # *** MUST BE HERE ***
	'nexttype'	=> '0',		    # Next Payload
	'critical'	=> '0',		    # Critical
	'reserved'	=> '0',		    # RESERVED
	'length'	=> '8',		    # Payload Length
	'id'		=> '0',		    # Protocol ID
	'spiSize'	=> '0',		    # SPI Size
	'type'		=> 'FAILED_CP_REQUIRED',	# Notify Type
};
my $common_exp_ike_auth_resp = $exp_ike_auth_resp->{$remote};
my @local_ike_auth_resp = @{$common_exp_ike_auth_resp};
# add Notify Payload
foreach my $payload (@local_ike_auth_resp) {
	if ($payload->{'self'} eq 'E') {
		$payload->{'innerPayloads'} = [ $notify ];
		last;
	}
}
# set correct nexttype
for (my $i = 0; $i < scalar(@local_ike_auth_resp); $i++) {
	my $payload = $local_ike_auth_resp[$i];

	if ($payload->{'self'} eq 'E') {
		for (my $j = 0; $j < scalar(@{$payload->{'innerPayloads'}}); $j++) {
			my $cur = $payload->{'innerPayloads'}->[$j];
			my $next = $payload->{'innerPayloads'}->[$j+1];
			unless (defined($next)) {
				$cur->{'nexttype'} = '0';
				next;
			}
			if ($cur->{'nexttype'} eq $next->{'self'}) {
				next;
			}
			$cur->{'nexttype'} = $next->{'self'};
		}

		next;
	}

}
my $index = 'no_configuration_payload.1';
$exp_ike_auth_resp->{$index} = \@local_ike_auth_resp;



#--------------------------------------#
# receive IKE_AUTH response by NUT     #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (R)             (I)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |-------------->| IKE_AUTH response (HDR, SK {N(FAILED_CP_REQUIRED)})\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);


$ike_auth_resp_param = devel_IKEv2receive_IKE_AUTH_response($session,
							    $index,
							    $ike_sa_init_req_param,
							    $ike_sa_init_resp_param,
							    $ike_auth_req_param,
							    $material);
unless(defined($ike_auth_resp_param)) {
	kLogHTML(kDump_Common_Error());
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe IKE_AUTH response.</FONT>');
	IKEv2exitFail();
}



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



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

=head1 Title

  Test IKEv2.SGW.R.2.1.2.4: No Configuration payload
  Part A: (ADVANCED)

=head1 Purpose

  To verify an IKEv2 device properly handles the message which does not include
  Configuration payload, when the device expects Configuration payload.

=head1 References

  * [RFC 4306] - Sections 2.19 and 3.10.1

=head1 Test Setup

  * Network Topology
      Connect the devices according to the following topology.

=begin html

<BLOCKQUOTE>
<PRE>

   TH1
  (Host)
    |
----+-------+--- Link B (Prefix B, MTU=1500)
            |
           NUT 
          (SGW)
            |
----+-------+--- Link A (Prefix A, MTU=1500)
    |
   TR1
 (Router)
    |
----+-------+--- Link X (Prefix X, MTU=1500)
            |
           TH1
        (End-Node)
<br>
Prefix A = 2001:0db8:0001:0001::/64
Prefix B = 2001:0db8:0001:0002::/64
Prefix X = 2001:0db8:000f:0001::/64
<br>

<TABLE BORDER>
<TR>
<TD>TH1</TD>
<TD>Host</TD>
<TD>Link B</TD>
<TD>Prefix B::f</TD>
</TR>
<TR>
<TD>NUT</TD>
<TD>SGW</TD>
<TD>Link B</TD>
<TD>Prefix B::any_interface_ID</TD>
</TR>
<TR>
<TD>TR1</TD>
<TD>Router</TD>
<TD>Link A</TD>
<TD>Prefix A::any_interface_ID</TD>
</TR>
<TR>
<TD rowspan="2">TN1</TD>
<TD rowspan="2">End-Node</TD>
<TD rowspan="2">Link X</TD>
<TD>Prefix X::1 (External Address)</TD>
</TR>
<TR>
<TD>Prefix B::1 (Internal Address) (assigned by CP)</TD>
</TR>
</TABLE>

</PRE>
</BLOCKQUOTE>

=end html

  * Configuration
      In each part, configure NUT according to the Common Configuration except the traffic
      selector. Configure NUT to transmit CFG_REPLY for INTERNAL_IP6_ADDRESS.
      Its IPv6 address is Prefix B::1/128. The traffic selector must be configured by the
      following table. NUT must narrow Traffic Selector to the following address table.

=begin html 

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD rowspan="3"></TD>
<TD colspan="6">Traffic Selector</TD>
</TR>
<TR>
<TD colspan="3">Source</TD>
<TD colspan="3">Destination</TD>
</TR>
<TR>
<TD>Address<br>Range</TD>
<TD>Next Layer<br>Protocol</TD>
<TD>Port<br>Range</TD>
<TD>Address<br>Range</TD>
<TD>Next Layer<br>Protocol</TD>
<TD>Port<br>Range</TD>
</TR>
<TR>
<TD>Inbound</TD>
<TD>TN1<br>(internal address)</TD>
<TD>ANY</TD>
<TD>ANY</TD>
<TD>Link B</TD>
<TD>ANY</TD>
<TD>ANY</TD>
</TR>
<TR>
<TD>Outbound</TD>
<TD>Link B</TD>
<TD>ANY</TD>
<TD>ANY</TD>
<TD>TN1<br>(internal address)</TD>
<TD>ANY</TD>
<TD>ANY</TD>
</TR>
</TABLE>
</BLOCKQUOTE>

=end html

  * Pre-Sequence and Cleanup Sequence
      IKEv2 on the NUT is disabled after each part.

=head1 Procedure

=begin html

<BLOCKQUOTE>
<PRE>   
    
   NUT                  TN1
  (SGW)              (End-Node)
    |                    |
    |<-------------------| IKE_SA_INIT request (HDR, SAi1, KEi, Ni)
    |                    | (Packet #1)
    |------------------->| IKE_SA_INIT Response (HDR, SAr1, KEr, Nr)
    |                    | (Judgement #1)
    |                    |
    |<-------------------| IKE_AUTH request (HDR, SK {IDi, AUTH, SAi2, TSi, TSr})
    |                    | (Packet #2)
    |------------------->| IKE_AUTH Response (HDR, SK {N(FAILED_CP_REQUIRED)})
    |                    | (Judgement #2)
    |                    |
    V                    V
</PRE>
</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 #5<br>This packet does not include CP payload.</TT></TD>
</TR>
</TABLE>
</BLOCKQUOTE>

=end html

  Part A (ADVANCED)
     1. TN1 transmits an IKE_SA_INIT request to NUT.
     2. Observe the messages transmitted on Link A.
     3. TN1 transmits an IKE_SA_INIT request to the NUT.
     4. 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 proposed
       algorithms.

       Step 4: Judgment #2
       The NUT transmits an IKE_AUTH response with a Notify payload of type
       FAILED_CP_REQUIRED.

=head1 Possible Problems

  * none.

=cut

