iPXE
Functions
chap.c File Reference

CHAP protocol. More...

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/crypto.h>
#include <ipxe/chap.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int chap_init (struct chap_response *chap, struct digest_algorithm *digest)
 Initialise CHAP challenge/response.
void chap_update (struct chap_response *chap, const void *data, size_t len)
 Add data to the CHAP challenge.
void chap_respond (struct chap_response *chap)
 Respond to the CHAP challenge.
void chap_finish (struct chap_response *chap)
 Free resources used by a CHAP response.

Detailed Description

CHAP protocol.

Definition in file chap.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int chap_init ( struct chap_response chap,
struct digest_algorithm digest 
)

Initialise CHAP challenge/response.

Parameters:
chapCHAP challenge/response
digestDigest algorithm to use
Return values:
rcReturn status code

Initialises a CHAP challenge/response structure. This routine allocates memory, and so may fail. The allocated memory must eventually be freed by a call to chap_finish().

Definition at line 51 of file chap.c.

References assert, digest_algorithm::ctxsize, DBG, chap_response::digest, digest, chap_response::digest_context, digest_init(), digest_algorithm::digestsize, ENOMEM, malloc(), digest_algorithm::name, NULL, chap_response::response, chap_response::response_len, and state.

Referenced by iscsi_handle_chap_i_value(), and iscsi_handle_chap_r_value().

                                                  {
        size_t state_len;
        void *state;

        assert ( chap->digest == NULL );
        assert ( chap->digest_context == NULL );
        assert ( chap->response == NULL );

        DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );

        state_len = ( digest->ctxsize + digest->digestsize );
        state = malloc ( state_len );
        if ( ! state ) {
                DBG ( "CHAP %p could not allocate %zd bytes for state\n",
                      chap, state_len );
                return -ENOMEM;
        }
        
        chap->digest = digest;
        chap->digest_context = state;
        chap->response = ( state + digest->ctxsize );
        chap->response_len = digest->digestsize;
        digest_init ( chap->digest, chap->digest_context );
        return 0;
}
void chap_update ( struct chap_response chap,
const void *  data,
size_t  len 
)

Add data to the CHAP challenge.

Parameters:
chapCHAP response
dataData to add
lenLength of data to add

Definition at line 85 of file chap.c.

References assert, chap_response::digest, chap_response::digest_context, digest_update(), and NULL.

Referenced by chap_set_identifier(), iscsi_handle_chap_c_value(), iscsi_handle_chap_i_value(), and iscsi_handle_chap_r_value().

                                {
        assert ( chap->digest != NULL );
        assert ( chap->digest_context != NULL );

        if ( ! chap->digest )
                return;

        digest_update ( chap->digest, chap->digest_context, data, len );
}
void chap_respond ( struct chap_response chap)

Respond to the CHAP challenge.

Parameters:
chapCHAP response

Calculates the final CHAP response value, and places it in chap->response, with a length of chap->response_len.

Definition at line 104 of file chap.c.

References assert, DBG, chap_response::digest, chap_response::digest_context, digest_final(), NULL, and chap_response::response.

Referenced by iscsi_handle_chap_c_value(), and iscsi_handle_chap_r_value().

                                                 {
        assert ( chap->digest != NULL );
        assert ( chap->digest_context != NULL );
        assert ( chap->response != NULL );

        DBG ( "CHAP %p responding to challenge\n", chap );

        if ( ! chap->digest )
                return;

        digest_final ( chap->digest, chap->digest_context, chap->response );
}
void chap_finish ( struct chap_response chap)

Free resources used by a CHAP response.

Parameters:
chapCHAP response

Definition at line 122 of file chap.c.

References DBG, chap_response::digest_context, free, memset(), and state.

Referenced by iscsi_close_connection(), iscsi_free(), iscsi_handle_chap_i_value(), iscsi_handle_chap_r_value(), and iscsi_login_request_done().

                                                {
        void *state = chap->digest_context;

        DBG ( "CHAP %p finished\n", chap );

        free ( state );
        memset ( chap, 0, sizeof ( *chap ) );
}