#!/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-I-1-1-6-8-A.seq,v 1.3 2008/11/28 10:16:25 doo Exp $
#
################################################################

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

END {}

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



my $remote	= 'common_remote_index';
my $selector	= 'common_selector_index_outbound';


my $session	= undef;
my $ret	= undef;
my $str = undef;
my $ike_sa_init_req_param	= undef;
my $ike_sa_init_resp_param	= undef;
my $material	= undef;

my @local_ike_sa_init_resp = 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('SGW', 'SGW')) {
	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($remote);
unless(defined($session)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">IKEv2 session creation failure</FONT>');
}



$str  = '<PRE>';
$str .= "    (I)             (R)\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);



#--------------------------------------#
# invoke IKE_SA_INIT request           #
#--------------------------------------#
unless(IKEv2initiateNUT($selector)) {
	IKEv2exitFatal('<FONT COLOR="#ff0000">NUT initiation failure</FONT>');
}



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



#------------------------------------------------------#
# prepare IKE_SA_INIT response (w/ NO_PROPOSAL_CHOSEN) #
#------------------------------------------------------#
my $send_notify =
{       # Notify Payload
        'self'          => 'N',         # *** MUST BE HERE ***
        'nexttype'      => undef,       # Next Payload
        'critical'      => undef,       # Critical
        'reserved'      => undef,       # RESERVED
        'length'        => '8',         # Payload Length
        'length_comparator'     => 'range',     # Payload Length
        'id'            => undef,       # Protocol ID
        'spiSize'       => undef,       # SPI Size
        'type'          => 'NO_PROPOSAL_CHOSEN',    # Notify Type
        'data'          => undef,       # Notification Data 
};

my $common_ike_sa_init_resp = $gen_ike_sa_init_resp->{'common_remote_index'};
@local_ike_sa_init_resp = @{$common_ike_sa_init_resp};

# remove payload without HDR
@local_ike_sa_init_resp = grep {
                my $ret = 0;
                if ($_->{'self'} eq 'HDR') { $ret = 1; }
                $ret;
        } @local_ike_sa_init_resp;

# add Notify payload
for (my $i = 0; $i < scalar(@local_ike_sa_init_resp); $i++) {
        if ($local_ike_sa_init_resp[$i]->{'self'} eq 'HDR') {
                splice(@local_ike_sa_init_resp, $i+1, 0, $send_notify);
                last;
        }
}
$gen_ike_sa_init_resp->{'SGW-I-1-1-6-4.1'} = \@local_ike_sa_init_resp;



#--------------------------------------#
# send IKE_SA_INIT response            #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (I)             (R)\n";
$str .= "    NUT             TN1\n";
$str .= "     |               |\n";
$str .= "     |<--------------| IKE_SA_INIT response (HDR, N(NO_PROPOSAL_CHOSEN))\n";
$str .= "     |               |\n";
$str .= "     V               V";
$str .= '</PRE>';

kLogHTML($str);


$ike_sa_init_resp_param = devel_IKEv2send_IKE_SA_INIT_response($session, 
                                                            'SGW-I-1-1-6-4.1',
                                                            $ike_sa_init_req_param);
unless(defined($ike_sa_init_resp_param)) {
        kLogHTML(kDump_Common_Error());
        IKEv2exitFatal('<FONT COLOR="#ff0000">Can\'t send IKE_SA_INIT request.</FONT>');
}



#--------------------------------------#
# receive IKE_SA_INIT request          #
#--------------------------------------#
$str  = '<PRE>';
$str .= "    (I)             (R)\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_IKEv2receive_IKE_SA_INIT_request($session, $remote);

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



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



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

=head1 Title

  Test IKEv2.SGW.I.1.1.6.8: Receipt of NO_PROPOSAL_CHOSEN
  Part A: (BASIC)

=head1 Purpose

  To verify an IKEv2 device properly handles IKE_SA_INIT response with a Notify payload
  of type NO_PROPOSAL_CHOSEN.

=head1 References

  * [RFC 4306] - Sections 2.7, 3.4 and 3.10.1
  * [RFC 4718] - Sections 2.1

=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>

   TH1            NUT           TN1           TH2
 (HOST)          (SGW)         (SGW)         (Host)
    |              |             |             |
    |              |------------>|             | IKE_SA_INIT request (HDR, SAi1, KEi, Ni)
    |              |             |             | (Judgement #1)
    |              |<------------|             | IKE_SA_INIT Response (HDR, SAr1, KEr, Nr)
    |              |             |             | (Packet #1)
    |              |             |             |
    |              |------------>|             | IKE_AUTH request (HDR, SK {IDi, AUTH, SAi2, TSi, TSr})
    |              |             |             | (Judgement #2)
    |              |<------------|             | IKE_AUTH Response (HDR, SK {IDr, AUTH, SAr2, TSi, TSr})
    |              |             |             | (Packet #2)
    |              |             |             |
   ---            ---           ---           ---
    |              |             |             |                             ---
    |<-------------+=============+-------------| IPSec {Echo Request}         | repeat Echo Exchange
    |              |             |             | (Packet #3) (Judgement #3)   | until lifetime of
    |--------------+=============+------------>| IPSec {Echo Reply}           | SA is expired
    |              |             |             | (Packet #4) (Judgement #4)   |
    |              |             |             |                             ---
   ---            ---           ---           ---
    |              |             |             |
    |              |------------>|             | CREATE_CHILD_SA request (HDR, SK {N, SA, Ni, KEi, TSi, TSr})
    |              |             |             | (Judgement #4)
    |              |<------------|             | CREATE_CHILD_SA response (HDR, SK {N(INVALID_PAYLOAD)})
    |              |             |             | (Packet #5)
    |              |             |             |
    |              |------------>|             | CREATE_CHILD_SA request (HDR, SK {N, SA, Ni, KEi', TSi, TSr})
    |              |             |             | (Judgement #5)
    |              |             |             |
    V              V             V             V<br>
</PRE>
</BLOCKQUOTE>

<BLOCKQUOTE>
<TABLE BORDER>
<TR>
<TD><TT>Packet #1</TT></TD>
<TD><TT>See Common Packet #2</TT></TD>
</TR>
<TR>
<TD><TT>Packet #2</TT></TD>
<TD><TT>See Common Packet #6</TT></TD>
</TR>
<TR>
<TD><TT>Packet #3</TT></TD>
<TD><TT>See Common Packet #21</TT></TD>
</TR>
<TR>
<TD><TT>Packet #4</TT></TD>
<TD><TT>See Common Packet #25</TT></TD>
</TR>
<TR>
<TD><TT>Packet #5</TT></TD>
<TD><TT>See below</TT></TD>
</TR>
</TABLE>
</BLOCKQUOTE>

<BLOCKQUOTE>
Packet #5: CREATE_CHILD_SA response
<TABLE BORDER>
<TR>
<TD>IPv6 Header</TD>
<TD colspan="2">Same as Common Packet #16</TD>
</TR>
<TR>
<TD>UDP Header</TD>
<TD colspan="2">Same as Common Packet #16</TD>
</TR>
<TR>
<TD>IKEv2 Header</TD>
<TD colspan="2">Same as Common Packet #16</TD>
</TR>
<TR>
<TD>E Payload</TD>
<TD colspan="2">Same as Common Packet #16</TD>
</TR>
<TR>
<TD rowspan="8">N Payload</TD>
<TD>Next Payload</TD>
<TD>0</TD>
</TR>
<TR>
<TD>Critical</TD>
<TD>0</TD>
</TR>
<TR>
<TD>Reserved</TD>
<TD>0</TD>
</TR>
<TR>
<TD>Payload Length</TD>
<TD>10</TD>
</TR>
<TR>
<TD>Protocol ID</TD>
<TD>0</TD>
</TR>
<TR>
<TD>SPI Size</TD>
<TD>0</TD>
</TR>
<TR>
<TD>Notify Message Type</TD>
<TD>INVALID_KE_PAYLOAD (17)</TD>
</TR>
<TR>
<TD>Notification Data</TD>
<TD>The accepted D-H Group # (2)</TD>
</TR>
</TABLE>
</BLOCKQUOTE>

=end html

  Part A: (BASIC)
     1. NUT starts to negotiate with TN1 by sending IKE_SA_INIT request.
     2. Observe the messages transmitted on Link B.
     3. TN1 responds with an IKE_SA_INIT response to the NUT.
     4. Observe the messages transmitted on Link B.
     5. After reception of IKE_AUTH request from the NUT, TN1 responds with an IKE_AUTH
        response to the NUT
     6. TN1 transmits an Echo Request with IPsec ESP using the first negotiated algorithms to NUT.
     7. Observe the messages transmitted on Link B.
     8. Repeat Steps 6 and 7 until lifetime of SA is expired.
     9. Observe the messages transmitted on Link B.
    10. After reception of CREATE_CHILD_SA request for rekeying from the NUT, TN1 responds
        with a CREATE_CHILD_SA response with a Notify payload of type
        NO_PROPOSAL_CHOSEN to the NUT.
    11. Observe the messages transmitted on Link B.

=head1 Observable Result

  Part A
       Step 2: Judgment #1
       The NUT transmits an IKE_SA_INIT request 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_SA_INIT request including "ENCR_3DES",
       "PRF_HMAC_SHA1", "AUTH_HMAC_SHA1_96" and "D-H group 2" as proposed
       algorithms and a Key Exchange payload which contains a recalculated Key Exchange Data.

       Step 7: Judgment #3
       The NUT transmits an Echo Reply with IPsec ESP using corresponding algorithms.

       Step 9: Judgment #4
       The NUT transmits a CREATE_CHILD_SA request including "ENCR_3DES",
       "AUTH_HMAC_SHA1_96" and "No Extended Sequence Numbers" as proposed
       algorithms. And the CREATE_CHILD_SA request includes a Notify payload of type
       REKEY_SA containing rekeyed CHILD_SA's SPI value in the SPI field.

       Step 11: Judgment #5
       The NUT transmits a CREATE_CHILD_SA request including "ENCR_3DES",
       "PRF_HMAC_SHA1", "AUTH_HMAC_SHA1_96" and "D-H group 2" as proposed
       algorithms. The new CREATE_CHILD_SA request is not a retransmitted request.

=head1 Possible Problems

  * None.
    
=cut


