Coverage Report

Created: 2023-12-08 06:48

/src/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
#if defined(USE_NTLM)
111
  /* Cleanup the NTLM structure */
112
  if(authused == SASL_MECH_NTLM) {
113
    Curl_auth_cleanup_ntlm(&conn->ntlm);
114
  }
115
#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
    sasl->resetprefs = FALSE;
209
0
    sasl->prefmech = SASL_AUTH_NONE;
210
0
    if(auth & CURLAUTH_BASIC)
211
0
      sasl->prefmech |= SASL_MECH_PLAIN | SASL_MECH_LOGIN;
212
0
    if(auth & CURLAUTH_DIGEST)
213
0
      sasl->prefmech |= SASL_MECH_DIGEST_MD5;
214
0
    if(auth & CURLAUTH_NTLM)
215
0
      sasl->prefmech |= SASL_MECH_NTLM;
216
0
    if(auth & CURLAUTH_BEARER)
217
0
      sasl->prefmech |= SASL_MECH_OAUTHBEARER | SASL_MECH_XOAUTH2;
218
0
    if(auth & CURLAUTH_GSSAPI)
219
0
      sasl->prefmech |= SASL_MECH_GSSAPI;
220
0
  }
221
0
}
222
223
/*
224
 * sasl_state()
225
 *
226
 * This is the ONLY way to change SASL state!
227
 */
228
static void sasl_state(struct SASL *sasl, struct Curl_easy *data,
229
                       saslstate newstate)
230
0
{
231
0
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
232
  /* for debug purposes */
233
0
  static const char * const names[]={
234
0
    "STOP",
235
0
    "PLAIN",
236
0
    "LOGIN",
237
0
    "LOGIN_PASSWD",
238
0
    "EXTERNAL",
239
0
    "CRAMMD5",
240
0
    "DIGESTMD5",
241
0
    "DIGESTMD5_RESP",
242
0
    "NTLM",
243
0
    "NTLM_TYPE2MSG",
244
0
    "GSSAPI",
245
0
    "GSSAPI_TOKEN",
246
0
    "GSSAPI_NO_DATA",
247
0
    "OAUTH2",
248
0
    "OAUTH2_RESP",
249
0
    "GSASL",
250
0
    "CANCEL",
251
0
    "FINAL",
252
    /* LAST */
253
0
  };
254
255
0
  if(sasl->state != newstate)
256
0
    infof(data, "SASL %p state change from %s to %s",
257
0
          (void *)sasl, names[sasl->state], names[newstate]);
258
#else
259
  (void) data;
260
#endif
261
262
0
  sasl->state = newstate;
263
0
}
264
265
#if defined(USE_NTLM) || defined(USE_GSASL) || defined(USE_KERBEROS5) || \
266
  !defined(CURL_DISABLE_DIGEST_AUTH)
267
/* Get the SASL server message and convert it to binary. */
268
static CURLcode get_server_message(struct SASL *sasl, struct Curl_easy *data,
269
                                   struct bufref *out)
270
0
{
271
0
  CURLcode result = CURLE_OK;
272
273
0
  result = sasl->params->getmessage(data, out);
274
0
  if(!result && (sasl->params->flags & SASL_FLAG_BASE64)) {
275
0
    unsigned char *msg;
276
0
    size_t msglen;
277
0
    const char *serverdata = (const char *) Curl_bufref_ptr(out);
278
279
0
    if(!*serverdata || *serverdata == '=')
280
0
      Curl_bufref_set(out, NULL, 0, NULL);
281
0
    else {
282
0
      result = Curl_base64_decode(serverdata, &msg, &msglen);
283
0
      if(!result)
284
0
        Curl_bufref_set(out, msg, msglen, curl_free);
285
0
    }
286
0
  }
287
0
  return result;
288
0
}
289
#endif
290
291
/* Encode the outgoing SASL message. */
292
static CURLcode build_message(struct SASL *sasl, struct bufref *msg)
293
0
{
294
0
  CURLcode result = CURLE_OK;
295
296
0
  if(sasl->params->flags & SASL_FLAG_BASE64) {
297
0
    if(!Curl_bufref_ptr(msg))                   /* Empty message. */
298
0
      Curl_bufref_set(msg, "", 0, NULL);
299
0
    else if(!Curl_bufref_len(msg))              /* Explicit empty response. */
300
0
      Curl_bufref_set(msg, "=", 1, NULL);
301
0
    else {
302
0
      char *base64;
303
0
      size_t base64len;
304
305
0
      result = Curl_base64_encode((const char *) Curl_bufref_ptr(msg),
306
0
                                  Curl_bufref_len(msg), &base64, &base64len);
307
0
      if(!result)
308
0
        Curl_bufref_set(msg, base64, base64len, curl_free);
309
0
    }
310
0
  }
311
312
0
  return result;
313
0
}
314
315
/*
316
 * Curl_sasl_can_authenticate()
317
 *
318
 * Check if we have enough auth data and capabilities to authenticate.
319
 */
