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