source: vendor/current/auth/kerberos/gssapi_pac.c

Last change on this file was 989, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.7

File size: 10.3 KB
Line 
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 */
35static 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
45const gss_OID_desc * const gss_mech_krb5 = krb5_gss_oid_array+0;
46const gss_OID_desc * const gss_mech_krb5_old = krb5_gss_oid_array+1;
47const 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
55gss_OID_desc gse_sesskey_inq_oid = {
56 GSS_KRB5_INQ_SSPI_SESSION_KEY_OID_LENGTH,
57 discard_const(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
65gss_OID_desc gse_sesskeytype_oid = {
66 GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH,
67 discard_const(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
75NTSTATUS 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/*
84 * gss_get_name_attribute() in MIT krb5 1.10.0 can return unintialized pac_display_buffer
85 * and later gss_release_buffer() will crash on attempting to release it.
86 *
87 * So always initialize the buffer descriptors.
88 *
89 * See following links for more details:
90 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=658514
91 * http://krbdev.mit.edu/rt/Ticket/Display.html?user=guest&pass=guest&id=7087
92 */
93 gss_buffer_desc pac_buffer = {
94 .value = NULL,
95 .length = 0
96 };
97 gss_buffer_desc pac_display_buffer = {
98 .value = NULL,
99 .length = 0
100 };
101 gss_buffer_desc pac_name = {
102 .value = discard_const("urn:mspac:"),
103 .length = sizeof("urn:mspac:")-1
104 };
105 int more = -1;
106 int authenticated = false;
107 int complete = false;
108
109 gss_maj = gss_get_name_attribute(
110 &gss_min, gss_client_name, &pac_name,
111 &authenticated, &complete,
112 &pac_buffer, &pac_display_buffer, &more);
113
114 if (gss_maj != 0) {
115 DEBUG(0, ("obtaining PAC via GSSAPI gss_get_name_attribute failed: %s\n",
116 gssapi_error_string(mem_ctx,
117 gss_maj,
118 gss_min,
119 discard_const_p(struct gss_OID_desc_struct,
120 gss_mech_krb5))));
121 return NT_STATUS_ACCESS_DENIED;
122 } else if (authenticated && complete) {
123 /* The PAC blob is returned directly */
124 *pac_blob = data_blob_talloc(mem_ctx, pac_buffer.value,
125 pac_buffer.length);
126
127 if (!pac_blob->data) {
128 status = NT_STATUS_NO_MEMORY;
129 } else {
130 status = NT_STATUS_OK;
131 }
132
133 gss_maj = gss_release_buffer(&gss_min, &pac_buffer);
134 gss_maj = gss_release_buffer(&gss_min, &pac_display_buffer);
135 return status;
136 } else {
137 DEBUG(0, ("obtaining PAC via GSSAPI failed: authenticated: %s, complete: %s, more: %s\n",
138 authenticated ? "true" : "false",
139 complete ? "true" : "false",
140 more ? "true" : "false"));
141 return NT_STATUS_ACCESS_DENIED;
142 }
143
144#elif defined(HAVE_GSS_INQUIRE_SEC_CONTEXT_BY_OID)
145 gss_OID_desc pac_data_oid = {
146 .elements = discard_const(EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID),
147 .length = EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID_LENGTH
148 };
149
150 gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
151
152 /* If we didn't have the routine to get a verified, validated
153 * PAC (supplied only by MIT at the time of writing), then try
154 * with the Heimdal OID (fetches the PAC directly and always
155 * validates) */
156 gss_maj = gss_inquire_sec_context_by_oid(
157 &gss_min, gssapi_context,
158 &pac_data_oid, &set);
159
160 /* First check for the error MIT gives for an unknown OID */
161 if (gss_maj == GSS_S_UNAVAILABLE) {
162 DEBUG(1, ("unable to obtain a PAC against this GSSAPI library. "
163 "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
164 } else if (gss_maj != 0) {
165 DEBUG(2, ("obtaining PAC via GSSAPI gss_inqiure_sec_context_by_oid (Heimdal OID) failed: %s\n",
166 gssapi_error_string(mem_ctx, gss_maj, gss_min, gss_mech_krb5)));
167 } else {
168 if (set == GSS_C_NO_BUFFER_SET) {
169 DEBUG(0, ("gss_inquire_sec_context_by_oid returned unknown "
170 "data in results.\n"));
171 return NT_STATUS_INTERNAL_ERROR;
172 }
173
174 /* The PAC blob is returned directly */
175 *pac_blob = data_blob_talloc(mem_ctx, set->elements[0].value,
176 set->elements[0].length);
177 if (!pac_blob->data) {
178 status = NT_STATUS_NO_MEMORY;
179 } else {
180 status = NT_STATUS_OK;
181 }
182
183 gss_maj = gss_release_buffer_set(&gss_min, &set);
184 return status;
185 }
186#else
187 DEBUG(1, ("unable to obtain a PAC against this GSSAPI library. "
188 "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
189#endif
190 return NT_STATUS_ACCESS_DENIED;
191}
192
193NTSTATUS gssapi_get_session_key(TALLOC_CTX *mem_ctx,
194 gss_ctx_id_t gssapi_context,
195 DATA_BLOB *session_key,
196 uint32_t *keytype)
197{
198 OM_uint32 gss_min, gss_maj;
199 gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
200
201 gss_maj = gss_inquire_sec_context_by_oid(
202 &gss_min, gssapi_context,
203 &gse_sesskey_inq_oid, &set);
204 if (gss_maj) {
205 DEBUG(0, ("gss_inquire_sec_context_by_oid failed [%s]\n",
206 gssapi_error_string(mem_ctx,
207 gss_maj,
208 gss_min,
209 discard_const_p(struct gss_OID_desc_struct,
210 gss_mech_krb5))));
211 return NT_STATUS_NO_USER_SESSION_KEY;
212 }
213
214 if ((set == GSS_C_NO_BUFFER_SET) ||
215 (set->count == 0)) {
216#ifdef HAVE_GSSKRB5_GET_SUBKEY
217 krb5_keyblock *subkey;
218 gss_maj = gsskrb5_get_subkey(&gss_min,
219 gssapi_context,
220 &subkey);
221 if (gss_maj != 0) {
222 DEBUG(1, ("NO session key for this mech\n"));
223 return NT_STATUS_NO_USER_SESSION_KEY;
224 }
225 if (session_key) {
226 *session_key = data_blob_talloc(mem_ctx,
227 KRB5_KEY_DATA(subkey), KRB5_KEY_LENGTH(subkey));
228 }
229 if (keytype) {
230 *keytype = KRB5_KEY_TYPE(subkey);
231 }
232 krb5_free_keyblock(NULL /* should be krb5_context */, subkey);
233 return NT_STATUS_OK;
234#else
235 DEBUG(0, ("gss_inquire_sec_context_by_oid didn't return any session key (and no alternative method available)\n"));
236 return NT_STATUS_NO_USER_SESSION_KEY;
237#endif
238 }
239
240 if (session_key) {
241 *session_key = data_blob_talloc(mem_ctx, set->elements[0].value,
242 set->elements[0].length);
243 }
244
245 if (keytype) {
246 int diflen, i;
247 const uint8_t *p;
248
249 *keytype = 0;
250 if (set->count < 2) {
251
252#ifdef HAVE_GSSKRB5_GET_SUBKEY
253 krb5_keyblock *subkey;
254 gss_maj = gsskrb5_get_subkey(&gss_min,
255 gssapi_context,
256 &subkey);
257 if (gss_maj == 0) {
258 *keytype = KRB5_KEY_TYPE(subkey);
259 krb5_free_keyblock(NULL /* should be krb5_context */, subkey);
260 }
261#endif
262 gss_maj = gss_release_buffer_set(&gss_min, &set);
263
264 return NT_STATUS_OK;
265
266 } else if (memcmp(set->elements[1].value,
267 gse_sesskeytype_oid.elements,
268 gse_sesskeytype_oid.length) != 0) {
269 /* Perhaps a non-krb5 session key */
270 gss_maj = gss_release_buffer_set(&gss_min, &set);
271 return NT_STATUS_OK;
272 }
273 p = (const uint8_t *)set->elements[1].value + gse_sesskeytype_oid.length;
274 diflen = set->elements[1].length - gse_sesskeytype_oid.length;
275 if (diflen <= 0) {
276 gss_maj = gss_release_buffer_set(&gss_min, &set);
277 return NT_STATUS_INVALID_PARAMETER;
278 }
279 for (i = 0; i < diflen; i++) {
280 *keytype = (*keytype << 7) | (p[i] & 0x7f);
281 if (i + 1 != diflen && (p[i] & 0x80) == 0) {
282 gss_maj = gss_release_buffer_set(&gss_min, &set);
283 return NT_STATUS_INVALID_PARAMETER;
284 }
285 }
286 }
287
288 gss_maj = gss_release_buffer_set(&gss_min, &set);
289 return NT_STATUS_OK;
290}
291
292
293char *gssapi_error_string(TALLOC_CTX *mem_ctx,
294 OM_uint32 maj_stat, OM_uint32 min_stat,
295 const gss_OID mech)
296{
297 OM_uint32 disp_min_stat, disp_maj_stat;
298 gss_buffer_desc maj_error_message;
299 gss_buffer_desc min_error_message;
300 char *maj_error_string, *min_error_string;
301 OM_uint32 msg_ctx = 0;
302
303 char *ret;
304
305 maj_error_message.value = NULL;
306 min_error_message.value = NULL;
307 maj_error_message.length = 0;
308 min_error_message.length = 0;
309
310 disp_maj_stat = gss_display_status(&disp_min_stat, maj_stat,
311 GSS_C_GSS_CODE, mech,
312 &msg_ctx, &maj_error_message);
313 if (disp_maj_stat != 0) {
314 maj_error_message.value = NULL;
315 maj_error_message.length = 0;
316 }
317 disp_maj_stat = gss_display_status(&disp_min_stat, min_stat,
318 GSS_C_MECH_CODE, mech,
319 &msg_ctx, &min_error_message);
320 if (disp_maj_stat != 0) {
321 min_error_message.value = NULL;
322 min_error_message.length = 0;
323 }
324
325 maj_error_string = talloc_strndup(mem_ctx,
326 (char *)maj_error_message.value,
327 maj_error_message.length);
328
329 min_error_string = talloc_strndup(mem_ctx,
330 (char *)min_error_message.value,
331 min_error_message.length);
332
333 ret = talloc_asprintf(mem_ctx, "%s: %s",
334 maj_error_string, min_error_string);
335
336 talloc_free(maj_error_string);
337 talloc_free(min_error_string);
338
339 gss_release_buffer(&disp_min_stat, &maj_error_message);
340 gss_release_buffer(&disp_min_stat, &min_error_message);
341
342 return ret;
343}
344
345#endif /* HAVE_KRB5 */
Note: See TracBrowser for help on using the repository browser.