1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * RFC4616 PLAIN authentication
22 * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
24 ***************************************************************************/
26 #include "curl_setup.h"
28 #include <curl/curl.h>
31 #include "vauth/vauth.h"
32 #include "curl_base64.h"
37 #include "curl_printf.h"
39 /* The last #include files should be: */
40 #include "curl_memory.h"
44 * Curl_auth_create_plain_message()
46 * This is used to generate an already encoded PLAIN message ready
47 * for sending to the recipient.
51 * data [in] - The session handle.
52 * userp [in] - The user name.
53 * passdwp [in] - The user's password.
54 * outptr [in/out] - The address where a pointer to newly allocated memory
55 * holding the result will be stored upon completion.
56 * outlen [out] - The length of the output message.
58 * Returns CURLE_OK on success.
60 CURLcode Curl_auth_create_plain_message(struct Curl_easy *data,
63 char **outptr, size_t *outlen)
74 plen = strlen(passwdp);
76 /* Compute binary message length, checking for overflows. */
79 return CURLE_OUT_OF_MEMORY;
82 return CURLE_OUT_OF_MEMORY;
85 return CURLE_OUT_OF_MEMORY;
87 plainauth = malloc(plainlen);
89 return CURLE_OUT_OF_MEMORY;
91 /* Calculate the reply */
92 memcpy(plainauth, userp, ulen);
93 plainauth[ulen] = '\0';
94 memcpy(plainauth + ulen + 1, userp, ulen);
95 plainauth[2 * ulen + 1] = '\0';
96 memcpy(plainauth + 2 * ulen + 2, passwdp, plen);
98 /* Base64 encode the reply */
99 result = Curl_base64_encode(data, plainauth, plainlen, outptr, outlen);
106 * Curl_auth_create_login_message()
108 * This is used to generate an already encoded LOGIN message containing the
109 * user name or password ready for sending to the recipient.
113 * data [in] - The session handle.
114 * valuep [in] - The user name or user's password.
115 * outptr [in/out] - The address where a pointer to newly allocated memory
116 * holding the result will be stored upon completion.
117 * outlen [out] - The length of the output message.
119 * Returns CURLE_OK on success.
121 CURLcode Curl_auth_create_login_message(struct Curl_easy *data,
122 const char *valuep, char **outptr,
125 size_t vlen = strlen(valuep);
128 /* Calculate an empty reply */
129 *outptr = strdup("=");
131 *outlen = (size_t) 1;
136 return CURLE_OUT_OF_MEMORY;
139 /* Base64 encode the value */
140 return Curl_base64_encode(data, valuep, vlen, outptr, outlen);
144 * Curl_auth_create_external_message()
146 * This is used to generate an already encoded EXTERNAL message containing
147 * the user name ready for sending to the recipient.
151 * data [in] - The session handle.
152 * user [in] - The user name.
153 * outptr [in/out] - The address where a pointer to newly allocated memory
154 * holding the result will be stored upon completion.
155 * outlen [out] - The length of the output message.
157 * Returns CURLE_OK on success.
159 CURLcode Curl_auth_create_external_message(struct Curl_easy *data,
160 const char *user, char **outptr,
163 /* This is the same formatting as the login message */
164 return Curl_auth_create_login_message(data, user, outptr, outlen);