320
bool Curl_sasl_can_authenticate(struct SASL *sasl, struct Curl_easy *data)
321
0
{
322
  /* Have credentials been provided? */
323
0
  if(data->state.aptr.user)
324
0
    return TRUE;
325
326
  /* EXTERNAL can authenticate without a user name and/or password */
327
0
  if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
328
0
    return TRUE;
329
330
0
  return FALSE;
331
0
}
332
333
/*
334
 * Curl_sasl_start()
335
 *
336
 * Calculate the required login details for SASL authentication.
337
 */
338
CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
339
                         bool force_ir, saslprogress *progress)
340
0
{
341
0
  CURLcode result = CURLE_OK;
342
0
  struct connectdata *conn = data->conn;
343
0
  unsigned short enabledmechs;
344
0
  const char *mech = NULL;
345
0
  struct bufref resp;
346
0
  saslstate state1 = SASL_STOP;
347
0
  saslstate state2 = SASL_FINAL;
348
0
  const char *hostname, *disp_hostname;
349
0
  int port;
350
#if defined(USE_KERBEROS5) || defined(USE_NTLM)
351
  const char *service = data->set.str[STRING_SERVICE_NAME] ?
352
    data->set.str[STRING_SERVICE_NAME] :
353
    sasl->params->service;
354
#endif
355
0
  const char *oauth_bearer = data->set.str[STRING_BEARER];
356
0
  struct bufref nullmsg;
357
358
0
  Curl_conn_get_host(data, FIRSTSOCKET, &hostname, &disp_hostname, &port);
359
0
  Curl_bufref_init(&nullmsg);
360
0
  Curl_bufref_init(&resp);
361
0
  sasl->force_ir = force_ir;    /* Latch for future use */
362
0
  sasl->authused = 0;           /* No mechanism used yet */
363
0
  enabledmechs = sasl->authmechs & sasl->prefmech;
364
0
  *progress = SASL_IDLE;
365
366
  /* Calculate the supported authentication mechanism, by decreasing order of
367
     security, as well as the initial response where appropriate */
368
0
  if((enabledmechs & SASL_MECH_EXTERNAL) && !conn->passwd[0]) {
369
0
    mech = SASL_MECH_STRING_EXTERNAL;
370
0
    state1 = SASL_EXTERNAL;
371
0
    sasl->authused = SASL_MECH_EXTERNAL;
372
373
0
    if(force_ir || data->set.sasl_ir)
374
0
      result = Curl_auth_create_external_message(conn->user, &resp);
375
0
  }
376
0
  else if(data->state.aptr.user) {
377
#if defined(USE_KERBEROS5)
378
    if((enabledmechs & SASL_MECH_GSSAPI) && Curl_auth_is_gssapi_supported() &&
379
       Curl_auth_user_contains_domain(conn->user)) {
380
      sasl->mutual_auth = FALSE;
381
      mech = SASL_MECH_STRING_GSSAPI;
382
      state1 = SASL_GSSAPI;
383
      state2 = SASL_GSSAPI_TOKEN;
384
      sasl->authused = SASL_MECH_GSSAPI;
385
386
      if(force_ir || data->set.sasl_ir)
387
        result = Curl_auth_create_gssapi_user_message(data, conn->user,
388
                                                      conn->passwd,
389
                                                      service,
390
                                                      conn->host.name,
391
                                                      sasl->mutual_auth,
392
                                                      NULL, &conn->krb5,
393
                                                      &resp);
394
    }
395
    else
396
#endif
397
#ifdef USE_GSASL
398
    if((enabledmechs & SASL_MECH_SCRAM_SHA_256) &&
399
       Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_256,
400
                                    &conn->gsasl)) {
401
      mech = SASL_MECH_STRING_SCRAM_SHA_256;
402
      sasl->authused = SASL_MECH_SCRAM_SHA_256;
403
      state1 = SASL_GSASL;
404
      state2 = SASL_GSASL;
405
406
      result = Curl_auth_gsasl_start(data, conn->user,
407
                                     conn->passwd, &conn->gsasl);
408
      if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
409
        result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
410
    }
411
    else if((enabledmechs & SASL_MECH_SCRAM_SHA_1) &&
412
            Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_1,
413
                                         &conn->gsasl)) {
414
      mech = SASL_MECH_STRING_SCRAM_SHA_1;
415
      sasl->authused = SASL_MECH_SCRAM_SHA_1;
416
      state1 = SASL_GSASL;
417
      state2 = SASL_GSASL;
418
419
      result = Curl_auth_gsasl_start(data, conn->user,
420
                                     conn->passwd, &conn->gsasl);
421
      if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
422
        result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
423
    }
