1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2011 - 2021, 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  ***************************************************************************/
22 
23 #include "curl_setup.h"
24 
25 #ifdef HAVE_GSSAPI
26 
27 #include "curl_gssapi.h"
28 #include "sendf.h"
29 
30 /* The last 3 #include files should be in this order */
31 #include "curl_printf.h"
32 #include "curl_memory.h"
33 #include "memdebug.h"
34 
35 gss_OID_desc Curl_spnego_mech_oid = {
36   6, (char *)"\x2b\x06\x01\x05\x05\x02"
37 };
38 gss_OID_desc Curl_krb5_mech_oid = {
39   9, (char *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"
40 };
41 
Curl_gss_init_sec_context(struct Curl_easy * data,OM_uint32 * minor_status,gss_ctx_id_t * context,gss_name_t target_name,gss_OID mech_type,gss_channel_bindings_t input_chan_bindings,gss_buffer_t input_token,gss_buffer_t output_token,const bool mutual_auth,OM_uint32 * ret_flags)42 OM_uint32 Curl_gss_init_sec_context(
43     struct Curl_easy *data,
44     OM_uint32 *minor_status,
45     gss_ctx_id_t *context,
46     gss_name_t target_name,
47     gss_OID mech_type,
48     gss_channel_bindings_t input_chan_bindings,
49     gss_buffer_t input_token,
50     gss_buffer_t output_token,
51     const bool mutual_auth,
52     OM_uint32 *ret_flags)
53 {
54   OM_uint32 req_flags = GSS_C_REPLAY_FLAG;
55 
56   if(mutual_auth)
57     req_flags |= GSS_C_MUTUAL_FLAG;
58 
59   if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_POLICY_FLAG) {
60 #ifdef GSS_C_DELEG_POLICY_FLAG
61     req_flags |= GSS_C_DELEG_POLICY_FLAG;
62 #else
63     infof(data, "warning: support for CURLGSSAPI_DELEGATION_POLICY_FLAG not "
64         "compiled in");
65 #endif
66   }
67 
68   if(data->set.gssapi_delegation & CURLGSSAPI_DELEGATION_FLAG)
69     req_flags |= GSS_C_DELEG_FLAG;
70 
71   return gss_init_sec_context(minor_status,
72                               GSS_C_NO_CREDENTIAL, /* cred_handle */
73                               context,
74                               target_name,
75                               mech_type,
76                               req_flags,
77                               0, /* time_req */
78                               input_chan_bindings,
79                               input_token,
80                               NULL, /* actual_mech_type */
81                               output_token,
82                               ret_flags,
83                               NULL /* time_rec */);
84 }
85 
86 #define GSS_LOG_BUFFER_LEN 1024
display_gss_error(OM_uint32 status,int type,char * buf,size_t len)87 static size_t display_gss_error(OM_uint32 status, int type,
88                                 char *buf, size_t len) {
89   OM_uint32 maj_stat;
90   OM_uint32 min_stat;
91   OM_uint32 msg_ctx = 0;
92   gss_buffer_desc status_string;
93 
94   do {
95     maj_stat = gss_display_status(&min_stat,
96                                   status,
97                                   type,
98                                   GSS_C_NO_OID,
99                                   &msg_ctx,
100                                   &status_string);
101     if(GSS_LOG_BUFFER_LEN > len + status_string.length + 3) {
102       len += msnprintf(buf + len, GSS_LOG_BUFFER_LEN - len,
103                        "%.*s. ", (int)status_string.length,
104                        (char *)status_string.value);
105     }
106     gss_release_buffer(&min_stat, &status_string);
107   } while(!GSS_ERROR(maj_stat) && msg_ctx);
108 
109   return len;
110 }
111 
112 /*
113  * Curl_gss_log_error()
114  *
115  * This is used to log a GSS-API error status.
116  *
117  * Parameters:
118  *
119  * data    [in] - The session handle.
120  * prefix  [in] - The prefix of the log message.
121  * major   [in] - The major status code.
122  * minor   [in] - The minor status code.
123  */
Curl_gss_log_error(struct Curl_easy * data,const char * prefix,OM_uint32 major,OM_uint32 minor)124 void Curl_gss_log_error(struct Curl_easy *data, const char *prefix,
125                         OM_uint32 major, OM_uint32 minor)
126 {
127   char buf[GSS_LOG_BUFFER_LEN];
128   size_t len = 0;
129 
130   if(major != GSS_S_FAILURE)
131     len = display_gss_error(major, GSS_C_GSS_CODE, buf, len);
132 
133   display_gss_error(minor, GSS_C_MECH_CODE, buf, len);
134 
135   infof(data, "%s%s", prefix, buf);
136 #ifdef CURL_DISABLE_VERBOSE_STRINGS
137   (void)data;
138   (void)prefix;
139 #endif
140 }
141 
142 #endif /* HAVE_GSSAPI */
143