Coverage Report

Created: 2024-02-25 06:14

/src/PROJ/curl/lib/curl_sasl.c
Line
Count
Source (jump to first uncovered line)
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
 * RFC2195 CRAM-MD5 authentication
24
 * RFC2617 Basic and Digest Access Authentication
25
 * RFC2831 DIGEST-MD5 authentication
26
 * RFC4422 Simple Authentication and Security Layer (SASL)
27
 * RFC4616 PLAIN authentication
28
 * RFC5802 SCRAM-SHA-1 authentication
29
 * RFC7677 SCRAM-SHA-256 authentication
30
 * RFC6749 OAuth 2.0 Authorization Framework
31
 * RFC7628 A Set of SASL Mechanisms for OAuth
32
 * Draft   LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
33
 *
34
 ***************************************************************************/
35
36
#include "curl_setup.h"
37
38
#if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
39
  !defined(CURL_DISABLE_POP3) || \
40
  (!defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP))
41
42
#include <curl/curl.h>
43
#include "urldata.h"
44
45
#include "curl_base64.h"
46
#include "curl_md5.h"
47
#include "vauth/vauth.h"
48
#include "cfilters.h"
49
#include "vtls/vtls.h"
50
#include "curl_hmac.h"
51
#include "curl_sasl.h"
52
#include "warnless.h"
53
#include "strtok.h"
54
#include "sendf.h"
55
/* The last 3 #include files should be in this order */
56
#include "curl_printf.h"
57
#include "curl_memory.h"
58
#include "memdebug.h"
59
60
/* Supported mechanisms */
61
static const struct {
62
  const char    *name;  /* Name */
63
  size_t         len;   /* Name length */
64
  unsigned short bit;   /* Flag bit */
65
} mechtable[] = {
66
  { "LOGIN",        5,  SASL_MECH_LOGIN },
67
  { "PLAIN",        5,  SASL_MECH_PLAIN },
68
  { "CRAM-MD5",     8,  SASL_MECH_CRAM_MD5 },
69
  { "DIGEST-MD5",   10, SASL_MECH_DIGEST_MD5 },
70
  { "GSSAPI",       6,  SASL_MECH_GSSAPI },
71
  { "EXTERNAL",     8,  SASL_MECH_EXTERNAL },
72
  { "NTLM",         4,  SASL_MECH_NTLM },
73
  { "XOAUTH2",      7,  SASL_MECH_XOAUTH2 },
74
  { "OAUTHBEARER",  11, SASL_MECH_OAUTHBEARER },
75
  { "SCRAM-SHA-1",  11, SASL_MECH_SCRAM_SHA_1 },
76
  { "SCRAM-SHA-256",13, SASL_MECH_SCRAM_SHA_256 },
77
  { ZERO_NULL,      0,  0 }
78
};
79
80
/*
81
 * Curl_sasl_cleanup()
82
 *
83
 * This is used to cleanup any libraries or curl modules used by the sasl
84
 * functions.
85
 *
86
 * Parameters:
87
 *
88
 * conn     [in]     - The connection data.
89
 * authused [in]     - The authentication mechanism used.
90
 */
91
void Curl_sasl_cleanup(struct connectdata *conn, unsigned short authused)
92
0
{
93
0
  (void)conn;
94
0
  (void)authused;
95
96
#if defined(USE_KERBEROS5)
97
  /* Cleanup the gssapi structure */
98
  if(authused == SASL_MECH_GSSAPI) {
99
    Curl_auth_cleanup_gssapi(&conn->krb5);
100
  }
101
#endif
102
103
#if defined(USE_GSASL)
104
  /* Cleanup the GSASL structure */
105
  if(authused & (SASL_MECH_SCRAM_SHA_1 | SASL_MECH_SCRAM_SHA_256)) {
106
    Curl_auth_gsasl_cleanup(&conn->gsasl);
107
  }
108
#endif
109
110
0
#if defined(USE_NTLM)
111
  /* Cleanup the NTLM structure */
112
0
  if(authused == SASL_MECH_NTLM) {
113
0
    Curl_auth_cleanup_ntlm(&conn->ntlm);
114
0
  }
115
0
#endif
116
0
}
117
118
/*
119
 * Curl_sasl_decode_mech()
120
 *
121
 * Convert a SASL mechanism name into a token.
122
 *
123
 * Parameters:
124
 *
125
 * ptr    [in]     - The mechanism string.
126
 * maxlen [in]     - Maximum mechanism string length.
127
 * len    [out]    - If not NULL, effective name length.
128
 *
129
 * Returns the SASL mechanism token or 0 if no match.
130
 */
