/*
 *            Copyright (c) 1998-2000 by NETsilicon Inc.
 *
 *  This software is copyrighted by and is the sole property of 
 *  NETsilicon.  All rights, title, ownership, or other interests
 *  in the software remain the property of NETsilicon.  This
 *  software may only be used in accordance with the corresponding
 *  license agreement.  Any unauthorized use, duplication, transmission,  
 *  distribution, or disclosure of this software is expressly forbidden.
 *
 *  This Copyright notice may not be removed or modified without prior
 *  written consent of NETsilicon.
 *
 *  NETsilicon, reserves the right to modify this software
 *  without notice.
 *
 *  NETsilicon
 *  411 Waverley Oaks Road                  USA 781.647.1234
 *  Suite 227                               http://www.netsilicon.com
 *  Waltham, MA 02452                       AmericaSales@netsilicon.com
 *
 *************************************************************************
 *
 *     Module Name: root.c
 *         Version: 1.00
 *   Original Date: 08/23/99	
 *          Author: Paul Jacobson
 *        Language: Ansi C
 * Compile Options: 
 * Compile defines: 
 *       Libraries: 
 *    Link Options: 
 *
 *    Entry Points: applicationStart(), applicationTcpDown()
 *
 * Description.
 * =======================================================================
 * applicationStart() is the entry point for user applications.  This
 * This function will be called after the kernel has started and the
 * TCP/IP stack has loaded.  applicationTcpDown() is called periodically
 * after the kernel has started while the system is waiting for the
 * TCP/IP stack to start.  
 *                                                                        
 *                                                                        
 * Edit Date/Ver   Edit Description
 * ==============  ===================================================
 * 01/16/04  JZW    Now using NAHSStartServer to start up the AWS. Allows
 *                  total configuration and ability to call secure web server
 *	1/31/07  JZW    Variable cipher_list made static since it is passed to the 
 *                  ssl thread adn the ssl thread will own it. If it is not static
 *                  then if root thread exists, the memory pointed to by cipher_list
 *                  could be reused.
 *
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tx_api.h>
#include <http_awsapi.h>
#include <sysAccess.h>
#include "ssl.h"
#include "appconf.h"
#include "server.h"     /*  For username/password */


/*
 *
 *  Function: void applicationTcpDown (void)
 *
 *  Description:
 *
 *      This routine will be called by the NET+OS root thread once every 
 *      clock tick while it is waiting for the TCP/IP stack to come up.  
 *      This function can increment a counter everytime it's called to 
 *      keep track of how long we've been waiting for the stack to start.
 *      If we've been waiting too long, then this function can do something
 *      to handle the error.  
 *
 *      This function will not be called once the stack has started.
 *
 *  Parameters:
 *
 *      none
 *
 *  Return Values:
 *
 *      none
 *
 */

void applicationTcpDown (void)

{
    static int ticksPassed = 0;

    ticksPassed++;
/*
 * Code to handle error condition if the stack doesn't come up goes here.
 */
}


/*
 *
 *  Function: void applicationStart (void)
 *
 *  Description:
 *
 *      This routine is responsible for starting the user application.  It should 
 *      create any threads or other resources the application needs.
 *
 *      ThreadX, the NET+OS device drivers, and the TCP/IP stack will be running
 *      when this function is called.
 *
 *  Parameters:
 *
 *      none
 *
 *  Return Values:
 *
 *      none
 *
 */

void applicationStart (void)
{

    int certificate_length;
    char* certificate = NULL;
	/* 
	 * added storage class static to cipher_list. A pointer to cipher_list is passed
	 * to the ssl thread. If this is not static, then as a stack variable, the memory
	 * in which cipher_list is stored could be subject to reuse.
	 */
    static char  cipher_list[]     = SSL_CIPHER_LIST_HTTPS_DEFAULT;
    int   cipher_list_len   = SSL_CIPHER_LIST_HTTPS_DEFAULT_LEN;
    char* key = NULL;
    int   key_data_len;
    RpHsSecureParams_t LocalHttp_Parameters;
   
    /*  Insecure account Netsilicon password sysadm can be used anywhere to access HTTP Realm 1 and FTP */
    NAsetSysAccess (NASYSACC_ADD, APP_USERNAME, APP_PASSWORD, NASYSACC_LEVEL_HTTP_R1 | NASYSACC_LEVEL_RW, NULL);

    /*  Secure account Safe can only be used through the Secure Gateway -- this account's password will never be exposed!   */
    NAsetSysAccess (NASYSACC_ADD, "Safe", "netsilicon", NASYSACC_LEVEL_HTTP_R1 | NASYSACC_LEVEL_RW, "127.0.0.1");
    
    /* Generate X.509 certificate for use by HTTPS server */
    printf ("Generating certificate.  This may take a few minutes.\n");
    certificate_length = NASSLX509Generate(&certificate, &key, &key_data_len, SSL_CERTIFICATE_KEY_TYPE_RSA, 1024, 1, "NAHTTPS Example Certificate", "020630120000Z", "120630120000Z");

    if(certificate_length <= 0 || key_data_len <= 0)
    {
        printf("Error generating X.509 certificate.\n");
        return;
    }

    /* set of the secure parameters */
    memset (&LocalHttp_Parameters, 0, sizeof(LocalHttp_Parameters));
    LocalHttp_Parameters.SecurityParams.cipher_list         = (unsigned char *)cipher_list;
    LocalHttp_Parameters.SecurityParams.cipher_list_len     = cipher_list_len;
    LocalHttp_Parameters.SecurityParams.certificate         = certificate;
    LocalHttp_Parameters.SecurityParams.certificate_len     = certificate_length;
    LocalHttp_Parameters.SecurityParams.key                 = key;
    LocalHttp_Parameters.SecurityParams.key_len             = key_data_len;
    LocalHttp_Parameters.SecurityParams.secure_only         = HTTPS_ONLY;

    /* the Lengthofstructure field defines the version of the protocol */
    LocalHttp_Parameters.WebServerParams.Lengthofstructure  = sizeof (HS_SERVER_PARAMETER);

    /* naSslSetProxyPortNumber(1234);*/ /* set SSL proxy socket to user specifed value */ 
    
    /* Start Advanced HTTPS server only */
    NARpStartServerSecure(&LocalHttp_Parameters);

    printf ("AWS HTTPS Web Server Sample Application is ready.\n");
}

