1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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 * SPDX-License-Identifier: curl
22 *
23 * RFC4178 Simple and Protected GSS-API Negotiation Mechanism
24 *
25 ***************************************************************************/
26
27 #include "curl_setup.h"
28
29 #if defined(HAVE_GSSAPI) && defined(USE_SPNEGO)
30
31 #include <curl/curl.h>
32
33 #include "vauth/vauth.h"
34 #include "urldata.h"
35 #include "curl_base64.h"
36 #include "curl_gssapi.h"
37 #include "warnless.h"
38 #include "curl_multibyte.h"
39 #include "sendf.h"
40
41 /* The last #include files should be: */
42 #include "curl_memory.h"
43 #include "memdebug.h"
44
45 #if defined(__GNUC__) && defined(__APPLE__)
46 #pragma GCC diagnostic push
47 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
48 #endif
49
50 /*
51 * Curl_auth_is_spnego_supported()
52 *
53 * This is used to evaluate if SPNEGO (Negotiate) is supported.
54 *
55 * Parameters: None
56 *
57 * Returns TRUE if Negotiate supported by the GSS-API library.
58 */
Curl_auth_is_spnego_supported(void)59 bool Curl_auth_is_spnego_supported(void)
60 {
61 return TRUE;
62 }
63
64 /*
65 * Curl_auth_decode_spnego_message()
66 *
67 * This is used to decode an already encoded SPNEGO (Negotiate) challenge
68 * message.
69 *
70 * Parameters:
71 *
72 * data [in] - The session handle.
73 * userp [in] - The username in the format User or Domain\User.
74 * passwdp [in] - The user's password.
75 * service [in] - The service type such as http, smtp, pop or imap.
76 * host [in] - The hostname.
77 * chlg64 [in] - The optional base64 encoded challenge message.
78 * nego [in/out] - The Negotiate data struct being used and modified.
79 *
80 * Returns CURLE_OK on success.
81 */
Curl_auth_decode_spnego_message(struct Curl_easy * data,const char * user,const char * password,const char * service,const char * host,const char * chlg64,struct negotiatedata * nego)82 CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
83 const char *user,
84 const char *password,
85 const char *service,
86 const char *host,
87 const char *chlg64,
88 struct negotiatedata *nego)
89 {
90 CURLcode result = CURLE_OK;
91 size_t chlglen = 0;
92 unsigned char *chlg = NULL;
93 OM_uint32 major_status;
94 OM_uint32 minor_status;
95 OM_uint32 unused_status;
96 gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
97 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
98 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
99 gss_channel_bindings_t chan_bindings = GSS_C_NO_CHANNEL_BINDINGS;
100 struct gss_channel_bindings_struct chan;
101
102 (void) user;
103 (void) password;
104
105 if(nego->context && nego->status == GSS_S_COMPLETE) {
106 /* We finished successfully our part of authentication, but server
107 * rejected it (since we are again here). Exit with an error since we
108 * cannot invent anything better */
109 Curl_auth_cleanup_spnego(nego);
110 return CURLE_LOGIN_DENIED;
111 }
112
113 if(!nego->spn) {
114 /* Generate our SPN */
115 char *spn = Curl_auth_build_spn(service, NULL, host);
116 if(!spn)
117 return CURLE_OUT_OF_MEMORY;
118
119 /* Populate the SPN structure */
120 spn_token.value = spn;
121 spn_token.length = strlen(spn);
122
123 /* Import the SPN */
124 major_status = gss_import_name(&minor_status, &spn_token,
125 GSS_C_NT_HOSTBASED_SERVICE,
126 &nego->spn);
127 if(GSS_ERROR(major_status)) {
128 Curl_gss_log_error(data, "gss_import_name() failed: ",
129 major_status, minor_status);
130
131 free(spn);
132
133 return CURLE_AUTH_ERROR;
134 }
135
136 free(spn);
137 }
138
139 if(chlg64 && *chlg64) {
140 /* Decode the base-64 encoded challenge message */
141 if(*chlg64 != '=') {
142 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
143 if(result)
144 return result;
145 }
146
147 /* Ensure we have a valid challenge message */
148 if(!chlg) {
149 infof(data, "SPNEGO handshake failure (empty challenge message)");
150 return CURLE_BAD_CONTENT_ENCODING;
151 }
152
153 /* Setup the challenge "input" security buffer */
154 input_token.value = chlg;
155 input_token.length = chlglen;
156 }
157
158 /* Set channel binding data if available */
159 if(nego->channel_binding_data.leng > 0) {
160 memset(&chan, 0, sizeof(struct gss_channel_bindings_struct));
161 chan.application_data.length = nego->channel_binding_data.leng;
162 chan.application_data.value = nego->channel_binding_data.bufr;
163 chan_bindings = &chan;
164 }
165
166 /* Generate our challenge-response message */
167 major_status = Curl_gss_init_sec_context(data,
168 &minor_status,
169 &nego->context,
170 nego->spn,
171 &Curl_spnego_mech_oid,
172 chan_bindings,
173 &input_token,
174 &output_token,
175 TRUE,
176 NULL);
177
178 /* Free the decoded challenge as it is not required anymore */
179 Curl_safefree(input_token.value);
180
181 nego->status = major_status;
182 if(GSS_ERROR(major_status)) {
183 if(output_token.value)
184 gss_release_buffer(&unused_status, &output_token);
185
186 Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
187 major_status, minor_status);
188
189 return CURLE_AUTH_ERROR;
190 }
191
192 if(!output_token.value || !output_token.length) {
193 if(output_token.value)
194 gss_release_buffer(&unused_status, &output_token);
195
196 return CURLE_AUTH_ERROR;
197 }
198
199 /* Free previous token */
200 if(nego->output_token.length && nego->output_token.value)
201 gss_release_buffer(&unused_status, &nego->output_token);
202
203 nego->output_token = output_token;
204
205 return CURLE_OK;
206 }
207
208 /*
209 * Curl_auth_create_spnego_message()
210 *
211 * This is used to generate an already encoded SPNEGO (Negotiate) response
212 * message ready for sending to the recipient.
213 *
214 * Parameters:
215 *
216 * data [in] - The session handle.
217 * nego [in/out] - The Negotiate data struct being used and modified.
218 * outptr [in/out] - The address where a pointer to newly allocated memory
219 * holding the result will be stored upon completion.
220 * outlen [out] - The length of the output message.
221 *
222 * Returns CURLE_OK on success.
223 */
Curl_auth_create_spnego_message(struct negotiatedata * nego,char ** outptr,size_t * outlen)224 CURLcode Curl_auth_create_spnego_message(struct negotiatedata *nego,
225 char **outptr, size_t *outlen)
226 {
227 CURLcode result;
228 OM_uint32 minor_status;
229
230 /* Base64 encode the already generated response */
231 result = Curl_base64_encode(nego->output_token.value,
232 nego->output_token.length,
233 outptr, outlen);
234
235 if(result) {
236 gss_release_buffer(&minor_status, &nego->output_token);
237 nego->output_token.value = NULL;
238 nego->output_token.length = 0;
239
240 return result;
241 }
242
243 if(!*outptr || !*outlen) {
244 gss_release_buffer(&minor_status, &nego->output_token);
245 nego->output_token.value = NULL;
246 nego->output_token.length = 0;
247
248 return CURLE_REMOTE_ACCESS_DENIED;
249 }
250
251 return CURLE_OK;
252 }
253
254 /*
255 * Curl_auth_cleanup_spnego()
256 *
257 * This is used to clean up the SPNEGO (Negotiate) specific data.
258 *
259 * Parameters:
260 *
261 * nego [in/out] - The Negotiate data struct being cleaned up.
262 *
263 */
Curl_auth_cleanup_spnego(struct negotiatedata * nego)264 void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
265 {
266 OM_uint32 minor_status;
267
268 /* Free our security context */
269 if(nego->context != GSS_C_NO_CONTEXT) {
270 gss_delete_sec_context(&minor_status, &nego->context, GSS_C_NO_BUFFER);
271 nego->context = GSS_C_NO_CONTEXT;
272 }
273
274 /* Free the output token */
275 if(nego->output_token.value) {
276 gss_release_buffer(&minor_status, &nego->output_token);
277 nego->output_token.value = NULL;
278 nego->output_token.length = 0;
279
280 }
281
282 /* Free the SPN */
283 if(nego->spn != GSS_C_NO_NAME) {
284 gss_release_name(&minor_status, &nego->spn);
285 nego->spn = GSS_C_NO_NAME;
286 }
287
288 /* Reset any variables */
289 nego->status = 0;
290 nego->noauthpersist = FALSE;
291 nego->havenoauthpersist = FALSE;
292 nego->havenegdata = FALSE;
293 nego->havemultiplerequests = FALSE;
294 }
295
296 #if defined(__GNUC__) && defined(__APPLE__)
297 #pragma GCC diagnostic pop
298 #endif
299
300 #endif /* HAVE_GSSAPI && USE_SPNEGO */
301