Coverage Report

Created: 2026-09-01 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curl_sasl.c
Line
Count
Source
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
#include "curl_setup.h"
36
37
#if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
38
  !defined(CURL_DISABLE_POP3) ||                                  \
39
  (!defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP))
40
41
#include "urldata.h"
42
#include "curlx/base64.h"
43
#include "vauth/vauth.h"
44
#include "cfilters.h"
45
#include "curl_sasl.h"
46
#include "curl_trc.h"
47
48
/* Supported mechanisms */
49
static const struct {
50
  const char    *name;  /* Name */
51
  size_t         len;   /* Name length */
52
  unsigned short bit;   /* Flag bit */
53
} mechtable[] = {
54
  { "LOGIN",        5,  SASL_MECH_LOGIN },
55
  { "PLAIN",        5,  SASL_MECH_PLAIN },
56
  { "CRAM-MD5",     8,  SASL_MECH_CRAM_MD5 },
57
  { "DIGEST-MD5",   10, SASL_MECH_DIGEST_MD5 },
58
  { "GSSAPI",       6,  SASL_MECH_GSSAPI },
59
  { "EXTERNAL",     8,  SASL_MECH_EXTERNAL },
60
  { "NTLM",         4,  SASL_MECH_NTLM },
61
  { "XOAUTH2",      7,  SASL_MECH_XOAUTH2 },
62
  { "OAUTHBEARER",  11, SASL_MECH_OAUTHBEARER },
63
  { "SCRAM-SHA-1",  11, SASL_MECH_SCRAM_SHA_1 },
64
  { "SCRAM-SHA-256",13, SASL_MECH_SCRAM_SHA_256 },
65
  { ZERO_NULL,      0,  0 }
66
};
67
68
/*
69
 * Curl_sasl_decode_mech()
70
 *
71
 * Convert a SASL mechanism name into a token.
72
 *
73
 * Parameters:
74
 *
75
 * ptr    [in]     - The mechanism string.
76
 * maxlen [in]     - Maximum mechanism string length.
77
 * len    [out]    - If not NULL, effective name length.
78
 *
79
 * Returns the SASL mechanism token or 0 if no match.
80
 */
81
unsigned short Curl_sasl_decode_mech(const char *ptr, size_t maxlen,
82
                                     size_t *len)
83
1.43k
{
84
1.43k
  unsigned int i;
85
1.43k
  char c;
86
87
14.7k
  for(i = 0; mechtable[i].name; i++) {
88
13.7k
    if(maxlen >= mechtable[i].len &&
89
5.29k
       curl_strnequal(ptr, mechtable[i].name, mechtable[i].len)) {
90
561
      if(len)
91
561
        *len = mechtable[i].len;
92
93
561
      if(maxlen == mechtable[i].len)
94
161
        return mechtable[i].bit;
95
96
400
      c = ptr[mechtable[i].len];
97
400
      if(!ISUPPER(c) && !ISDIGIT(c) && c != '-' && c != '_')
98
210
        return mechtable[i].bit;
99
400
    }
100
13.7k
  }
101
102
1.05k
  return 0;
103
1.43k
}
104
105
/*
106
 * Curl_sasl_parse_url_auth_option()
107
 *
108
 * Parse the URL login options.
109
 */
110
CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
111
                                         const char *value, size_t len)
112
73
{
113
73
  CURLcode result = CURLE_OK;
114
73
  size_t mechlen;
115
116
73
  if(!len)
117
22
    return CURLE_URL_MALFORMAT;
118
119
51
  if(sasl->resetprefs) {
120
34
    sasl->resetprefs = FALSE;
121
34
    sasl->prefmech = SASL_AUTH_NONE;
122
34
  }
123
124
51
  if(!strncmp(value, "*", len))
125
6
    sasl->prefmech = SASL_AUTH_DEFAULT;
126
45
  else {
127
45
    unsigned short mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
128
45
    if(mechbit && mechlen == len)
129
15
      sasl->prefmech |= mechbit;
130
30
    else
131
30
      result = CURLE_URL_MALFORMAT;
132
45
  }
133
134
51
  return result;
135
73
}
136
137
/*
138
 * Curl_sasl_init()
139
 *
140
 * Initializes the SASL structure.
141
 */
142
void Curl_sasl_init(struct SASL *sasl, struct Curl_easy *data,
143
                    const struct SASLproto *params)
