cram.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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. * RFC2195 CRAM-MD5 authentication
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_CRYPTO_AUTH)
  26. #include <curl/curl.h>
  27. #include "urldata.h"
  28. #include "vauth/vauth.h"
  29. #include "curl_hmac.h"
  30. #include "curl_md5.h"
  31. #include "warnless.h"
  32. #include "curl_printf.h"
  33. /* The last #include files should be: */
  34. #include "curl_memory.h"
  35. #include "memdebug.h"
  36. /*
  37. * Curl_auth_create_cram_md5_message()
  38. *
  39. * This is used to generate a CRAM-MD5 response message ready for sending to
  40. * the recipient.
  41. *
  42. * Parameters:
  43. *
  44. * chlg [in] - The challenge.
  45. * userp [in] - The user name.
  46. * passwdp [in] - The user's password.
  47. * out [out] - The result storage.
  48. *
  49. * Returns CURLE_OK on success.
  50. */
  51. CURLcode Curl_auth_create_cram_md5_message(const struct bufref *chlg,
  52. const char *userp,
  53. const char *passwdp,
  54. struct bufref *out)
  55. {
  56. struct HMAC_context *ctxt;
  57. unsigned char digest[MD5_DIGEST_LEN];
  58. char *response;
  59. /* Compute the digest using the password as the key */
  60. ctxt = Curl_HMAC_init(Curl_HMAC_MD5,
  61. (const unsigned char *) passwdp,
  62. curlx_uztoui(strlen(passwdp)));
  63. if(!ctxt)
  64. return CURLE_OUT_OF_MEMORY;
  65. /* Update the digest with the given challenge */
  66. if(Curl_bufref_len(chlg))
  67. Curl_HMAC_update(ctxt, Curl_bufref_ptr(chlg),
  68. curlx_uztoui(Curl_bufref_len(chlg)));
  69. /* Finalise the digest */
  70. Curl_HMAC_final(ctxt, digest);
  71. /* Generate the response */
  72. response = aprintf(
  73. "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  74. userp, digest[0], digest[1], digest[2], digest[3], digest[4],
  75. digest[5], digest[6], digest[7], digest[8], digest[9], digest[10],
  76. digest[11], digest[12], digest[13], digest[14], digest[15]);
  77. if(!response)
  78. return CURLE_OUT_OF_MEMORY;
  79. Curl_bufref_set(out, response, strlen(response), curl_free);
  80. return CURLE_OK;
  81. }
  82. #endif /* !CURL_DISABLE_CRYPTO_AUTH */