Coverage Report

Created: 2026-07-30 07:26

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
2.63k
{
84
2.63k
  unsigned int i;
85
2.63k
  char c;
86
87
28.4k
  for(i = 0; mechtable[i].name; i++) {
88
26.2k
    if(maxlen >= mechtable[i].len &&
89
9.13k
       curl_strnequal(ptr, mechtable[i].name, mechtable[i].len)) {
90
701
      if(len)
91
701
        *len = mechtable[i].len;
92
93
701
      if(maxlen == mechtable[i].len)
94
273
        return mechtable[i].bit;
95
96
428
      c = ptr[mechtable[i].len];
97
428
      if(!ISUPPER(c) && !ISDIGIT(c) && c != '-' && c != '_')
98
139
        return mechtable[i].bit;
99
428
    }
100
26.2k
  }
101
102
2.21k
  return 0;
103
2.63k
}
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
136
{
113
136
  CURLcode result = CURLE_OK;
114
136
  size_t mechlen;
115
116
136
  if(!len)
117
25
    return CURLE_URL_MALFORMAT;
118
119
111
  if(sasl->resetprefs) {
120
72
    sasl->resetprefs = FALSE;
121
72
    sasl->prefmech = SASL_AUTH_NONE;
122
72
  }
123
124
111
  if(!strncmp(value, "*", len))
125
31
    sasl->prefmech = SASL_AUTH_DEFAULT;
126
80
  else {
127
80
    unsigned short mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
128
80
    if(mechbit && mechlen == len)
129
27
      sasl->prefmech |= mechbit;
130
53
    else
131
53
      result = CURLE_URL_MALFORMAT;
132
80
  }
133
134
111
  return result;
135
136
}
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
14.4k
{
145
14.4k
  unsigned long auth = data->set.httpauth;
146
147
14.4k
  sasl->params = params;           /* Set protocol dependent parameters */
148
14.4k
  sasl->state = SASL_STOP;         /* Not yet running */
149
14.4k
  sasl->curmech = NULL;            /* No mechanism yet. */
150
14.4k
  sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
151
14.4k
  sasl->prefmech = params->defmechs; /* Default preferred mechanisms */
152
14.4k
  sasl->authused = SASL_AUTH_NONE; /* The authentication mechanism used */
153
14.4k
  sasl->resetprefs = TRUE;         /* Reset prefmech upon AUTH parsing. */
154
14.4k
  sasl->mutual_auth = FALSE;       /* No mutual authentication (GSSAPI only) */
155
14.4k
  sasl->force_ir = FALSE;          /* Respect external option */
156
157
14.4k
  if(auth != CURLAUTH_BASIC) {
158
1.53k
    unsigned short mechs = SASL_AUTH_NONE;
159
160
    /* If some usable http authentication options have been set, determine
161
       new defaults from them. */
162
1.53k
    if(auth & CURLAUTH_BASIC)
163
263
      mechs |= SASL_MECH_PLAIN | SASL_MECH_LOGIN;
164
1.53k
    if(auth & CURLAUTH_DIGEST)
165
328
      mechs |= SASL_MECH_DIGEST_MD5;
166
1.53k
    if(auth & CURLAUTH_NTLM)
167
0
      mechs |= SASL_MECH_NTLM;
168
1.53k
    if(auth & CURLAUTH_BEARER)
169
249
      mechs |= SASL_MECH_OAUTHBEARER | SASL_MECH_XOAUTH2;
170
1.53k
    if(auth & CURLAUTH_GSSAPI)
171
0
      mechs |= SASL_MECH_GSSAPI;
172
173
1.53k
    if(mechs != SASL_AUTH_NONE)
174
426
      sasl->prefmech = mechs;
175
1.53k
  }
176
14.4k
}
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
430
{
186
430
#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
187
  /* for debug purposes */
188
430
  static const char * const names[] = {
189
430
    "STOP",
190
430
    "PLAIN",
191
430
    "LOGIN",
192
430
    "LOGIN_PASSWD",
193
430
    "EXTERNAL",
194
430
    "CRAMMD5",
195
430
    "DIGESTMD5",
196
430
    "DIGESTMD5_RESP",
197
430
    "NTLM",
198
430
    "NTLM_TYPE2MSG",
199
430
    "GSSAPI",
200
430
    "GSSAPI_TOKEN",
201
430
    "GSSAPI_NO_DATA",
202
430
    "OAUTH2",
203
430
    "OAUTH2_RESP",
204
430
    "GSASL",
205
430
    "CANCEL",
206
430
    "FINAL",
207
    /* LAST */
208
430
  };
209
210
430
  if(sasl->state != newstate)
211
430
    infof(data, "SASL %p state change from %s to %s",
212
430
          (void *)sasl, names[sasl->state], names[newstate]);
213
#else
214
  (void)data;
215
#endif
216
217
430
  sasl->state = newstate;
218
430
}
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
185
{
226
185
  CURLcode result = CURLE_OK;
227
228
185
  result = sasl->params->getmessage(data, out);
229
185
  if(!result && (sasl->params->flags & SASL_FLAG_BASE64)) {
230
185
    const char *serverdata = Curl_bufref_ptr(out);
231
232
185
    if(!*serverdata)
233
35
      Curl_bufref_set(out, NULL, 0, NULL);
234
150
    else {
235
150
      unsigned char *msg;
236
150
      size_t msglen;
237
238
150
      result = curlx_base64_decode(serverdata, &msg, &msglen);
239
150
      if(!result)
240
57
        Curl_bufref_set(out, msg, msglen, curl_free);
241
150
    }
242
185
  }
243
185
  return result;
244
185
}
245
#endif
246
247
/* Encode the outgoing SASL message. */
248
static CURLcode build_message(struct SASL *sasl, struct bufref *msg)
249
50
{
250
50
  CURLcode result = CURLE_OK;
251
252
50
  if(sasl->params->flags & SASL_FLAG_BASE64) {
253
50
    if(!Curl_bufref_ptr(msg))                   /* Empty message. */
254
0
      Curl_bufref_set(msg, "", 0, NULL);
255
50
    else if(Curl_bufref_len(msg)) {
256
47
      char *base64;
257
47
      size_t base64len;
258
259
47
      result = curlx_base64_encode(Curl_bufref_uptr(msg),
260
47
                                   Curl_bufref_len(msg), &base64, &base64len);
261
47
      if(!result)
262
47
        Curl_bufref_set(msg, base64, base64len, curl_free);
263
47
    }
264
50
  }
265
266
50
  return result;
267
50
}
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
2.38k
{
276
  /* Have credentials been provided? */
277
2.38k
  if(data->conn->creds)
278
253
    return TRUE;
279
280
  /* EXTERNAL can authenticate without a username and/or password */
281
2.13k
  if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
282
0
    return TRUE;
283
284
2.13k
  return FALSE;
285
2.13k
}
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
279
{
300
279
  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
279
  return FALSE;
312
279
}
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
279
{
389
279
  (void)data;
390
279
  if((sctx->enabledmechs & SASL_MECH_DIGEST_MD5) &&
391
123
     Curl_auth_is_digest_supported()) {
392
123
    sctx->mech = SASL_MECH_STRING_DIGEST_MD5;
393
123
    sctx->state1 = SASL_DIGESTMD5;
394
123
    sctx->sasl->authused = SASL_MECH_DIGEST_MD5;
395
123
    return TRUE;
396
123
  }
397
156
  else if(sctx->enabledmechs & SASL_MECH_CRAM_MD5) {
398
75
    sctx->mech = SASL_MECH_STRING_CRAM_MD5;
399
75
    sctx->state1 = SASL_CRAMMD5;
400
75
    sctx->sasl->authused = SASL_MECH_CRAM_MD5;
401
75
    return TRUE;
402
75
  }
403
81
  return FALSE;
404
279
}
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
81
{
438
81
  if(Curl_creds_has_oauth_bearer(data->state.creds) &&
439
12
     (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
81
  return FALSE;
456
81
}
457
458
static bool sasl_choose_oauth2(struct Curl_easy *data, struct sasl_ctx *sctx)
459
81
{
460
81
  if(Curl_creds_has_oauth_bearer(sctx->conn->creds) &&
461
12
     (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
81
  return FALSE;
472
81
}
473
474
static bool sasl_choose_plain(struct Curl_easy *data, struct sasl_ctx *sctx)
475
81
{
476
81
  if(sctx->enabledmechs & SASL_MECH_PLAIN) {
477
9
    sctx->mech = SASL_MECH_STRING_PLAIN;
478
9
    sctx->state1 = SASL_PLAIN;
479
9
    sctx->sasl->authused = SASL_MECH_PLAIN;
480
481
9
    if(sctx->sasl->force_ir || data->set.sasl_ir)
482
1
      sctx->result =
483
1
        Curl_auth_create_plain_message(sctx->conn->creds, &sctx->resp);
484
9
    return TRUE;
485
9
  }
486
72
  return FALSE;
487
81
}
488
489
static bool sasl_choose_login(struct Curl_easy *data, struct sasl_ctx *sctx)
490
72
{
491
72
  if(sctx->enabledmechs & SASL_MECH_LOGIN) {
492
3
    sctx->mech = SASL_MECH_STRING_LOGIN;
493
3
    sctx->state1 = SASL_LOGIN;
494
3
    sctx->state2 = SASL_LOGIN_PASSWD;
495
3
    sctx->sasl->authused = SASL_MECH_LOGIN;
496
497
3
    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
3
    return TRUE;
501
3
  }
502
69
  return FALSE;
503
72
}
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
279
{
513
279
  struct sasl_ctx sctx;
514
515
279
  sasl->force_ir = force_ir;    /* Latch for future use */
516
279
  sasl->authused = 0;           /* No mechanism used yet */
517
279
  *progress = SASL_IDLE;
518
519
279
  memset(&sctx, 0, sizeof(sctx));
520
279
  sctx.sasl = sasl;
521
279
  sctx.conn = data->conn;
522
279
  Curl_bufref_init(&sctx.resp);
523
279
  sctx.enabledmechs = sasl->authmechs & sasl->prefmech;
524
279
  sctx.state1 = SASL_STOP;
525
279
  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
279
  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
279
#ifndef CURL_DISABLE_DIGEST_AUTH
537
279
     sasl_choose_digest(data, &sctx) ||
538
81
#endif
539
#ifdef USE_NTLM
540
     sasl_choose_ntlm(data, &sctx) ||
541
#endif
542
81
     sasl_choose_oauth(data, &sctx) ||
543
81
     sasl_choose_oauth2(data, &sctx) ||
544
81
     sasl_choose_plain(data, &sctx) ||
545
210
     sasl_choose_login(data, &sctx)) {
546
    /* selected, either we have a mechanism or a failure */
547
210
    DEBUGASSERT(sctx.mech || sctx.result);
548
210
  }
549
550
279
  if(!sctx.result && sctx.mech) {
551
210
    sasl->curmech = sctx.mech;
552
210
    if(Curl_bufref_ptr(&sctx.resp))
553
1
      sctx.result = build_message(sasl, &sctx.resp);
554
555
210
    if(sasl->params->maxirlen &&
556
210
       strlen(sctx.mech) + Curl_bufref_len(&sctx.resp) >
557
210
         sasl->params->maxirlen)
558
0
      Curl_bufref_free(&sctx.resp);
559
560
210
    if(!sctx.result)
561
210
      sctx.result = sasl->params->sendauth(data, sctx.mech, &sctx.resp);
562
563
210
    if(!sctx.result) {
564
210
      *progress = SASL_INPROGRESS;
565
210
      sasl_state(sasl, data, Curl_bufref_ptr(&sctx.resp) ?
566
209
                 sctx.state2 : sctx.state1);
567
210
    }
568
210
  }
569
570
279
  Curl_bufref_free(&sctx.resp);
571
279
  return sctx.result;
572
279
}
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
261
{
582
261
  CURLcode result = CURLE_OK;
583
261
  struct connectdata *conn = data->conn;
584
261
  saslstate newstate = SASL_FINAL;
585
261
  struct bufref resp;
586
261
  const char *hostname;
587
261
  int port;
588
261
  struct bufref serverdata;
589
590
261
  Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, &port);
591
261
  Curl_bufref_init(&serverdata);
592
261
  Curl_bufref_init(&resp);
593
261
  *progress = SASL_INPROGRESS;
594
595
261
  if(sasl->state == SASL_FINAL) {
596
14
    if(code != sasl->params->finalcode)
597
13
      result = CURLE_LOGIN_DENIED;
598
14
    *progress = SASL_DONE;
599
14
    sasl_state(sasl, data, SASL_STOP);
600
14
    return result;
601
14
  }
602
603
247
  if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
604
206
     code != sasl->params->contcode) {
605
13
    *progress = SASL_DONE;
606
13
    sasl_state(sasl, data, SASL_STOP);
607
13
    return CURLE_LOGIN_DENIED;
608
13
  }
609
610
234
  switch(sasl->state) {
611
0
  case SASL_STOP:
612
0
    *progress = SASL_DONE;
613
0
    return result;
614
3
  case SASL_PLAIN:
615
3
    result = Curl_auth_create_plain_message(conn->creds, &resp);
616
3
    break;
617
3
  case SASL_LOGIN:
618
3
    Curl_auth_create_login_message(Curl_creds_user(conn->creds), &resp);
619
3
    newstate = SASL_LOGIN_PASSWD;
620
3
    break;
621
2
  case SASL_LOGIN_PASSWD:
622
2
    Curl_auth_create_login_message(Curl_creds_passwd(conn->creds), &resp);
623
2
    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
66
  case SASL_CRAMMD5:
641
66
    result = get_server_message(sasl, data, &serverdata);
642
66
    if(!result)
643
41
      result = Curl_auth_create_cram_md5_message(&serverdata, conn->creds,
644
41
                                                 &resp);
645
66
    break;
646
119
  case SASL_DIGESTMD5:
647
119
    result = get_server_message(sasl, data, &serverdata);
648
119
    if(!result)
649
51
      result = Curl_auth_create_digest_md5_message(data, &serverdata,
650
51
                                                   conn->creds,
651
51
                                                   sasl->params->service,
652
51
                                                   &resp);
653
119
    if(!result && (sasl->params->flags & SASL_FLAG_BASE64))
654
0
      newstate = SASL_DIGESTMD5_RESP;
655
119
    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
41
  case SASL_CANCEL:
772
    /* Remove the offending mechanism from the supported list */
773
41
    sasl->authmechs &= (unsigned short)~sasl->authused;
774
41
    sasl->authused = SASL_AUTH_NONE;
775
41
    sasl->curmech = NULL;
776
777
    /* Start an alternative SASL authentication */
778
41
    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
234
  }
784
785
193
  Curl_bufref_free(&serverdata);
786
787
193
  switch(result) {
788
144
  case CURLE_BAD_CONTENT_ENCODING:
789
    /* Cancel dialog */
790
144
    result = sasl->params->cancelauth(data, sasl->curmech);
791
144
    newstate = SASL_CANCEL;
792
144
    break;
793
49
  case CURLE_OK:
794
49
    result = build_message(sasl, &resp);
795
49
    if(!result)
796
49
      result = sasl->params->contauth(data, sasl->curmech, &resp);
797
49
    break;
798
0
  default:
799
0
    newstate = SASL_STOP;    /* Stop on error */
800
0
    *progress = SASL_DONE;
801
0
    break;
802
193
  }
803
804
193
  Curl_bufref_free(&resp);
805
806
193
  sasl_state(sasl, data, newstate);
807
808
193
  return result;
809
193
}
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
16
{
817
16
  const char *mname = NULL;
818
16
  size_t i;
819
820
16
  if(!(enabledmechs & mech))
821
14
    return;
822
823
14
  for(i = 0; mechtable[i].name; ++i) {
824
14
    if(mechtable[i].bit == mech) {
825
2
      mname = mechtable[i].name;
826
2
      break;
827
2
    }
828
14
  }
829
2
  if(!mname)  /* should not happen */
830
0
    return;
831
2
  if(!built_in)
832
0
    infof(data, "SASL: %s not builtin", mname);
833
2
  else if(!platform)
834
2
    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
2
}
842
#endif /* CURLVERBOSE */
843
844
CURLcode Curl_sasl_is_blocked(struct SASL *sasl, struct Curl_easy *data)
845
12
{
846
12
#ifdef CURLVERBOSE
847
#ifdef USE_KERBEROS5
848
#define CURL_SASL_KERBEROS5   TRUE
849
#else
850
12
#define CURL_SASL_KERBEROS5   FALSE
851
12
#endif
852
#ifdef USE_GSASL
853
#define CURL_SASL_GASL        TRUE
854
#else
855
12
#define CURL_SASL_GASL        FALSE
856
12
#endif
857
#ifdef CURL_DISABLE_DIGEST_AUTH
858
#define CURL_SASL_DIGEST      TRUE
859
#else
860
12
#define CURL_SASL_DIGEST      FALSE
861
12
#endif
862
12
#ifndef USE_NTLM
863
12
#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
12
  unsigned short enabledmechs = sasl->authmechs & sasl->prefmech;
872
873
12
  if(!sasl->authmechs)
874
8
    infof(data, "SASL: no auth mechanism was offered or recognized");
875
4
  else if(!enabledmechs)
876
2
    infof(data, "SASL: no overlap between offered and configured "
877
4
          "auth mechanisms");
878
2
  else {
879
2
    infof(data, "SASL: no auth mechanism offered could be selected");
880
2
    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
2
    sasl_unchosen(data, SASL_MECH_GSSAPI, enabledmechs,
884
2
                  CURL_SASL_KERBEROS5, Curl_auth_is_gssapi_supported(), NULL);
885
2
    sasl_unchosen(data, SASL_MECH_SCRAM_SHA_256, enabledmechs,
886
2
                  CURL_SASL_GASL, FALSE, NULL);
887
2
    sasl_unchosen(data, SASL_MECH_SCRAM_SHA_1, enabledmechs,
888
2
                  CURL_SASL_GASL, FALSE, NULL);
889
2
    sasl_unchosen(data, SASL_MECH_DIGEST_MD5, enabledmechs,
890
2
                  CURL_SASL_DIGEST, Curl_auth_is_digest_supported(), NULL);
891
2
    sasl_unchosen(data, SASL_MECH_CRAM_MD5, enabledmechs,
892
2
                  CURL_SASL_DIGEST, TRUE, NULL);
893
2
    sasl_unchosen(data, SASL_MECH_NTLM, enabledmechs,
894
2
                  CURL_SASL_NTLM, Curl_auth_is_ntlm_supported(), NULL);
895
2
    sasl_unchosen(data, SASL_MECH_OAUTHBEARER, enabledmechs, TRUE, TRUE,
896
2
                  Curl_creds_has_oauth_bearer(data->conn->creds) ?
897
2
                  NULL : "CURLOPT_XOAUTH2_BEARER");
898
2
    sasl_unchosen(data, SASL_MECH_XOAUTH2, enabledmechs, TRUE, TRUE,
899
2
                  Curl_creds_has_oauth_bearer(data->conn->creds) ?
900
2
                  NULL : "CURLOPT_XOAUTH2_BEARER");
901
2
  }
902
12
#endif /* CURLVERBOSE */
903
12
  (void)sasl;
904
12
  (void)data;
905
12
  return CURLE_LOGIN_DENIED;
906
12
}
907
908
#endif /* protocols are enabled that use SASL */