⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des_auth.c

📁 基于nucleus实时操作系统的webserver源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/************************************************************************
*                                                                          
*    CopyrIght (c)  1993 - 2001 Accelerated Technology, Inc.               
*                                                                          
* PROPRIETARY RIGHTS of Accelerated Technology are involved in the subject 
* matter of this material.  All manufacturing, reproduction, use and sales 
* rights pertaining to this subject matter are governed by the license     
* agreement.  The recipient of this software implicity accepts the terms   
* of the license.                                                          
*                                                                          
*************************************************************************/

/************************************************************************
*                                                                       
* FILE NAME                                             VERSION          
*                                                                       
*       des_auth.c - DES Authentication                   1.5      
*                                                                       
* COMPONENT                                             
*                
*       Nucleus WebServ                                                                       
*                                                                       
* DESCRIPTION          
*
*       This file links the java application with the actual
*       encryption process.
*                                                                       
* DATA STRUCTURES                                                       
*
*       None                                                                       
*                                                                       
* FUNCTIONS                                                             
*                                                                       
*       DES_Auth_Initialize
*       DES_Rand
*       DES_Send_Url
*       DES_New_Key
*       DES_Auth_Add
*       DES_Auth_Del
*       DES_Auth_Add_Entry
*       DES_Auth_Delete_Entry
*       DES_Send_Auth_Salt
*       DES_Make_New_Salt                                                        
*                                                                       
* DEPENDENCIES                                                          
*                          
*       nu_websrv.h
*       auth.h                                             
*                                                                       
************************************************************************/

#include "webserv/inc/nu_websr.h"
#include "net/inc/netevent.h"

#ifdef   WS_AUTH_PLUGIN
#include "webserv/inc/wpw_auth.h"


static INT         DES_Rand(WS_REQUEST * req);
static INT         DES_Send_Url(WS_REQUEST * req);
static INT         DES_New_Key(WS_REQUEST * req);
static INT         DES_Auth_Add(WS_REQUEST * req);
static INT         DES_Auth_Del(WS_REQUEST * req);
static INT16       DES_Auth_Delete_Entry(CHAR *user_id, CHAR *password);
static INT16       DES_Auth_Add_Entry(CHAR *user_id, CHAR *password);
static VOID        DES_Send_Auth_Salt(WS_REQUEST * req);
static VOID        DES_Make_New_Salt(CHAR * salt);

extern NU_MEMORY_POOL  System_Memory;
extern WS_SERVER WS_Master_Server; /* declare space for the master server structure */
extern struct WPW_AUTH_NODE WPW_Table[];

CHAR * DES_Key_String[9] = {"12345678"}; /*  Contains the static key */
WPW_INFO_LIST  DES_Pw_List_Info;
INT     DES_Little_Endian = 0;