424
    else
425
#endif
426
0
#ifndef CURL_DISABLE_DIGEST_AUTH
427
0
    if((enabledmechs & SASL_MECH_DIGEST_MD5) &&
428
0
       Curl_auth_is_digest_supported()) {
429
0
      mech = SASL_MECH_STRING_DIGEST_MD5;
430
0
      state1 = SASL_DIGESTMD5;
431
0
      sasl->authused = SASL_MECH_DIGEST_MD5;
432
0
    }
433
0
    else if(enabledmechs & SASL_MECH_CRAM_MD5) {
434
0
      mech = SASL_MECH_STRING_CRAM_MD5;
435
0
      state1 = SASL_CRAMMD5;
436
0
      sasl->authused = SASL_MECH_CRAM_MD5;
437
0
    }
438
0
    else
439
0
#endif
440
#ifdef USE_NTLM
441
    if((enabledmechs & SASL_MECH_NTLM) && Curl_auth_is_ntlm_supported()) {
442
      mech = SASL_MECH_STRING_NTLM;
443
      state1 = SASL_NTLM;
444
      state2 = SASL_NTLM_TYPE2MSG;
445
      sasl->authused = SASL_MECH_NTLM;
446
447
      if(force_ir || data->set.sasl_ir)
448
        result = Curl_auth_create_ntlm_type1_message(data,
449
                                                     conn->user, conn->passwd,
450
                                                     service,
451
                                                     hostname,
452
                                                     &conn->ntlm, &resp);
453
      }
454
    else
455
#endif
456
0
    if((enabledmechs & SASL_MECH_OAUTHBEARER) && oauth_bearer) {
457
0
      mech = SASL_MECH_STRING_OAUTHBEARER;
458
0
      state1 = SASL_OAUTH2;
459
0
      state2 = SASL_OAUTH2_RESP;
460
0
      sasl->authused = SASL_MECH_OAUTHBEARER;
461
462
0
      if(force_ir || data->set.sasl_ir)
463
0
        result = Curl_auth_create_oauth_bearer_message(conn->user,
464
0
                                                       hostname,
465
0
                                                       port,
466
0
                                                       oauth_bearer,
467
0
                                                       &resp);
468
0
    }