131
unsigned short Curl_sasl_decode_mech(const char *ptr, size_t maxlen,
132
                                     size_t *len)
133
0
{
134
0
  unsigned int i;
135
0
  char c;
136
137
0
  for(i = 0; mechtable[i].name; i++) {
138
0
    if(maxlen >= mechtable[i].len &&
139
0
       !memcmp(ptr, mechtable[i].name, mechtable[i].len)) {
140
0
      if(len)
141
0
        *len = mechtable[i].len;
142
143
0
      if(maxlen == mechtable[i].len)
144
0
        return mechtable[i].bit;
145
146
0
      c = ptr[mechtable[i].len];
147
0
      if(!ISUPPER(c) && !ISDIGIT(c) && c != '-' && c != '_')
148
0
        return mechtable[i].bit;
149
0
    }
150
0
  }
151
152
0
  return 0;
153
0
}
154
155
/*
156
 * Curl_sasl_parse_url_auth_option()
157
 *
158
 * Parse the URL login options.
159
 */
160
CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
161
                                         const char *value, size_t len)
162
0
{
163
0
  CURLcode result = CURLE_OK;
164
0
  size_t mechlen;
165
166
0
  if(!len)
167
0
    return CURLE_URL_MALFORMAT;
168
169
0
  if(sasl->resetprefs) {
170
0
    sasl->resetprefs = FALSE;
171
0
    sasl->prefmech = SASL_AUTH_NONE;
172
0
  }
173
174
0
  if(!strncmp(value, "*", len))
175
0
    sasl->prefmech = SASL_AUTH_DEFAULT;
176
0
  else {
177
0
    unsigned short mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
178
0
    if(mechbit && mechlen == len)
179
0
      sasl->prefmech |= mechbit;
180
0
    else
181
0
      result = CURLE_URL_MALFORMAT;
182
0
  }
183
184
0
  return result;
185
0
}
186
187
/*
188
 * Curl_sasl_init()
189
 *
190
 * Initializes the SASL structure.
191
 */
192
void Curl_sasl_init(struct SASL *sasl, struct Curl_easy *data,
193
                    const struct SASLproto *params)
194
0
{
195
0
  unsigned long auth = data->set.httpauth;
196
197
0
  sasl->params = params;           /* Set protocol dependent parameters */
198
0
  sasl->state = SASL_STOP;         /* Not yet running */
199
0
  sasl->curmech = NULL;            /* No mechanism yet. */
200
0
  sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
201
0
  sasl->prefmech = params->defmechs; /* Default preferred mechanisms */
202
0
  sasl->authused = SASL_AUTH_NONE; /* The authentication mechanism used */
203
0
  sasl->resetprefs = TRUE;         /* Reset prefmech upon AUTH parsing. */
204
0
  sasl->mutual_auth = FALSE;       /* No mutual authentication (GSSAPI only) */
205
0
  sasl->force_ir = FALSE;          /* Respect external option */
206
207
0
  if(auth != CURLAUTH_BASIC) {
208
0
    unsigned short mechs = SASL_AUTH_NONE;
209
210
    /* If some usable http authentication options have been set, determine
211
       new defaults from them. */
212
0
    if(auth & CURLAUTH_BASIC)
213
0
      mechs |= SASL_MECH_PLAIN | SASL_MECH_LOGIN;
214
0
    if(auth & CURLAUTH_DIGEST)
215
0
      mechs |= SASL_MECH_DIGEST_MD5;
216
0
    if(auth & CURLAUTH_NTLM)
217
0
      mechs |= SASL_MECH_NTLM;
218
0
    if(auth & CURLAUTH_BEARER)
219
0
      mechs |= SASL_MECH_OAUTHBEARER | SASL_MECH_XOAUTH2;
220
0
    if(auth & CURLAUTH_GSSAPI)
221
0
      mechs |= SASL_MECH_GSSAPI;
222
223
0
    if(mechs != SASL_AUTH_NONE)
224
0
      sasl->prefmech = mechs;
225
0
  }
226
0
}
227
228
/*
229
 * sasl_state()
230
 *
231
 * This is the ONLY way to change SASL state!
232
 */
