chap-md5.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * chap-md5.c - New CHAP/MD5 implementation.
  3. *
  4. * Copyright (c) 2003 Paul Mackerras. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. The name(s) of the authors of this software must not be used to
  14. * endorse or promote products derived from this software without
  15. * prior written permission.
  16. *
  17. * 3. Redistributions of any form whatsoever must retain the following
  18. * acknowledgment:
  19. * "This product includes software developed by Paul Mackerras
  20. * <paulus@samba.org>".
  21. *
  22. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  23. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  24. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  25. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  26. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  27. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  28. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  29. */
  30. #define RCSID "$Id: chap-md5.c,v 1.4 2004/11/09 22:39:25 paulus Exp $"
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "pppd.h"
  34. #include "chap-new.h"
  35. #include "chap-md5.h"
  36. #include "magic.h"
  37. #include "md5.h"
  38. #define MD5_HASH_SIZE 16
  39. #define MD5_MIN_CHALLENGE 16
  40. #define MD5_MAX_CHALLENGE 24
  41. static void
  42. chap_md5_generate_challenge(unsigned char *cp)
  43. {
  44. int clen;
  45. clen = (int)(drand48() * (MD5_MAX_CHALLENGE - MD5_MIN_CHALLENGE))
  46. + MD5_MIN_CHALLENGE;
  47. *cp++ = clen;
  48. random_bytes(cp, clen);
  49. }
  50. static int
  51. chap_md5_verify_response(int id, char *name,
  52. unsigned char *secret, int secret_len,
  53. unsigned char *challenge, unsigned char *response,
  54. char *message, int message_space)
  55. {
  56. MD5_CTX ctx;
  57. unsigned char idbyte = id;
  58. unsigned char hash[MD5_HASH_SIZE];
  59. int challenge_len, response_len;
  60. challenge_len = *challenge++;
  61. response_len = *response++;
  62. if (response_len == MD5_HASH_SIZE) {
  63. /* Generate hash of ID, secret, challenge */
  64. MD5_Init(&ctx);
  65. MD5_Update(&ctx, &idbyte, 1);
  66. MD5_Update(&ctx, secret, secret_len);
  67. MD5_Update(&ctx, challenge, challenge_len);
  68. MD5_Final(hash, &ctx);
  69. /* Test if our hash matches the peer's response */
  70. if (memcmp(hash, response, MD5_HASH_SIZE) == 0) {
  71. slprintf(message, message_space, "Access granted");
  72. return 1;
  73. }
  74. }
  75. slprintf(message, message_space, "Access denied");
  76. return 0;
  77. }
  78. static void
  79. chap_md5_make_response(unsigned char *response, int id, char *our_name,
  80. unsigned char *challenge, char *secret, int secret_len,
  81. unsigned char *private)
  82. {
  83. MD5_CTX ctx;
  84. unsigned char idbyte = id;
  85. int challenge_len = *challenge++;
  86. MD5_Init(&ctx);
  87. MD5_Update(&ctx, &idbyte, 1);
  88. MD5_Update(&ctx, (u_char *)secret, secret_len);
  89. MD5_Update(&ctx, challenge, challenge_len);
  90. MD5_Final(&response[1], &ctx);
  91. response[0] = MD5_HASH_SIZE;
  92. }
  93. static struct chap_digest_type md5_digest = {
  94. CHAP_MD5, /* code */
  95. chap_md5_generate_challenge,
  96. chap_md5_verify_response,
  97. chap_md5_make_response,
  98. NULL, /* check_success */
  99. NULL, /* handle_failure */
  100. };
  101. void
  102. chap_md5_init(void)
  103. {
  104. chap_register_digest(&md5_digest);
  105. }