113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Simon Josefsson, <simon@josefsson.org>, et al.
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci *
1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci *
1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci * KIND, either express or implied.
2013498266Sopenharmony_ci *
2113498266Sopenharmony_ci * SPDX-License-Identifier: curl
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * RFC5802 SCRAM-SHA-1 authentication
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include "curl_setup.h"
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#ifdef USE_GSASL
3013498266Sopenharmony_ci
3113498266Sopenharmony_ci#include <curl/curl.h>
3213498266Sopenharmony_ci
3313498266Sopenharmony_ci#include "vauth/vauth.h"
3413498266Sopenharmony_ci#include "urldata.h"
3513498266Sopenharmony_ci#include "sendf.h"
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci#include <gsasl.h>
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci/* The last 3 #include files should be in this order */
4013498266Sopenharmony_ci#include "curl_printf.h"
4113498266Sopenharmony_ci#include "curl_memory.h"
4213498266Sopenharmony_ci#include "memdebug.h"
4313498266Sopenharmony_ci
4413498266Sopenharmony_cibool Curl_auth_gsasl_is_supported(struct Curl_easy *data,
4513498266Sopenharmony_ci                                  const char *mech,
4613498266Sopenharmony_ci                                  struct gsasldata *gsasl)
4713498266Sopenharmony_ci{
4813498266Sopenharmony_ci  int res;
4913498266Sopenharmony_ci
5013498266Sopenharmony_ci  res = gsasl_init(&gsasl->ctx);
5113498266Sopenharmony_ci  if(res != GSASL_OK) {
5213498266Sopenharmony_ci    failf(data, "gsasl init: %s\n", gsasl_strerror(res));
5313498266Sopenharmony_ci    return FALSE;
5413498266Sopenharmony_ci  }
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci  res = gsasl_client_start(gsasl->ctx, mech, &gsasl->client);
5713498266Sopenharmony_ci  if(res != GSASL_OK) {
5813498266Sopenharmony_ci    gsasl_done(gsasl->ctx);
5913498266Sopenharmony_ci    return FALSE;
6013498266Sopenharmony_ci  }
6113498266Sopenharmony_ci
6213498266Sopenharmony_ci  return true;
6313498266Sopenharmony_ci}
6413498266Sopenharmony_ci
6513498266Sopenharmony_ciCURLcode Curl_auth_gsasl_start(struct Curl_easy *data,
6613498266Sopenharmony_ci                               const char *userp,
6713498266Sopenharmony_ci                               const char *passwdp,
6813498266Sopenharmony_ci                               struct gsasldata *gsasl)
6913498266Sopenharmony_ci{
7013498266Sopenharmony_ci#if GSASL_VERSION_NUMBER >= 0x010b00
7113498266Sopenharmony_ci  int res;
7213498266Sopenharmony_ci  res =
7313498266Sopenharmony_ci#endif
7413498266Sopenharmony_ci    gsasl_property_set(gsasl->client, GSASL_AUTHID, userp);
7513498266Sopenharmony_ci#if GSASL_VERSION_NUMBER >= 0x010b00
7613498266Sopenharmony_ci  if(res != GSASL_OK) {
7713498266Sopenharmony_ci    failf(data, "setting AUTHID failed: %s\n", gsasl_strerror(res));
7813498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
7913498266Sopenharmony_ci  }
8013498266Sopenharmony_ci#endif
8113498266Sopenharmony_ci
8213498266Sopenharmony_ci#if GSASL_VERSION_NUMBER >= 0x010b00
8313498266Sopenharmony_ci  res =
8413498266Sopenharmony_ci#endif
8513498266Sopenharmony_ci    gsasl_property_set(gsasl->client, GSASL_PASSWORD, passwdp);
8613498266Sopenharmony_ci#if GSASL_VERSION_NUMBER >= 0x010b00
8713498266Sopenharmony_ci  if(res != GSASL_OK) {
8813498266Sopenharmony_ci    failf(data, "setting PASSWORD failed: %s\n", gsasl_strerror(res));
8913498266Sopenharmony_ci    return CURLE_OUT_OF_MEMORY;
9013498266Sopenharmony_ci  }
9113498266Sopenharmony_ci#endif
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci  (void)data;
9413498266Sopenharmony_ci
9513498266Sopenharmony_ci  return CURLE_OK;
9613498266Sopenharmony_ci}
9713498266Sopenharmony_ci
9813498266Sopenharmony_ciCURLcode Curl_auth_gsasl_token(struct Curl_easy *data,
9913498266Sopenharmony_ci                               const struct bufref *chlg,
10013498266Sopenharmony_ci                               struct gsasldata *gsasl,
10113498266Sopenharmony_ci                               struct bufref *out)
10213498266Sopenharmony_ci{
10313498266Sopenharmony_ci  int res;
10413498266Sopenharmony_ci  char *response;
10513498266Sopenharmony_ci  size_t outlen;
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci  res = gsasl_step(gsasl->client,
10813498266Sopenharmony_ci                   (const char *) Curl_bufref_ptr(chlg), Curl_bufref_len(chlg),
10913498266Sopenharmony_ci                   &response, &outlen);
11013498266Sopenharmony_ci  if(res != GSASL_OK && res != GSASL_NEEDS_MORE) {
11113498266Sopenharmony_ci    failf(data, "GSASL step: %s\n", gsasl_strerror(res));
11213498266Sopenharmony_ci    return CURLE_BAD_CONTENT_ENCODING;
11313498266Sopenharmony_ci  }
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci  Curl_bufref_set(out, response, outlen, gsasl_free);
11613498266Sopenharmony_ci  return CURLE_OK;
11713498266Sopenharmony_ci}
11813498266Sopenharmony_ci
11913498266Sopenharmony_civoid Curl_auth_gsasl_cleanup(struct gsasldata *gsasl)
12013498266Sopenharmony_ci{
12113498266Sopenharmony_ci  gsasl_finish(gsasl->client);
12213498266Sopenharmony_ci  gsasl->client = NULL;
12313498266Sopenharmony_ci
12413498266Sopenharmony_ci  gsasl_done(gsasl->ctx);
12513498266Sopenharmony_ci  gsasl->ctx = NULL;
12613498266Sopenharmony_ci}
12713498266Sopenharmony_ci#endif
128