113498266Sopenharmony_ci/***************************************************************************
213498266Sopenharmony_ci *                                  _   _ ____  _
313498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci *                             / __| | | | |_) | |
513498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci *
813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 ***************************************************************************/
2413498266Sopenharmony_ci
2513498266Sopenharmony_ci#include "curl_setup.h"
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#ifdef HAVE_GSSAPI
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#include "curl_gssapi.h"
3013498266Sopenharmony_ci#include "sendf.h"
3113498266Sopenharmony_ci
3213498266Sopenharmony_ci/* The last 3 #include files should be in this order */
3313498266Sopenharmony_ci#include "curl_printf.h"
3413498266Sopenharmony_ci#include "curl_memory.h"
3513498266Sopenharmony_ci#include "memdebug.h"
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci#if defined(__GNUC__)
3813498266Sopenharmony_ci#define CURL_ALIGN8   __attribute__ ((aligned(8)))
3913498266Sopenharmony_ci#else
4013498266Sopenharmony_ci#define CURL_ALIGN8
4113498266Sopenharmony_ci#endif
4213498266Sopenharmony_ci
4313498266Sopenharmony_cigss_OID_desc Curl_spnego_mech_oid CURL_ALIGN8 = {
4413498266Sopenharmony_ci  6, (char *)"\x2b\x06\x01\x05\x05\x02"
4513498266Sopenharmony_ci};
4613498266Sopenharmony_cigss_OID_desc Curl_krb5_mech_oid CURL_ALIGN8 = {
4713498266Sopenharmony_ci  9, (char *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"
4813498266Sopenharmony_ci};
4913498266Sopenharmony_ci
5013498266Sopenharmony_ciOM_uint32 Curl_gss_init_sec_context(
5113498266Sopenharmony_ci    struct Curl_easy *data,
5213498266Sopenharmony_ci    OM_uint32 *minor_status,
5313498266Sopenharmony_ci    gss_ctx_id_t *context,
5413498266Sopenharmony_ci    gss_name_t target_name,
5513498266Sopenharmony_ci    gss_OID mech_type,
5613498266Sopenharmony_ci    gss_channel_bindings_t input_chan_bindings,
5713498266Sopenharmony_ci    gss_buffer_t input_token,
5813498266Sopenharmony_ci    gss_buffer_t output_token,
5913498266Sopenharmony_ci    const bool mutual_auth,
6013498266Sopenharmony_ci    OM_uint32 *ret_flags)
6113498266Sopenharmony_ci{
6213498266Sopenharmony_ci  OM_uint32 req_flags = GSS_C_REPLAY_FLAG;
6313498266Sopenharmony_ci
6413498266Sopenharmony_ci  if(mutual_auth)
6513498266Sopenharmony_ci    req_flags |= GSS_C_MUTUAL_FLAG;
6613498266Sopenharmony_ci
6713498266Sopenharmony_ci  if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_POLICY_FLAG) {
6813498266Sopenharmony_ci#ifdef GSS_C_DELEG_POLICY_FLAG
6913498266Sopenharmony_ci    req_flags |= GSS_C_DELEG_POLICY_FLAG;
7013498266Sopenharmony_ci#else
7113498266Sopenharmony_ci    infof(data, "WARNING: support for CURLGSSAPI_DELEGATION_POLICY_FLAG not "
7213498266Sopenharmony_ci        "compiled in");
7313498266Sopenharmony_ci#endif
7413498266Sopenharmony_ci  }
7513498266Sopenharmony_ci
7613498266Sopenharmony_ci  if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_FLAG)
7713498266Sopenharmony_ci    req_flags |= GSS_C_DELEG_FLAG;
7813498266Sopenharmony_ci
7913498266Sopenharmony_ci  return gss_init_sec_context(minor_status,
8013498266Sopenharmony_ci                              GSS_C_NO_CREDENTIAL, /* cred_handle */
8113498266Sopenharmony_ci                              context,
8213498266Sopenharmony_ci                              target_name,
8313498266Sopenharmony_ci                              mech_type,
8413498266Sopenharmony_ci                              req_flags,
8513498266Sopenharmony_ci                              0, /* time_req */
8613498266Sopenharmony_ci                              input_chan_bindings,
8713498266Sopenharmony_ci                              input_token,
8813498266Sopenharmony_ci                              NULL, /* actual_mech_type */
8913498266Sopenharmony_ci                              output_token,
9013498266Sopenharmony_ci                              ret_flags,
9113498266Sopenharmony_ci                              NULL /* time_rec */);
9213498266Sopenharmony_ci}
9313498266Sopenharmony_ci
9413498266Sopenharmony_ci#define GSS_LOG_BUFFER_LEN 1024
9513498266Sopenharmony_cistatic size_t display_gss_error(OM_uint32 status, int type,
9613498266Sopenharmony_ci                                char *buf, size_t len) {
9713498266Sopenharmony_ci  OM_uint32 maj_stat;
9813498266Sopenharmony_ci  OM_uint32 min_stat;
9913498266Sopenharmony_ci  OM_uint32 msg_ctx = 0;
10013498266Sopenharmony_ci  gss_buffer_desc status_string = GSS_C_EMPTY_BUFFER;
10113498266Sopenharmony_ci
10213498266Sopenharmony_ci  do {
10313498266Sopenharmony_ci    maj_stat = gss_display_status(&min_stat,
10413498266Sopenharmony_ci                                  status,
10513498266Sopenharmony_ci                                  type,
10613498266Sopenharmony_ci                                  GSS_C_NO_OID,
10713498266Sopenharmony_ci                                  &msg_ctx,
10813498266Sopenharmony_ci                                  &status_string);
10913498266Sopenharmony_ci    if(maj_stat == GSS_S_COMPLETE && status_string.length > 0) {
11013498266Sopenharmony_ci      if(GSS_LOG_BUFFER_LEN > len + status_string.length + 3) {
11113498266Sopenharmony_ci        len += msnprintf(buf + len, GSS_LOG_BUFFER_LEN - len,
11213498266Sopenharmony_ci                         "%.*s. ", (int)status_string.length,
11313498266Sopenharmony_ci                         (char *)status_string.value);
11413498266Sopenharmony_ci      }
11513498266Sopenharmony_ci    }
11613498266Sopenharmony_ci    gss_release_buffer(&min_stat, &status_string);
11713498266Sopenharmony_ci  } while(!GSS_ERROR(maj_stat) && msg_ctx);
11813498266Sopenharmony_ci
11913498266Sopenharmony_ci  return len;
12013498266Sopenharmony_ci}
12113498266Sopenharmony_ci
12213498266Sopenharmony_ci/*
12313498266Sopenharmony_ci * Curl_gss_log_error()
12413498266Sopenharmony_ci *
12513498266Sopenharmony_ci * This is used to log a GSS-API error status.
12613498266Sopenharmony_ci *
12713498266Sopenharmony_ci * Parameters:
12813498266Sopenharmony_ci *
12913498266Sopenharmony_ci * data    [in] - The session handle.
13013498266Sopenharmony_ci * prefix  [in] - The prefix of the log message.
13113498266Sopenharmony_ci * major   [in] - The major status code.
13213498266Sopenharmony_ci * minor   [in] - The minor status code.
13313498266Sopenharmony_ci */
13413498266Sopenharmony_civoid Curl_gss_log_error(struct Curl_easy *data, const char *prefix,
13513498266Sopenharmony_ci                        OM_uint32 major, OM_uint32 minor)
13613498266Sopenharmony_ci{
13713498266Sopenharmony_ci  char buf[GSS_LOG_BUFFER_LEN];
13813498266Sopenharmony_ci  size_t len = 0;
13913498266Sopenharmony_ci
14013498266Sopenharmony_ci  if(major != GSS_S_FAILURE)
14113498266Sopenharmony_ci    len = display_gss_error(major, GSS_C_GSS_CODE, buf, len);
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci  display_gss_error(minor, GSS_C_MECH_CODE, buf, len);
14413498266Sopenharmony_ci
14513498266Sopenharmony_ci  infof(data, "%s%s", prefix, buf);
14613498266Sopenharmony_ci#ifdef CURL_DISABLE_VERBOSE_STRINGS
14713498266Sopenharmony_ci  (void)data;
14813498266Sopenharmony_ci  (void)prefix;
14913498266Sopenharmony_ci#endif
15013498266Sopenharmony_ci}
15113498266Sopenharmony_ci
15213498266Sopenharmony_ci#endif /* HAVE_GSSAPI */
153