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