233
static void sasl_state(struct SASL *sasl, struct Curl_easy *data,
234
                       saslstate newstate)
235
0
{
236
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
237
  /* for debug purposes */
238
  static const char * const names[]={
239
    "STOP",
240
    "PLAIN",
241
    "LOGIN",
242
    "LOGIN_PASSWD",
243
    "EXTERNAL",
244
    "CRAMMD5",
245
    "DIGESTMD5",
246
    "DIGESTMD5_RESP",
247
    "NTLM",
248
    "NTLM_TYPE2MSG",
249
    "GSSAPI",
250
    "GSSAPI_TOKEN",
251
    "GSSAPI_NO_DATA",
252
    "OAUTH2",
253
    "OAUTH2_RESP",
254
    "GSASL",
255
    "CANCEL",
256
    "FINAL",
257
    /* LAST */
258
  };
259
260
  if(sasl->state != newstate)
261
    infof(data, "SASL %p state change from %s to %s",
262
          (void *)sasl, names[sasl->state], names[newstate]);
263
#else
264
0
  (void) data;
265
0
#endif
266
267
0
  sasl->state = newstate;
268
0
}
269
270
#if defined(USE_NTLM) || defined(USE_GSASL) || defined(USE_KERBEROS5) || \
271
  !defined(CURL_DISABLE_DIGEST_AUTH)
272
/* Get the SASL server message and convert it to binary. */
273
static CURLcode get_server_message(struct SASL *sasl, struct Curl_easy *data,
274
                                   struct bufref *out)
275
0
{
276
0
  CURLcode result = CURLE_OK;
277
278
0
  result = sasl->params->getmessage(data, out);
279
0
  if(!result && (sasl->params->flags & SASL_FLAG_BASE64)) {
280
0
    unsigned char *msg;
281
0
    size_t msglen;
282
0
    const char *serverdata = (const char *) Curl_bufref_ptr(out);
283
284
0
    if(!*serverdata || *serverdata == '=')
285
0
      Curl_bufref_set(out, NULL, 0, NULL);
286
0
    else {
287
0
      result = Curl_base64_decode(serverdata, &msg, &msglen);
288
0
      if(!result)
289
0
        Curl_bufref_set(out, msg, msglen, curl_free);
290
0
    }
291
0
  }
292
0
  return result;
293
0
}
294
#endif
295
296
/* Encode the outgoing SASL message. */
297
static CURLcode build_message(struct SASL *sasl, struct bufref *msg)
298
0
{
299
0
  CURLcode result = CURLE_OK;
300
301
0
  if(sasl->params->flags & SASL_FLAG_BASE64) {
302
0
    if(!Curl_bufref_ptr(msg))                   /* Empty message. */
303
0
      Curl_bufref_set(msg, "", 0, NULL);
304
0
    else if(!Curl_bufref_len(msg))              /* Explicit empty response. */
305
0
      Curl_bufref_set(msg, "=", 1, NULL);
306
0
    else {
307
0
      char *base64;
308
0
      size_t base64len;
309
310
0
      result = Curl_base64_encode((const char *) Curl_bufref_ptr(msg),
311
0
                                  Curl_bufref_len(msg), &base64, &base64len);
312
0
      if(!result)
313
0
        Curl_bufref_set(msg, base64, base64len, curl_free);
314
0
    }
315
0
  }
316
317
0
  return result;
318
0
}
319
320
/*
321
 * Curl_sasl_can_authenticate()
322
 *
323
 * Check if we have enough auth data and capabilities to authenticate.
324
 */