144
1.36k
{
145
1.36k
  unsigned long auth = data->set.httpauth;
146
147
1.36k
  sasl->params = params;           /* Set protocol dependent parameters */
148
1.36k
  sasl->state = SASL_STOP;         /* Not yet running */
149
1.36k
  sasl->curmech = NULL;            /* No mechanism yet. */
150
1.36k
  sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
151
1.36k
  sasl->prefmech = params->defmechs; /* Default preferred mechanisms */
152
1.36k
  sasl->authused = SASL_AUTH_NONE; /* The authentication mechanism used */
153
1.36k
  sasl->resetprefs = TRUE;         /* Reset prefmech upon AUTH parsing. */
154
1.36k
  sasl->mutual_auth = FALSE;       /* No mutual authentication (GSSAPI only) */
155
1.36k
  sasl->force_ir = FALSE;          /* Respect external option */
156
157
1.36k
  if(auth != CURLAUTH_BASIC) {
158
45
    unsigned short mechs = SASL_AUTH_NONE;
159
160
    /* If some usable http authentication options have been set, determine
161
       new defaults from them. */
162
45
    if(auth & CURLAUTH_BASIC)
163
16
      mechs |= SASL_MECH_PLAIN | SASL_MECH_LOGIN;
164
45
    if(auth & CURLAUTH_DIGEST)
165
20
      mechs |= SASL_MECH_DIGEST_MD5;
166
45
    if(auth & CURLAUTH_NTLM)
167
0
      mechs |= SASL_MECH_NTLM;
168
45
    if(auth & CURLAUTH_BEARER)
169
12
      mechs |= SASL_MECH_OAUTHBEARER | SASL_MECH_XOAUTH2;
170
45
    if(auth & CURLAUTH_GSSAPI)
171
0
      mechs |= SASL_MECH_GSSAPI;
172
173
45
    if(mechs != SASL_AUTH_NONE)
174
27
      sasl->prefmech = mechs;
175
45
  }
176
1.36k
}
177
178
/*
179
 * sasl_state()
180
 *
181
 * This is the ONLY way to change SASL state!
182
 */
183
static void sasl_state(struct SASL *sasl, struct Curl_easy *data,
184
                       saslstate newstate)
185
244
{
186
244
#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
187
  /* for debug purposes */
188
244
  static const char * const names[] = {
189
244
    "STOP",
190
244
    "PLAIN",
191
244
    "LOGIN",
192
244
    "LOGIN_PASSWD",
193
244
    "EXTERNAL",
194
244
    "CRAMMD5",
195
244
    "DIGESTMD5",
196
244
    "DIGESTMD5_RESP",
197
244
    "NTLM",
198
244
    "NTLM_TYPE2MSG",
199
244
    "GSSAPI",
200
244
    "GSSAPI_TOKEN",
201
244
    "GSSAPI_NO_DATA",
202
244
    "OAUTH2",
203
244
    "OAUTH2_RESP",
204
244
    "GSASL",
205
244
    "CANCEL",
206
244
    "FINAL",
207
    /* LAST */
208
244
  };
209
210
244
  if(sasl->state != newstate)
211
244
    infof(data, "SASL %p state change from %s to %s",
212
244
          (void *)sasl, names[sasl->state], names[newstate]);
213
#else
214
  (void)data;
215
#endif
216
217
244
  sasl->state = newstate;
218
244
}
219
220
#if defined(USE_NTLM) || defined(USE_GSASL) || defined(USE_KERBEROS5) || \
221
  !defined(CURL_DISABLE_DIGEST_AUTH)
222
/* Get the SASL server message and convert it to binary. */
223
static CURLcode get_server_message(struct SASL *sasl, struct Curl_easy *data,
224
                                   struct bufref *out)
225
117
{
226
117
  CURLcode result = CURLE_OK;
227
228
117
  result = sasl->params->getmessage(data, out);
229
117
  if(!result && (sasl->params->flags & SASL_FLAG_BASE64)) {
230
117
    const char *serverdata = Curl_bufref_ptr(out);
231
232
117
    if(!*serverdata)
233
40
      Curl_bufref_set(out, NULL, 0, NULL);
234
77
    else {
235
77
      unsigned char *msg;
236
77
      size_t msglen;
237
238
77
      result = curlx_base64_decode(serverdata, &msg, &msglen);
239
77
      if(!result)
240
25
        Curl_bufref_set(out, msg, msglen, curl_free);
241
77
    }
242
117
  }
243
117
  return result;
244
117
}
245
#endif
246
247
/* Encode the outgoing SASL message. */
248
static CURLcode build_message(struct SASL *sasl, struct bufref *msg)
249
37
{
250
37
  CURLcode result = CURLE_OK;
251
252
37
  if(sasl->params->flags & SASL_FLAG_BASE64) {
253
37
    if(!Curl_bufref_ptr(msg))                   /* Empty message. */
254
0
      Curl_bufref_set(msg, "", 0, NULL);
255
37
    else if(Curl_bufref_len(msg)) {
256
37
      char *base64;
257
37
      size_t base64len;
258
259
37
      result = curlx_base64_encode(Curl_bufref_uptr(msg),
260
37
                                   Curl_bufref_len(msg), &base64, &base64len);
261
37
      if(!result)
262
37
        Curl_bufref_set(msg, base64, base64len, curl_free);
263
37
    }
264
37
  }
265
266
37
  return result;
267
37
}
268
269
/*
270
 * Curl_sasl_can_authenticate()
271
 *
272
 * Check if we have enough auth data and capabilities to authenticate.
273
 */
