challenge.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * challenge.c
  3. * ptunnel is licensed under the BSD license:
  4. *
  5. * Copyright (c) 2004-2011, Daniel Stoedle <daniels@cs.uit.no>,
  6. * Yellow Lemon Software. All rights reserved.
  7. *
  8. * Copyright (c) 2017-2019, Toni Uhlig <matzeton@googlemail.com>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. *
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. *
  20. * - Neither the name of the Yellow Lemon Software nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Contacting the author:
  37. * You can get in touch with me, Daniel Stødle (that's the Norwegian letter oe,
  38. * in case your text editor didn't realize), here: <daniels@cs.uit.no>
  39. *
  40. * The official ptunnel website is here:
  41. * <http://www.cs.uit.no/~daniels/PingTunnel/>
  42. *
  43. * Note that the source code is best viewed with tabs set to 4 spaces.
  44. */
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <sys/time.h>
  48. #include "challenge.h"
  49. #include "options.h"
  50. #include "md5.h"
  51. #include "utils.h"
  52. /* generate_challenge: Generates a random challenge, incorporating the current
  53. * local timestamp to avoid replay attacks.
  54. */
  55. challenge_t* generate_challenge(void) {
  56. struct timeval tt;
  57. challenge_t *c;
  58. int i;
  59. c = (challenge_t *) calloc(1, sizeof(challenge_t));
  60. gettimeofday(&tt, 0);
  61. c->sec = tt.tv_sec;
  62. c->usec_rnd = tt.tv_usec + pt_random();
  63. for (i=0;i<6;i++)
  64. c->random[i] = pt_random();
  65. return c;
  66. }
  67. /* generate_response: Generates a response to the given challenge. The response
  68. * is generated by combining the concatenating the challenge data with the
  69. * md5 digest of the password, and then calculating the MD5 digest of the
  70. * entire buffer. The result is stored in the passed-in challenge, overwriting
  71. * the challenge data.
  72. */
  73. void generate_response(challenge_t *challenge) {
  74. md5_byte_t buf[sizeof(challenge_t)+kMD5_digest_size];
  75. md5_state_t state;
  76. memcpy(buf, challenge, sizeof(challenge_t));
  77. memcpy(&buf[sizeof(challenge_t)], opts.password_digest, kMD5_digest_size);
  78. memset(challenge, 0, sizeof(challenge_t));
  79. md5_init(&state);
  80. md5_append(&state, buf, sizeof(challenge_t)+kMD5_digest_size);
  81. md5_finish(&state, (md5_byte_t*)challenge);
  82. }
  83. /* validate_challenge: Checks whether a given response matches the expected
  84. * response, returning 1 if validation succeeded, and 0 otherwise. Note that
  85. * overwriting the local challenge with the challenge result is not a problem,
  86. * as the data will not be used again anyway (authentication either succeeds,
  87. * or the connection is closed down).
  88. */
  89. int validate_challenge(challenge_t *local, challenge_t *remote) {
  90. generate_response(local);
  91. if (memcmp(local, remote, sizeof(challenge_t)) == 0)
  92. return 1;
  93. return 0;
  94. }