WHATSNEW: prepare for 4.0 beta4
[Samba.git] / auth / kerberos / gssapi_pac.c
bloba17405211d06f7d56d9c8565334bf3ef2557ac8e
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.
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.
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.
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/>.
21 #include "includes.h"
22 #ifdef HAVE_KRB5
24 #include "auth/kerberos/pac_utils.h"
26 #if 0
27 /* FIXME - need proper configure/waf test
28 * to determine if gss_mech_krb5 and friends
29 * exist. JRA.
32 * These are not exported by Solaris -lkrb5
33 * Maybe move to libreplace somewhere?
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 }
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
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
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
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
65 gss_OID_desc gse_sesskeytype_oid = {
66 GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH,
67 (void *)GSS_KRB5_SESSION_KEY_ENCTYPE_OID
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"
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)
80 NTSTATUS status;
81 OM_uint32 gss_maj, gss_min;
82 #ifdef HAVE_GSS_GET_NAME_ATTRIBUTE
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.
87 * So always initialize the buffer descriptors.
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
93 gss_buffer_desc pac_buffer = {
94 .value = NULL,
95 .length = 0
97 gss_buffer_desc pac_display_buffer = {
98 .value = NULL,
99 .length = 0
101 gss_buffer_desc pac_name = {
102 .value = discard_const("urn:mspac:"),
103 .length = sizeof("urn:mspac:")-1
105 int more = -1;
106 int authenticated = false;
107 int complete = false;
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);
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, gss_maj, gss_min, gss_mech_krb5)));
117 return NT_STATUS_ACCESS_DENIED;
118 } else if (authenticated && complete) {
119 /* The PAC blob is returned directly */
120 *pac_blob = data_blob_talloc(mem_ctx, pac_buffer.value,
121 pac_buffer.length);
123 if (!pac_blob->data) {
124 status = NT_STATUS_NO_MEMORY;
125 } else {
126 status = NT_STATUS_OK;
129 gss_maj = gss_release_buffer(&gss_min, &pac_buffer);
130 gss_maj = gss_release_buffer(&gss_min, &pac_display_buffer);
131 return status;
132 } else {
133 DEBUG(0, ("obtaining PAC via GSSAPI failed: authenticated: %s, complete: %s, more: %s\n",
134 authenticated ? "true" : "false",
135 complete ? "true" : "false",
136 more ? "true" : "false"));
137 return NT_STATUS_ACCESS_DENIED;
140 #elif defined(HAVE_GSS_INQUIRE_SEC_CONTEXT_BY_OID)
141 gss_OID_desc pac_data_oid = {
142 .elements = discard_const(EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID),
143 .length = EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID_LENGTH
146 gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
148 /* If we didn't have the routine to get a verified, validated
149 * PAC (supplied only by MIT at the time of writing), then try
150 * with the Heimdal OID (fetches the PAC directly and always
151 * validates) */
152 gss_maj = gss_inquire_sec_context_by_oid(
153 &gss_min, gssapi_context,
154 &pac_data_oid, &set);
156 /* First check for the error MIT gives for an unknown OID */
157 if (gss_maj == GSS_S_UNAVAILABLE) {
158 DEBUG(1, ("unable to obtain a PAC against this GSSAPI library. "
159 "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
160 } else if (gss_maj != 0) {
161 DEBUG(2, ("obtaining PAC via GSSAPI gss_inqiure_sec_context_by_oid (Heimdal OID) failed: %s\n",
162 gssapi_error_string(mem_ctx, gss_maj, gss_min, gss_mech_krb5)));
163 } else {
164 if (set == GSS_C_NO_BUFFER_SET) {
165 DEBUG(0, ("gss_inquire_sec_context_by_oid returned unknown "
166 "data in results.\n"));
167 return NT_STATUS_INTERNAL_ERROR;
170 /* The PAC blob is returned directly */
171 *pac_blob = data_blob_talloc(mem_ctx, set->elements[0].value,
172 set->elements[0].length);
173 if (!pac_blob->data) {
174 status = NT_STATUS_NO_MEMORY;
175 } else {
176 status = NT_STATUS_OK;
179 gss_maj = gss_release_buffer_set(&gss_min, &set);
180 return status;
182 #else
183 DEBUG(1, ("unable to obtain a PAC against this GSSAPI library. "
184 "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
185 #endif
186 return NT_STATUS_ACCESS_DENIED;
189 NTSTATUS gssapi_get_session_key(TALLOC_CTX *mem_ctx,
190 gss_ctx_id_t gssapi_context,
191 DATA_BLOB *session_key,
192 uint32_t *keytype)
194 OM_uint32 gss_min, gss_maj;
195 gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
197 gss_maj = gss_inquire_sec_context_by_oid(
198 &gss_min, gssapi_context,
199 &gse_sesskey_inq_oid, &set);
200 if (gss_maj) {
201 DEBUG(0, ("gss_inquire_sec_context_by_oid failed [%s]\n",
202 gssapi_error_string(mem_ctx, gss_maj, gss_min, gss_mech_krb5)));
203 return NT_STATUS_NO_USER_SESSION_KEY;
206 if ((set == GSS_C_NO_BUFFER_SET) ||
207 (set->count == 0)) {
208 #ifdef HAVE_GSSKRB5_GET_SUBKEY
209 krb5_keyblock *subkey;
210 gss_maj = gsskrb5_get_subkey(&gss_min,
211 gssapi_context,
212 &subkey);
213 if (gss_maj != 0) {
214 DEBUG(1, ("NO session key for this mech\n"));
215 return NT_STATUS_NO_USER_SESSION_KEY;
217 if (session_key) {
218 *session_key = data_blob_talloc(mem_ctx,
219 KRB5_KEY_DATA(subkey), KRB5_KEY_LENGTH(subkey));
221 if (keytype) {
222 *keytype = KRB5_KEY_TYPE(subkey);
224 krb5_free_keyblock(NULL /* should be krb5_context */, subkey);
225 return NT_STATUS_OK;
226 #else
227 DEBUG(0, ("gss_inquire_sec_context_by_oid didn't return any session key (and no alternative method available)\n"));
228 return NT_STATUS_NO_USER_SESSION_KEY;
229 #endif
232 if (session_key) {
233 *session_key = data_blob_talloc(mem_ctx, set->elements[0].value,
234 set->elements[0].length);
237 if (keytype) {
238 int diflen, i;
239 const char *p;
241 if (set->count < 2) {
243 #ifdef HAVE_GSSKRB5_GET_SUBKEY
244 krb5_keyblock *subkey;
245 gss_maj = gsskrb5_get_subkey(&gss_min,
246 gssapi_context,
247 &subkey);
248 if (gss_maj == 0) {
249 *keytype = KRB5_KEY_TYPE(subkey);
250 krb5_free_keyblock(NULL /* should be krb5_context */, subkey);
251 } else
252 #else
254 *keytype = 0;
256 #endif
257 gss_maj = gss_release_buffer_set(&gss_min, &set);
259 return NT_STATUS_OK;
261 } else if (memcmp(set->elements[1].value,
262 gse_sesskeytype_oid.elements,
263 gse_sesskeytype_oid.length) != 0) {
264 /* Perhaps a non-krb5 session key */
265 *keytype = 0;
266 gss_maj = gss_release_buffer_set(&gss_min, &set);
267 return NT_STATUS_OK;
269 p = set->elements[1].value + gse_sesskeytype_oid.length;
270 diflen = set->elements[1].length - gse_sesskeytype_oid.length;
271 if (diflen <= 0) {
272 gss_maj = gss_release_buffer_set(&gss_min, &set);
273 return NT_STATUS_INVALID_PARAMETER;
275 *keytype = 0;
276 for (i = 0; i < diflen; i++) {
277 *keytype = (*keytype << 7) | (p[i] & 0x7f);
278 if (i + 1 != diflen && (p[i] & 0x80) == 0) {
279 gss_maj = gss_release_buffer_set(&gss_min, &set);
280 return NT_STATUS_INVALID_PARAMETER;
285 gss_maj = gss_release_buffer_set(&gss_min, &set);
286 return NT_STATUS_OK;
290 char *gssapi_error_string(TALLOC_CTX *mem_ctx,
291 OM_uint32 maj_stat, OM_uint32 min_stat,
292 const gss_OID mech)
294 OM_uint32 disp_min_stat, disp_maj_stat;
295 gss_buffer_desc maj_error_message;
296 gss_buffer_desc min_error_message;
297 char *maj_error_string, *min_error_string;
298 OM_uint32 msg_ctx = 0;
300 char *ret;
302 maj_error_message.value = NULL;
303 min_error_message.value = NULL;
304 maj_error_message.length = 0;
305 min_error_message.length = 0;
307 disp_maj_stat = gss_display_status(&disp_min_stat, maj_stat,
308 GSS_C_GSS_CODE, mech,
309 &msg_ctx, &maj_error_message);
310 disp_maj_stat = gss_display_status(&disp_min_stat, min_stat,
311 GSS_C_MECH_CODE, mech,
312 &msg_ctx, &min_error_message);
314 maj_error_string = talloc_strndup(mem_ctx,
315 (char *)maj_error_message.value,
316 maj_error_message.length);
318 min_error_string = talloc_strndup(mem_ctx,
319 (char *)min_error_message.value,
320 min_error_message.length);
322 ret = talloc_asprintf(mem_ctx, "%s: %s",
323 maj_error_string, min_error_string);
325 talloc_free(maj_error_string);
326 talloc_free(min_error_string);
328 gss_release_buffer(&disp_min_stat, &maj_error_message);
329 gss_release_buffer(&disp_min_stat, &min_error_message);
331 return ret;
334 #endif /* HAVE_KRB5 */