cram.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. * RFC2195 CRAM-MD5 authentication
  24. *
  25. ***************************************************************************/
  26. #include "../curl_setup.h"
  27. #ifndef CURL_DISABLE_DIGEST_AUTH
  28. #include <curl/curl.h>
  29. #include "../urldata.h"
  30. #include "vauth.h"
  31. #include "../curl_hmac.h"
  32. #include "../curl_md5.h"
  33. #include "../curlx/warnless.h"
  34. /* The last #include files should be: */
  35. #include "../curl_memory.h"
  36. #include "../memdebug.h"
  37. /*
  38. * Curl_auth_create_cram_md5_message()
  39. *
  40. * This is used to generate a CRAM-MD5 response message ready for sending to
  41. * the recipient.
  42. *
  43. * Parameters:
  44. *
  45. * chlg [in] - The challenge.
  46. * userp [in] - The username.
  47. * passwdp [in] - The user's password.
  48. * out [out] - The result storage.
  49. *
  50. * Returns CURLE_OK on success.
  51. */
  52. CURLcode Curl_auth_create_cram_md5_message(const struct bufref *chlg,
  53. const char *userp,
  54. const char *passwdp,
  55. struct bufref *out)
  56. {
  57. struct HMAC_context *ctxt;
  58. unsigned char digest[MD5_DIGEST_LEN];
  59. char *response;
  60. /* Compute the digest using the password as the key */
  61. ctxt = Curl_HMAC_init(&Curl_HMAC_MD5,
  62. (const unsigned char *) passwdp,
  63. curlx_uztoui(strlen(passwdp)));
  64. if(!ctxt)
  65. return CURLE_OUT_OF_MEMORY;
  66. /* Update the digest with the given challenge */
  67. if(Curl_bufref_len(chlg))
  68. Curl_HMAC_update(ctxt, Curl_bufref_ptr(chlg),
  69. curlx_uztoui(Curl_bufref_len(chlg)));
  70. /* Finalise the digest */
  71. Curl_HMAC_final(ctxt, digest);
  72. /* Generate the response */
  73. response = curl_maprintf(
  74. "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  75. userp, digest[0], digest[1], digest[2], digest[3], digest[4],
  76. digest[5], digest[6], digest[7], digest[8], digest[9], digest[10],
  77. digest[11], digest[12], digest[13], digest[14], digest[15]);
  78. if(!response)
  79. return CURLE_OUT_OF_MEMORY;
  80. Curl_bufref_set(out, response, strlen(response), curl_free);
  81. return CURLE_OK;
  82. }
  83. #endif /* !CURL_DISABLE_DIGEST_AUTH */