274
bool Curl_sasl_can_authenticate(struct SASL *sasl, struct Curl_easy *data)
275
589
{
276
  /* Have credentials been provided? */
277
589
  if(data->conn->creds)
278
126
    return TRUE;
279
280
  /* EXTERNAL can authenticate without a username and/or password */
281
463
  if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
282
0
    return TRUE;
283
284
463
  return FALSE;
285
463
}
286
287
struct sasl_ctx {
288
  struct SASL *sasl;
289
  struct connectdata *conn;
290
  unsigned short enabledmechs;
291
  const char *mech;
292
  saslstate state1;
293
  saslstate state2;
294
  struct bufref resp;
295
  CURLcode result;
296
};
297
298
static bool sasl_choose_external(struct Curl_easy *data, struct sasl_ctx *sctx)
299
135
{
300
135
  if((sctx->enabledmechs & SASL_MECH_EXTERNAL) &&
301
0
     !Curl_creds_has_passwd(sctx->conn->creds)) {
302
0
    sctx->mech = SASL_MECH_STRING_EXTERNAL;
303
0
    sctx->state1 = SASL_EXTERNAL;
304
0
    sctx->sasl->authused = SASL_MECH_EXTERNAL;
305
306
0
    if(sctx->sasl->force_ir || data->set.sasl_ir)
307
0
      Curl_auth_create_external_message(
308
0
        Curl_creds_user(sctx->conn->creds), &sctx->resp);
309
0
    return TRUE;
310
0
  }
311
135
  return FALSE;
312
135
}
313
314
#ifdef USE_KERBEROS5
315
static bool sasl_choose_krb5(struct Curl_easy *data, struct sasl_ctx *sctx)
316
{
317
  if((sctx->enabledmechs & SASL_MECH_GSSAPI) &&
318
     Curl_auth_is_gssapi_supported() &&
319
     Curl_auth_user_contains_domain(sctx->conn->creds)) {
320
    const char *service = Curl_creds_has_sasl_service(sctx->conn->creds) ?
321
      Curl_creds_sasl_service(sctx->conn->creds) : sctx->sasl->params->service;
322
323
    sctx->sasl->mutual_auth = FALSE;
324
    sctx->mech = SASL_MECH_STRING_GSSAPI;
325
    sctx->state1 = SASL_GSSAPI;
326
    sctx->state2 = SASL_GSSAPI_TOKEN;
327
    sctx->sasl->authused = SASL_MECH_GSSAPI;
328
329
    if(sctx->sasl->force_ir || data->set.sasl_ir) {
330
      struct kerberos5data *krb5 = Curl_auth_krb5_get(sctx->conn);
331
      sctx->result = !krb5 ? CURLE_OUT_OF_MEMORY :
332
        Curl_auth_create_gssapi_user_message(data, sctx->conn->creds,
333
                                             service,
334
                                             sctx->conn->origin->hostname,
335
                                             (bool)sctx->sasl->mutual_auth,
336
                                             NULL, krb5, &sctx->resp);
337
    }
338
    return TRUE;
339
  }
340
  return FALSE;
341
}
342
#endif /* USE_KERBEROS5 */
343
344
#ifdef USE_GSASL
345
static bool sasl_choose_gsasl(struct Curl_easy *data, struct sasl_ctx *sctx)
346
{
347
  struct gsasldata *gsasl;
348
  struct bufref nullmsg;
349
350
  if((sctx->enabledmechs &
351
      (SASL_MECH_SCRAM_SHA_256 | SASL_MECH_SCRAM_SHA_1))) {
352
    gsasl = Curl_auth_gsasl_get(sctx->conn);
353
    if(!gsasl) {
354
      sctx->result = CURLE_OUT_OF_MEMORY;
355
      return TRUE; /* attempted, but failed */
356
    }
357
358
    if((sctx->enabledmechs & SASL_MECH_SCRAM_SHA_256) &&
359
       Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_256,
360
                                    gsasl)) {
361
      sctx->mech = SASL_MECH_STRING_SCRAM_SHA_256;
362
      sctx->sasl->authused = SASL_MECH_SCRAM_SHA_256;
363
    }
364
    else if((sctx->enabledmechs & SASL_MECH_SCRAM_SHA_1) &&
365
            Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_1,
366
                                         gsasl)) {
367
      sctx->mech = SASL_MECH_STRING_SCRAM_SHA_1;
368
      sctx->sasl->authused = SASL_MECH_SCRAM_SHA_1;
369
    }
370
    else
371
      return FALSE;
372
373
    Curl_bufref_init(&nullmsg);
374
    sctx->state1 = SASL_GSASL;
375
    sctx->state2 = SASL_GSASL;
376
    sctx->result = Curl_auth_gsasl_start(data, sctx->conn->creds, gsasl);
377
    if(!sctx->result && (sctx->sasl->force_ir || data->set.sasl_ir))