469
0
    else if((enabledmechs & SASL_MECH_XOAUTH2) && oauth_bearer) {
470
0
      mech = SASL_MECH_STRING_XOAUTH2;
471
0
      state1 = SASL_OAUTH2;
472
0
      sasl->authused = SASL_MECH_XOAUTH2;
473
474
0
      if(force_ir || data->set.sasl_ir)
475
0
        result = Curl_auth_create_xoauth_bearer_message(conn->user,
476
0
                                                        oauth_bearer,
477
0
                                                        &resp);
478
0
    }
479
0
    else if(enabledmechs & SASL_MECH_PLAIN) {
480
0
      mech = SASL_MECH_STRING_PLAIN;
481
0
      state1 = SASL_PLAIN;
482
0
      sasl->authused = SASL_MECH_PLAIN;
483
484
0
      if(force_ir || data->set.sasl_ir)
485
0
        result = Curl_auth_create_plain_message(conn->sasl_authzid,
486
0
                                                conn->user, conn->passwd,
487
0
                                                &resp);
488
0
    }
489
0
    else if(enabledmechs & SASL_MECH_LOGIN) {
490
0
      mech = SASL_MECH_STRING_LOGIN;
491
0
      state1 = SASL_LOGIN;
492
0
      state2 = SASL_LOGIN_PASSWD;
493
0
      sasl->authused = SASL_MECH_LOGIN;
494
495
0
      if(force_ir || data->set.sasl_ir)
496
0
        result = Curl_auth_create_login_message(conn->user, &resp);
497
0
    }
498
0
  }
499
500
0
  if(!result && mech) {
501
0
    sasl->curmech = mech;
502
0
    if(Curl_bufref_ptr(&resp))
503
0
      result = build_message(sasl, &resp);
504
505
0
    if(sasl->params->maxirlen &&
506
0
       strlen(mech) + Curl_bufref_len(&resp) > sasl->params->maxirlen)
507
0
      Curl_bufref_free(&resp);
508
509
0
    if(!result)
510
0
      result = sasl->params->sendauth(data, mech, &resp);
511
512
0
    if(!result) {
513
0
      *progress = SASL_INPROGRESS;
514
0
      sasl_state(sasl, data, Curl_bufref_ptr(&resp) ? state2 : state1);
515
0
    }
516
0
  }
517
518
0
  Curl_bufref_free(&resp);
519
0
  return result;
520
0
}
521
522
/*
523
 * Curl_sasl_continue()
524
 *
525
 * Continue the authentication.
526
 */
527
CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
528
                            int code, saslprogress *progress)
529
0
{
530
0
  CURLcode result = CURLE_OK;
531
0
  struct connectdata *conn = data->conn;
532
0
  saslstate newstate = SASL_FINAL;
533
0
  struct bufref resp;
534
0
  const char *hostname, *disp_hostname;
535
0
  int port;
536
0
#if defined(USE_KERBEROS5) || defined(USE_NTLM) \
537
0
    || !defined(CURL_DISABLE_DIGEST_AUTH)
538
0
  const char *service = data->set.str[STRING_SERVICE_NAME] ?
539
0
    data->set.str[STRING_SERVICE_NAME] :
540
0
    sasl->params->service;
541
0
#endif
542
0
  const char *oauth_bearer = data->set.str[STRING_BEARER];
543
0
  struct bufref serverdata;
544
545
0
  Curl_conn_get_host(data, FIRSTSOCKET, &hostname, &disp_hostname, &port);
546
0
  Curl_bufref_init(&serverdata);
547
0
  Curl_bufref_init(&resp);
548
0
  *progress = SASL_INPROGRESS;
549
550
0
  if(sasl->state == SASL_FINAL) {
551
0
    if(code != sasl->params->finalcode)
552
0
      result = CURLE_LOGIN_DENIED;
553
0
    *progress = SASL_DONE;
554
0
    sasl_state(sasl, data, SASL_STOP);
555
0
    return result;
556
0
  }
557
558
0
  if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
559
0
     code != sasl->params->contcode) {
560
0
    *progress = SASL_DONE;
561
0
    sasl_state(sasl, data, SASL_STOP);
562
0
    return CURLE_LOGIN_DENIED;
563
0
  }
