s3-gse: Don't release the mech OID from gss_accept_security_context
[metze/samba/wip.git] / source3 / librpc / crypto / gse.c
1 /*
2  *  GSSAPI Security Extensions
3  *  RPC Pipe client and server routines
4  *  Copyright (C) Simo Sorce 2010.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /* We support only GSSAPI/KRB5 here */
21
22 #include "includes.h"
23 #include "gse.h"
24
25 #if defined(HAVE_KRB5) && defined(HAVE_GSS_WRAP_IOV)
26
27 #include "smb_krb5.h"
28 #include "gse_krb5.h"
29
30 #include <gssapi/gssapi.h>
31 #include <gssapi/gssapi_krb5.h>
32 #ifdef HAVE_GSSAPI_GSSAPI_EXT_H
33 #include <gssapi/gssapi_ext.h>
34 #endif
35
36 #ifndef GSS_KRB5_INQ_SSPI_SESSION_KEY_OID
37 #define GSS_KRB5_INQ_SSPI_SESSION_KEY_OID_LENGTH 11
38 #define GSS_KRB5_INQ_SSPI_SESSION_KEY_OID "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x05\x05"
39 #endif
40
41 gss_OID_desc gse_sesskey_inq_oid = {
42         GSS_KRB5_INQ_SSPI_SESSION_KEY_OID_LENGTH,
43         (void *)GSS_KRB5_INQ_SSPI_SESSION_KEY_OID
44 };
45
46 #ifndef GSS_KRB5_SESSION_KEY_ENCTYPE_OID
47 #define GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH 10
48 #define GSS_KRB5_SESSION_KEY_ENCTYPE_OID  "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x04"
49 #endif
50
51 gss_OID_desc gse_sesskeytype_oid = {
52         GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH,
53         (void *)GSS_KRB5_SESSION_KEY_ENCTYPE_OID
54 };
55
56 #define GSE_EXTRACT_RELEVANT_AUTHZ_DATA_OID_LENGTH 12
57 /*                                          EXTRACTION OID                                 AUTHZ ID */
58 #define GSE_EXTRACT_RELEVANT_AUTHZ_DATA_OID "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x05\x0a" "\x01"
59
60 gss_OID_desc gse_authz_data_oid = {
61         GSE_EXTRACT_RELEVANT_AUTHZ_DATA_OID_LENGTH,
62         (void *)GSE_EXTRACT_RELEVANT_AUTHZ_DATA_OID
63 };
64
65 static char *gse_errstr(TALLOC_CTX *mem_ctx, OM_uint32 maj, OM_uint32 min);
66
67 struct gse_context {
68         krb5_context k5ctx;
69         krb5_ccache ccache;
70         krb5_keytab keytab;
71
72         gss_ctx_id_t gss_ctx;
73
74         gss_OID_desc gss_mech;
75         OM_uint32 gss_c_flags;
76         gss_cred_id_t creds;
77         gss_name_t server_name;
78
79         gss_OID ret_mech;
80         OM_uint32 ret_flags;
81         gss_cred_id_t delegated_creds;
82         gss_name_t client_name;
83
84         bool more_processing;
85         bool authenticated;
86 };
87
88 #ifndef HAVE_GSS_OID_EQUAL
89
90 static bool gss_oid_equal(const gss_OID o1, const gss_OID o2)
91 {
92         if (o1 == o2) {
93                 return true;
94         }
95         if ((o1 == NULL && o2 != NULL) || (o1 != NULL && o2 == NULL)) {
96                 return false;
97         }
98         if (o1->length != o2->length) {
99                 return false;
100         }
101         return memcmp(o1->elements, o2->elements, o1->length) == false;
102 }
103
104 #endif
105
106 /* free non talloc dependent contexts */
107 static int gse_context_destructor(void *ptr)
108 {
109         struct gse_context *gse_ctx;
110         OM_uint32 gss_min, gss_maj;
111
112         gse_ctx = talloc_get_type_abort(ptr, struct gse_context);
113         if (gse_ctx->k5ctx) {
114                 if (gse_ctx->ccache) {
115                         krb5_cc_close(gse_ctx->k5ctx, gse_ctx->ccache);
116                         gse_ctx->ccache = NULL;
117                 }
118                 if (gse_ctx->keytab) {
119                         krb5_kt_close(gse_ctx->k5ctx, gse_ctx->keytab);
120                         gse_ctx->keytab = NULL;
121                 }
122                 krb5_free_context(gse_ctx->k5ctx);
123                 gse_ctx->k5ctx = NULL;
124         }
125         if (gse_ctx->gss_ctx != GSS_C_NO_CONTEXT) {
126                 gss_maj = gss_delete_sec_context(&gss_min,
127                                                  &gse_ctx->gss_ctx,
128                                                  GSS_C_NO_BUFFER);
129         }
130         if (gse_ctx->server_name) {
131                 gss_maj = gss_release_name(&gss_min,
132                                            &gse_ctx->server_name);
133         }
134         if (gse_ctx->client_name) {
135                 gss_maj = gss_release_name(&gss_min,
136                                            &gse_ctx->client_name);
137         }
138         if (gse_ctx->creds) {
139                 gss_maj = gss_release_cred(&gss_min,
140                                            &gse_ctx->creds);
141         }
142         if (gse_ctx->delegated_creds) {
143                 gss_maj = gss_release_cred(&gss_min,
144                                            &gse_ctx->delegated_creds);
145         }
146
147         /* MIT and Heimdal differ as to if you can call
148          * gss_release_oid() on this OID, generated by
149          * gss_{accept,init}_sec_context().  However, as long as the
150          * oid is gss_mech_krb5 (which it always is at the moment),
151          * then this is a moot point, as both declare this particular
152          * OID static, and so no memory is lost.  This assert is in
153          * place to ensure that the programmer who wishes to extend
154          * this code to EAP or other GSS mechanisms determines an
155          * implementation-dependent way of releasing any dynamically
156          * allocated OID */
157         SMB_ASSERT(gss_oid_equal(&gse_ctx->gss_mech, GSS_C_NO_OID) || gss_oid_equal(&gse_ctx->gss_mech, gss_mech_krb5));
158
159         return 0;
160 }
161
162 static NTSTATUS gse_context_init(TALLOC_CTX *mem_ctx,
163                                  bool do_sign, bool do_seal,
164                                  const char *ccache_name,
165                                  uint32_t add_gss_c_flags,
166                                  struct gse_context **_gse_ctx)
167 {
168         struct gse_context *gse_ctx;
169         krb5_error_code k5ret;
170         NTSTATUS status;
171
172         gse_ctx = talloc_zero(mem_ctx, struct gse_context);
173         if (!gse_ctx) {
174                 return NT_STATUS_NO_MEMORY;
175         }
176         talloc_set_destructor((TALLOC_CTX *)gse_ctx, gse_context_destructor);
177
178         memcpy(&gse_ctx->gss_mech, gss_mech_krb5, sizeof(gss_OID_desc));
179
180         gse_ctx->gss_c_flags = GSS_C_MUTUAL_FLAG |
181                                 GSS_C_DELEG_FLAG |
182                                 GSS_C_DELEG_POLICY_FLAG |
183                                 GSS_C_REPLAY_FLAG |
184                                 GSS_C_SEQUENCE_FLAG;
185         if (do_sign) {
186                 gse_ctx->gss_c_flags |= GSS_C_INTEG_FLAG;
187         }
188         if (do_seal) {
189                 gse_ctx->gss_c_flags |= GSS_C_CONF_FLAG;
190         }
191
192         gse_ctx->gss_c_flags |= add_gss_c_flags;
193
194         /* Initialize Kerberos Context */
195         initialize_krb5_error_table();
196
197         k5ret = krb5_init_context(&gse_ctx->k5ctx);
198         if (k5ret) {
199                 DEBUG(0, ("Failed to initialize kerberos context! (%s)\n",
200                           error_message(k5ret)));
201                 status = NT_STATUS_INTERNAL_ERROR;
202                 goto err_out;
203         }
204
205         if (!ccache_name) {
206                 ccache_name = krb5_cc_default_name(gse_ctx->k5ctx);
207         }
208         k5ret = krb5_cc_resolve(gse_ctx->k5ctx, ccache_name,
209                                 &gse_ctx->ccache);
210         if (k5ret) {
211                 DEBUG(1, ("Failed to resolve credential cache! (%s)\n",
212                           error_message(k5ret)));
213                 status = NT_STATUS_INTERNAL_ERROR;
214                 goto err_out;
215         }
216
217         /* TODO: Should we enforce a enc_types list ?
218         ret = krb5_set_default_tgs_ktypes(gse_ctx->k5ctx, enc_types);
219         */
220
221         *_gse_ctx = gse_ctx;
222         return NT_STATUS_OK;
223
224 err_out:
225         TALLOC_FREE(gse_ctx);
226         return status;
227 }
228
229 NTSTATUS gse_init_client(TALLOC_CTX *mem_ctx,
230                           bool do_sign, bool do_seal,
231                           const char *ccache_name,
232                           const char *server,
233                           const char *service,
234                           const char *username,
235                           const char *password,
236                           uint32_t add_gss_c_flags,
237                           struct gse_context **_gse_ctx)
238 {
239         struct gse_context *gse_ctx;
240         OM_uint32 gss_maj, gss_min;
241         gss_buffer_desc name_buffer = {0, NULL};
242         gss_OID_set_desc mech_set;
243         NTSTATUS status;
244
245         if (!server || !service) {
246                 return NT_STATUS_INVALID_PARAMETER;
247         }
248
249         status = gse_context_init(mem_ctx, do_sign, do_seal,
250                                   ccache_name, add_gss_c_flags,
251                                   &gse_ctx);
252         if (!NT_STATUS_IS_OK(status)) {
253                 return NT_STATUS_NO_MEMORY;
254         }
255
256         name_buffer.value = talloc_asprintf(gse_ctx,
257                                             "%s@%s", service, server);
258         if (!name_buffer.value) {
259                 status = NT_STATUS_NO_MEMORY;
260                 goto err_out;
261         }
262         name_buffer.length = strlen((char *)name_buffer.value);
263         gss_maj = gss_import_name(&gss_min, &name_buffer,
264                                   GSS_C_NT_HOSTBASED_SERVICE,
265                                   &gse_ctx->server_name);
266         if (gss_maj) {
267                 DEBUG(0, ("gss_import_name failed for %s, with [%s]\n",
268                           (char *)name_buffer.value,
269                           gse_errstr(gse_ctx, gss_maj, gss_min)));
270                 status = NT_STATUS_INTERNAL_ERROR;
271                 goto err_out;
272         }
273
274         /* TODO: get krb5 ticket using username/password, if no valid
275          * one already available in ccache */
276
277         mech_set.count = 1;
278         mech_set.elements = &gse_ctx->gss_mech;
279
280         gss_maj = gss_acquire_cred(&gss_min,
281                                    GSS_C_NO_NAME,
282                                    GSS_C_INDEFINITE,
283                                    &mech_set,
284                                    GSS_C_INITIATE,
285                                    &gse_ctx->creds,
286                                    NULL, NULL);
287         if (gss_maj) {
288                 DEBUG(0, ("gss_acquire_creds failed for %s, with [%s]\n",
289                           (char *)name_buffer.value,
290                           gse_errstr(gse_ctx, gss_maj, gss_min)));
291                 status = NT_STATUS_INTERNAL_ERROR;
292                 goto err_out;
293         }
294
295         *_gse_ctx = gse_ctx;
296         TALLOC_FREE(name_buffer.value);
297         return NT_STATUS_OK;
298
299 err_out:
300         TALLOC_FREE(name_buffer.value);
301         TALLOC_FREE(gse_ctx);
302         return status;
303 }
304
305 NTSTATUS gse_get_client_auth_token(TALLOC_CTX *mem_ctx,
306                                    struct gse_context *gse_ctx,
307                                    DATA_BLOB *token_in,
308                                    DATA_BLOB *token_out)
309 {
310         OM_uint32 gss_maj, gss_min;
311         gss_buffer_desc in_data;
312         gss_buffer_desc out_data;
313         DATA_BLOB blob = data_blob_null;
314         NTSTATUS status;
315
316         in_data.value = token_in->data;
317         in_data.length = token_in->length;
318
319         gss_maj = gss_init_sec_context(&gss_min,
320                                         gse_ctx->creds,
321                                         &gse_ctx->gss_ctx,
322                                         gse_ctx->server_name,
323                                         &gse_ctx->gss_mech,
324                                         gse_ctx->gss_c_flags,
325                                         0, GSS_C_NO_CHANNEL_BINDINGS,
326                                         &in_data, NULL, &out_data,
327                                         NULL, NULL);
328         switch (gss_maj) {
329         case GSS_S_COMPLETE:
330                 /* we are done with it */
331                 gse_ctx->more_processing = false;
332                 status = NT_STATUS_OK;
333                 break;
334         case GSS_S_CONTINUE_NEEDED:
335                 /* we will need a third leg */
336                 gse_ctx->more_processing = true;
337                 /* status = NT_STATUS_MORE_PROCESSING_REQUIRED; */
338                 status = NT_STATUS_OK;
339                 break;
340         default:
341                 DEBUG(0, ("gss_init_sec_context failed with [%s]\n",
342                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
343                 status = NT_STATUS_INTERNAL_ERROR;
344                 goto done;
345         }
346
347         blob = data_blob_talloc(mem_ctx, out_data.value, out_data.length);
348         if (!blob.data) {
349                 status = NT_STATUS_NO_MEMORY;
350         }
351
352         gss_maj = gss_release_buffer(&gss_min, &out_data);
353
354 done:
355         *token_out = blob;
356         return status;
357 }
358
359 NTSTATUS gse_init_server(TALLOC_CTX *mem_ctx,
360                          bool do_sign, bool do_seal,
361                          uint32_t add_gss_c_flags,
362                          struct gse_context **_gse_ctx)
363 {
364         struct gse_context *gse_ctx;
365         OM_uint32 gss_maj, gss_min;
366         krb5_error_code ret;
367         NTSTATUS status;
368         const char *ktname;
369         gss_OID_set_desc mech_set;
370
371         status = gse_context_init(mem_ctx, do_sign, do_seal,
372                                   NULL, add_gss_c_flags, &gse_ctx);
373         if (!NT_STATUS_IS_OK(status)) {
374                 return NT_STATUS_NO_MEMORY;
375         }
376
377         ret = gse_krb5_get_server_keytab(gse_ctx->k5ctx,
378                                          &gse_ctx->keytab);
379         if (ret) {
380                 status = NT_STATUS_INTERNAL_ERROR;
381                 goto done;
382         }
383
384 #ifdef HAVE_GSS_KRB5_IMPORT_CRED
385         /* This creates a GSSAPI cred_id_t with the principal and keytab set */
386         gss_maj = gss_krb5_import_cred(&gss_min, NULL, NULL, gse_ctx->keytab, 
387                                         &gse_ctx->creds);
388         if (gss_maj) {
389                 DEBUG(0, ("gss_krb5_import_cred failed with [%s]\n",
390                           gse_errstr(gse_ctx, gss_maj, gss_min)));
391                 status = NT_STATUS_INTERNAL_ERROR;
392                 goto done;
393         }
394 #else
395         /* FIXME!!!
396          * This call sets the default keytab for the whole server, not
397          * just for this context. Need to find a way that does not alter
398          * the state of the whole server ... */
399
400         ret = smb_krb5_keytab_name(gse_ctx, gse_ctx->k5ctx,
401                                    gse_ctx->keytab, &ktname);
402         if (ret) {
403                 status = NT_STATUS_INTERNAL_ERROR;
404                 goto done;
405         }
406
407         ret = gsskrb5_register_acceptor_identity(ktname);
408         if (ret) {
409                 status = NT_STATUS_INTERNAL_ERROR;
410                 goto done;
411         }
412
413         mech_set.count = 1;
414         mech_set.elements = &gse_ctx->gss_mech;
415         
416         gss_maj = gss_acquire_cred(&gss_min,
417                                    GSS_C_NO_NAME,
418                                    GSS_C_INDEFINITE,
419                                    &mech_set,
420                                    GSS_C_ACCEPT,
421                                    &gse_ctx->creds,
422                                    NULL, NULL);
423
424         if (gss_maj) {
425                 DEBUG(0, ("gss_acquire_creds failed with [%s]\n",
426                           gse_errstr(gse_ctx, gss_maj, gss_min)));
427                 status = NT_STATUS_INTERNAL_ERROR;
428                 goto done;
429         }
430 #endif
431         status = NT_STATUS_OK;
432
433 done:
434         if (!NT_STATUS_IS_OK(status)) {
435                 TALLOC_FREE(gse_ctx);
436         }
437
438         *_gse_ctx = gse_ctx;
439         return status;
440 }
441
442 NTSTATUS gse_get_server_auth_token(TALLOC_CTX *mem_ctx,
443                                    struct gse_context *gse_ctx,
444                                    DATA_BLOB *token_in,
445                                    DATA_BLOB *token_out)
446 {
447         OM_uint32 gss_maj, gss_min;
448         gss_buffer_desc in_data;
449         gss_buffer_desc out_data;
450         DATA_BLOB blob = data_blob_null;
451         NTSTATUS status;
452
453         in_data.value = token_in->data;
454         in_data.length = token_in->length;
455
456         gss_maj = gss_accept_sec_context(&gss_min,
457                                          &gse_ctx->gss_ctx,
458                                          gse_ctx->creds,
459                                          &in_data,
460                                          GSS_C_NO_CHANNEL_BINDINGS,
461                                          &gse_ctx->client_name,
462                                          &gse_ctx->ret_mech,
463                                          &out_data,
464                                          &gse_ctx->ret_flags, NULL,
465                                          &gse_ctx->delegated_creds);
466         switch (gss_maj) {
467         case GSS_S_COMPLETE:
468                 /* we are done with it */
469                 gse_ctx->more_processing = false;
470                 gse_ctx->authenticated = true;
471                 status = NT_STATUS_OK;
472                 break;
473         case GSS_S_CONTINUE_NEEDED:
474                 /* we will need a third leg */
475                 gse_ctx->more_processing = true;
476                 /* status = NT_STATUS_MORE_PROCESSING_REQUIRED; */
477                 status = NT_STATUS_OK;
478                 break;
479         default:
480                 DEBUG(0, ("gss_init_sec_context failed with [%s]\n",
481                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
482
483                 if (gse_ctx->gss_ctx) {
484                         gss_delete_sec_context(&gss_min,
485                                                 &gse_ctx->gss_ctx,
486                                                 GSS_C_NO_BUFFER);
487                 }
488
489                 status = NT_STATUS_INTERNAL_ERROR;
490                 goto done;
491         }
492
493         /* we may be told to return nothing */
494         if (out_data.length) {
495                 blob = data_blob_talloc(mem_ctx, out_data.value, out_data.length);
496                 if (!blob.data) {
497                         status = NT_STATUS_NO_MEMORY;
498                 }
499                 gss_maj = gss_release_buffer(&gss_min, &out_data);
500         }
501
502
503 done:
504         *token_out = blob;
505         return status;
506 }
507
508 NTSTATUS gse_verify_server_auth_flags(struct gse_context *gse_ctx)
509 {
510         if (!gse_ctx->authenticated) {
511                 return NT_STATUS_INVALID_HANDLE;
512         }
513
514         if (memcmp(gse_ctx->ret_mech,
515                    gss_mech_krb5, sizeof(gss_OID_desc)) != 0) {
516                 return NT_STATUS_ACCESS_DENIED;
517         }
518
519         /* GSS_C_MUTUAL_FLAG */
520         if (gse_ctx->gss_c_flags & GSS_C_MUTUAL_FLAG) {
521                 if (!(gse_ctx->ret_flags & GSS_C_MUTUAL_FLAG)) {
522                         return NT_STATUS_ACCESS_DENIED;
523                 }
524         }
525
526         /* GSS_C_DELEG_FLAG */
527         /* GSS_C_DELEG_POLICY_FLAG */
528         /* GSS_C_REPLAY_FLAG */
529         /* GSS_C_SEQUENCE_FLAG */
530
531         /* GSS_C_INTEG_FLAG */
532         if (gse_ctx->gss_c_flags & GSS_C_INTEG_FLAG) {
533                 if (!(gse_ctx->ret_flags & GSS_C_INTEG_FLAG)) {
534                         return NT_STATUS_ACCESS_DENIED;
535                 }
536         }
537
538         /* GSS_C_CONF_FLAG */
539         if (gse_ctx->gss_c_flags & GSS_C_CONF_FLAG) {
540                 if (!(gse_ctx->ret_flags & GSS_C_CONF_FLAG)) {
541                         return NT_STATUS_ACCESS_DENIED;
542                 }
543         }
544
545         return NT_STATUS_OK;
546 }
547
548 static char *gse_errstr(TALLOC_CTX *mem_ctx, OM_uint32 maj, OM_uint32 min)
549 {
550         OM_uint32 gss_min, gss_maj;
551         gss_buffer_desc msg_min;
552         gss_buffer_desc msg_maj;
553         OM_uint32 msg_ctx = 0;
554
555         char *errstr = NULL;
556
557         ZERO_STRUCT(msg_min);
558         ZERO_STRUCT(msg_maj);
559
560         gss_maj = gss_display_status(&gss_min, maj, GSS_C_GSS_CODE,
561                                      GSS_C_NO_OID, &msg_ctx, &msg_maj);
562         if (gss_maj) {
563                 goto done;
564         }
565         gss_maj = gss_display_status(&gss_min, min, GSS_C_MECH_CODE,
566                                      (gss_OID)discard_const(gss_mech_krb5),
567                                      &msg_ctx, &msg_min);
568         if (gss_maj) {
569                 goto done;
570         }
571
572         errstr = talloc_strndup(mem_ctx,
573                                 (char *)msg_maj.value,
574                                         msg_maj.length);
575         if (!errstr) {
576                 goto done;
577         }
578         errstr = talloc_strdup_append_buffer(errstr, ": ");
579         if (!errstr) {
580                 goto done;
581         }
582         errstr = talloc_strndup_append_buffer(errstr,
583                                                 (char *)msg_min.value,
584                                                         msg_min.length);
585         if (!errstr) {
586                 goto done;
587         }
588
589 done:
590         if (msg_min.value) {
591                 gss_maj = gss_release_buffer(&gss_min, &msg_min);
592         }
593         if (msg_maj.value) {
594                 gss_maj = gss_release_buffer(&gss_min, &msg_maj);
595         }
596         return errstr;
597 }
598
599 bool gse_require_more_processing(struct gse_context *gse_ctx)
600 {
601         return gse_ctx->more_processing;
602 }
603
604 DATA_BLOB gse_get_session_key(TALLOC_CTX *mem_ctx,
605                                 struct gse_context *gse_ctx)
606 {
607         OM_uint32 gss_min, gss_maj;
608         gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
609         DATA_BLOB ret;
610
611         gss_maj = gss_inquire_sec_context_by_oid(
612                                 &gss_min, gse_ctx->gss_ctx,
613                                 &gse_sesskey_inq_oid, &set);
614         if (gss_maj) {
615                 DEBUG(0, ("gss_inquire_sec_context_by_oid failed [%s]\n",
616                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
617                 return data_blob_null;
618         }
619
620         if ((set == GSS_C_NO_BUFFER_SET) ||
621             (set->count != 2) ||
622             (memcmp(set->elements[1].value,
623                     gse_sesskeytype_oid.elements,
624                     gse_sesskeytype_oid.length) != 0)) {
625                 DEBUG(0, ("gss_inquire_sec_context_by_oid returned unknown "
626                           "OID for data in results:\n"));
627                 dump_data(1, (uint8_t *)set->elements[1].value,
628                              set->elements[1].length);
629                 return data_blob_null;
630         }
631
632         ret = data_blob_talloc(mem_ctx, set->elements[0].value,
633                                         set->elements[0].length);
634
635         gss_maj = gss_release_buffer_set(&gss_min, &set);
636         return ret;
637 }
638
639 NTSTATUS gse_get_client_name(struct gse_context *gse_ctx,
640                              TALLOC_CTX *mem_ctx, char **cli_name)
641 {
642         OM_uint32 gss_min, gss_maj;
643         gss_buffer_desc name_buffer;
644
645         if (!gse_ctx->authenticated) {
646                 return NT_STATUS_ACCESS_DENIED;
647         }
648
649         if (!gse_ctx->client_name) {
650                 return NT_STATUS_NOT_FOUND;
651         }
652
653         /* TODO: check OID matches KRB5 Principal Name OID ? */
654
655         gss_maj = gss_display_name(&gss_min,
656                                    gse_ctx->client_name,
657                                    &name_buffer, NULL);
658         if (gss_maj) {
659                 DEBUG(0, ("gss_display_name failed [%s]\n",
660                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
661                 return NT_STATUS_INTERNAL_ERROR;
662         }
663
664         *cli_name = talloc_strndup(talloc_tos(),
665                                         (char *)name_buffer.value,
666                                         name_buffer.length);
667
668         gss_maj = gss_release_buffer(&gss_min, &name_buffer);
669
670         if (!*cli_name) {
671                 return NT_STATUS_NO_MEMORY;
672         }
673
674         return NT_STATUS_OK;
675 }
676
677 NTSTATUS gse_get_authz_data(struct gse_context *gse_ctx,
678                             TALLOC_CTX *mem_ctx, DATA_BLOB *pac)
679 {
680         OM_uint32 gss_min, gss_maj;
681         gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
682
683         if (!gse_ctx->authenticated) {
684                 return NT_STATUS_ACCESS_DENIED;
685         }
686
687         gss_maj = gss_inquire_sec_context_by_oid(
688                                 &gss_min, gse_ctx->gss_ctx,
689                                 &gse_authz_data_oid, &set);
690         if (gss_maj) {
691                 DEBUG(0, ("gss_inquire_sec_context_by_oid failed [%s]\n",
692                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
693                 return NT_STATUS_NOT_FOUND;
694         }
695
696         if (set == GSS_C_NO_BUFFER_SET) {
697                 DEBUG(0, ("gss_inquire_sec_context_by_oid returned unknown "
698                           "data in results.\n"));
699                 return NT_STATUS_INTERNAL_ERROR;
700         }
701
702         /* for now we just hope it is the first value */
703         *pac = data_blob_talloc(mem_ctx,
704                                 set->elements[0].value,
705                                 set->elements[0].length);
706
707         gss_maj = gss_release_buffer_set(&gss_min, &set);
708
709         return NT_STATUS_OK;
710 }
711
712 NTSTATUS gse_get_pac_blob(struct gse_context *gse_ctx,
713                           TALLOC_CTX *mem_ctx, DATA_BLOB *pac_blob)
714 {
715         if (!gse_ctx->authenticated) {
716                 return NT_STATUS_ACCESS_DENIED;
717         }
718
719         return gssapi_obtain_pac_blob(mem_ctx, gse_ctx->gss_ctx,
720                                         gse_ctx->client_name, pac_blob);
721 }
722
723 size_t gse_get_signature_length(struct gse_context *gse_ctx,
724                                 int seal, size_t payload_size)
725 {
726         OM_uint32 gss_min, gss_maj;
727         gss_iov_buffer_desc iov[2];
728         uint8_t fakebuf[payload_size];
729         int sealed;
730
731         iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER;
732         iov[0].buffer.value = NULL;
733         iov[0].buffer.length = 0;
734         iov[1].type = GSS_IOV_BUFFER_TYPE_DATA;
735         iov[1].buffer.value = fakebuf;
736         iov[1].buffer.length = payload_size;
737
738         gss_maj = gss_wrap_iov_length(&gss_min, gse_ctx->gss_ctx,
739                                         seal, GSS_C_QOP_DEFAULT,
740                                         &sealed, iov, 2);
741         if (gss_maj) {
742                 DEBUG(0, ("gss_wrap_iov_length failed with [%s]\n",
743                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
744                 return 0;
745         }
746
747         return iov[0].buffer.length;
748 }
749
750 NTSTATUS gse_seal(TALLOC_CTX *mem_ctx, struct gse_context *gse_ctx,
751                   DATA_BLOB *data, DATA_BLOB *signature)
752 {
753         OM_uint32 gss_min, gss_maj;
754         gss_iov_buffer_desc iov[2];
755         int req_seal = 1; /* setting to 1 means we request sign+seal */
756         int sealed;
757         NTSTATUS status;
758
759         /* allocate the memory ourselves so we do not need to talloc_memdup */
760         signature->length = gse_get_signature_length(gse_ctx, 1, data->length);
761         if (!signature->length) {
762                 return NT_STATUS_INTERNAL_ERROR;
763         }
764         signature->data = (uint8_t *)talloc_size(mem_ctx, signature->length);
765         if (!signature->data) {
766                 return NT_STATUS_NO_MEMORY;
767         }
768         iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER;
769         iov[0].buffer.value = signature->data;
770         iov[0].buffer.length = signature->length;
771
772         /* data is encrypted in place, which is ok */
773         iov[1].type = GSS_IOV_BUFFER_TYPE_DATA;
774         iov[1].buffer.value = data->data;
775         iov[1].buffer.length = data->length;
776
777         gss_maj = gss_wrap_iov(&gss_min, gse_ctx->gss_ctx,
778                                 req_seal, GSS_C_QOP_DEFAULT,
779                                 &sealed, iov, 2);
780         if (gss_maj) {
781                 DEBUG(0, ("gss_wrap_iov failed with [%s]\n",
782                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
783                 status = NT_STATUS_ACCESS_DENIED;
784                 goto done;
785         }
786
787         if (!sealed) {
788                 DEBUG(0, ("gss_wrap_iov says data was not sealed!\n"));
789                 status = NT_STATUS_ACCESS_DENIED;
790                 goto done;
791         }
792
793         status = NT_STATUS_OK;
794
795         DEBUG(10, ("Sealed %d bytes, and got %d bytes header/signature.\n",
796                    (int)iov[1].buffer.length, (int)iov[0].buffer.length));
797
798 done:
799         return status;
800 }
801
802 NTSTATUS gse_unseal(TALLOC_CTX *mem_ctx, struct gse_context *gse_ctx,
803                     DATA_BLOB *data, DATA_BLOB *signature)
804 {
805         OM_uint32 gss_min, gss_maj;
806         gss_iov_buffer_desc iov[2];
807         int sealed;
808         NTSTATUS status;
809
810         iov[0].type = GSS_IOV_BUFFER_TYPE_HEADER;
811         iov[0].buffer.value = signature->data;
812         iov[0].buffer.length = signature->length;
813
814         /* data is decrypted in place, which is ok */
815         iov[1].type = GSS_IOV_BUFFER_TYPE_DATA;
816         iov[1].buffer.value = data->data;
817         iov[1].buffer.length = data->length;
818
819         gss_maj = gss_unwrap_iov(&gss_min, gse_ctx->gss_ctx,
820                                  &sealed, NULL, iov, 2);
821         if (gss_maj) {
822                 DEBUG(0, ("gss_unwrap_iov failed with [%s]\n",
823                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
824                 status = NT_STATUS_ACCESS_DENIED;
825                 goto done;
826         }
827
828         if (!sealed) {
829                 DEBUG(0, ("gss_unwrap_iov says data is not sealed!\n"));
830                 status = NT_STATUS_ACCESS_DENIED;
831                 goto done;
832         }
833
834         status = NT_STATUS_OK;
835
836         DEBUG(10, ("Unsealed %d bytes, with %d bytes header/signature.\n",
837                    (int)iov[1].buffer.length, (int)iov[0].buffer.length));
838
839 done:
840         return status;
841 }
842
843 NTSTATUS gse_sign(TALLOC_CTX *mem_ctx, struct gse_context *gse_ctx,
844                   DATA_BLOB *data, DATA_BLOB *signature)
845 {
846         OM_uint32 gss_min, gss_maj;
847         gss_buffer_desc in_data = { 0, NULL };
848         gss_buffer_desc out_data = { 0, NULL};
849         NTSTATUS status;
850
851         in_data.value = data->data;
852         in_data.length = data->length;
853
854         gss_maj = gss_get_mic(&gss_min, gse_ctx->gss_ctx,
855                               GSS_C_QOP_DEFAULT,
856                               &in_data, &out_data);
857         if (gss_maj) {
858                 DEBUG(0, ("gss_get_mic failed with [%s]\n",
859                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
860                 status = NT_STATUS_ACCESS_DENIED;
861                 goto done;
862         }
863
864         *signature = data_blob_talloc(mem_ctx,
865                                         out_data.value, out_data.length);
866         if (!signature->data) {
867                 status = NT_STATUS_NO_MEMORY;
868                 goto done;
869         }
870
871         status = NT_STATUS_OK;
872
873 done:
874         if (out_data.value) {
875                 gss_maj = gss_release_buffer(&gss_min, &out_data);
876         }
877         return status;
878 }
879
880 NTSTATUS gse_sigcheck(TALLOC_CTX *mem_ctx, struct gse_context *gse_ctx,
881                       DATA_BLOB *data, DATA_BLOB *signature)
882 {
883         OM_uint32 gss_min, gss_maj;
884         gss_buffer_desc in_data = { 0, NULL };
885         gss_buffer_desc in_token = { 0, NULL};
886         NTSTATUS status;
887
888         in_data.value = data->data;
889         in_data.length = data->length;
890         in_token.value = signature->data;
891         in_token.length = signature->length;
892
893         gss_maj = gss_verify_mic(&gss_min, gse_ctx->gss_ctx,
894                                  &in_data, &in_token, NULL);
895         if (gss_maj) {
896                 DEBUG(0, ("gss_verify_mic failed with [%s]\n",
897                           gse_errstr(talloc_tos(), gss_maj, gss_min)));
898                 status = NT_STATUS_ACCESS_DENIED;
899                 goto done;
900         }
901
902         status = NT_STATUS_OK;
903
904 done:
905         return status;
906 }
907
908 #else
909
910 NTSTATUS gse_init_client(TALLOC_CTX *mem_ctx,
911                           bool do_sign, bool do_seal,
912                           const char *ccache_name,
913                           const char *server,
914                           const char *service,
915                           const char *username,
916                           const char *password,
917                           uint32_t add_gss_c_flags,
918                           struct gse_context **_gse_ctx)
919 {
920         return NT_STATUS_NOT_IMPLEMENTED;
921 }
922
923 NTSTATUS gse_get_client_auth_token(TALLOC_CTX *mem_ctx,
924                                    struct gse_context *gse_ctx,
925                                    DATA_BLOB *token_in,
926                                    DATA_BLOB *token_out)
927 {
928         return NT_STATUS_NOT_IMPLEMENTED;
929 }
930
931 NTSTATUS gse_init_server(TALLOC_CTX *mem_ctx,
932                          bool do_sign, bool do_seal,
933                          uint32_t add_gss_c_flags,
934                          struct gse_context **_gse_ctx)
935 {
936         return NT_STATUS_NOT_IMPLEMENTED;
937 }
938
939 NTSTATUS gse_get_server_auth_token(TALLOC_CTX *mem_ctx,
940                                    struct gse_context *gse_ctx,
941                                    DATA_BLOB *token_in,
942                                    DATA_BLOB *token_out)
943 {
944         return NT_STATUS_NOT_IMPLEMENTED;
945 }
946
947 NTSTATUS gse_verify_server_auth_flags(struct gse_context *gse_ctx)
948 {
949         return NT_STATUS_NOT_IMPLEMENTED;
950 }
951
952 bool gse_require_more_processing(struct gse_context *gse_ctx)
953 {
954         return false;
955 }
956
957 DATA_BLOB gse_get_session_key(TALLOC_CTX *mem_ctx,
958                               struct gse_context *gse_ctx)
959 {
960         return data_blob_null;
961 }
962
963 NTSTATUS gse_get_client_name(struct gse_context *gse_ctx,
964                              TALLOC_CTX *mem_ctx, char **cli_name)
965 {
966         return NT_STATUS_NOT_IMPLEMENTED;
967 }
968
969 NTSTATUS gse_get_authz_data(struct gse_context *gse_ctx,
970                             TALLOC_CTX *mem_ctx, DATA_BLOB *pac)
971 {
972         return NT_STATUS_NOT_IMPLEMENTED;
973 }
974
975 NTSTATUS gse_get_authtime(struct gse_context *gse_ctx, time_t *authtime)
976 {
977         return NT_STATUS_NOT_IMPLEMENTED;
978 }
979
980 size_t gse_get_signature_length(struct gse_context *gse_ctx,
981                                 int seal, size_t payload_size)
982 {
983         return 0;
984 }
985
986 NTSTATUS gse_seal(TALLOC_CTX *mem_ctx, struct gse_context *gse_ctx,
987                   DATA_BLOB *data, DATA_BLOB *signature)
988 {
989         return NT_STATUS_NOT_IMPLEMENTED;
990 }
991
992 NTSTATUS gse_unseal(TALLOC_CTX *mem_ctx, struct gse_context *gse_ctx,
993                     DATA_BLOB *data, DATA_BLOB *signature)
994 {
995         return NT_STATUS_NOT_IMPLEMENTED;
996 }
997
998 NTSTATUS gse_sign(TALLOC_CTX *mem_ctx, struct gse_context *gse_ctx,
999                   DATA_BLOB *data, DATA_BLOB *signature)
1000 {
1001         return NT_STATUS_NOT_IMPLEMENTED;
1002 }
1003
1004 NTSTATUS gse_sigcheck(TALLOC_CTX *mem_ctx, struct gse_context *gse_ctx,
1005                       DATA_BLOB *data, DATA_BLOB *signature)
1006 {
1007         return NT_STATUS_NOT_IMPLEMENTED;
1008 }
1009
1010 #endif /* HAVE_KRB5 && HAVE_GSS_WRAP_IOV */