325
bool Curl_sasl_can_authenticate(struct SASL *sasl, struct Curl_easy *data)
326
0
{
327
  /* Have credentials been provided? */
328
0
  if(data->state.aptr.user)
329
0
    return TRUE;
330
331
  /* EXTERNAL can authenticate without a user name and/or password */
332
0
  if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
333
0
    return TRUE;
334
335
0
  return FALSE;
336
0
}
337
338
/*
339
 * Curl_sasl_start()
340
 *
341
 * Calculate the required login details for SASL authentication.
342
 */
343
CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
344
                         bool force_ir, saslprogress *progress)
345
0
{
346
0
  CURLcode result = CURLE_OK;
347
0
  struct connectdata *conn = data->conn;
348
0
  unsigned short enabledmechs;
349
0
  const char *mech = NULL;
350
0
  struct bufref resp;
351
0
  saslstate state1 = SASL_STOP;
352
0
  saslstate state2 = SASL_FINAL;
353
0
  const char *hostname, *disp_hostname;
354
0
  int port;
355
0
#if defined(USE_KERBEROS5) || defined(USE_NTLM)
356
0
  const char *service = data->set.str[STRING_SERVICE_NAME] ?
357
0
    data->set.str[STRING_SERVICE_NAME] :
358
0
    sasl->params->service;
359
0
#endif
360
0
  const char *oauth_bearer = data->set.str[STRING_BEARER];
361
0
  struct bufref nullmsg;
362
363
0
  Curl_conn_get_host(data, FIRSTSOCKET, &hostname, &disp_hostname, &port);
364
0
  Curl_bufref_init(&nullmsg);
365
0
  Curl_bufref_init(&resp);
366
0
  sasl->force_ir = force_ir;    /* Latch for future use */
367
0
  sasl->authused = 0;           /* No mechanism used yet */
368
0
  enabledmechs = sasl->authmechs & sasl->prefmech;
369
0
  *progress = SASL_IDLE;
370
371
  /* Calculate the supported authentication mechanism, by decreasing order of
372
     security, as well as the initial response where appropriate */
373
0
  if((enabledmechs & SASL_MECH_EXTERNAL) && !conn->passwd[0]) {
374
0
    mech = SASL_MECH_STRING_EXTERNAL;
375
0
    state1 = SASL_EXTERNAL;
376
0
    sasl->authused = SASL_MECH_EXTERNAL;
377
378
0
    if(force_ir || data->set.sasl_ir)
379
0
      result = Curl_auth_create_external_message(conn->user, &resp);
380
0
  }
381
0
  else if(data->state.aptr.user) {
382
#if defined(USE_KERBEROS5)
383
    if((enabledmechs & SASL_MECH_GSSAPI) && Curl_auth_is_gssapi_supported() &&
384
       Curl_auth_user_contains_domain(conn->user)) {
385
      sasl->mutual_auth = FALSE;
386
      mech = SASL_MECH_STRING_GSSAPI;
387
      state1 = SASL_GSSAPI;
388
      state2 = SASL_GSSAPI_TOKEN;
389
      sasl->authused = SASL_MECH_GSSAPI;
390
391
      if(force_ir || data->set.sasl_ir)
392
        result = Curl_auth_create_gssapi_user_message(data, conn->user,
393
                                                      conn->passwd,
394
                                                      service,
395
                                                      conn->host.name,
396
                                                      sasl->mutual_auth,
397
                                                      NULL, &conn->krb5,
398
                                                      &resp);
399
    }
400
    else
401
#endif
402
#ifdef USE_GSASL
403
    if((enabledmechs & SASL_MECH_SCRAM_SHA_256) &&
404
       Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_256,
405
                                    &conn->gsasl)) {
406
      mech = SASL_MECH_STRING_SCRAM_SHA_256;
407
      sasl->authused = SASL_MECH_SCRAM_SHA_256;
408
      state1 = SASL_GSASL;
409
      state2 = SASL_GSASL;
410
411
      result = Curl_auth_gsasl_start(data, conn->user,
412
                                     conn->passwd, &conn->gsasl);
413
      if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
414
        result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
415
    }
416
    else if((enabledmechs & SASL_MECH_SCRAM_SHA_1) &&
417
            Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_1,
418
                                         &conn->gsasl)) {
419
      mech = SASL_MECH_STRING_SCRAM_SHA_1;
420
      sasl->authused = SASL_MECH_SCRAM_SHA_1;
421
      state1 = SASL_GSASL;
422
      state2 = SASL_GSASL;
423
424
      result = Curl_auth_gsasl_start(data, conn->user,
425
                                     conn->passwd, &conn->gsasl);
426
      if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
427
        result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
428
    }
