2 * Copyright (c) 2010 PADL Software Pty Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Redistributions in any form must be accompanied by information on
17 * how to obtain complete source code for the GS2 software and any
18 * accompanying software that uses the GS2 software. The source code
19 * must either be included in the distribution or be available for no
20 * more than the cost of distribution plus a nominal fee, and must be
21 * freely redistributable under reasonable conditions. For an
22 * executable file, complete source code means the source code for all
23 * modules it contains. It does not include source code for modules or
24 * files that typically accompany the major components of the operating
25 * system on which the executable file runs.
27 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE ``AS IS'' AND ANY EXPRESS
28 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
30 * NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37 * THE POSSIBILITY OF SUCH DAMAGE.
40 * Copyright (c) 1998-2003 Carnegie Mellon University.
41 * All rights reserved.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in
52 * the documentation and/or other materials provided with the
55 * 3. The name "Carnegie Mellon University" must not be used to
56 * endorse or promote products derived from this software without
57 * prior written permission. For permission or any other legal
58 * details, please contact
59 * Office of Technology Transfer
60 * Carnegie Mellon University
62 * Pittsburgh, PA 15213-3890
63 * (412) 268-4387, fax: (412) 268-7395
64 * tech-transfer@andrew.cmu.edu
66 * 4. Redistributions of any form whatsoever must retain the following
68 * "This product includes software developed by Computing Services
69 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
71 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
72 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
73 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
74 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
75 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
76 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
77 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
81 #include <gssapi/gssapi.h>
82 #include <gssapi/gssapi_ext.h>
89 #include "plugin_common.h"
97 #include "gs2_token.h"
99 #define GS2_CB_FLAG_MASK 0x0F
100 #define GS2_CB_FLAG_N 0x00
101 #define GS2_CB_FLAG_P 0x01
102 #define GS2_CB_FLAG_Y 0x02
103 #define GS2_NONSTD_FLAG 0x10
105 typedef struct context {
106 gss_ctx_id_t gss_ctx;
107 gss_name_t client_name;
108 gss_name_t server_name;
109 gss_cred_id_t server_creds;
110 gss_cred_id_t client_creds;
112 unsigned out_buf_len;
113 const sasl_utils_t *utils;
117 sasl_client_plug_t *client;
118 sasl_server_plug_t *server;
123 struct gss_channel_bindings_struct gss_cbindings;
124 sasl_secret_t *password;
125 unsigned int free_password;
129 static gss_OID_set gs2_mechs = GSS_C_NO_OID_SET;
131 static int gs2_ask_user_info(context_t *context,
132 sasl_client_params_t *params,
133 char **realms, int nrealm,
134 sasl_interact_t **prompt_need,
135 sasl_out_params_t *oparams);
137 static int gs2_verify_initial_message(context_t *text,
138 sasl_server_params_t *sparams,
143 static int gs2_make_header(context_t *text,
144 sasl_client_params_t *cparams,
149 static int gs2_make_message(context_t *text,
150 sasl_client_params_t *cparams,
151 int initialContextToken,
156 static int gs2_get_mech_attrs(const sasl_utils_t *utils,
158 unsigned int *security_flags,
159 unsigned int *features);
161 static int gs2_indicate_mechs(const sasl_utils_t *utils);
163 static int gs2_map_sasl_name(const sasl_utils_t *utils,
167 static int gs2_duplicate_buffer(const sasl_utils_t *utils,
168 const gss_buffer_t src,
171 static int gs2_unescape_authzid(const sasl_utils_t *utils,
176 static int gs2_escape_authzid(const sasl_utils_t *utils,
181 /* sasl_gs_log: only logs status string returned from gss_display_status() */
182 #define sasl_gs2_log(x,y,z) sasl_gs2_seterror_(x,y,z,1)
183 #define sasl_gs2_seterror(x,y,z) sasl_gs2_seterror_(x,y,z,0)
186 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
190 sasl_gs2_new_context(const sasl_utils_t *utils)
194 ret = utils->malloc(sizeof(context_t));
198 memset(ret, 0, sizeof(context_t));
205 sasl_gs2_free_context_contents(context_t *text)
212 if (text->gss_ctx != GSS_C_NO_CONTEXT) {
213 gss_delete_sec_context(&min_stat,&text->gss_ctx,
215 text->gss_ctx = GSS_C_NO_CONTEXT;
218 if (text->client_name != GSS_C_NO_NAME) {
219 gss_release_name(&min_stat,&text->client_name);
220 text->client_name = GSS_C_NO_NAME;
223 if (text->server_name != GSS_C_NO_NAME) {
224 gss_release_name(&min_stat,&text->server_name);
225 text->server_name = GSS_C_NO_NAME;
228 if (text->server_creds != GSS_C_NO_CREDENTIAL) {
229 gss_release_cred(&min_stat, &text->server_creds);
230 text->server_creds = GSS_C_NO_CREDENTIAL;
233 if (text->client_creds != GSS_C_NO_CREDENTIAL) {
234 gss_release_cred(&min_stat, &text->client_creds);
235 text->client_creds = GSS_C_NO_CREDENTIAL;
238 if (text->authid != NULL) {
239 text->utils->free(text->authid);
243 if (text->authzid != NULL) {
244 text->utils->free(text->authzid);
245 text->authzid = NULL;
249 if (text->mechanism != NULL) {
250 gss_release_oid(&min_stat, &text->mechanism);
251 text->mechanism = GSS_C_NO_OID;
255 gss_release_buffer(&min_stat, &text->gss_cbindings.application_data);
257 if (text->out_buf != NULL) {
258 text->utils->free(text->out_buf);
259 text->out_buf = NULL;
262 text->out_buf_len = 0;
264 if (text->cbindingname != NULL) {
265 text->utils->free(text->cbindingname);
266 text->cbindingname = NULL;
269 if (text->free_password)
270 _plug_free_secret(text->utils, &text->password);
272 memset(text, 0, sizeof(*text));
278 gs2_common_mech_dispose(void *conn_context, const sasl_utils_t *utils)
280 sasl_gs2_free_context_contents((context_t *)(conn_context));
281 utils->free(conn_context);
285 gs2_common_mech_free(void *global_context __attribute__((unused)),
286 const sasl_utils_t *utils)
290 if (gs2_mechs != GSS_C_NO_OID_SET) {
291 gss_release_oid_set(&minor, &gs2_mechs);
292 gs2_mechs = GSS_C_NO_OID_SET;
296 /***************************** Server Section *****************************/
299 gs2_server_mech_new(void *glob_context,
300 sasl_server_params_t *params,
301 const char *challenge __attribute__((unused)),
302 unsigned challen __attribute__((unused)),
308 text = sasl_gs2_new_context(params->utils);
310 MEMERROR(params->utils);
314 text->gss_ctx = GSS_C_NO_CONTEXT;
315 text->client_name = GSS_C_NO_NAME;
316 text->server_name = GSS_C_NO_NAME;
317 text->server_creds = GSS_C_NO_CREDENTIAL;
318 text->client_creds = GSS_C_NO_CREDENTIAL;
319 text->plug.server = glob_context;
321 ret = gs2_map_sasl_name(params->utils, text->plug.server->mech_name,
323 if (ret != SASL_OK) {
324 gs2_common_mech_dispose(text, params->utils);
328 *conn_context = text;
334 gs2_server_mech_step(void *conn_context,
335 sasl_server_params_t *params,
336 const char *clientin,
337 unsigned clientinlen,
338 const char **serverout,
339 unsigned *serveroutlen,
340 sasl_out_params_t *oparams)
342 context_t *text = (context_t *)conn_context;
343 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
344 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
345 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
346 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
347 gss_buffer_desc short_name_buf = GSS_C_EMPTY_BUFFER;
348 gss_name_t without = GSS_C_NO_NAME;
349 gss_OID_set_desc mechs;
350 gss_OID actual_mech = GSS_C_NO_OID;
351 OM_uint32 out_flags = 0;
352 int ret = 0, equal = 0;
353 int initialContextToken = (text->gss_ctx == GSS_C_NO_CONTEXT);
356 if (serverout == NULL) {
357 PARAMERROR(text->utils);
358 return SASL_BADPARAM;
364 if (initialContextToken) {
365 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
366 name_buf.value = params->utils->malloc(name_buf.length + 1);
367 if (name_buf.value == NULL) {
368 MEMERROR(text->utils);
372 snprintf(name_buf.value, name_buf.length + 1,
373 "%s@%s", params->service, params->serverFQDN);
374 maj_stat = gss_import_name(&min_stat,
376 GSS_C_NT_HOSTBASED_SERVICE,
378 params->utils->free(name_buf.value);
379 name_buf.value = NULL;
381 if (GSS_ERROR(maj_stat))
384 assert(text->server_creds == GSS_C_NO_CREDENTIAL);
387 mechs.elements = (gss_OID)text->mechanism;
389 if (params->gss_creds == GSS_C_NO_CREDENTIAL) {
390 maj_stat = gss_acquire_cred(&min_stat,
398 if (GSS_ERROR(maj_stat))
402 ret = gs2_verify_initial_message(text,
410 input_token.value = (void *)clientin;
411 input_token.length = clientinlen;
414 maj_stat = gss_accept_sec_context(&min_stat,
416 (params->gss_creds != GSS_C_NO_CREDENTIAL)
417 ? (gss_cred_id_t)params->gss_creds
418 : text->server_creds,
420 &text->gss_cbindings,
426 &text->client_creds);
427 if (GSS_ERROR(maj_stat)) {
428 sasl_gs2_log(text->utils, maj_stat, min_stat);
429 text->utils->seterror(text->utils->conn, SASL_NOLOG,
430 "GS2 Failure: gss_accept_sec_context");
431 ret = (maj_stat == GSS_S_BAD_BINDINGS) ? SASL_BADBINDING : SASL_BADAUTH;
435 *serveroutlen = output_token.length;
436 if (output_token.value != NULL) {
437 ret = _plug_buf_alloc(text->utils, &text->out_buf,
438 &text->out_buf_len, *serveroutlen);
441 memcpy(text->out_buf, output_token.value, *serveroutlen);
442 *serverout = text->out_buf;
444 /* No output token, send an empty string */
449 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
454 assert(maj_stat == GSS_S_COMPLETE);
456 if (!g_OID_equal(text->mechanism, actual_mech)) {
457 ret = SASL_WRONGMECH;
460 if ((out_flags & GSS_C_SEQUENCE_FLAG) == 0) {
465 maj_stat = gss_display_name(&min_stat, text->client_name,
467 if (GSS_ERROR(maj_stat))
470 ret = gs2_duplicate_buffer(params->utils, &name_buf, &short_name_buf);
474 p = (char *)memchr(name_buf.value, '@', name_buf.length);
476 short_name_buf.length = (p - (char *)name_buf.value);
478 maj_stat = gss_import_name(&min_stat,
482 if (GSS_ERROR(maj_stat)) {
487 maj_stat = gss_compare_name(&min_stat, text->client_name,
489 if (GSS_ERROR(maj_stat)) {
495 ((char *)short_name_buf.value)[short_name_buf.length] = '\0';
498 text->authid = (char *)short_name_buf.value;
499 short_name_buf.value = NULL;
500 short_name_buf.length = 0;
502 if (text->authzid != NULL) {
503 ret = params->canon_user(params->utils->conn,
505 SASL_CU_AUTHZID, oparams);
510 ret = params->canon_user(params->utils->conn,
512 text->authzid == NULL
513 ? (SASL_CU_AUTHZID | SASL_CU_AUTHID)
519 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
521 oparams->cbindingdisp = SASL_CB_DISP_NONE;
524 oparams->cbindingdisp = SASL_CB_DISP_USED;
525 oparams->cbindingname = text->cbindingname;
528 oparams->cbindingdisp = SASL_CB_DISP_WANT;
532 if (text->client_creds != GSS_C_NO_CREDENTIAL)
533 oparams->client_creds = &text->client_creds;
535 oparams->client_creds = NULL;
537 oparams->gss_peer_name = text->client_name;
538 oparams->gss_local_name = text->server_name;
539 oparams->maxoutbuf = 0xFFFFFF;
540 oparams->encode = NULL;
541 oparams->decode = NULL;
542 oparams->mech_ssf = 0;
543 oparams->doneflag = 1;
548 if (initialContextToken)
549 gss_release_buffer(&min_stat, &input_token);
550 gss_release_buffer(&min_stat, &name_buf);
551 gss_release_buffer(&min_stat, &short_name_buf);
552 gss_release_buffer(&min_stat, &output_token);
553 gss_release_name(&min_stat, &without);
554 gss_release_oid(&min_stat, &actual_mech);
556 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
557 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
560 if (ret != SASL_OK && ret != SASL_CONTINUE)
561 sasl_gs2_free_context_contents(text);
567 gs2_common_plug_init(const sasl_utils_t *utils,
569 int (*plug_alloc)(const sasl_utils_t *,
576 OM_uint32 major, minor;
583 if (gs2_indicate_mechs(utils) != SASL_OK) {
587 plugs = utils->malloc(2 * gs2_mechs->count * plugsize);
592 memset(plugs, 0, 2 * gs2_mechs->count * plugsize);
594 for (i = 0; i < gs2_mechs->count; i++) {
595 gss_buffer_desc sasl_mech_name = GSS_C_EMPTY_BUFFER;
597 major = gss_inquire_saslname_for_mech(&minor,
598 &gs2_mechs->elements[i],
602 if (GSS_ERROR(major))
605 #define PLUG_AT(index) (void *)((unsigned char *)plugs + (count * plugsize))
607 if (plug_alloc(utils, PLUG_AT(count), &sasl_mech_name,
608 &gs2_mechs->elements[i]) == SASL_OK)
611 gss_release_buffer(&minor, &sasl_mech_name);
626 gs2_server_plug_alloc(const sasl_utils_t *utils,
628 gss_buffer_t sasl_name,
632 sasl_server_plug_t *splug = (sasl_server_plug_t *)plug;
635 memset(splug, 0, sizeof(*splug));
637 ret = gs2_get_mech_attrs(utils, mech,
638 &splug->security_flags,
643 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
647 splug->mech_name = (char *)buf.value;
648 splug->glob_context = plug;
649 splug->mech_new = gs2_server_mech_new;
650 splug->mech_step = gs2_server_mech_step;
651 splug->mech_dispose = gs2_common_mech_dispose;
652 splug->mech_free = gs2_common_mech_free;
657 static sasl_server_plug_t *gs2_server_plugins;
658 static int gs2_server_plugcount;
661 gs2_server_plug_init(const sasl_utils_t *utils,
664 sasl_server_plug_t **pluglist,
672 if (maxversion < SASL_SERVER_PLUG_VERSION)
675 *outversion = SASL_SERVER_PLUG_VERSION;
677 if (gs2_server_plugins == NULL) {
678 ret = gs2_common_plug_init(utils,
679 sizeof(sasl_server_plug_t),
680 gs2_server_plug_alloc,
681 (void **)&gs2_server_plugins,
682 &gs2_server_plugcount);
687 *pluglist = gs2_server_plugins;
688 *plugcount = gs2_server_plugcount;
693 /***************************** Client Section *****************************/
695 static int gs2_client_mech_step(void *conn_context,
696 sasl_client_params_t *params,
697 const char *serverin,
698 unsigned serverinlen,
699 sasl_interact_t **prompt_need,
700 const char **clientout,
701 unsigned *clientoutlen,
702 sasl_out_params_t *oparams)
704 context_t *text = (context_t *)conn_context;
705 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
706 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
707 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
708 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
709 OM_uint32 req_flags, ret_flags;
710 gss_OID actual_mech = GSS_C_NO_OID;
712 int initialContextToken;
717 if (text->gss_ctx == GSS_C_NO_CONTEXT) {
718 ret = gs2_ask_user_info(text, params, NULL, 0, prompt_need, oparams);
722 if (params->gss_creds == GSS_C_NO_CREDENTIAL &&
723 text->password != NULL && text->password->len != 0) {
724 gss_buffer_desc password_buf;
725 gss_buffer_desc name_buf;
726 gss_OID_set_desc mechs;
728 name_buf.length = strlen(oparams->authid);
729 name_buf.value = (void *)oparams->authid;
731 password_buf.length = text->password->len;
732 password_buf.value = text->password->data;
735 mechs.elements = (gss_OID)text->mechanism;
737 maj_stat = gss_import_name(&min_stat,
741 if (GSS_ERROR(maj_stat))
744 maj_stat = gss_acquire_cred_with_password(&min_stat,
753 if (GSS_ERROR(maj_stat))
757 initialContextToken = 1;
759 initialContextToken = 0;
761 if (text->server_name == GSS_C_NO_NAME) { /* only once */
762 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
763 name_buf.value = params->utils->malloc(name_buf.length + 1);
764 if (name_buf.value == NULL) {
768 if (params->serverFQDN == NULL ||
769 strlen(params->serverFQDN) == 0) {
770 SETERROR(text->utils, "GS2 Failure: no serverFQDN");
775 snprintf(name_buf.value, name_buf.length + 1,
776 "%s@%s", params->service, params->serverFQDN);
778 maj_stat = gss_import_name (&min_stat,
780 GSS_C_NT_HOSTBASED_SERVICE,
782 params->utils->free(name_buf.value);
783 name_buf.value = NULL;
785 if (GSS_ERROR(maj_stat))
789 /* From GSSAPI plugin: apparently this is for some IMAP bug workaround */
790 if (serverinlen == 0 && text->gss_ctx != GSS_C_NO_CONTEXT) {
791 gss_delete_sec_context(&min_stat, &text->gss_ctx, GSS_C_NO_BUFFER);
792 text->gss_ctx = GSS_C_NO_CONTEXT;
795 input_token.value = (void *)serverin;
796 input_token.length = serverinlen;
798 if (initialContextToken) {
799 if ((text->plug.client->features & SASL_FEAT_GSS_FRAMING) == 0)
800 text->gs2_flags |= GS2_NONSTD_FLAG;
802 switch (params->cbindingdisp) {
803 case SASL_CB_DISP_NONE:
804 text->gs2_flags |= GS2_CB_FLAG_N;
806 case SASL_CB_DISP_USED:
807 text->gs2_flags |= GS2_CB_FLAG_P;
809 case SASL_CB_DISP_WANT:
810 text->gs2_flags |= GS2_CB_FLAG_Y;
814 ret = gs2_make_header(text, params,
815 strcmp(oparams->user, oparams->authid) ?
816 (char *) oparams->user : NULL,
817 &text->out_buf, &text->out_buf_len);
822 req_flags = GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG;
824 maj_stat = gss_init_sec_context(&min_stat,
825 (params->gss_creds != GSS_C_NO_CREDENTIAL)
826 ? (gss_cred_id_t)params->gss_creds
827 : text->client_creds,
830 (gss_OID)text->mechanism,
833 &text->gss_cbindings,
834 serverinlen ? &input_token : GSS_C_NO_BUFFER,
839 if (GSS_ERROR(maj_stat))
842 ret = gs2_make_message(text, params, initialContextToken, &output_token,
843 &text->out_buf, &text->out_buf_len);
847 *clientout = text->out_buf;
848 *clientoutlen = text->out_buf_len;
850 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
855 if (text->client_name != GSS_C_NO_NAME)
856 gss_release_name(&min_stat, &text->client_name);
858 maj_stat = gss_inquire_context(&min_stat,
864 &ret_flags, /* flags */
867 if (GSS_ERROR(maj_stat))
870 if (!g_OID_equal(text->mechanism, actual_mech)) {
871 ret = SASL_WRONGMECH;
874 if ((ret_flags & req_flags) != req_flags) {
875 maj_stat = SASL_BADAUTH;
879 maj_stat = gss_display_name(&min_stat,
883 if (GSS_ERROR(maj_stat))
886 oparams->gss_peer_name = text->server_name;
887 oparams->gss_local_name = text->client_name;
888 oparams->encode = NULL;
889 oparams->decode = NULL;
890 oparams->mech_ssf = 0;
891 oparams->maxoutbuf = 0xFFFFFF;
892 oparams->doneflag = 1;
895 gss_release_buffer(&min_stat, &output_token);
896 gss_release_buffer(&min_stat, &name_buf);
897 gss_release_oid(&min_stat, &actual_mech);
899 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
900 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
903 if (ret != SASL_OK && ret != SASL_CONTINUE)
904 sasl_gs2_free_context_contents(text);
909 static int gs2_client_mech_new(void *glob_context,
910 sasl_client_params_t *params,
916 text = sasl_gs2_new_context(params->utils);
918 MEMERROR(params->utils);
922 text->gss_ctx = GSS_C_NO_CONTEXT;
923 text->client_name = GSS_C_NO_NAME;
924 text->server_creds = GSS_C_NO_CREDENTIAL;
925 text->client_creds = GSS_C_NO_CREDENTIAL;
926 text->plug.client = glob_context;
928 ret = gs2_map_sasl_name(params->utils, text->plug.client->mech_name,
930 if (ret != SASL_OK) {
931 gs2_common_mech_dispose(text, params->utils);
935 *conn_context = text;
940 static const unsigned long gs2_required_prompts[] = {
945 gs2_client_plug_alloc(const sasl_utils_t *utils,
947 gss_buffer_t sasl_name,
951 sasl_client_plug_t *cplug = (sasl_client_plug_t *)plug;
954 memset(cplug, 0, sizeof(*cplug));
956 ret = gs2_get_mech_attrs(utils, mech,
957 &cplug->security_flags,
962 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
966 cplug->mech_name = (char *)buf.value;
967 cplug->features |= SASL_FEAT_NEEDSERVERFQDN;
968 cplug->glob_context = plug;
969 cplug->mech_new = gs2_client_mech_new;
970 cplug->mech_step = gs2_client_mech_step;
971 cplug->mech_dispose = gs2_common_mech_dispose;
972 cplug->mech_free = gs2_common_mech_free;
973 cplug->required_prompts = gs2_required_prompts;
978 static sasl_client_plug_t *gs2_client_plugins;
979 static int gs2_client_plugcount;
982 gs2_client_plug_init(const sasl_utils_t *utils,
985 sasl_client_plug_t **pluglist,
993 if (maxversion < SASL_CLIENT_PLUG_VERSION)
996 *outversion = SASL_CLIENT_PLUG_VERSION;
998 if (gs2_client_plugins == NULL) {
999 ret = gs2_common_plug_init(utils,
1000 sizeof(sasl_client_plug_t),
1001 gs2_client_plug_alloc,
1002 (void **)&gs2_client_plugins,
1003 &gs2_client_plugcount);
1008 *pluglist = gs2_client_plugins;
1009 *plugcount = gs2_client_plugcount;
1015 * Copy header and application channel bindings to GSS channel bindings
1016 * structure in context.
1019 gs2_save_cbindings(context_t *text,
1020 gss_buffer_t header,
1021 const sasl_channel_binding_t *cbinding)
1023 gss_buffer_t gss_cbindings = &text->gss_cbindings.application_data;
1027 assert(gss_cbindings->value == NULL);
1030 * The application-data field MUST be set to the gs2-header, excluding
1031 * the initial [gs2-nonstd-flag ","] part, concatenated with, when a
1032 * gs2-cb-flag of "p" is used, the application's channel binding data.
1034 len = header->length;
1035 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1039 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
1041 len += cbinding->len;
1044 gss_cbindings->length = len;
1045 gss_cbindings->value = text->utils->malloc(len);
1046 if (gss_cbindings->value == NULL)
1049 p = (unsigned char *)gss_cbindings->value;
1050 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1051 memcpy(p, (unsigned char *)header->value + 2, header->length - 2);
1052 p += header->length - 2;
1054 memcpy(p, header->value, header->length);
1055 p += header->length;
1058 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
1060 memcpy(p, cbinding->data, cbinding->len);
1066 #define CHECK_REMAIN(n) do { if (remain < (n)) return SASL_BADPROT; } while (0)
1069 * Verify gs2-header, save authzid and channel bindings to context.
1072 gs2_verify_initial_message(context_t *text,
1073 sasl_server_params_t *sparams,
1078 char *p = (char *)in;
1079 unsigned remain = inlen;
1081 gss_buffer_desc buf = GSS_C_EMPTY_BUFFER;
1083 assert(text->cbindingname == NULL);
1084 assert(text->authzid == NULL);
1087 token->value = NULL;
1089 /* minimum header includes CB flag and non-zero GSS token */
1090 CHECK_REMAIN(4); /* [pny],,. */
1092 /* non-standard GSS framing flag */
1093 if (remain > 1 && memcmp(p, "F,", 2) == 0) {
1094 text->gs2_flags |= GS2_NONSTD_FLAG;
1099 /* SASL channel bindings */
1100 CHECK_REMAIN(1); /* [pny] */
1104 CHECK_REMAIN(1); /* = */
1107 return SASL_BADPROT;
1109 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->cbindingname);
1113 text->gs2_flags |= GS2_CB_FLAG_P;
1116 text->gs2_flags |= GS2_CB_FLAG_N;
1119 text->gs2_flags |= GS2_CB_FLAG_Y;
1123 CHECK_REMAIN(1); /* , */
1126 return SASL_BADPROT;
1128 /* authorization identity */
1129 if (remain > 1 && memcmp(p, "a=", 2) == 0) {
1134 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->authzid);
1140 CHECK_REMAIN(1); /* , */
1143 return SASL_BADPROT;
1145 buf.length = inlen - remain;
1146 buf.value = (void *)in;
1148 /* stash channel bindings to pass into gss_accept_sec_context() */
1149 ret = gs2_save_cbindings(text, &buf, sparams->cbinding);
1153 buf.length = remain;
1156 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1157 token->value = text->utils->malloc(buf.length);
1158 if (token->value == NULL)
1161 token->length = buf.length;
1162 memcpy(token->value, buf.value, buf.length);
1164 unsigned int token_size;
1166 /* create a properly formed GSS token */
1167 token_size = gs2_token_size(text->mechanism, buf.length);
1168 token->value = text->utils->malloc(token_size);
1169 if (token->value == NULL)
1172 token->length = token_size;
1174 p = (char *)token->value;
1175 gs2_make_token_header(text->mechanism, buf.length,
1176 (unsigned char **)&p);
1177 memcpy(p, buf.value, buf.length);
1184 * Create gs2-header, save channel bindings to context.
1187 gs2_make_header(context_t *text,
1188 sasl_client_params_t *cparams,
1189 const char *authzid,
1193 size_t required = 0;
1194 size_t wire_authzid_len = 0, cbnamelen = 0;
1195 char *wire_authzid = NULL;
1198 gss_buffer_desc buf;
1203 /* non-standard GSS framing flag */
1204 if (text->gs2_flags & GS2_NONSTD_FLAG)
1205 required += 2; /* F, */
1207 /* SASL channel bindings */
1208 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1210 if (!SASL_CB_PRESENT(cparams))
1211 return SASL_BADPARAM;
1212 cbnamelen = strlen(cparams->cbinding->name);
1213 required += 1 /*=*/ + cbnamelen;
1217 required += 2; /* [pny], */
1220 return SASL_BADPARAM;
1223 /* authorization identity */
1224 if (authzid != NULL) {
1225 ret = gs2_escape_authzid(text->utils, authzid,
1226 strlen(authzid), &wire_authzid);
1230 wire_authzid_len = strlen(wire_authzid);
1231 required += 2 /* a= */ + wire_authzid_len;
1234 required += 1; /* trailing comma */
1236 ret = _plug_buf_alloc(text->utils, out, outlen, required);
1237 if (ret != SASL_OK) {
1238 text->utils->free(wire_authzid);
1242 *out = text->out_buf;
1245 p = (char *)text->out_buf;
1246 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1250 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1253 memcpy(p + 2, cparams->cbinding->name, cbnamelen);
1264 if (wire_authzid != NULL) {
1266 memcpy(p + 2, wire_authzid, wire_authzid_len);
1267 text->utils->free(wire_authzid);
1268 p += 2 + wire_authzid_len;
1272 assert(p == (char *)text->out_buf + required);
1274 buf.length = required;
1277 ret = gs2_save_cbindings(text, &buf, cparams->cbinding);
1285 * Convert a GSS token to a GS2 one
1288 gs2_make_message(context_t *text,
1289 sasl_client_params_t *cparams __attribute__((unused)),
1290 int initialContextToken,
1295 OM_uint32 major, minor;
1296 unsigned char *mech_token_data;
1297 size_t mech_token_size;
1299 unsigned header_len = 0;
1302 mech_token_size = token->length;
1303 mech_token_data = (unsigned char *)token->value;
1305 if (initialContextToken) {
1306 header_len = *outlen;
1308 major = gs2_verify_token_header(&minor, text->mechanism,
1309 &mech_token_size, &mech_token_data,
1311 if ((major == GSS_S_DEFECTIVE_TOKEN &&
1312 (text->plug.client->features & SASL_FEAT_GSS_FRAMING)) ||
1317 ret = _plug_buf_alloc(text->utils, out, outlen,
1318 header_len + mech_token_size);
1322 p = *out + header_len;
1323 memcpy(p, mech_token_data, mech_token_size);
1325 *outlen = header_len + mech_token_size;
1331 * Map GSS mechanism attributes to SASL ones
1334 gs2_get_mech_attrs(const sasl_utils_t *utils,
1336 unsigned int *security_flags,
1337 unsigned int *features)
1339 OM_uint32 major, minor;
1341 gss_OID_set attrs = GSS_C_NO_OID_SET;
1343 major = gss_inquire_attrs_for_mech(&minor, mech, &attrs, NULL);
1344 if (GSS_ERROR(major)) {
1345 utils->seterror(utils->conn, SASL_NOLOG,
1346 "GS2 Failure: gss_inquire_attrs_for_mech");
1350 *security_flags = SASL_SEC_NOPLAINTEXT | SASL_SEC_NOACTIVE;
1351 *features = SASL_FEAT_WANT_CLIENT_FIRST | SASL_FEAT_CHANNEL_BINDING;
1353 #define MA_PRESENT(a) (gss_test_oid_set_member(&minor, (gss_OID)(a), \
1354 attrs, &present) == GSS_S_COMPLETE && \
1359 if (MA_PRESENT(GSS_C_MA_PFS))
1360 *security_flags |= SASL_SEC_FORWARD_SECRECY;
1361 if (!MA_PRESENT(GSS_C_MA_AUTH_INIT_ANON))
1362 *security_flags |= SASL_SEC_NOANONYMOUS;
1363 if (MA_PRESENT(GSS_C_MA_DELEG_CRED))
1364 *security_flags |= SASL_SEC_PASS_CREDENTIALS;
1365 if (MA_PRESENT(GSS_C_MA_AUTH_TARG))
1366 *security_flags |= SASL_SEC_MUTUAL_AUTH;
1367 if (MA_PRESENT(GSS_C_MA_ITOK_FRAMED))
1368 *features |= SASL_FEAT_GSS_FRAMING;
1370 gss_release_oid_set(&minor, &attrs);
1375 * Enumerate GSS mechanisms that can be used for GS2
1377 static int gs2_indicate_mechs(const sasl_utils_t *utils)
1379 OM_uint32 major, minor;
1380 gss_OID_desc desired_oids[3];
1381 gss_OID_set_desc desired_attrs;
1382 gss_OID_desc except_oids[3];
1383 gss_OID_set_desc except_attrs;
1385 if (gs2_mechs != GSS_C_NO_OID_SET)
1388 desired_oids[0] = *GSS_C_MA_AUTH_INIT;
1389 desired_oids[1] = *GSS_C_MA_AUTH_TARG;
1390 desired_oids[2] = *GSS_C_MA_CBINDINGS;
1391 desired_attrs.count = sizeof(desired_oids)/sizeof(desired_oids[0]);
1392 desired_attrs.elements = desired_oids;
1394 except_oids[0] = *GSS_C_MA_MECH_NEGO;
1395 except_oids[1] = *GSS_C_MA_NOT_MECH;
1396 except_oids[2] = *GSS_C_MA_DEPRECATED;
1398 except_attrs.count = sizeof(except_oids)/sizeof(except_oids[0]);
1399 except_attrs.elements = except_oids;
1401 major = gss_indicate_mechs_by_attrs(&minor,
1406 if (GSS_ERROR(major)) {
1407 utils->seterror(utils->conn, SASL_NOLOG,
1408 "GS2 Failure: gss_indicate_mechs_by_attrs");
1412 return (gs2_mechs->count > 0) ? SASL_OK : SASL_NOMECH;
1416 * Map SASL mechanism name to OID
1419 gs2_map_sasl_name(const sasl_utils_t *utils,
1423 OM_uint32 major, minor;
1424 gss_buffer_desc buf;
1426 buf.length = strlen(mech);
1427 buf.value = (void *)mech;
1429 major = gss_inquire_mech_for_saslname(&minor, &buf, oid);
1430 if (GSS_ERROR(major)) {
1431 utils->seterror(utils->conn, SASL_NOLOG,
1432 "GS2 Failure: gss_inquire_mech_for_saslname");
1440 gs2_duplicate_buffer(const sasl_utils_t *utils,
1441 const gss_buffer_t src,
1444 dst->value = utils->malloc(src->length + 1);
1445 if (dst->value == NULL)
1448 memcpy(dst->value, src->value, src->length);
1449 ((char *)dst->value)[src->length] = '\0';
1450 dst->length = src->length;
1456 gs2_unescape_authzid(const sasl_utils_t *utils,
1462 size_t i, len, inlen = *remain;
1467 for (i = 0, len = 0; i < inlen; i++) {
1472 } else if (in[i] == '=') {
1474 return SASL_BADPROT;
1480 if (len == 0 || *endp == NULL)
1481 return SASL_BADPROT;
1483 p = *authzid = utils->malloc(len + 1);
1484 if (*authzid == NULL)
1487 for (i = 0; i < inlen; i++) {
1490 else if (in[i] == '=') {
1491 if (memcmp(&in[i + 1], "2C", 2) == 0)
1493 else if (memcmp(&in[i + 1], "3D", 2) == 0)
1496 utils->free(*authzid);
1498 return SASL_BADPROT;
1511 gs2_escape_authzid(const sasl_utils_t *utils,
1519 p = *authzid = utils->malloc((inlen * 3) + 1);
1520 if (*authzid == NULL)
1523 for (i = 0; i < inlen; i++) {
1525 memcpy(p, "=2C", 3);
1527 } else if (in[i] == '=') {
1528 memcpy(p, "=3D", 3);
1541 gs2_ask_user_info(context_t *text,
1542 sasl_client_params_t *params,
1543 char **realms __attribute__((unused)),
1544 int nrealm __attribute__((unused)),
1545 sasl_interact_t **prompt_need,
1546 sasl_out_params_t *oparams)
1548 int result = SASL_OK;
1549 const char *authid = NULL, *userid = NULL;
1550 int user_result = SASL_OK;
1551 int auth_result = SASL_OK;
1552 int pass_result = SASL_OK;
1554 /* try to get the authid */
1555 if (oparams->authid == NULL) {
1556 auth_result = _plug_get_authid(params->utils, &authid, prompt_need);
1558 if (auth_result != SASL_OK && auth_result != SASL_INTERACT) {
1563 /* try to get the userid */
1564 if (oparams->user == NULL) {
1565 user_result = _plug_get_userid(params->utils, &userid, prompt_need);
1567 if (user_result != SASL_OK && user_result != SASL_INTERACT) {
1572 /* try to get the password */
1573 if (text->password == NULL) {
1574 pass_result = _plug_get_password(params->utils, &text->password,
1575 &text->free_password, prompt_need);
1576 if (pass_result != SASL_OK && pass_result != SASL_INTERACT) {
1581 /* free prompts we got */
1582 if (prompt_need && *prompt_need) {
1583 params->utils->free(*prompt_need);
1584 *prompt_need = NULL;
1587 /* if there are prompts not filled in */
1588 if (user_result == SASL_INTERACT || auth_result == SASL_INTERACT ||
1589 pass_result == SASL_INTERACT) {
1591 /* make the prompt list */
1593 _plug_make_prompts(params->utils, prompt_need,
1594 user_result == SASL_INTERACT ?
1595 "Please enter your authorization name" : NULL,
1597 auth_result == SASL_INTERACT ?
1598 "Please enter your authentication name" : NULL,
1600 pass_result == SASL_INTERACT ?
1601 "Please enter your password" : NULL, NULL,
1605 if (result == SASL_OK) return SASL_INTERACT;
1610 if (oparams->authid == NULL) {
1611 if (userid == NULL || userid[0] == '\0') {
1612 result = params->canon_user(params->utils->conn, authid, 0,
1613 SASL_CU_AUTHID | SASL_CU_AUTHZID,
1616 result = params->canon_user(params->utils->conn,
1617 authid, 0, SASL_CU_AUTHID, oparams);
1618 if (result != SASL_OK) return result;
1620 result = params->canon_user(params->utils->conn,
1621 userid, 0, SASL_CU_AUTHZID, oparams);
1623 if (result != SASL_OK)
1631 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
1634 OM_uint32 maj_stat, min_stat;
1635 gss_buffer_desc msg;
1639 unsigned int len, curlen = 0;
1640 const char prefix[] = "GSSAPI Error: ";
1642 len = sizeof(prefix);
1643 ret = _plug_buf_alloc(utils, &out, &curlen, 256);
1647 strcpy(out, prefix);
1651 maj_stat = gss_display_status(&min_stat, maj,
1652 GSS_C_GSS_CODE, GSS_C_NULL_OID,
1655 if (GSS_ERROR(maj_stat)) {
1657 utils->log(utils->conn, SASL_LOG_FAIL,
1658 "GS2 Failure: (could not get major error message)");
1660 utils->seterror(utils->conn, 0,
1662 "(could not get major error message)");
1668 len += len + msg.length;
1669 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1670 if (ret != SASL_OK) {
1675 strcat(out, msg.value);
1677 gss_release_buffer(&min_stat, &msg);
1683 /* Now get the minor status */
1686 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1687 if (ret != SASL_OK) {
1696 maj_stat = gss_display_status(&min_stat, min,
1697 GSS_C_MECH_CODE, GSS_C_NULL_OID,
1700 if (GSS_ERROR(maj_stat)) {
1702 utils->log(utils->conn, SASL_LOG_FAIL,
1703 "GS2 Failure: (could not get minor error message)");
1705 utils->seterror(utils->conn, 0,
1707 "(could not get minor error message)");
1713 len += len + msg.length;
1715 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1716 if (ret != SASL_OK) {
1721 strcat(out, msg.value);
1723 gss_release_buffer(&min_stat, &msg);
1730 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1731 if (ret != SASL_OK) {
1739 utils->log(utils->conn, SASL_LOG_FAIL, out);
1741 utils->seterror(utils->conn, 0, out);