Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

chap.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <assert.h>
  29. #include <ipxe/crypto.h>
  30. #include <ipxe/chap.h>
  31. /** @file
  32. *
  33. * CHAP protocol
  34. *
  35. */
  36. /**
  37. * Initialise CHAP challenge/response
  38. *
  39. * @v chap CHAP challenge/response
  40. * @v digest Digest algorithm to use
  41. * @ret rc Return status code
  42. *
  43. * Initialises a CHAP challenge/response structure. This routine
  44. * allocates memory, and so may fail. The allocated memory must
  45. * eventually be freed by a call to chap_finish().
  46. */
  47. int chap_init ( struct chap_response *chap,
  48. struct digest_algorithm *digest ) {
  49. size_t state_len;
  50. void *state;
  51. assert ( chap->digest == NULL );
  52. assert ( chap->digest_context == NULL );
  53. assert ( chap->response == NULL );
  54. DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
  55. state_len = ( digest->ctxsize + digest->digestsize );
  56. state = malloc ( state_len );
  57. if ( ! state ) {
  58. DBG ( "CHAP %p could not allocate %zd bytes for state\n",
  59. chap, state_len );
  60. return -ENOMEM;
  61. }
  62. chap->digest = digest;
  63. chap->digest_context = state;
  64. chap->response = ( state + digest->ctxsize );
  65. chap->response_len = digest->digestsize;
  66. digest_init ( chap->digest, chap->digest_context );
  67. return 0;
  68. }
  69. /**
  70. * Add data to the CHAP challenge
  71. *
  72. * @v chap CHAP response
  73. * @v data Data to add
  74. * @v len Length of data to add
  75. */
  76. void chap_update ( struct chap_response *chap, const void *data,
  77. size_t len ) {
  78. assert ( chap->digest != NULL );
  79. assert ( chap->digest_context != NULL );
  80. if ( ! chap->digest )
  81. return;
  82. digest_update ( chap->digest, chap->digest_context, data, len );
  83. }
  84. /**
  85. * Respond to the CHAP challenge
  86. *
  87. * @v chap CHAP response
  88. *
  89. * Calculates the final CHAP response value, and places it in @c
  90. * chap->response, with a length of @c chap->response_len.
  91. */
  92. void chap_respond ( struct chap_response *chap ) {
  93. assert ( chap->digest != NULL );
  94. assert ( chap->digest_context != NULL );
  95. assert ( chap->response != NULL );
  96. DBG ( "CHAP %p responding to challenge\n", chap );
  97. if ( ! chap->digest )
  98. return;
  99. digest_final ( chap->digest, chap->digest_context, chap->response );
  100. }
  101. /**
  102. * Free resources used by a CHAP response
  103. *
  104. * @v chap CHAP response
  105. */
  106. void chap_finish ( struct chap_response *chap ) {
  107. void *state = chap->digest_context;
  108. DBG ( "CHAP %p finished\n", chap );
  109. free ( state );
  110. memset ( chap, 0, sizeof ( *chap ) );
  111. }