429
    else
430
#endif
431
0
#ifndef CURL_DISABLE_DIGEST_AUTH
432
0
    if((enabledmechs & SASL_MECH_DIGEST_MD5) &&
433
0
       Curl_auth_is_digest_supported()) {
434
0
      mech = SASL_MECH_STRING_DIGEST_MD5;
435
0
      state1 = SASL_DIGESTMD5;
436
0
      sasl->authused = SASL_MECH_DIGEST_MD5;
437
0
    }
438
0
    else if(enabledmechs & SASL_MECH_CRAM_MD5) {
439
0
      mech = SASL_MECH_STRING_CRAM_MD5;
440
0
      state1 = SASL_CRAMMD5;
441
0
      sasl->authused = SASL_MECH_CRAM_MD5;
442
0
    }
443
0
    else
444
0
#endif
445
0
#ifdef USE_NTLM
446
0
    if((enabledmechs & SASL_MECH_NTLM) && Curl_auth_is_ntlm_supported()) {
447
0
      mech = SASL_MECH_STRING_NTLM;
448
0
      state1 = SASL_NTLM;
449
0
      state2 = SASL_NTLM_TYPE2MSG;
450
0
      sasl->authused = SASL_MECH_NTLM;
451
452
0
      if(force_ir || data->set.sasl_ir)
453
0
        result = Curl_auth_create_ntlm_type1_message(data,
454
0
                                                     conn->user, conn->passwd,
455
0
                                                     service,
456
0
                                                     hostname,
457
0
                                                     &conn->ntlm, &resp);
458
0
      }
459
0
    else
460
0
#endif
461
0
    if((enabledmechs & SASL_MECH_OAUTHBEARER) && oauth_bearer) {
462
0
      mech = SASL_MECH_STRING_OAUTHBEARER;
463
0
      state1 = SASL_OAUTH2;
464
0
      state2 = SASL_OAUTH2_RESP;
465
0
      sasl->authused = SASL_MECH_OAUTHBEARER;
466
467
0
      if(force_ir || data->set.sasl_ir)
468
0
        result = Curl_auth_create_oauth_bearer_message(conn->user,
469
0
                                                       hostname,
470
0
                                                       port,
471
0
                                                       oauth_bearer,
472
0
                                                       &resp);
473
0
    }
474
0
    else if((enabledmechs & SASL_MECH_XOAUTH2) && oauth_bearer) {
475
0
      mech = SASL_MECH_STRING_XOAUTH2;
476
0
      state1 = SASL_OAUTH2;
477
0
      sasl->authused = SASL_MECH_XOAUTH2;
478
479
0
      if(force_ir || data->set.sasl_ir)
480
0
        result = Curl_auth_create_xoauth_bearer_message(conn->user,
481
0
                                                        oauth_bearer,
482
0
                                                        &resp);
483
0
    }
