1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2018, 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 /*
44 * Curl_auth_create_plain_message()
45 *
46 * This is used to generate an already encoded PLAIN message ready
47 * for sending to the recipient.
48 *
49 * Parameters:
50 *
51 * data [in] - The session handle.
52 * userp [in] - The user name.
53 * passwdp [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.
57 *
58 * Returns CURLE_OK on success.
59 */
Curl_auth_create_plain_message(struct Curl_easy * data,const char * userp,const char * passwdp,char ** outptr,size_t * outlen)60 CURLcode Curl_auth_create_plain_message(struct Curl_easy *data,
61 const char *userp,
62 const char *passwdp,
63 char **outptr, size_t *outlen)
64 {
65 CURLcode result;
66 char *plainauth;
67 size_t ulen;
68 size_t plen;
69 size_t plainlen;
70
71 *outlen = 0;
72 *outptr = NULL;
73 ulen = strlen(userp);
74 plen = strlen(passwdp);
75
76 /* Compute binary message length. Check for overflows. */
77 if((ulen > SIZE_T_MAX/4) || (plen > (SIZE_T_MAX/2 - 2)))
78 return CURLE_OUT_OF_MEMORY;
79 plainlen = 2 * ulen + plen + 2;
80
81 plainauth = malloc(plainlen);
82 if(!plainauth)
83 return CURLE_OUT_OF_MEMORY;
84
85 /* Calculate the reply */
86 memcpy(plainauth, userp, ulen);
87 plainauth[ulen] = '\0';
88 memcpy(plainauth + ulen + 1, userp, ulen);
89 plainauth[2 * ulen + 1] = '\0';
90 memcpy(plainauth + 2 * ulen + 2, passwdp, plen);
91
92 /* Base64 encode the reply */
93 result = Curl_base64_encode(data, plainauth, plainlen, outptr, outlen);
94 free(plainauth);
95
96 return result;
97 }
98
99 /*
100 * Curl_auth_create_login_message()
101 *
102 * This is used to generate an already encoded LOGIN message containing the
103 * user name or password ready for sending to the recipient.
104 *
105 * Parameters:
106 *
107 * data [in] - The session handle.
108 * valuep [in] - The user name or user's password.
109 * outptr [in/out] - The address where a pointer to newly allocated memory
110 * holding the result will be stored upon completion.
111 * outlen [out] - The length of the output message.
112 *
113 * Returns CURLE_OK on success.
114 */
Curl_auth_create_login_message(struct Curl_easy * data,const char * valuep,char ** outptr,size_t * outlen)115 CURLcode Curl_auth_create_login_message(struct Curl_easy *data,
116 const char *valuep, char **outptr,
117 size_t *outlen)
118 {
119 size_t vlen = strlen(valuep);
120
121 if(!vlen) {
122 /* Calculate an empty reply */
123 *outptr = strdup("=");
124 if(*outptr) {
125 *outlen = (size_t) 1;
126 return CURLE_OK;
127 }
128
129 *outlen = 0;
130 return CURLE_OUT_OF_MEMORY;
131 }
132
133 /* Base64 encode the value */
134 return Curl_base64_encode(data, valuep, vlen, outptr, outlen);
135 }
136
137 /*
138 * Curl_auth_create_external_message()
139 *
140 * This is used to generate an already encoded EXTERNAL message containing
141 * the user name ready for sending to the recipient.
142 *
143 * Parameters:
144 *
145 * data [in] - The session handle.
146 * user [in] - The user name.
147 * outptr [in/out] - The address where a pointer to newly allocated memory
148 * holding the result will be stored upon completion.
149 * outlen [out] - The length of the output message.
150 *
151 * Returns CURLE_OK on success.
152 */
Curl_auth_create_external_message(struct Curl_easy * data,const char * user,char ** outptr,size_t * outlen)153 CURLcode Curl_auth_create_external_message(struct Curl_easy *data,
154 const char *user, char **outptr,
155 size_t *outlen)
156 {
157 /* This is the same formatting as the login message */
158 return Curl_auth_create_login_message(data, user, outptr, outlen);
159 }
160