378
      sctx->result = Curl_auth_gsasl_token(data, &nullmsg, gsasl, &sctx->resp);
379
    return TRUE;
380
  }
381
  return FALSE;
382
}
383
384
#endif /* USE_GSASL */
385
386
#ifndef CURL_DISABLE_DIGEST_AUTH
387
static bool sasl_choose_digest(struct Curl_easy *data, struct sasl_ctx *sctx)
388
135
{
389
135
  (void)data;
390
135
  if((sctx->enabledmechs & SASL_MECH_DIGEST_MD5) &&
391
75
     Curl_auth_is_digest_supported()) {
392
75
    sctx->mech = SASL_MECH_STRING_DIGEST_MD5;
393
75
    sctx->state1 = SASL_DIGESTMD5;
394
75
    sctx->sasl->authused = SASL_MECH_DIGEST_MD5;
395
75
    return TRUE;
396
75
  }
397
60
  else if(sctx->enabledmechs & SASL_MECH_CRAM_MD5) {
398
45
    sctx->mech = SASL_MECH_STRING_CRAM_MD5;
399
45
    sctx->state1 = SASL_CRAMMD5;
400
45
    sctx->sasl->authused = SASL_MECH_CRAM_MD5;
401
45
    return TRUE;
402
45
  }
403
15
  return FALSE;
404
135
}
405
#endif /* !CURL_DISABLE_DIGEST_AUTH */
406
407
#ifdef USE_NTLM
408
static bool sasl_choose_ntlm(struct Curl_easy *data, struct sasl_ctx *sctx)
409
{
410
  if((sctx->enabledmechs & SASL_MECH_NTLM) &&
411
     Curl_auth_is_ntlm_supported()) {
412
     const char *service = Curl_creds_has_sasl_service(sctx->conn->creds) ?
413
      Curl_creds_sasl_service(sctx->conn->creds) : sctx->sasl->params->service;
414
    const char *hostname;
415
416
    Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, NULL);
417
418
    sctx->mech = SASL_MECH_STRING_NTLM;
419
    sctx->state1 = SASL_NTLM;
420
    sctx->state2 = SASL_NTLM_TYPE2MSG;
421
    sctx->sasl->authused = SASL_MECH_NTLM;
422
423
    if(sctx->sasl->force_ir || data->set.sasl_ir) {
424
      struct ntlmdata *ntlm = Curl_auth_ntlm_get(sctx->conn, FALSE);
425
      sctx->result = !ntlm ? CURLE_OUT_OF_MEMORY :
426
        Curl_auth_create_ntlm_type1_message(data, sctx->conn->creds,
427
                                            service, hostname,
428
                                            ntlm, &sctx->resp);
429
    }
430
    return TRUE;
431
  }
432
  return FALSE;
433
}
434
#endif /* USE_NTLM */
435
436
static bool sasl_choose_oauth(struct Curl_easy *data, struct sasl_ctx *sctx)
437
15
{
438
15
  if(Curl_creds_has_oauth_bearer(data->state.creds) &&
439
1
     (sctx->enabledmechs & SASL_MECH_OAUTHBEARER)) {
440
0
    const char *hostname;
441
0
    int port;
442
0
    Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, &port);
443
444
0
    sctx->mech = SASL_MECH_STRING_OAUTHBEARER;
445
0
    sctx->state1 = SASL_OAUTH2;
446
0
    sctx->state2 = SASL_OAUTH2_RESP;
447
0
    sctx->sasl->authused = SASL_MECH_OAUTHBEARER;
448
449
0
    if(sctx->sasl->force_ir || data->set.sasl_ir)
450
0
      sctx->result =
451
0
        Curl_auth_create_oauth_bearer_message(sctx->conn->creds,
452
0
                                              hostname, port, &sctx->resp);
453
0
    return TRUE;
454
0
  }
455
15
  return FALSE;
456
15
}
457
458
static bool sasl_choose_oauth2(struct Curl_easy *data, struct sasl_ctx *sctx)
459
15
{
460
15
  if(Curl_creds_has_oauth_bearer(sctx->conn->creds) &&
461
1
     (sctx->enabledmechs & SASL_MECH_XOAUTH2)) {
462
0
    sctx->mech = SASL_MECH_STRING_XOAUTH2;
463
0
    sctx->state1 = SASL_OAUTH2;
464
0
    sctx->sasl->authused = SASL_MECH_XOAUTH2;
465
466
0
    if(sctx->sasl->force_ir || data->set.sasl_ir)
467
0
      sctx->result = Curl_auth_create_xoauth_bearer_message(
468
0
        sctx->conn->creds, &sctx->resp);
469
0
    return TRUE;
470
0
  }
471
15
  return FALSE;
472
15
}
473
474
static bool sasl_choose_plain(struct Curl_easy *data, struct sasl_ctx *sctx)
475
15
{
476
15
  if(sctx->enabledmechs & SASL_MECH_PLAIN) {
477
0
    sctx->mech = SASL_MECH_STRING_PLAIN;
478
0
    sctx->state1 = SASL_PLAIN;
479
0
    sctx->sasl->authused = SASL_MECH_PLAIN;
480
481
0
    if(sctx->sasl->force_ir || data->set.sasl_ir)
482
0
      sctx->result =
483
0
        Curl_auth_create_plain_message(sctx->conn->creds, &sctx->resp);
484
0
    return TRUE;
485
0
  }
486
15
  return FALSE;
487
15
}
488
489
static bool sasl_choose_login(struct Curl_easy *data, struct sasl_ctx *sctx)
490
15
{
491
15
  if(sctx->enabledmechs & SASL_MECH_LOGIN) {
492
0
    sctx->mech = SASL_MECH_STRING_LOGIN;
493
0
    sctx->state1 = SASL_LOGIN;
494
0
    sctx->state2 = SASL_LOGIN_PASSWD;
495
0
    sctx->sasl->authused = SASL_MECH_LOGIN;
496
497
0
    if(sctx->sasl->force_ir || data->set.sasl_ir)
498
0
      Curl_auth_create_login_message(
499
0
        Curl_creds_user(sctx->conn->creds), &sctx->resp);
500
0
    return TRUE;
501
0
  }
502
15
  return FALSE;
503
15
}
504
505
/*
506
 * Curl_sasl_start()
507
 *
508
 * Calculate the required login details for SASL authentication.
509
 */
510
CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
511
                         bool force_ir, saslprogress *progress)
512
135
{
513
135
  struct sasl_ctx sctx;
514
515
135
  sasl->force_ir = force_ir;    /* Latch for future use */
516
135
  sasl->authused = 0;           /* No mechanism used yet */
517
135
  *progress = SASL_IDLE;
518
519
135
  memset(&sctx, 0, sizeof(sctx));
520
135
  sctx.sasl = sasl;
521
135
  sctx.conn = data->conn;
522
135
  Curl_bufref_init(&sctx.resp);
523
135
  sctx.enabledmechs = sasl->authmechs & sasl->prefmech;
524
135
  sctx.state1 = SASL_STOP;
525
135
  sctx.state2 = SASL_FINAL;
526
527
  /* Calculate the supported authentication mechanism, by decreasing order of
528
     security, as well as the initial response where appropriate */
529
135
  if(sasl_choose_external(data, &sctx) ||
530
#ifdef USE_KERBEROS5
531
     sasl_choose_krb5(data, &sctx) ||
532
#endif
533
#ifdef USE_GSASL
534
     sasl_choose_gsasl(data, &sctx) ||
535
#endif
536
135
#ifndef CURL_DISABLE_DIGEST_AUTH
537
135
     sasl_choose_digest(data, &sctx) ||
538
15
#endif
539
#ifdef USE_NTLM
540
     sasl_choose_ntlm(data, &sctx) ||
541
#endif
542
15
     sasl_choose_oauth(data, &sctx) ||
543
15
     sasl_choose_oauth2(data, &sctx) ||
544
15
     sasl_choose_plain(data, &sctx) ||
545
120
     sasl_choose_login(data, &sctx)) {
546
    /* selected, either we have a mechanism or a failure */
547
120
    DEBUGASSERT(sctx.mech || sctx.result);
548
120
  }
549
550
135
  if(!sctx.result && sctx.mech) {
551
120
    sasl->curmech = sctx.mech;
552
120
    if(Curl_bufref_ptr(&sctx.resp))
553
0
      sctx.result = build_message(sasl, &sctx.resp);
554
555
120
    if(sasl->params->maxirlen &&
556
120
       strlen(sctx.mech) + Curl_bufref_len(&sctx.resp) >
557
120
         sasl->params->maxirlen)
558
0
      Curl_bufref_free(&sctx.resp);
559
560
120
    if(!sctx.result)
561
120
      sctx.result = sasl->params->sendauth(data, sctx.mech, &sctx.resp);
562
563
120
    if(!sctx.result) {
564
120
      *progress = SASL_INPROGRESS;
565
120
      sasl_state(sasl, data, Curl_bufref_ptr(&sctx.resp) ?
566
120
                 sctx.state2 : sctx.state1);
567
120
    }
568
120
  }
569
570
135
  Curl_bufref_free(&sctx.resp);
571
135
  return sctx.result;
572
135
}
573
574
/*
575
 * Curl_sasl_continue()
576
 *
577
 * Continue the authentication.
578
 */
579
CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
580
                            int code, saslprogress *progress)
581
145
{
582
145
  CURLcode result = CURLE_OK;
583
145
  struct connectdata *conn = data->conn;
584
145
  saslstate newstate = SASL_FINAL;
585
145
  struct bufref resp;
586
145
  const char *hostname;
587
145
  int port;
588
145
  struct bufref serverdata;
589
590
145
  Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, &port);
591
145
  Curl_bufref_init(&serverdata);
592
145
  Curl_bufref_init(&resp);
593
145
  *progress = SASL_INPROGRESS;
594
595
145
  if(sasl->state == SASL_FINAL) {
596
6
    if(code != sasl->params->finalcode)
597
5
      result = CURLE_LOGIN_DENIED;
598
6
    *progress = SASL_DONE;
599
6
    sasl_state(sasl, data, SASL_STOP);
600
6
    return result;
601
6
  }
602
603
139
  if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
604
118
     code != sasl->params->contcode) {
605
1
    *progress = SASL_DONE;
606
1
    sasl_state(sasl, data, SASL_STOP);
607
1
    return CURLE_LOGIN_DENIED;
608
1
  }
609
610
138
  switch(sasl->state) {
611
0
  case SASL_STOP:
612
0
    *progress = SASL_DONE;
613
0
    return result;
614
0
  case SASL_PLAIN:
615
0
    result = Curl_auth_create_plain_message(conn->creds, &resp);
616
0
    break;
617
0
  case SASL_LOGIN:
618
0
    Curl_auth_create_login_message(Curl_creds_user(conn->creds), &resp);
619
0
    newstate = SASL_LOGIN_PASSWD;
620
0
    break;
621
0
  case SASL_LOGIN_PASSWD:
622
0
    Curl_auth_create_login_message(Curl_creds_passwd(conn->creds), &resp);
623
0
    break;
624
0
  case SASL_EXTERNAL:
625
0
    Curl_auth_create_external_message(Curl_creds_user(conn->creds), &resp);
626
0
    break;
627
#ifdef USE_GSASL
628
  case SASL_GSASL:
629
    result = get_server_message(sasl, data, &serverdata);
630
    if(!result) {
631
      struct gsasldata *gsasl = Curl_auth_gsasl_get(conn);
632
      result = !gsasl ? CURLE_OUT_OF_MEMORY :
633
        Curl_auth_gsasl_token(data, &serverdata, gsasl, &resp);
634
    }
635
    if(!result && Curl_bufref_len(&resp) > 0)
636
      newstate = SASL_GSASL;
637
    break;
638
#endif
639
0
#ifndef CURL_DISABLE_DIGEST_AUTH
640
43
  case SASL_CRAMMD5:
641
43
    result = get_server_message(sasl, data, &serverdata);
642
43
    if(!result)
643
37
      result = Curl_auth_create_cram_md5_message(&serverdata, conn->creds,
644
37
                                                 &resp);
645
43
    break;
646
74
  case SASL_DIGESTMD5:
647
74
    result = get_server_message(sasl, data, &serverdata);
648
74
    if(!result)
649
28
      result = Curl_auth_create_digest_md5_message(data, &serverdata,
650
28
                                                   conn->creds,
651
28
                                                   sasl->params->service,
652
28
                                                   &resp);
653
74
    if(!result && (sasl->params->flags & SASL_FLAG_BASE64))
654
0
      newstate = SASL_DIGESTMD5_RESP;
655
74
    break;
656
0
  case SASL_DIGESTMD5_RESP:
657
    /* Keep response NULL to output an empty line. */
658
0
    break;
659
0
#endif
660
661
#ifdef USE_NTLM
662
  case SASL_NTLM: {
663
    /* Create the type-1 message */
664
    struct ntlmdata *ntlm = Curl_auth_ntlm_get(conn, FALSE);
665
    result = !ntlm ? CURLE_OUT_OF_MEMORY :
666
      Curl_auth_create_ntlm_type1_message(data, conn->creds,
667
                                          sasl->params->service, hostname,
668
                                          ntlm, &resp);
669
    newstate = SASL_NTLM_TYPE2MSG;
670
    break;
671
  }
672
  case SASL_NTLM_TYPE2MSG: {
673
    /* Decode the type-2 message */
674
    struct ntlmdata *ntlm = Curl_auth_ntlm_get(conn, FALSE);
675
    result = !ntlm ? CURLE_OUT_OF_MEMORY :
676
      get_server_message(sasl, data, &serverdata);
677
    if(!result)
678
      result = Curl_auth_decode_ntlm_type2_message(data, &serverdata, ntlm);
679
    if(!result)
680
      result = Curl_auth_create_ntlm_type3_message(data, conn->creds,
681
                                                   ntlm, &resp);
682
    break;
683
  }
684
#endif
685
686
#ifdef USE_KERBEROS5
687
  case SASL_GSSAPI: {
688
    struct kerberos5data *krb5 = Curl_auth_krb5_get(conn);
689
    result = !krb5 ? CURLE_OUT_OF_MEMORY :
690
      Curl_auth_create_gssapi_user_message(data, conn->creds,
691
                                           sasl->params->service,
692
                                           conn->origin->hostname,
693
                                           (bool)sasl->mutual_auth, NULL,
694
                                           krb5, &resp);
695
    newstate = SASL_GSSAPI_TOKEN;
696
    break;
697
  }
698
  case SASL_GSSAPI_TOKEN:
699
    result = get_server_message(sasl, data, &serverdata);
700
    if(!result) {
701
      struct kerberos5data *krb5 = Curl_auth_krb5_get(conn);
702
      if(!krb5)
703
        result = CURLE_OUT_OF_MEMORY;
704
      else if(sasl->mutual_auth) {
705
        /* Decode the user token challenge and create the optional response
706
           message */
707
        result = Curl_auth_create_gssapi_user_message(data, NULL,
708
                                                      NULL, NULL,
709
                                                      (bool)sasl->mutual_auth,
710
                                                      &serverdata,
711
                                                      krb5, &resp);
712
        newstate = SASL_GSSAPI_NO_DATA;
713
      }
714
      else
715
        /* Decode the security challenge and create the response message */
716
        result = Curl_auth_create_gssapi_security_message(
717
          data, Curl_creds_sasl_authzid(conn->creds), &serverdata,
718
          krb5, &resp);
719
    }
720
    break;
721
  case SASL_GSSAPI_NO_DATA:
722
    /* Decode the security challenge and create the response message */
723
    result = get_server_message(sasl, data, &serverdata);
724
    if(!result) {
725
      struct kerberos5data *krb5 = Curl_auth_krb5_get(conn);
726
      if(!krb5)
727
        result = CURLE_OUT_OF_MEMORY;
728
      else
729
        result = Curl_auth_create_gssapi_security_message(
730
          data, Curl_creds_sasl_authzid(conn->creds), &serverdata,
731
          krb5, &resp);
732
    }
733
    break;
734
#endif
735
736
0
  case SASL_OAUTH2:
737
    /* Create the authorization message */
738
0
    if(sasl->authused == SASL_MECH_OAUTHBEARER) {
739
0
      result = Curl_auth_create_oauth_bearer_message(conn->creds,
740
0
                                                     hostname,
741
0
                                                     port,
742
0
                                                     &resp);
743
744
      /* Failures maybe sent by the server as continuations for OAUTHBEARER */
745
0
      newstate = SASL_OAUTH2_RESP;
746
0
    }
747
0
    else
748
0
      result = Curl_auth_create_xoauth_bearer_message(conn->creds,
749
0
                                                      &resp);
750
0
    break;
751
752
0
  case SASL_OAUTH2_RESP:
753
    /* The continuation is optional so check the response code */
754
0
    if(code == sasl->params->finalcode) {
755
      /* Final response was received so we are done */
756
0
      *progress = SASL_DONE;
757
0
      sasl_state(sasl, data, SASL_STOP);
758
0
      return result;
759
0
    }
760
0
    else if(code == sasl->params->contcode) {
761
      /* Acknowledge the continuation by sending a 0x01 response. */
762
0
      Curl_bufref_set(&resp, "\x01", 1, NULL);
763
0
      break;
764
0
    }
765
0
    else {
766
0
      *progress = SASL_DONE;
767
0
      sasl_state(sasl, data, SASL_STOP);
768
0
      return CURLE_LOGIN_DENIED;
769
0
    }
770
771
21
  case SASL_CANCEL:
772
    /* Remove the offending mechanism from the supported list */
773
21
    sasl->authmechs &= (unsigned short)~sasl->authused;
774
21
    sasl->authused = SASL_AUTH_NONE;
775
21
    sasl->curmech = NULL;
776
777
    /* Start an alternative SASL authentication */
778
21
    return Curl_sasl_start(sasl, data, (bool)sasl->force_ir, progress);
779
0
  default:
780
0
    failf(data, "Unsupported SASL authentication mechanism");
781
0
    result = CURLE_UNSUPPORTED_PROTOCOL;  /* Should not happen */
782
0
    break;
783
138
  }