484
0
    else if(enabledmechs & SASL_MECH_PLAIN) {
485
0
      mech = SASL_MECH_STRING_PLAIN;
486
0
      state1 = SASL_PLAIN;
487
0
      sasl->authused = SASL_MECH_PLAIN;
488
489
0
      if(force_ir || data->set.sasl_ir)
490
0
        result = Curl_auth_create_plain_message(conn->sasl_authzid,
491
0
                                                conn->user, conn->passwd,
492
0
                                                &resp);
493
0
    }
494
0
    else if(enabledmechs & SASL_MECH_LOGIN) {
495
0
      mech = SASL_MECH_STRING_LOGIN;
496
0
      state1 = SASL_LOGIN;
497
0
      state2 = SASL_LOGIN_PASSWD;
498
0
      sasl->authused = SASL_MECH_LOGIN;
499
500
0
      if(force_ir || data->set.sasl_ir)
501
0
        result = Curl_auth_create_login_message(conn->user, &resp);
502
0
    }
503
0
  }
504
505
0
  if(!result && mech) {
506
0
    sasl->curmech = mech;
507
0
    if(Curl_bufref_ptr(&resp))
508
0
      result = build_message(sasl, &resp);
509
510
0
    if(sasl->params->maxirlen &&
511
0
       strlen(mech) + Curl_bufref_len(&resp) > sasl->params->maxirlen)
512
0
      Curl_bufref_free(&resp);
513
514
0
    if(!result)
515
0
      result = sasl->params->sendauth(data, mech, &resp);
516
517
0
    if(!result) {
518
0
      *progress = SASL_INPROGRESS;
519
0
      sasl_state(sasl, data, Curl_bufref_ptr(&resp) ? state2 : state1);
520
0
    }
521
0
  }
522
523
0
  Curl_bufref_free(&resp);
524
0
  return result;
525
0
}
526
527
/*
528
 * Curl_sasl_continue()
529
 *
530
 * Continue the authentication.
531
 */
532
CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
533
                            int code, saslprogress *progress)
534
0
{
535
0
  CURLcode result = CURLE_OK;
536
0
  struct connectdata *conn = data->conn;
537
0
  saslstate newstate = SASL_FINAL;
538
0
  struct bufref resp;
539
0
  const char *hostname, *disp_hostname;
540
0
  int port;
541
0
#if defined(USE_KERBEROS5) || defined(USE_NTLM) \
542
0
    || !defined(CURL_DISABLE_DIGEST_AUTH)
543
0
  const char *service = data->set.str[STRING_SERVICE_NAME] ?
544
0
    data->set.str[STRING_SERVICE_NAME] :
545
0
    sasl->params->service;
546
0
#endif
547
0
  const char *oauth_bearer = data->set.str[STRING_BEARER];
548
0
  struct bufref serverdata;
549
550
0
  Curl_conn_get_host(data, FIRSTSOCKET, &hostname, &disp_hostname, &port);
551
0
  Curl_bufref_init(&serverdata);
552
0
  Curl_bufref_init(&resp);
553
0
  *progress = SASL_INPROGRESS;
554
555
0
  if(sasl->state == SASL_FINAL) {
556
0
    if(code != sasl->params->finalcode)
557
0
      result = CURLE_LOGIN_DENIED;
558
0
    *progress = SASL_DONE;
559
0
    sasl_state(sasl, data, SASL_STOP);
560
0
    return result;
561
0
  }
562
563
0
  if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
564
0
     code != sasl->params->contcode) {
565
0
    *progress = SASL_DONE;
566
0
    sasl_state(sasl, data, SASL_STOP);
567
0
    return CURLE_LOGIN_DENIED;
568
0
  }
