lib/replace: split out GSSAPI from lib/replace/system/kerberos.h into lib/replace...
[kai/samba-autobuild/.git] / auth / kerberos / gssapi_pac.c
1 /*
2    Unix SMB/CIFS implementation.
3    kerberos authorization data (PAC) utility library
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2011
5    Copyright (C) Simo Sorce 2010.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #ifdef HAVE_KRB5
23
24 #include "auth/kerberos/pac_utils.h"
25
26 #if 0
27 /* FIXME - need proper configure/waf test
28  * to determine if gss_mech_krb5 and friends
29  * exist. JRA.
30  */
31 /*
32  * These are not exported by Solaris -lkrb5
33  * Maybe move to libreplace somewhere?
34  */
35 static const gss_OID_desc krb5_gss_oid_array[] = {
36         /* this is the official, rfc-specified OID */
37         { 9, "\052\206\110\206\367\022\001\002\002" },
38         /* this is the pre-RFC mech OID */
39         { 5, "\053\005\001\005\002" },
40         /* this is the unofficial, incorrect mech OID emitted by MS */
41         { 9, "\052\206\110\202\367\022\001\002\002" },
42         { 0, 0 }
43 };
44
45 const gss_OID_desc * const gss_mech_krb5              = krb5_gss_oid_array+0;
46 const gss_OID_desc * const gss_mech_krb5_old          = krb5_gss_oid_array+1;
47 const gss_OID_desc * const gss_mech_krb5_wrong        = krb5_gss_oid_array+2;
48 #endif
49
50 #ifndef GSS_KRB5_INQ_SSPI_SESSION_KEY_OID
51 #define GSS_KRB5_INQ_SSPI_SESSION_KEY_OID_LENGTH 11
52 #define GSS_KRB5_INQ_SSPI_SESSION_KEY_OID "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x05\x05"
53 #endif
54
55 gss_OID_desc gse_sesskey_inq_oid = {
56         GSS_KRB5_INQ_SSPI_SESSION_KEY_OID_LENGTH,
57         (void *)GSS_KRB5_INQ_SSPI_SESSION_KEY_OID
58 };
59
60 #ifndef GSS_KRB5_SESSION_KEY_ENCTYPE_OID
61 #define GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH 10
62 #define GSS_KRB5_SESSION_KEY_ENCTYPE_OID  "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x04"
63 #endif
64
65 gss_OID_desc gse_sesskeytype_oid = {
66         GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH,
67         (void *)GSS_KRB5_SESSION_KEY_ENCTYPE_OID
68 };
69
70 /* The Heimdal OID for getting the PAC */
71 #define EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID_LENGTH 8
72 /*                                                      EXTRACTION OID             AUTHZ ID */
73 #define EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID "\x2a\x85\x70\x2b\x0d\x03" "\x81\x00"
74
75 NTSTATUS gssapi_obtain_pac_blob(TALLOC_CTX *mem_ctx,
76                                 gss_ctx_id_t gssapi_context,
77                                 gss_name_t gss_client_name,
78                                 DATA_BLOB *pac_blob)
79 {
80         NTSTATUS status;
81         OM_uint32 gss_maj, gss_min;
82 #ifdef HAVE_GSS_GET_NAME_ATTRIBUTE
83         gss_buffer_desc pac_buffer;
84         gss_buffer_desc pac_display_buffer;
85         gss_buffer_desc pac_name = {
86                 .value = discard_const("urn:mspac:"),
87                 .length = sizeof("urn:mspac:")-1
88         };
89         int more = -1;
90         int authenticated = false;
91         int complete = false;
92
93         gss_maj = gss_get_name_attribute(
94                 &gss_min, gss_client_name, &pac_name,
95                 &authenticated, &complete,
96                 &pac_buffer, &pac_display_buffer, &more);
97
98         if (gss_maj != 0) {
99                 DEBUG(0, ("obtaining PAC via GSSAPI gss_get_name_attribute failed: %s\n",
100                           gssapi_error_string(mem_ctx, gss_maj, gss_min, gss_mech_krb5)));
101                 return NT_STATUS_ACCESS_DENIED;
102         } else if (authenticated && complete) {
103                 /* The PAC blob is returned directly */
104                 *pac_blob = data_blob_talloc(mem_ctx, pac_buffer.value,
105                                             pac_buffer.length);
106
107                 if (!pac_blob->data) {
108                         status = NT_STATUS_NO_MEMORY;
109                 } else {
110                         status = NT_STATUS_OK;
111                 }
112
113                 gss_maj = gss_release_buffer(&gss_min, &pac_buffer);
114                 gss_maj = gss_release_buffer(&gss_min, &pac_display_buffer);
115                 return status;
116         } else {
117                 DEBUG(0, ("obtaining PAC via GSSAPI failed: authenticated: %s, complete: %s, more: %s\n",
118                           authenticated ? "true" : "false",
119                           complete ? "true" : "false",
120                           more ? "true" : "false"));
121                 return NT_STATUS_ACCESS_DENIED;
122         }
123
124 #elif defined(HAVE_GSS_INQUIRE_SEC_CONTEXT_BY_OID)
125         gss_OID_desc pac_data_oid = {
126                 .elements = discard_const(EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID),
127                 .length = EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID_LENGTH
128         };
129
130         gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
131
132         /* If we didn't have the routine to get a verified, validated
133          * PAC (supplied only by MIT at the time of writing), then try
134          * with the Heimdal OID (fetches the PAC directly and always
135          * validates) */
136         gss_maj = gss_inquire_sec_context_by_oid(
137                                 &gss_min, gssapi_context,
138                                 &pac_data_oid, &set);
139
140         /* First check for the error MIT gives for an unknown OID */
141         if (gss_maj == GSS_S_UNAVAILABLE) {
142                 DEBUG(1, ("unable to obtain a PAC against this GSSAPI library.  "
143                           "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
144         } else if (gss_maj != 0) {
145                 DEBUG(2, ("obtaining PAC via GSSAPI gss_inqiure_sec_context_by_oid (Heimdal OID) failed: %s\n",
146                           gssapi_error_string(mem_ctx, gss_maj, gss_min, gss_mech_krb5)));
147         } else {
148                 if (set == GSS_C_NO_BUFFER_SET) {
149                         DEBUG(0, ("gss_inquire_sec_context_by_oid returned unknown "
150                                   "data in results.\n"));
151                         return NT_STATUS_INTERNAL_ERROR;
152                 }
153
154                 /* The PAC blob is returned directly */
155                 *pac_blob = data_blob_talloc(mem_ctx, set->elements[0].value,
156                                             set->elements[0].length);
157                 if (!pac_blob->data) {
158                         status = NT_STATUS_NO_MEMORY;
159                 } else {
160                         status = NT_STATUS_OK;
161                 }
162
163                 gss_maj = gss_release_buffer_set(&gss_min, &set);
164                 return status;
165         }
166 #else
167         DEBUG(1, ("unable to obtain a PAC against this GSSAPI library.  "
168                   "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
169 #endif
170         return NT_STATUS_ACCESS_DENIED;
171 }
172
173 NTSTATUS gssapi_get_session_key(TALLOC_CTX *mem_ctx,
174                                 gss_ctx_id_t gssapi_context,
175                                 DATA_BLOB *session_key, 
176                                 uint32_t *keytype)
177 {
178         OM_uint32 gss_min, gss_maj;
179         gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
180
181         gss_maj = gss_inquire_sec_context_by_oid(
182                                 &gss_min, gssapi_context,
183                                 &gse_sesskey_inq_oid, &set);
184         if (gss_maj) {
185                 DEBUG(0, ("gss_inquire_sec_context_by_oid failed [%s]\n",
186                           gssapi_error_string(mem_ctx, gss_maj, gss_min, gss_mech_krb5)));
187                 return NT_STATUS_NO_USER_SESSION_KEY;
188         }
189
190         if ((set == GSS_C_NO_BUFFER_SET) ||
191             (set->count == 0)) {
192 #ifdef HAVE_GSSKRB5_GET_SUBKEY
193                 krb5_keyblock *subkey;
194                 gss_maj = gsskrb5_get_subkey(&gss_min,
195                                              gssapi_context,
196                                              &subkey);
197                 if (gss_maj != 0) {
198                         DEBUG(1, ("NO session key for this mech\n"));
199                         return NT_STATUS_NO_USER_SESSION_KEY;
200                 }
201                 if (session_key) {
202                         *session_key = data_blob_talloc(mem_ctx,
203                                                         KRB5_KEY_DATA(subkey), KRB5_KEY_LENGTH(subkey));
204                 }
205                 if (keytype) {
206                         *keytype = KRB5_KEY_TYPE(subkey);
207                 }
208                 krb5_free_keyblock(NULL /* should be krb5_context */, subkey);
209                 return NT_STATUS_OK;
210 #else
211                 DEBUG(0, ("gss_inquire_sec_context_by_oid didn't return any session key (and no alternative method available)\n"));
212                 return NT_STATUS_NO_USER_SESSION_KEY;
213 #endif
214         }
215
216         if (session_key) {
217                 *session_key = data_blob_talloc(mem_ctx, set->elements[0].value,
218                                                 set->elements[0].length);
219         }
220
221         if (keytype) {
222                 int diflen, i;
223                 const char *p;
224
225                 if (set->count < 2) {
226
227 #ifdef HAVE_GSSKRB5_GET_SUBKEY
228                         krb5_keyblock *subkey;
229                         gss_maj = gsskrb5_get_subkey(&gss_min,
230                                                      gssapi_context,
231                                                      &subkey);
232                         if (gss_maj == 0) {
233                                 *keytype = KRB5_KEY_TYPE(subkey);
234                                 krb5_free_keyblock(NULL /* should be krb5_context */, subkey);
235                         } else
236 #else
237                         {
238                                 *keytype = 0;
239                         }
240 #endif
241                         gss_maj = gss_release_buffer_set(&gss_min, &set);
242         
243                         return NT_STATUS_OK;
244
245                 } else if (memcmp(set->elements[1].value,
246                                   gse_sesskeytype_oid.elements,
247                                   gse_sesskeytype_oid.length) != 0) {
248                         /* Perhaps a non-krb5 session key */
249                         *keytype = 0;
250                         gss_maj = gss_release_buffer_set(&gss_min, &set);
251                         return NT_STATUS_OK;
252                 }
253                 p = set->elements[1].value + gse_sesskeytype_oid.length;
254                 diflen = set->elements[1].length - gse_sesskeytype_oid.length;
255                 if (diflen <= 0) {
256                         gss_maj = gss_release_buffer_set(&gss_min, &set);
257                         return NT_STATUS_INVALID_PARAMETER;
258                 }
259                 *keytype = 0;
260                 for (i = 0; i < diflen; i++) {
261                         *keytype = (*keytype << 7) | (p[i] & 0x7f);
262                         if (i + 1 != diflen && (p[i] & 0x80) == 0) {
263                                 gss_maj = gss_release_buffer_set(&gss_min, &set);
264                                 return NT_STATUS_INVALID_PARAMETER;
265                         }
266                 }
267         }
268
269         gss_maj = gss_release_buffer_set(&gss_min, &set);
270         return NT_STATUS_OK;
271 }
272
273
274 char *gssapi_error_string(TALLOC_CTX *mem_ctx,
275                           OM_uint32 maj_stat, OM_uint32 min_stat,
276                           const gss_OID mech)
277 {
278         OM_uint32 disp_min_stat, disp_maj_stat;
279         gss_buffer_desc maj_error_message;
280         gss_buffer_desc min_error_message;
281         char *maj_error_string, *min_error_string;
282         OM_uint32 msg_ctx = 0;
283
284         char *ret;
285
286         maj_error_message.value = NULL;
287         min_error_message.value = NULL;
288         maj_error_message.length = 0;
289         min_error_message.length = 0;
290
291         disp_maj_stat = gss_display_status(&disp_min_stat, maj_stat,
292                                            GSS_C_GSS_CODE, mech,
293                                            &msg_ctx, &maj_error_message);
294         disp_maj_stat = gss_display_status(&disp_min_stat, min_stat,
295                                            GSS_C_MECH_CODE, mech,
296                                            &msg_ctx, &min_error_message);
297
298         maj_error_string = talloc_strndup(mem_ctx,
299                                           (char *)maj_error_message.value,
300                                           maj_error_message.length);
301
302         min_error_string = talloc_strndup(mem_ctx,
303                                           (char *)min_error_message.value,
304                                           min_error_message.length);
305
306         ret = talloc_asprintf(mem_ctx, "%s: %s",
307                                 maj_error_string, min_error_string);
308
309         talloc_free(maj_error_string);
310         talloc_free(min_error_string);
311
312         gss_release_buffer(&disp_min_stat, &maj_error_message);
313         gss_release_buffer(&disp_min_stat, &min_error_message);
314
315         return ret;
316 }
317
318 #endif /* HAVE_KRB5 */