Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

chap.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stddef.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <gpxe/crypto.h>
  25. #include <gpxe/chap.h>
  26. /** @file
  27. *
  28. * CHAP protocol
  29. *
  30. */
  31. /**
  32. * Initialise CHAP challenge/response
  33. *
  34. * @v chap CHAP challenge/response
  35. * @v digest Digest algorithm to use
  36. * @ret rc Return status code
  37. *
  38. * Initialises a CHAP challenge/response structure. This routine
  39. * allocates memory, and so may fail. The allocated memory must
  40. * eventually be freed by a call to chap_finish().
  41. */
  42. int chap_init ( struct chap_response *chap,
  43. struct digest_algorithm *digest ) {
  44. size_t state_len;
  45. void *state;
  46. assert ( chap->digest == NULL );
  47. assert ( chap->digest_context == NULL );
  48. assert ( chap->response == NULL );
  49. DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
  50. state_len = ( digest->ctxsize + digest->digestsize );
  51. state = malloc ( state_len );
  52. if ( ! state ) {
  53. DBG ( "CHAP %p could not allocate %zd bytes for state\n",
  54. chap, state_len );
  55. return -ENOMEM;
  56. }
  57. chap->digest = digest;
  58. chap->digest_context = state;
  59. chap->response = ( state + digest->ctxsize );
  60. chap->response_len = digest->digestsize;
  61. digest_init ( chap->digest, chap->digest_context );
  62. return 0;
  63. }
  64. /**
  65. * Add data to the CHAP challenge
  66. *
  67. * @v chap CHAP response
  68. * @v data Data to add
  69. * @v len Length of data to add
  70. */
  71. void chap_update ( struct chap_response *chap, const void *data,
  72. size_t len ) {
  73. assert ( chap->digest != NULL );
  74. assert ( chap->digest_context != NULL );
  75. if ( ! chap->digest )
  76. return;
  77. digest_update ( chap->digest, chap->digest_context, data, len );
  78. }
  79. /**
  80. * Respond to the CHAP challenge
  81. *
  82. * @v chap CHAP response
  83. *
  84. * Calculates the final CHAP response value, and places it in @c
  85. * chap->response, with a length of @c chap->response_len.
  86. */
  87. void chap_respond ( struct chap_response *chap ) {
  88. assert ( chap->digest != NULL );
  89. assert ( chap->digest_context != NULL );
  90. assert ( chap->response != NULL );
  91. DBG ( "CHAP %p responding to challenge\n", chap );
  92. if ( ! chap->digest )
  93. return;
  94. digest_final ( chap->digest, chap->digest_context, chap->response );
  95. }
  96. /**
  97. * Free resources used by a CHAP response
  98. *
  99. * @v chap CHAP response
  100. */
  101. void chap_finish ( struct chap_response *chap ) {
  102. void *state = chap->digest_context;
  103. DBG ( "CHAP %p finished\n", chap );
  104. free ( state );
  105. memset ( chap, 0, sizeof ( *chap ) );
  106. }