yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2016, 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.haxx.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 | * RFC4616 PLAIN authentication |
| 22 | * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt> |
| 23 | * |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #include "curl_setup.h" |
| 27 | |
| 28 | #include <curl/curl.h> |
| 29 | #include "urldata.h" |
| 30 | |
| 31 | #include "vauth/vauth.h" |
| 32 | #include "curl_base64.h" |
| 33 | #include "curl_md5.h" |
| 34 | #include "warnless.h" |
| 35 | #include "strtok.h" |
| 36 | #include "sendf.h" |
| 37 | #include "curl_printf.h" |
| 38 | |
| 39 | /* The last #include files should be: */ |
| 40 | #include "curl_memory.h" |
| 41 | #include "memdebug.h" |
| 42 | |
| 43 | #define SIZE_T_MAX UINT_MAX |
| 44 | |
| 45 | /* |
| 46 | * Curl_auth_create_plain_message() |
| 47 | * |
| 48 | * This is used to generate an already encoded PLAIN message ready |
| 49 | * for sending to the recipient. |
| 50 | * |
| 51 | * Parameters: |
| 52 | * |
| 53 | * data [in] - The session handle. |
| 54 | * userp [in] - The user name. |
| 55 | * passdwp [in] - The user's password. |
| 56 | * outptr [in/out] - The address where a pointer to newly allocated memory |
| 57 | * holding the result will be stored upon completion. |
| 58 | * outlen [out] - The length of the output message. |
| 59 | * |
| 60 | * Returns CURLE_OK on success. |
| 61 | */ |
| 62 | CURLcode Curl_auth_create_plain_message(struct Curl_easy *data, |
| 63 | const char *userp, |
| 64 | const char *passwdp, |
| 65 | char **outptr, size_t *outlen) |
| 66 | { |
| 67 | CURLcode result; |
| 68 | char *plainauth; |
| 69 | size_t ulen; |
| 70 | size_t plen; |
| 71 | size_t plainlen; |
| 72 | |
| 73 | *outlen = 0; |
| 74 | *outptr = NULL; |
| 75 | ulen = strlen(userp); |
| 76 | plen = strlen(passwdp); |
| 77 | |
| 78 | /* Compute binary message length, checking for overflows. */ |
| 79 | //hub CVE-2018-16839 |
| 80 | if((ulen > SIZE_T_MAX/4) || (plen > (SIZE_T_MAX/2 - 2))) |
| 81 | return CURLE_OUT_OF_MEMORY; |
| 82 | |
| 83 | plainlen = 2 * ulen; |
| 84 | if(plainlen < ulen) |
| 85 | return CURLE_OUT_OF_MEMORY; |
| 86 | plainlen += plen; |
| 87 | if(plainlen < plen) |
| 88 | return CURLE_OUT_OF_MEMORY; |
| 89 | plainlen += 2; |
| 90 | if(plainlen < 2) |
| 91 | return CURLE_OUT_OF_MEMORY; |
| 92 | |
| 93 | plainauth = malloc(plainlen); |
| 94 | if(!plainauth) |
| 95 | return CURLE_OUT_OF_MEMORY; |
| 96 | |
| 97 | /* Calculate the reply */ |
| 98 | memcpy(plainauth, userp, ulen); |
| 99 | plainauth[ulen] = '\0'; |
| 100 | memcpy(plainauth + ulen + 1, userp, ulen); |
| 101 | plainauth[2 * ulen + 1] = '\0'; |
| 102 | memcpy(plainauth + 2 * ulen + 2, passwdp, plen); |
| 103 | |
| 104 | /* Base64 encode the reply */ |
| 105 | result = Curl_base64_encode(data, plainauth, plainlen, outptr, outlen); |
| 106 | free(plainauth); |
| 107 | |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Curl_auth_create_login_message() |
| 113 | * |
| 114 | * This is used to generate an already encoded LOGIN message containing the |
| 115 | * user name or password ready for sending to the recipient. |
| 116 | * |
| 117 | * Parameters: |
| 118 | * |
| 119 | * data [in] - The session handle. |
| 120 | * valuep [in] - The user name or user's password. |
| 121 | * outptr [in/out] - The address where a pointer to newly allocated memory |
| 122 | * holding the result will be stored upon completion. |
| 123 | * outlen [out] - The length of the output message. |
| 124 | * |
| 125 | * Returns CURLE_OK on success. |
| 126 | */ |
| 127 | CURLcode Curl_auth_create_login_message(struct Curl_easy *data, |
| 128 | const char *valuep, char **outptr, |
| 129 | size_t *outlen) |
| 130 | { |
| 131 | size_t vlen = strlen(valuep); |
| 132 | |
| 133 | if(!vlen) { |
| 134 | /* Calculate an empty reply */ |
| 135 | *outptr = strdup("="); |
| 136 | if(*outptr) { |
| 137 | *outlen = (size_t) 1; |
| 138 | return CURLE_OK; |
| 139 | } |
| 140 | |
| 141 | *outlen = 0; |
| 142 | return CURLE_OUT_OF_MEMORY; |
| 143 | } |
| 144 | |
| 145 | /* Base64 encode the value */ |
| 146 | return Curl_base64_encode(data, valuep, vlen, outptr, outlen); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Curl_auth_create_external_message() |
| 151 | * |
| 152 | * This is used to generate an already encoded EXTERNAL message containing |
| 153 | * the user name ready for sending to the recipient. |
| 154 | * |
| 155 | * Parameters: |
| 156 | * |
| 157 | * data [in] - The session handle. |
| 158 | * user [in] - The user name. |
| 159 | * outptr [in/out] - The address where a pointer to newly allocated memory |
| 160 | * holding the result will be stored upon completion. |
| 161 | * outlen [out] - The length of the output message. |
| 162 | * |
| 163 | * Returns CURLE_OK on success. |
| 164 | */ |
| 165 | CURLcode Curl_auth_create_external_message(struct Curl_easy *data, |
| 166 | const char *user, char **outptr, |
| 167 | size_t *outlen) |
| 168 | { |
| 169 | /* This is the same formatting as the login message */ |
| 170 | return Curl_auth_create_login_message(data, user, outptr, outlen); |
| 171 | } |