564
565
0
  switch(sasl->state) {
566
0
  case SASL_STOP:
567
0
    *progress = SASL_DONE;
568
0
    return result;
569
0
  case SASL_PLAIN:
570
0
    result = Curl_auth_create_plain_message(conn->sasl_authzid,
571
0
                                            conn->user, conn->passwd, &resp);
572
0
    break;
573
0
  case SASL_LOGIN:
574
0
    result = Curl_auth_create_login_message(conn->user, &resp);
575
0
    newstate = SASL_LOGIN_PASSWD;
576
0
    break;
577
0
  case SASL_LOGIN_PASSWD:
578
0
    result = Curl_auth_create_login_message(conn->passwd, &resp);
579
0
    break;
580
0
  case SASL_EXTERNAL:
581
0
    result = Curl_auth_create_external_message(conn->user, &resp);
582
0
    break;
583
#ifdef USE_GSASL
584
  case SASL_GSASL:
585
    result = get_server_message(sasl, data, &serverdata);
586
    if(!result)
587
      result = Curl_auth_gsasl_token(data, &serverdata, &conn->gsasl, &resp);
588
    if(!result && Curl_bufref_len(&resp) > 0)
589
      newstate = SASL_GSASL;
590
    break;
591
#endif
592
0
#ifndef CURL_DISABLE_DIGEST_AUTH
593
0
  case SASL_CRAMMD5:
594
0
    result = get_server_message(sasl, data, &serverdata);
595
0
    if(!result)
596
0
      result = Curl_auth_create_cram_md5_message(&serverdata, conn->user,
597
0
                                                 conn->passwd, &resp);
598
0
    break;
599
0
  case SASL_DIGESTMD5:
600
0
    result = get_server_message(sasl, data, &serverdata);
601
0
    if(!result)
602
0
      result = Curl_auth_create_digest_md5_message(data, &serverdata,
603
0
                                                   conn->user, conn->passwd,
604
0
                                                   service, &resp);
605
0
    if(!result && (sasl->params->flags & SASL_FLAG_BASE64))
606
0
      newstate = SASL_DIGESTMD5_RESP;
607
0
    break;
608
0
  case SASL_DIGESTMD5_RESP:
609
    /* Keep response NULL to output an empty line. */
610
0
    break;
611
0
#endif
612
613
#ifdef USE_NTLM
614
  case SASL_NTLM:
615
    /* Create the type-1 message */
616
    result = Curl_auth_create_ntlm_type1_message(data,
617
                                                 conn->user, conn->passwd,
618
                                                 service, hostname,
619
                                                 &conn->ntlm, &resp);
620
    newstate = SASL_NTLM_TYPE2MSG;
621
    break;
622
  case SASL_NTLM_TYPE2MSG:
623
    /* Decode the type-2 message */
624
    result = get_server_message(sasl, data, &serverdata);
625
    if(!result)
626
      result = Curl_auth_decode_ntlm_type2_message(data, &serverdata,
627
                                                   &conn->ntlm);
628
    if(!result)
629
      result = Curl_auth_create_ntlm_type3_message(data, conn->user,
630
                                                   conn->passwd, &conn->ntlm,
631
                                                   &resp);
632
    break;
633
#endif
634
635
#if defined(USE_KERBEROS5)
636
  case SASL_GSSAPI:
637
    result = Curl_auth_create_gssapi_user_message(data, conn->user,
638
                                                  conn->passwd,
639
                                                  service,
640
                                                  conn->host.name,
641
                                                  sasl->mutual_auth, NULL,
642
                                                  &conn->krb5,
643
                                                  &resp);
644
    newstate = SASL_GSSAPI_TOKEN;
645
    break;
646
  case SASL_GSSAPI_TOKEN:
647
    result = get_server_message(sasl, data, &serverdata);
648
    if(!result) {
649
      if(sasl->mutual_auth) {
650
        /* Decode the user token challenge and create the optional response
651
           message */
652
        result = Curl_auth_create_gssapi_user_message(data, NULL, NULL,
653
                                                      NULL, NULL,
654
                                                      sasl->mutual_auth,
655
                                                      &serverdata,
656
                                                      &conn->krb5,
657
                                                      &resp);
658
        newstate = SASL_GSSAPI_NO_DATA;
659
      }
660
      else
661
        /* Decode the security challenge and create the response message */
662
        result = Curl_auth_create_gssapi_security_message(data,
663
                                                          conn->sasl_authzid,
664
                                                          &serverdata,
665
                                                          &conn->krb5,
666
                                                          &resp);
667
    }
668
    break;
669
  case SASL_GSSAPI_NO_DATA:
670
    /* Decode the security challenge and create the response message */
671
    result = get_server_message(sasl, data, &serverdata);
672
    if(!result)
673
      result = Curl_auth_create_gssapi_security_message(data,
674
                                                        conn->sasl_authzid,
675
                                                        &serverdata,
676
                                                        &conn->krb5,
677
                                                        &resp);
678
    break;
679
#endif
680
681
0
  case SASL_OAUTH2:
682
    /* Create the authorization message */
683
0
    if(sasl->authused == SASL_MECH_OAUTHBEARER) {
684
0
      result = Curl_auth_create_oauth_bearer_message(conn->user,
685
0
                                                     hostname,
686
0
                                                     port,
687
0
                                                     oauth_bearer,
688
0
                                                     &resp);
689
690
      /* Failures maybe sent by the server as continuations for OAUTHBEARER */
691
0
      newstate = SASL_OAUTH2_RESP;
692
0
    }
693
0
    else
694
0
      result = Curl_auth_create_xoauth_bearer_message(conn->user,
695
0
                                                      oauth_bearer,
696
0
                                                      &resp);
697
0
    break;
698
699
0
  case SASL_OAUTH2_RESP:
700
    /* The continuation is optional so check the response code */
701
0
    if(code == sasl->params->finalcode) {
702
      /* Final response was received so we are done */
703
0
      *progress = SASL_DONE;
704
0
      sasl_state(sasl, data, SASL_STOP);
705
0
      return result;
706
0
    }
707
0
    else if(code == sasl->params->contcode) {
708
      /* Acknowledge the continuation by sending a 0x01 response. */
709
0
      Curl_bufref_set(&resp, "\x01", 1, NULL);
710
0
      break;
711
0
    }
712
0
    else {
713
0
      *progress = SASL_DONE;
714
0
      sasl_state(sasl, data, SASL_STOP);
715
0
      return CURLE_LOGIN_DENIED;
716
0
    }
717
718
0
  case SASL_CANCEL:
719
    /* Remove the offending mechanism from the supported list */
720
0
    sasl->authmechs ^= sasl->authused;
721
722
    /* Start an alternative SASL authentication */
723
0
    return Curl_sasl_start(sasl, data, sasl->force_ir, progress);
724
0
  default:
725
0
    failf(data, "Unsupported SASL authentication mechanism");
726
0
    result = CURLE_UNSUPPORTED_PROTOCOL;  /* Should not happen */
727
0
    break;
728
0
  }
729
730
0
  Curl_bufref_free(&serverdata);
731
732
0
  switch(result) {
733
0
  case CURLE_BAD_CONTENT_ENCODING:
734
    /* Cancel dialog */
735
0
    result = sasl->params->cancelauth(data, sasl->curmech);
736
0
    newstate = SASL_CANCEL;
737
0
    break;
738
0
  case CURLE_OK:
739
0
    result = build_message(sasl, &resp);
740
0
    if(!result)
741
0
      result = sasl->params->contauth(data, sasl->curmech, &resp);
742
0
    break;
743
0
  default:
744
0
    newstate = SASL_STOP;    /* Stop on error */
745
0
    *progress = SASL_DONE;
746
0
    break;
747
0
  }
748
749
0
  Curl_bufref_free(&resp);
750
751
0
  sasl_state(sasl, data, newstate);
752
753
0
  return result;
754
0
}
755
#endif /* protocols are enabled that use SASL */