UINT8 DES_Reg[WPW_SRG_SIZE]={
    0,1,1,0,1,0,0,1,1,0,0,1,1,1,0,1,1,0,0,1,1,1,0,1,0,0,1,1,0,0,1,1,
    0,1,0,1,0,1,1,0,0,0,1,0,1,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,1,
    1,0,1,1,0,0,1,1,0,1,0,1,0,1,0,1,1,0,0,0,1,1,1,0,0,1,1,0,1,0,1,0,
    1,1,1,0,0,1,1,0,0,1,1,0,0,0,1,1,1,0,0,1,0,0,1,0,1,0,1,0,1,0,0,0};

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       DES_Auth_Initialize                                                        
*                                                                       
* DESCRIPTION                                                           
*       
*
*                                                                
* INPUTS                                                                
*                                                                       
*       None.                                                            
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       None.                                                            
*                                                                       
************************************************************************/
VOID DES_Auth_Initialize()
{
    WPW_INFO_NODE   *apwlist_info;
    INT             i;
    WS_SERVER       *p;

    /* Endian Test */    
    CHAR            list1[4];
    UNSIGNED        cmp_value = 0x10203040ul;
    VOID            *cmp1,
                    *cmp2;
    /* 
     * The following tests if this is a big or little endian
     * machine.  The global is set for use in the encryption
     * process.
     */
    list1[0] = (CHAR)0x10;
    list1[1] = (CHAR)0x20;
    list1[2] = (CHAR)0x40;
    list1[3] = (CHAR)0x80;

    cmp1 = list1;
    cmp2 = &cmp_value;

    if(((char *)cmp1)[0] == ((char *)cmp2)[0])
        DES_Little_Endian = NU_FALSE;
    else
        DES_Little_Endian = NU_TRUE;
    /* End endian test */
    
    p = &WS_Master_Server; 
    
    /*  Register All plug-ins used for authentication */
    HTTP_Register_Plugin(DES_Rand,"ps_rand");
    HTTP_Register_Plugin(DES_Send_Url,"ps_sendurl");
    HTTP_Register_Plugin(DES_New_Key,"auth_newkey");
    HTTP_Register_Plugin(DES_Auth_Del,"auth_del");
    HTTP_Register_Plugin(DES_Auth_Add,"auth_add");

    /*  Initailize Linked List for password structure */
    for (i = 0; WPW_Table[i].wpw_user_id[0]; i++)
    {
        if (NU_Allocate_Memory (&System_Memory, (VOID **)&apwlist_info,
                            sizeof (WPW_INFO_NODE),
                            NU_NO_SUSPEND) != NU_SUCCESS)
        {
            return;
        }

        /* Setup the User Id name */
        strcpy(apwlist_info->wpw_user, WPW_Table[i].wpw_user_id);

        /* Setup the Password */
        strcpy(apwlist_info->wpw_password, WPW_Table[i].wpw_password);
        
        /* Add this host to the list. */
        DLL_Enqueue((tqe_t *) &DES_Pw_List_Info, (tqe_t *) apwlist_info);
    }

    /*  Add the crypto key to the server */
    HTTP_Auth_Control(p, WS_A_CREDS, DES_Key_String);

    /*  Add the authentication Universal Resource Locator to login.htm */
    HTTP_Auth_Control(p, WS_A_AUTH_URI, WS_AUTH_SCREEN_URI);
    /*  Set the Authentication State to in process */
    p->ws_master.ws_auth_state = 1;
    
    /*  Enable Authentication  */
    HTTP_Auth_Control(p, WS_A_ENABLE, NU_NULL);
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       DES_Rand                                                          
*                                                                       
* DESCRIPTION                                                           
*                                                                       
*       This plugin is called in the authentiction process a 32          
*       byte random number is created the random number is               
*       encrypted with the encryption key and sent to the client         
*       as a hex ascii string.                                           
*                                                                       
* INPUTS                                                                
*                                                                       
*       *env                                                              
*       *req                                                              
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       NU_SUCCESS
*                                                                       
************************************************************************/
static INT DES_Rand(WS_REQUEST *req)
{
    DES_Send_Auth_Salt(req);
    return(WS_REQ_PROCEED);
}

/************************************************************************
*                                                                       
* FUNCTION                                                              
*                                                                       
*       DES_Send_Url                                                       
*                                                                       
* DESCRIPTION                                                           
*        
*                                                               
*                                                                       
* INPUTS                                                                
*                                                                       
*       *env                                                              
*       *req                                                              
*                                                                       
* OUTPUTS                                                               
*                                                                       
*       NU_SUCCESS
*                                                                       
************************************************************************/
static INT DES_Send_Url(WS_REQUEST * req)
{
    CHAR            success[250];
    CHAR            fail[250];
    WPW_INFO_NODE   *apwlist;
    UINT8           cypher_a[17];
    UINT8           cypher_b[17];
    UINT8           xor_mask[17];
    CHAR            temp[10];
    INT             i;
    INT16           found = 0;
    CHAR            *s;
    WS_SERVER       *p;
    
    UTL_Zero(success,250);
    UTL_Zero(fail,250);

    p = &WS_Master_Server; 

    /* get the VALUE string from the POST request */
    s = HTTP_Token_Value_by_Name("VALUE", req);

#ifdef NU_WEBSERV_DEBUG
    printf("VALUE= %s\n",s);
#endif

    /* convert the user ( cypher_a)
     * and the passwd ( cypher_b)
     * and the mask ( xor_mask) 
     * to binary
     */ 

    HTTP_Packed_Hex_To_Bin((CHAR *)cypher_a, s, 32);
    HTTP_Packed_Hex_To_Bin((CHAR *)cypher_b, s + 32, 32);
    HTTP_Packed_Hex_To_Bin((CHAR *)xor_mask, s + 64, 32);
#ifdef NU_WEBSERV_DEBUG
    printf("cypher_a = %s\n", cypher_a);
    printf("cypher_b = %s\n", cypher_b);
    printf("xor_mask = %s\n", xor_mask);
#endif

#ifdef NU_WEBSERV_DEBUG
    printf("KEY = %s\n", req->ws_server->ws_master.ws_key);
#endif

    /* decrypt each string */

    ENC_Decrypt((CHAR *)req->ws_server->ws_master.ws_key,(CHAR *)xor_mask,2);
    ENC_Decrypt((CHAR *)req->ws_server->ws_master.ws_key,(CHAR *)cypher_a,2);
    ENC_Decrypt((CHAR *)req->ws_server->ws_master.ws_key,(CHAR *)cypher_b,2);
#ifdef NU_WEBSERV_DEBUG
    printf("cypher_a = %s\n", cypher_a);
    printf("cypher_b = %s\n", cypher_b);
    printf("xor_mask = %s\n", xor_mask);
#endif

    /* XOR our decrypted user and passwd with the 
     * decrypted salt
     */

    for(i=0; i<16; i++)
    {
        cypher_a[i] ^= xor_mask[i];
        cypher_b[i] ^= xor_mask[i];
    }

    cypher_a[16]=0;                                 /* make sure there is a null */
    cypher_b[16]=0;                                 /* in case we decrypt to garbage */

#ifdef NU_WEBSERV_DEBUG
    printf("string 1:%s\n",cypher_a);
    printf("string 2:%s\n",cypher_b);
#endif

    /*  Traverse through Pw list structure to see if user is verified. */
    for(apwlist = DES_Pw_List_Info.wpw_list_head ; apwlist ; apwlist = apwlist->wpw_list_next)
    {
        if(strncmp((CHAR *)apwlist->wpw_user,(CHAR *)cypher_a,strlen(apwlist->wpw_user)) == 0 )
        {
            if(strncmp((CHAR *)apwlist->wpw_password,(CHAR *)cypher_b,strlen(apwlist->wpw_password))==0)
            {
                found++;
                break;
            }
        }
    }

    if(found)
    {
        /*  Enable Authentication  */
        HTTP_Auth_Control(p, WS_A_ENABLE, NU_NULL);
        
        /*  Add the authenticated IP address */
        HTTP_Auth_Control(p, WS_A_AUTH_ADD, req->ws_ip);

        /*
         * Send to the Java applet the Redirection Ip address. 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -