Coverage Report

Created: 2026-02-26 06:33

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