2006-11-21 13:26:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
2006-11-21 16:01:12 +00:00
|
|
|
#include <string.h>
|
2006-11-21 13:26:59 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <gpxe/crypto.h>
|
|
|
|
#include <gpxe/chap.h>
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* CHAP protocol
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise CHAP challenge/response
|
|
|
|
*
|
|
|
|
* @v chap CHAP challenge/response
|
|
|
|
* @v digest Digest algorithm to use
|
|
|
|
* @ret rc Return 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().
|
|
|
|
*/
|
|
|
|
int chap_init ( struct chap_challenge *chap,
|
|
|
|
struct digest_algorithm *digest ) {
|
2006-11-21 16:01:12 +00:00
|
|
|
size_t state_len;
|
|
|
|
void *state;
|
|
|
|
|
2006-11-21 13:26:59 +00:00
|
|
|
assert ( chap->digest == NULL );
|
|
|
|
assert ( chap->digest_context == NULL );
|
|
|
|
assert ( chap->response == NULL );
|
|
|
|
|
2006-11-21 16:14:17 +00:00
|
|
|
DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
|
|
|
|
|
2006-11-21 16:01:12 +00:00
|
|
|
state_len = ( digest->context_len + digest->digest_len );
|
|
|
|
state = malloc ( state_len );
|
2006-11-21 16:14:17 +00:00
|
|
|
if ( ! state ) {
|
|
|
|
DBG ( "CHAP %p could not allocate %d bytes for state\n",
|
|
|
|
chap, state_len );
|
2006-11-21 16:01:12 +00:00
|
|
|
return -ENOMEM;
|
2006-11-21 16:14:17 +00:00
|
|
|
}
|
2006-11-21 16:01:12 +00:00
|
|
|
|
2006-11-21 13:26:59 +00:00
|
|
|
chap->digest = digest;
|
2006-11-21 16:01:12 +00:00
|
|
|
chap->digest_context = state;
|
|
|
|
chap->response = ( state + digest->context_len );
|
2006-11-21 13:26:59 +00:00
|
|
|
chap->response_len = digest->digest_len;
|
|
|
|
chap->digest->init ( chap->digest_context );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add data to the CHAP challenge
|
|
|
|
*
|
|
|
|
* @v chap CHAP challenge/response
|
|
|
|
* @v data Data to add
|
|
|
|
* @v len Length of data to add
|
|
|
|
*/
|
|
|
|
void chap_update ( struct chap_challenge *chap, const void *data,
|
|
|
|
size_t len ) {
|
|
|
|
assert ( chap->digest != NULL );
|
|
|
|
assert ( chap->digest_context != NULL );
|
|
|
|
|
2006-11-21 16:14:17 +00:00
|
|
|
if ( ! chap->digest )
|
|
|
|
return;
|
|
|
|
|
2006-11-21 13:26:59 +00:00
|
|
|
chap->digest->update ( chap->digest_context, data, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Respond to the CHAP challenge
|
|
|
|
*
|
|
|
|
* @v chap CHAP challenge/response
|
|
|
|
*
|
|
|
|
* Calculates the final CHAP response value, and places it in @c
|
|
|
|
* chap->response, with a length of @c chap->response_len.
|
|
|
|
*/
|
|
|
|
void chap_respond ( struct chap_challenge *chap ) {
|
|
|
|
assert ( chap->digest != NULL );
|
|
|
|
assert ( chap->digest_context != NULL );
|
|
|
|
assert ( chap->response != NULL );
|
|
|
|
|
2006-11-21 16:14:17 +00:00
|
|
|
DBG ( "CHAP %p responding to challenge\n", chap );
|
|
|
|
|
|
|
|
if ( ! chap->digest )
|
|
|
|
return;
|
|
|
|
|
2006-11-21 13:26:59 +00:00
|
|
|
chap->digest->finish ( chap->digest_context, chap->response );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free resources used by a CHAP challenge/response
|
|
|
|
*
|
|
|
|
* @v chap CHAP challenge/response
|
|
|
|
*/
|
|
|
|
void chap_finish ( struct chap_challenge *chap ) {
|
2006-11-21 16:01:12 +00:00
|
|
|
void *state = chap->digest_context;
|
|
|
|
|
2006-11-21 16:14:17 +00:00
|
|
|
DBG ( "CHAP %p finished\n", chap );
|
|
|
|
|
2006-11-21 16:01:12 +00:00
|
|
|
free ( state );
|
|
|
|
memset ( chap, 0, sizeof ( *chap ) );
|
2006-11-21 13:26:59 +00:00
|
|
|
}
|