569
570
0
  switch(sasl->state) {
571
0
  case SASL_STOP:
572
0
    *progress = SASL_DONE;
573
0
    return result;
574
0
  case SASL_PLAIN:
575
0
    result = Curl_auth_create_plain_message(conn->sasl_authzid,
576
0
                                            conn->user, conn->passwd, &resp);
577
0
    break;
578
0
  case SASL_LOGIN:
579
0
    result = Curl_auth_create_login_message(conn->user, &resp);
580
0
    newstate = SASL_LOGIN_PASSWD;
581
0
    break;
582
0
  case SASL_LOGIN_PASSWD:
583
0
    result = Curl_auth_create_login_message(conn->passwd, &resp);
584
0
    break;
585
0
  case SASL_EXTERNAL:
586
0
    result = Curl_auth_create_external_message(conn->user, &resp);
587
0
    break;
588
#ifdef USE_GSASL
589
  case SASL_GSASL:
590
    result = get_server_message(sasl, data, &serverdata);
591
    if(!result)
592
      result = Curl_auth_gsasl_token(data, &serverdata, &conn->gsasl, &resp);
593
    if(!result && Curl_bufref_len(&resp) > 0)
594
      newstate = SASL_GSASL;
595
    break;
596
#endif
597
0
#ifndef CURL_DISABLE_DIGEST_AUTH
598
0
  case SASL_CRAMMD5:
599
0
    result = get_server_message(sasl, data, &serverdata);
600
0
    if(!result)
601
0
      result = Curl_auth_create_cram_md5_message(&serverdata, conn->user,
602
0
                                                 conn->passwd, &resp);
603
0
    break;
604
0
  case SASL_DIGESTMD5:
605
0
    result = get_server_message(sasl, data, &serverdata);
606
0
    if(!result)
607
0
      result = Curl_auth_create_digest_md5_message(data, &serverdata,
608
0
                                                   conn->user, conn->passwd,
609
0
                                                   service, &resp);
610
0
    if(!result && (sasl->params->flags & SASL_FLAG_BASE64))
611
0
      newstate = SASL_DIGESTMD5_RESP;
612
0
    break;
613
0
  case SASL_DIGESTMD5_RESP:
614
    /* Keep response NULL to output an empty line. */
615
0
    break;
616
0
#endif
617
618
0
#ifdef USE_NTLM
619
0
  case SASL_NTLM:
620
    /* Create the type-1 message */
621
0
    result = Curl_auth_create_ntlm_type1_message(data,
622
0
                                                 conn->user, conn->passwd,
623
0
                                                 service, hostname,
624
0
                                                 &conn->ntlm, &resp);
625
0
    newstate = SASL_NTLM_TYPE2MSG;
626
0
    break;
627
0
  case SASL_NTLM_TYPE2MSG:
628
    /* Decode the type-2 message */
629
0
    result = get_server_message(sasl, data, &serverdata);
630
0
    if(!result)
631
0
      result = Curl_auth_decode_ntlm_type2_message(data, &serverdata,
632
0
                                                   &conn->ntlm);
633
0
    if(!result)
634
0
      result = Curl_auth_create_ntlm_type3_message(data, conn->user,
635
0
                                                   conn->passwd, &conn->ntlm,
636
0
                                                   &resp);
637
0
    break;
638
0
#endif
639
640
#if defined(USE_KERBEROS5)
641
  case SASL_GSSAPI:
642
    result = Curl_auth_create_gssapi_user_message(data, conn->user,
643
                                                  conn->passwd,
644
                                                  service,
645
                                                  conn->host.name,
646
                                                  sasl->mutual_auth, NULL,
647
                                                  &conn->krb5,
648
                                                  &resp);
649
    newstate = SASL_GSSAPI_TOKEN;
650
    break;
651
  case SASL_GSSAPI_TOKEN:
652
    result = get_server_message(sasl, data, &serverdata);
653
    if(!result) {
654
      if(sasl->mutual_auth) {
655
        /* Decode the user token challenge and create the optional response
656
           message */
657
        result = Curl_auth_create_gssapi_user_message(data, NULL, NULL,
658
                                                      NULL, NULL,
659
                                                      sasl->mutual_auth,
660
                                                      &serverdata,
661
                                                      &conn->krb5,
662
                                                      &resp);
663
        newstate = SASL_GSSAPI_NO_DATA;
664
      }
665
      else
666
        /* Decode the security challenge and create the response message */
667
        result = Curl_auth_create_gssapi_security_message(data,
668
                                                          conn->sasl_authzid,
669
                                                          &serverdata,
670
                                                          &conn->krb5,
671
                                                          &resp);
672
    }
673
    break;
674
  case SASL_GSSAPI_NO_DATA:
675
    /* Decode the security challenge and create the response message */
676
    result = get_server_message(sasl, data, &serverdata);
677
    if(!result)
678
      result = Curl_auth_create_gssapi_security_message(data,
679
                                                        conn->sasl_authzid,
680
                                                        &serverdata,
681
                                                        &conn->krb5,
682
                                                        &resp);
683
    break;
684
#endif
685
686
0
  case SASL_OAUTH2:
687
    /* Create the authorization message */
688
0
    if(sasl->authused == SASL_MECH_OAUTHBEARER) {
689
0
      result = Curl_auth_create_oauth_bearer_message(conn->user,
690
0
                                                     hostname,
691
0
                                                     port,
692
0
                                                     oauth_bearer,
693
0
                                                     &resp);
694
695
      /* Failures maybe sent by the server as continuations for OAUTHBEARER */
696
0
      newstate = SASL_OAUTH2_RESP;
697
0
    }
698
0
    else
699
0
      result = Curl_auth_create_xoauth_bearer_message(conn->user,
700
0
                                                      oauth_bearer,
701
0
                                                      &resp);
702
0
    break;
703
704
0
  case SASL_OAUTH2_RESP:
705
    /* The continuation is optional so check the response code */
706
0
    if(code == sasl->params->finalcode) {
707
      /* Final response was received so we are done */
708
0
      *progress = SASL_DONE;
709
0
      sasl_state(sasl, data, SASL_STOP);
710
0
      return result;
711
0
    }
712
0
    else if(code == sasl->params->contcode) {
713
      /* Acknowledge the continuation by sending a 0x01 response. */
714
0
      Curl_bufref_set(&resp, "\x01", 1, NULL);
715
0
      break;
716
0
    }
717
0
    else {
718
0
      *progress = SASL_DONE;
719
0
      sasl_state(sasl, data, SASL_STOP);
720
0
      return CURLE_LOGIN_DENIED;
721
0
    }
722
723
0
  case SASL_CANCEL:
724
    /* Remove the offending mechanism from the supported list */
725
0
    sasl->authmechs ^= sasl->authused;
726
727
    /* Start an alternative SASL authentication */
728
0
    return Curl_sasl_start(sasl, data, sasl->force_ir, progress);
729
0
  default:
730
0
    failf(data, "Unsupported SASL authentication mechanism");
731
0
    result = CURLE_UNSUPPORTED_PROTOCOL;  /* Should not happen */
732
0
    break;
733
0
  }
734
735
0
  Curl_bufref_free(&serverdata);
736
737
0
  switch(result) {
738
0
  case CURLE_BAD_CONTENT_ENCODING:
739
    /* Cancel dialog */
740
0
    result = sasl->params->cancelauth(data, sasl->curmech);
741
0
    newstate = SASL_CANCEL;
742
0
    break;
743
0
  case CURLE_OK:
744
0
    result = build_message(sasl, &resp);
745
0
    if(!result)
746
0
      result = sasl->params->contauth(data, sasl->curmech, &resp);
747
0
    break;
748
0
  default:
749
0
    newstate = SASL_STOP;    /* Stop on error */
750
0
    *progress = SASL_DONE;
751
0
    break;
752
0
  }
753
754
0
  Curl_bufref_free(&resp);
755
756
0
  sasl_state(sasl, data, newstate);
757
758
0
  return result;
759
0
}
760
#endif /* protocols are enabled that use SASL */