#!/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-1-1-6-9-A.seq,v 1.5 2009/05/28 07:26:46 akisada Exp $
#
################################################################



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

END { }



use IKEv2;
use strict;



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 $informational_req_param	= undef;
my $informational_resp_param	= undef;

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



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

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

unless(IKEv2initialize($tn, $nut)) {
	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>');
}



#------------------------------#
# 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>');
}



#----------------------------------#
# prepare IKE_AUTH Request         #
#----------------------------------#
my $common_ike_auth_req = $gen_ike_auth_req->{$remote};
my @local_ike_auth_req = @{$common_ike_auth_req};
# change Encryption Algorithm
for (my $i = 0; $i < scalar(@local_ike_auth_req); $i++) {
	my $payload = $local_ike_auth_req[$i];

	if ($payload->{'self'} eq 'SA') {
		for (my $j = 0; $j < scalar(@{$payload->{'proposals'}}); $j++) {
			my $proposal = @{$payload->{'proposals'}}[$j];

			if (defined($proposal->{'transforms'})) {
				for (my $k = 0; $k < scalar(@{$proposal->{'transforms'}}); $k++) {
					my $transform = @{$proposal->{'transforms'}}[$k];

					if ($transform->{'type'} eq 'ENCR') {
						$transform->{'id'} = 'AES_CBC';

						my $attribute = {
							'af'		=> '1',
							'type'		=> 'Key Length',
							'value'		=> '128',
							'length'	=> undef,
						};
						push(@{$transform->{'attributes'}}, $attribute);
					}
				}
			}
		}
	}
}
my $index = 'creating_IKE_SA_without_CHILD_SA.1';
$gen_ike_auth_req->{$index} = \@local_ike_auth_req;



#--------------------------------------#
# 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,
						       $index,
						       $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            #
#--------------------------------------#
my $common_exp_ike_auth_resp = $exp_ike_auth_resp->{'common_remote_index'};
my @local_exp_ike_auth_resp = @{$common_exp_ike_auth_resp};
my $notify_payload =
{       # Notify Payload
	'self'		=> 'N',		# *** MUST BE HERE ***
	'nexttype'	=> '0',	# Next Payload
	'critical'	=> undef,	# Critical
	'reserved'	=> undef,	# RESERVED
	'length'	=> '8',		# Payload Length
	'id'		=> undef,	# Protocol ID
	'spiSize'	=> undef,	# SPI Size
	'type'		=> 'NO_PROPOSAL_CHOSEN',    # Notify Type
	'data'		=> undef,	# Notification Data 
};

foreach my $payload (@local_exp_ike_auth_resp) {
	if ($payload->{'self'} eq 'E') {
		$payload->{'innerPayloads'} = [$notify_payload];
		$payload->{'innerType'} = $payload->{'innerPayloads'}->[0]->{'self'};

		last;
	}

}
$index = 'creating_IKE_SA_without_CHILD_SA2';
$exp_ike_auth_resp->{$index} = \@local_exp_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 {IDr, AUTH, SAr2, TSi, TSr)\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();
}



#-----------------------------------------#
# send INFORMATIONAL Request              #
#-----------------------------------------#
$str  = '<PRE>';
$str .= "    (I)             (R)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |               |\n";
$str .= "     |<--------------| INFORMATIONAL Request (HDR, SK {})\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);


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


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

kLogHTML($str);


$informational_resp_param = devel_IKEv2receive_INFORMATIONAL_response($session,
								      $remote,
								      $ike_sa_init_req_param,
								      $ike_sa_init_resp_param,
								      $informational_req_param,
								      $material);
unless (defined($informational_resp_param)) {
	kLogHTML(kDump_Common_Error());
	kLogHTML('<FONT COLOR="#ff0000">Can\'t observe INFORMATIONAL Response.</FONT>');
	IKEv2exitFail();
}



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



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

=head1 Title
    
  Test IKEv2.SGW.R.1.1.6.9: Creating an IKE_SA without a CHILD_SA
  Part A: (BASIC)

=head1 Purpose

  To verify that an IKEv2 device can handles a failure of creating a CHILD_SA during the
  IKE_AUTH exchange.

=head1 References

  * [RFC 4718] - Sections 4.2

=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.In addition,
      set IKE_SA Lifetime to 300 seconds and set CHILD_SA Lifetime to 30 seconds.
  * Pre-Sequence and Cleanup Sequence
      IKEv2 on the NUT is disabled after each part.

=head1 Procedure

=begin html

<BLOCKQUOTE>
<PRE>
   NUT                  TN1
  (SGW)                (SGW)
    |                    |
    |<-------------------| 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)
    |---------X          | IKE_AUTH response (HDR, SK {IDr, AUTH, SAr2, TSi, TSr})
    |                    | (Judgement #2)
    |                    |
    |<-------------------| INFORMATIONAL request (HDR, SK {})
    |                    | (Packet #3)
    |------------------->| INFORMATIONAL Response (HDR, SK {})
    |                    | (Judgement #3)
    |                    |
    V                    V
N: USE_TRANSPORT_MODE
</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 below</TT></TD>
</TR>
<TR>
<TD><TT>Packet #3</TT></TD>
<TD><TT>See Common Packet #17</TT></TD>
</TR>
</TABLE>
</BLOCKQUOTE>
<br>

=end html

  Packet #2: IKE_AUTH request
    Packet #2 is same as Common Packet #5 except SA Transform proposed in each test.
    Part A:
       SA Transform of Tranform Type ENCR is replaced by the following SA Transfrom.

=begin html

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD rowspan="8">SA Transform</TD>
<TD colspan="2">Next Payload</TD>
<TD>3 (more)</TD>
</TR>
<TR>
<TD colspan="2">Reserved</TD>
<TD>0</TD>
</TR>
<TR>
<TD colspan="2">Transform Length</TD>
<TD>8</TD>
</TR>
<TR>
<TD colspan="2">Transform Type</TD>
<TD>1 (ENCR)</TD>
</TR>
<TR>
<TD colspan="2">Reserved</TD>
<TD>0</TD>
</TR>
<TR>
<TD colspan="2">Transform ID</TD>
<TD>12 (AES_CBC)</TD>
</TR>
<TR>
<TD rowspan="2">SA Attribute</TD>
<TD>Attribute Type</TD>
<TD>14 (Key Length)</TD>
</TR>
<TR>
<TD>Attribute Value</TD>
<TD>128</TD>
</TR>
</TABLE>
</BLOCKQUOTE>

=end html 

  Part A: (BASIC)
     1. TN1 starts to negotiate with NUT by sending IKE_SA_INIT request.
     2. Observe the messages transmitted on Link A.
     3. After reception of IKE_AUTH response from the NUT, TN1 transmits an IKE_AUTH
        request with unacceptable SA proposal for the NUT.
     4. Observe the messages transmitted on Link A.
     5. TN1 transmits an INFORMATIONAL request with no payloads.
     6. 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 4: Judgment #2
       The NUT never transmits an IKE_AUTH response including "ENCR_3DES",
       "AUTH_HMAC_SHA1_96" and "No Extended Sequence Numbers" as accepted algorithms.

       Step 6: Judgment #3
       The NUT transmits an INFORMATIONAL Response followed by an Encrypted payload with
       no payloads contained in it.

=head1 Possible Problems

  * Step 4
    The NUT can transmits an IKE_AUTH response with a Notify payload of type
    NO_PROPOSAL_CHOSEN.

=cut