784
785
117
  Curl_bufref_free(&serverdata);
786
787
117
  switch(result) {
788
80
  case CURLE_BAD_CONTENT_ENCODING:
789
    /* Cancel dialog */
790
80
    result = sasl->params->cancelauth(data, sasl->curmech);
791
80
    newstate = SASL_CANCEL;
792
80
    break;
793
37
  case CURLE_OK:
794
37
    result = build_message(sasl, &resp);
795
37
    if(!result)
796
37
      result = sasl->params->contauth(data, sasl->curmech, &resp);
797
37
    break;
798
0
  default:
799
0
    newstate = SASL_STOP;    /* Stop on error */
800
0
    *progress = SASL_DONE;
801
0
    break;
802
117
  }
803
804
117
  Curl_bufref_free(&resp);
805
806
117
  sasl_state(sasl, data, newstate);
807
808
117
  return result;
809
117
}
810
811
#ifdef CURLVERBOSE
812
static void sasl_unchosen(struct Curl_easy *data, unsigned short mech,
813
                          unsigned short enabledmechs,
814
                          bool built_in, bool platform,
815
                          const char *param_missing)
816
8
{
817
8
  const char *mname = NULL;
818
8
  size_t i;
819
820
8
  if(!(enabledmechs & mech))
821
7
    return;
822
823
7
  for(i = 0; mechtable[i].name; ++i) {
824
7
    if(mechtable[i].bit == mech) {
825
1
      mname = mechtable[i].name;
826
1
      break;
827
1
    }
828
7
  }
829
1
  if(!mname)  /* should not happen */
830
0
    return;
831
1
  if(!built_in)
832
0
    infof(data, "SASL: %s not builtin", mname);
833
1
  else if(!platform)
834
1
    infof(data, "SASL: %s not supported by the platform/libraries", mname);
835
0
  else {
836
0
    if(param_missing)
837
0
      infof(data, "SASL: %s is missing %s", mname, param_missing);
838
0
    if(!Curl_creds_has_user(data->conn->creds))
839
0
      infof(data, "SASL: %s is missing username", mname);
840
0
  }
841
1
}
842
#endif /* CURLVERBOSE */
843
844
CURLcode Curl_sasl_is_blocked(struct SASL *sasl, struct Curl_easy *data)
845
5
{
846
5
#ifdef CURLVERBOSE
847
#ifdef USE_KERBEROS5
848
#define CURL_SASL_KERBEROS5   TRUE
849
#else
850
5
#define CURL_SASL_KERBEROS5   FALSE
851
5
#endif
852
#ifdef USE_GSASL
853
#define CURL_SASL_GASL        TRUE
854
#else
855
5
#define CURL_SASL_GASL        FALSE
856
5
#endif
857
#ifdef CURL_DISABLE_DIGEST_AUTH
858
#define CURL_SASL_DIGEST      TRUE
859
#else
860
5
#define CURL_SASL_DIGEST      FALSE
861
5
#endif
862
5
#ifndef USE_NTLM
863
5
#define CURL_SASL_NTLM        TRUE
864
#else
865
#define CURL_SASL_NTLM        FALSE
866
#endif
867
  /* Failing SASL authentication is a pain. Give a helping hand if
868
   * we were unable to select an AUTH mechanism.
869
   * `sasl->authmechs` are mechanisms offered by the peer
870
   * `sasl->prefmech`  are mechanisms preferred by us */
871
5
  unsigned short enabledmechs = sasl->authmechs & sasl->prefmech;
872
873
5
  if(!sasl->authmechs)
874
3
    infof(data, "SASL: no auth mechanism was offered or recognized");
875
2
  else if(!enabledmechs)
876
1
    infof(data, "SASL: no overlap between offered and configured "
877
2
          "auth mechanisms");
878
1
  else {
879
1
    infof(data, "SASL: no auth mechanism offered could be selected");
880
1
    if((enabledmechs & SASL_MECH_EXTERNAL) &&
881
0
       Curl_creds_has_passwd(data->conn->creds))
882
0
      infof(data, "SASL: auth EXTERNAL not chosen with password");
883
1
    sasl_unchosen(data, SASL_MECH_GSSAPI, enabledmechs,
884
1
                  CURL_SASL_KERBEROS5, Curl_auth_is_gssapi_supported(), NULL);
885
1
    sasl_unchosen(data, SASL_MECH_SCRAM_SHA_256, enabledmechs,
886
1
                  CURL_SASL_GASL, FALSE, NULL);
887
1
    sasl_unchosen(data, SASL_MECH_SCRAM_SHA_1, enabledmechs,
888
1
                  CURL_SASL_GASL, FALSE, NULL);
889
1
    sasl_unchosen(data, SASL_MECH_DIGEST_MD5, enabledmechs,
890
1
                  CURL_SASL_DIGEST, Curl_auth_is_digest_supported(), NULL);
891
1
    sasl_unchosen(data, SASL_MECH_CRAM_MD5, enabledmechs,
892
1
                  CURL_SASL_DIGEST, TRUE, NULL);
893
1
    sasl_unchosen(data, SASL_MECH_NTLM, enabledmechs,
894
1
                  CURL_SASL_NTLM, Curl_auth_is_ntlm_supported(), NULL);
895
1
    sasl_unchosen(data, SASL_MECH_OAUTHBEARER, enabledmechs, TRUE, TRUE,
896
1
                  Curl_creds_has_oauth_bearer(data->conn->creds) ?
897
1
                  NULL : "CURLOPT_XOAUTH2_BEARER");
898
1
    sasl_unchosen(data, SASL_MECH_XOAUTH2, enabledmechs, TRUE, TRUE,
899
1
                  Curl_creds_has_oauth_bearer(data->conn->creds) ?
900
1
                  NULL : "CURLOPT_XOAUTH2_BEARER");
901
1
  }
902
5
#endif /* CURLVERBOSE */
903
5
  (void)sasl;
904
5
  (void)data;
905
5
  return CURLE_LOGIN_DENIED;
906
5
}
907
908
#endif /* protocols are enabled that use SASL */