Coverage Report

Created: 2023-06-07 07:02

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