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 bindings;
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;
248 if (text->mechanism != NULL) {
249 gss_release_oid(&min_stat, &text->mechanism);
250 text->mechanism = GSS_C_NO_OID;
253 gss_release_buffer(&min_stat, &text->bindings.application_data);
255 if (text->out_buf != NULL) {
256 text->utils->free(text->out_buf);
257 text->out_buf = NULL;
260 text->out_buf_len = 0;
262 if (text->cbindingname != NULL) {
263 text->utils->free(text->cbindingname);
264 text->cbindingname = NULL;
267 if (text->free_password)
268 _plug_free_secret(text->utils, &text->password);
270 memset(text, 0, sizeof(*text));
276 gs2_common_mech_dispose(void *conn_context, const sasl_utils_t *utils)
278 sasl_gs2_free_context_contents((context_t *)(conn_context));
279 utils->free(conn_context);
283 gs2_common_mech_free(void *global_context __attribute__((unused)),
284 const sasl_utils_t *utils)
288 if (gs2_mechs != GSS_C_NO_OID_SET) {
289 gss_release_oid_set(&minor, &gs2_mechs);
290 gs2_mechs = GSS_C_NO_OID_SET;
294 /***************************** Server Section *****************************/
297 gs2_server_mech_new(void *glob_context,
298 sasl_server_params_t *params,
299 const char *challenge __attribute__((unused)),
300 unsigned challen __attribute__((unused)),
306 text = sasl_gs2_new_context(params->utils);
308 MEMERROR(params->utils);
312 text->gss_ctx = GSS_C_NO_CONTEXT;
313 text->client_name = GSS_C_NO_NAME;
314 text->server_name = GSS_C_NO_NAME;
315 text->server_creds = GSS_C_NO_CREDENTIAL;
316 text->client_creds = GSS_C_NO_CREDENTIAL;
317 text->plug.server = glob_context;
319 ret = gs2_map_sasl_name(params->utils, text->plug.server->mech_name,
321 if (ret != SASL_OK) {
322 gs2_common_mech_dispose(text, params->utils);
326 *conn_context = text;
332 gs2_server_mech_step(void *conn_context,
333 sasl_server_params_t *params,
334 const char *clientin,
335 unsigned clientinlen,
336 const char **serverout,
337 unsigned *serveroutlen,
338 sasl_out_params_t *oparams)
340 context_t *text = (context_t *)conn_context;
341 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
342 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
343 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
344 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
345 gss_buffer_desc short_name_buf = GSS_C_EMPTY_BUFFER;
346 gss_name_t without = GSS_C_NO_NAME;
347 gss_OID_set_desc mechs;
348 gss_OID actual_mech = GSS_C_NO_OID;
349 OM_uint32 out_flags = 0;
350 int ret = 0, equal = 0;
351 int initialContextToken = (text->gss_ctx == GSS_C_NO_CONTEXT);
354 if (serverout == NULL) {
355 PARAMERROR(text->utils);
356 return SASL_BADPARAM;
362 if (initialContextToken) {
363 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
364 name_buf.value = params->utils->malloc(name_buf.length + 1);
365 if (name_buf.value == NULL) {
366 MEMERROR(text->utils);
370 snprintf(name_buf.value, name_buf.length + 1,
371 "%s@%s", params->service, params->serverFQDN);
372 maj_stat = gss_import_name(&min_stat,
374 GSS_C_NT_HOSTBASED_SERVICE,
376 params->utils->free(name_buf.value);
377 name_buf.value = NULL;
379 if (GSS_ERROR(maj_stat))
382 assert(text->server_creds == GSS_C_NO_CREDENTIAL);
385 mechs.elements = (gss_OID)text->mechanism;
387 if (params->gss_creds == GSS_C_NO_CREDENTIAL) {
388 maj_stat = gss_acquire_cred(&min_stat,
396 if (GSS_ERROR(maj_stat))
400 ret = gs2_verify_initial_message(text,
408 input_token.value = (void *)clientin;
409 input_token.length = clientinlen;
412 maj_stat = gss_accept_sec_context(&min_stat,
414 (params->gss_creds != GSS_C_NO_CREDENTIAL)
415 ? (gss_cred_id_t)params->gss_creds
416 : text->server_creds,
424 &text->client_creds);
425 if (GSS_ERROR(maj_stat)) {
426 sasl_gs2_log(text->utils, maj_stat, min_stat);
427 text->utils->seterror(text->utils->conn, SASL_NOLOG,
428 "GS2 Failure: gss_accept_sec_context");
433 *serveroutlen = output_token.length;
434 if (output_token.value != NULL) {
435 ret = _plug_buf_alloc(text->utils, &text->out_buf,
436 &text->out_buf_len, *serveroutlen);
439 memcpy(text->out_buf, output_token.value, *serveroutlen);
440 *serverout = text->out_buf;
442 /* No output token, send an empty string */
447 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
452 assert(maj_stat == GSS_S_COMPLETE);
454 if (!g_OID_equal(text->mechanism, actual_mech)) {
455 ret = SASL_WRONGMECH;
458 if ((out_flags & GSS_C_SEQUENCE_FLAG) == 0) {
463 maj_stat = gss_display_name(&min_stat, text->client_name,
465 if (GSS_ERROR(maj_stat))
468 ret = gs2_duplicate_buffer(params->utils, &name_buf, &short_name_buf);
472 p = (char *)memchr(name_buf.value, '@', name_buf.length);
474 short_name_buf.length = (p - (char *)name_buf.value);
476 maj_stat = gss_import_name(&min_stat,
480 if (GSS_ERROR(maj_stat)) {
485 maj_stat = gss_compare_name(&min_stat, text->client_name,
487 if (GSS_ERROR(maj_stat)) {
493 ((char *)short_name_buf.value)[short_name_buf.length] = '\0';
496 text->authid = (char *)short_name_buf.value;
497 short_name_buf.value = NULL;
498 short_name_buf.length = 0;
500 if (text->authzid != NULL) {
501 ret = params->canon_user(params->utils->conn,
503 SASL_CU_AUTHZID, oparams);
508 ret = params->canon_user(params->utils->conn,
510 text->authzid == NULL
511 ? (SASL_CU_AUTHZID | SASL_CU_AUTHID)
517 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
519 oparams->cbindingdisp = SASL_CB_DISP_NONE;
522 oparams->cbindingdisp = SASL_CB_DISP_USED;
523 oparams->cbindingname = text->cbindingname;
526 oparams->cbindingdisp = SASL_CB_DISP_WANT;
530 if (text->client_creds != GSS_C_NO_CREDENTIAL)
531 oparams->client_creds = &text->client_creds;
533 oparams->client_creds = NULL;
535 oparams->gss_peer_name = text->client_name;
536 oparams->gss_local_name = text->server_name;
537 oparams->maxoutbuf = 0xFFFFFF;
538 oparams->encode = NULL;
539 oparams->decode = NULL;
540 oparams->mech_ssf = 0;
541 oparams->doneflag = 1;
546 if (initialContextToken)
547 gss_release_buffer(&min_stat, &input_token);
548 gss_release_buffer(&min_stat, &name_buf);
549 gss_release_buffer(&min_stat, &short_name_buf);
550 gss_release_buffer(&min_stat, &output_token);
551 gss_release_name(&min_stat, &without);
552 gss_release_oid(&min_stat, &actual_mech);
554 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
555 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
558 if (ret != SASL_OK && ret != SASL_CONTINUE)
559 sasl_gs2_free_context_contents(text);
565 gs2_common_plug_init(const sasl_utils_t *utils,
567 int (*plug_alloc)(const sasl_utils_t *,
574 OM_uint32 major, minor;
581 if (gs2_indicate_mechs(utils) != SASL_OK) {
585 plugs = utils->malloc(2 * gs2_mechs->count * plugsize);
590 memset(plugs, 0, 2 * gs2_mechs->count * plugsize);
592 for (i = 0; i < gs2_mechs->count; i++) {
593 gss_buffer_desc sasl_mech_name = GSS_C_EMPTY_BUFFER;
595 major = gss_inquire_saslname_for_mech(&minor,
596 &gs2_mechs->elements[i],
600 if (GSS_ERROR(major))
603 #define PLUG_AT(index) (void *)((unsigned char *)plugs + (count * plugsize))
605 if (plug_alloc(utils, PLUG_AT(count), &sasl_mech_name,
606 &gs2_mechs->elements[i]) == SASL_OK)
609 gss_release_buffer(&minor, &sasl_mech_name);
624 gs2_server_plug_alloc(const sasl_utils_t *utils,
626 gss_buffer_t sasl_name,
630 sasl_server_plug_t *splug = (sasl_server_plug_t *)plug;
633 memset(splug, 0, sizeof(*splug));
635 ret = gs2_get_mech_attrs(utils, mech,
636 &splug->security_flags,
641 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
645 splug->mech_name = (char *)buf.value;
646 splug->glob_context = plug;
647 splug->mech_new = gs2_server_mech_new;
648 splug->mech_step = gs2_server_mech_step;
649 splug->mech_dispose = gs2_common_mech_dispose;
650 splug->mech_free = gs2_common_mech_free;
655 static sasl_server_plug_t *gs2_server_plugins;
656 static int gs2_server_plugcount;
659 gs2_server_plug_init(const sasl_utils_t *utils,
662 sasl_server_plug_t **pluglist,
670 if (maxversion < SASL_SERVER_PLUG_VERSION)
673 *outversion = SASL_SERVER_PLUG_VERSION;
675 if (gs2_server_plugins == NULL) {
676 ret = gs2_common_plug_init(utils,
677 sizeof(sasl_server_plug_t),
678 gs2_server_plug_alloc,
679 (void **)&gs2_server_plugins,
680 &gs2_server_plugcount);
685 *pluglist = gs2_server_plugins;
686 *plugcount = gs2_server_plugcount;
691 /***************************** Client Section *****************************/
693 static int gs2_client_mech_step(void *conn_context,
694 sasl_client_params_t *params,
695 const char *serverin,
696 unsigned serverinlen,
697 sasl_interact_t **prompt_need,
698 const char **clientout,
699 unsigned *clientoutlen,
700 sasl_out_params_t *oparams)
702 context_t *text = (context_t *)conn_context;
703 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
704 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
705 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
706 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
707 OM_uint32 req_flags, ret_flags;
708 gss_OID actual_mech = GSS_C_NO_OID;
710 int initialContextToken;
715 if (text->gss_ctx == GSS_C_NO_CONTEXT) {
716 ret = gs2_ask_user_info(text, params, NULL, 0, prompt_need, oparams);
720 if (params->gss_creds == GSS_C_NO_CREDENTIAL &&
721 text->password != NULL && text->password->len != 0) {
722 gss_buffer_desc password_buf;
723 gss_buffer_desc name_buf;
724 gss_OID_set_desc mechs;
726 name_buf.length = strlen(oparams->authid);
727 name_buf.value = (void *)oparams->authid;
729 password_buf.length = text->password->len;
730 password_buf.value = text->password->data;
733 mechs.elements = (gss_OID)text->mechanism;
735 maj_stat = gss_import_name(&min_stat,
739 if (GSS_ERROR(maj_stat))
742 maj_stat = gss_acquire_cred_with_password(&min_stat,
751 if (GSS_ERROR(maj_stat))
755 initialContextToken = 1;
757 initialContextToken = 0;
759 if (text->server_name == GSS_C_NO_NAME) { /* only once */
760 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
761 name_buf.value = params->utils->malloc(name_buf.length + 1);
762 if (name_buf.value == NULL) {
766 if (params->serverFQDN == NULL ||
767 strlen(params->serverFQDN) == 0) {
768 SETERROR(text->utils, "GS2 Failure: no serverFQDN");
773 snprintf(name_buf.value, name_buf.length + 1,
774 "%s@%s", params->service, params->serverFQDN);
776 maj_stat = gss_import_name (&min_stat,
778 GSS_C_NT_HOSTBASED_SERVICE,
780 params->utils->free(name_buf.value);
781 name_buf.value = NULL;
783 if (GSS_ERROR(maj_stat))
787 /* From GSSAPI plugin: apparently this is for some IMAP bug workaround */
788 if (serverinlen == 0 && text->gss_ctx != GSS_C_NO_CONTEXT) {
789 gss_delete_sec_context(&min_stat, &text->gss_ctx, GSS_C_NO_BUFFER);
790 text->gss_ctx = GSS_C_NO_CONTEXT;
793 input_token.value = (void *)serverin;
794 input_token.length = serverinlen;
796 if (initialContextToken) {
797 if ((text->plug.client->features & SASL_FEAT_GSS_FRAMING) == 0)
798 text->gs2_flags |= GS2_NONSTD_FLAG;
800 switch (params->cbindingdisp) {
801 case SASL_CB_DISP_NONE:
802 text->gs2_flags |= GS2_CB_FLAG_N;
804 case SASL_CB_DISP_USED:
805 text->gs2_flags |= GS2_CB_FLAG_P;
807 case SASL_CB_DISP_WANT:
808 text->gs2_flags |= GS2_CB_FLAG_Y;
812 ret = gs2_make_header(text, params,
813 strcmp(oparams->user, oparams->authid) ?
814 (char *) oparams->user : NULL,
815 &text->out_buf, &text->out_buf_len);
820 req_flags = GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG;
822 maj_stat = gss_init_sec_context(&min_stat,
823 (params->gss_creds != GSS_C_NO_CREDENTIAL)
824 ? (gss_cred_id_t)params->gss_creds
825 : text->client_creds,
828 (gss_OID)text->mechanism,
832 serverinlen ? &input_token : GSS_C_NO_BUFFER,
837 if (GSS_ERROR(maj_stat))
840 ret = gs2_make_message(text, params, initialContextToken, &output_token,
841 &text->out_buf, &text->out_buf_len);
845 *clientout = text->out_buf;
846 *clientoutlen = text->out_buf_len;
848 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
853 if (text->client_name != GSS_C_NO_NAME)
854 gss_release_name(&min_stat, &text->client_name);
856 maj_stat = gss_inquire_context(&min_stat,
862 &ret_flags, /* flags */
865 if (GSS_ERROR(maj_stat))
868 if (!g_OID_equal(text->mechanism, actual_mech)) {
869 ret = SASL_WRONGMECH;
872 if ((ret_flags & req_flags) != req_flags) {
873 maj_stat = SASL_BADAUTH;
877 maj_stat = gss_display_name(&min_stat,
881 if (GSS_ERROR(maj_stat))
884 oparams->gss_peer_name = text->server_name;
885 oparams->gss_local_name = text->client_name;
886 oparams->encode = NULL;
887 oparams->decode = NULL;
888 oparams->mech_ssf = 0;
889 oparams->maxoutbuf = 0xFFFFFF;
890 oparams->doneflag = 1;
893 gss_release_buffer(&min_stat, &output_token);
894 gss_release_buffer(&min_stat, &name_buf);
895 gss_release_oid(&min_stat, &actual_mech);
897 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
898 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
901 if (ret != SASL_OK && ret != SASL_CONTINUE)
902 sasl_gs2_free_context_contents(text);
907 static int gs2_client_mech_new(void *glob_context,
908 sasl_client_params_t *params,
914 text = sasl_gs2_new_context(params->utils);
916 MEMERROR(params->utils);
920 text->gss_ctx = GSS_C_NO_CONTEXT;
921 text->client_name = GSS_C_NO_NAME;
922 text->server_creds = GSS_C_NO_CREDENTIAL;
923 text->client_creds = GSS_C_NO_CREDENTIAL;
924 text->plug.client = glob_context;
926 ret = gs2_map_sasl_name(params->utils, text->plug.client->mech_name,
928 if (ret != SASL_OK) {
929 gs2_common_mech_dispose(text, params->utils);
933 *conn_context = text;
938 static const unsigned long gs2_required_prompts[] = {
943 gs2_client_plug_alloc(const sasl_utils_t *utils,
945 gss_buffer_t sasl_name,
949 sasl_client_plug_t *cplug = (sasl_client_plug_t *)plug;
952 memset(cplug, 0, sizeof(*cplug));
954 ret = gs2_get_mech_attrs(utils, mech,
955 &cplug->security_flags,
960 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
964 cplug->mech_name = (char *)buf.value;
965 cplug->features |= SASL_FEAT_NEEDSERVERFQDN;
966 cplug->glob_context = plug;
967 cplug->mech_new = gs2_client_mech_new;
968 cplug->mech_step = gs2_client_mech_step;
969 cplug->mech_dispose = gs2_common_mech_dispose;
970 cplug->mech_free = gs2_common_mech_free;
971 cplug->required_prompts = gs2_required_prompts;
976 static sasl_client_plug_t *gs2_client_plugins;
977 static int gs2_client_plugcount;
980 gs2_client_plug_init(const sasl_utils_t *utils,
983 sasl_client_plug_t **pluglist,
991 if (maxversion < SASL_CLIENT_PLUG_VERSION)
994 *outversion = SASL_CLIENT_PLUG_VERSION;
996 if (gs2_client_plugins == NULL) {
997 ret = gs2_common_plug_init(utils,
998 sizeof(sasl_client_plug_t),
999 gs2_client_plug_alloc,
1000 (void **)&gs2_client_plugins,
1001 &gs2_client_plugcount);
1006 *pluglist = gs2_client_plugins;
1007 *plugcount = gs2_client_plugcount;
1013 * Copy header and application channel bindings to GSS channel bindings
1014 * structure in context.
1017 gs2_save_cbindings(context_t *text,
1018 gss_buffer_t header,
1019 const sasl_channel_binding_t *cbinding)
1021 gss_buffer_t gss_bindings = &text->bindings.application_data;
1025 assert(gss_bindings->value == NULL);
1028 * The application-data field MUST be set to the gs2-header, excluding
1029 * the initial [gs2-nonstd-flag ","] part, concatenated with, when a
1030 * gs2-cb-flag of "p" is used, the application's channel binding data.
1032 len = header->length;
1033 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1037 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
1039 len += cbinding->len;
1042 gss_bindings->length = len;
1043 gss_bindings->value = text->utils->malloc(len);
1044 if (gss_bindings->value == NULL)
1047 p = (unsigned char *)gss_bindings->value;
1048 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1049 memcpy(p, (unsigned char *)header->value + 2, header->length - 2);
1050 p += header->length - 2;
1052 memcpy(p, header->value, header->length);
1053 p += header->length;
1056 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
1058 memcpy(p, cbinding->data, cbinding->len);
1064 #define CHECK_REMAIN(n) do { if (remain < (n)) return SASL_BADAUTH; } while (0)
1067 * Verify gs2-header, save authzid and channel bindings to context.
1070 gs2_verify_initial_message(context_t *text,
1071 sasl_server_params_t *sparams,
1076 char *p = (char *)in;
1077 unsigned remain = inlen;
1079 gss_buffer_desc buf = GSS_C_EMPTY_BUFFER;
1081 assert(text->cbindingname == NULL);
1082 assert(text->authzid == NULL);
1085 token->value = NULL;
1087 /* minimum header includes CB flag and non-zero GSS token */
1088 CHECK_REMAIN(4); /* [pny],,. */
1090 /* non-standard GSS framing flag */
1091 if (remain > 1 && memcmp(p, "F,", 2) == 0) {
1092 text->gs2_flags |= GS2_NONSTD_FLAG;
1097 /* SASL channel bindings */
1098 CHECK_REMAIN(1); /* [pny] */
1102 CHECK_REMAIN(1); /* = */
1105 return SASL_BADAUTH;
1107 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->cbindingname);
1111 text->gs2_flags |= GS2_CB_FLAG_P;
1114 text->gs2_flags |= GS2_CB_FLAG_N;
1117 text->gs2_flags |= GS2_CB_FLAG_Y;
1121 CHECK_REMAIN(1); /* , */
1124 return SASL_BADAUTH;
1126 /* authorization identity */
1127 if (remain > 1 && memcmp(p, "a=", 2) == 0) {
1132 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->authzid);
1138 CHECK_REMAIN(1); /* , */
1141 return SASL_BADAUTH;
1143 buf.length = inlen - remain;
1144 buf.value = (void *)in;
1146 /* stash channel bindings to pass into gss_accept_sec_context() */
1147 ret = gs2_save_cbindings(text, &buf, sparams->cbinding);
1151 buf.length = remain;
1154 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1155 token->value = text->utils->malloc(buf.length);
1156 if (token->value == NULL)
1159 token->length = buf.length;
1160 memcpy(token->value, buf.value, buf.length);
1162 unsigned int token_size;
1164 /* create a properly formed GSS token */
1165 token_size = gs2_token_size(text->mechanism, buf.length);
1166 token->value = text->utils->malloc(token_size);
1167 if (token->value == NULL)
1170 token->length = token_size;
1172 p = (char *)token->value;
1173 gs2_make_token_header(text->mechanism, buf.length,
1174 (unsigned char **)&p);
1175 memcpy(p, buf.value, buf.length);
1182 * Create gs2-header, save channel bindings to context.
1185 gs2_make_header(context_t *text,
1186 sasl_client_params_t *cparams,
1187 const char *authzid,
1191 size_t required = 0;
1192 size_t wire_authzid_len = 0, cbnamelen = 0;
1193 char *wire_authzid = NULL;
1196 gss_buffer_desc buf;
1201 /* non-standard GSS framing flag */
1202 if (text->gs2_flags & GS2_NONSTD_FLAG)
1203 required += 2; /* F, */
1205 /* SASL channel bindings */
1206 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1208 if (!SASL_CB_PRESENT(cparams))
1209 return SASL_BADPARAM;
1210 cbnamelen = strlen(cparams->cbinding->name);
1211 required += 1 /*=*/ + cbnamelen;
1215 required += 2; /* [pny], */
1218 return SASL_BADPARAM;
1221 /* authorization identity */
1222 if (authzid != NULL) {
1223 ret = gs2_escape_authzid(text->utils, authzid,
1224 strlen(authzid), &wire_authzid);
1228 wire_authzid_len = strlen(wire_authzid);
1229 required += 2 /* a= */ + wire_authzid_len;
1232 required += 1; /* trailing comma */
1234 ret = _plug_buf_alloc(text->utils, out, outlen, required);
1235 if (ret != SASL_OK) {
1236 text->utils->free(wire_authzid);
1240 *out = text->out_buf;
1243 p = (char *)text->out_buf;
1244 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1248 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1251 memcpy(p + 2, cparams->cbinding->name, cbnamelen);
1262 if (wire_authzid != NULL) {
1264 memcpy(p + 2, wire_authzid, wire_authzid_len);
1265 text->utils->free(wire_authzid);
1266 p += 2 + wire_authzid_len;
1270 assert(p == (char *)text->out_buf + required);
1272 buf.length = required;
1275 ret = gs2_save_cbindings(text, &buf, cparams->cbinding);
1283 * Convert a GSS token to a GS2 one
1286 gs2_make_message(context_t *text,
1287 sasl_client_params_t *cparams __attribute__((unused)),
1288 int initialContextToken,
1293 OM_uint32 major, minor;
1294 unsigned char *mech_token_data;
1295 size_t mech_token_size;
1297 unsigned header_len = 0;
1300 mech_token_size = token->length;
1301 mech_token_data = (unsigned char *)token->value;
1303 if (initialContextToken) {
1304 header_len = *outlen;
1306 major = gs2_verify_token_header(&minor, text->mechanism,
1307 &mech_token_size, &mech_token_data,
1309 if ((major == GSS_S_DEFECTIVE_TOKEN &&
1310 (text->plug.client->features & SASL_FEAT_GSS_FRAMING)) ||
1315 ret = _plug_buf_alloc(text->utils, out, outlen,
1316 header_len + mech_token_size);
1320 p = *out + header_len;
1321 memcpy(p, mech_token_data, mech_token_size);
1323 *outlen = header_len + mech_token_size;
1329 * Map GSS mechanism attributes to SASL ones
1332 gs2_get_mech_attrs(const sasl_utils_t *utils,
1334 unsigned int *security_flags,
1335 unsigned int *features)
1337 OM_uint32 major, minor;
1339 gss_OID_set attrs = GSS_C_NO_OID_SET;
1341 major = gss_inquire_attrs_for_mech(&minor, mech, &attrs, NULL);
1342 if (GSS_ERROR(major)) {
1343 utils->seterror(utils->conn, SASL_NOLOG,
1344 "GS2 Failure: gss_inquire_attrs_for_mech");
1348 *security_flags = SASL_SEC_NOPLAINTEXT | SASL_SEC_NOACTIVE;
1349 *features = SASL_FEAT_WANT_CLIENT_FIRST | SASL_FEAT_CHANNEL_BINDING;
1351 #define MA_PRESENT(a) (gss_test_oid_set_member(&minor, (gss_OID)(a), \
1352 attrs, &present) == GSS_S_COMPLETE && \
1357 if (MA_PRESENT(GSS_C_MA_PFS))
1358 *security_flags |= SASL_SEC_FORWARD_SECRECY;
1359 if (!MA_PRESENT(GSS_C_MA_AUTH_INIT_ANON))
1360 *security_flags |= SASL_SEC_NOANONYMOUS;
1361 if (MA_PRESENT(GSS_C_MA_DELEG_CRED))
1362 *security_flags |= SASL_SEC_PASS_CREDENTIALS;
1363 if (MA_PRESENT(GSS_C_MA_AUTH_TARG))
1364 *security_flags |= SASL_SEC_MUTUAL_AUTH;
1365 if (MA_PRESENT(GSS_C_MA_ITOK_FRAMED))
1366 *features |= SASL_FEAT_GSS_FRAMING;
1368 gss_release_oid_set(&minor, &attrs);
1373 * Enumerate GSS mechanisms that can be used for GS2
1375 static int gs2_indicate_mechs(const sasl_utils_t *utils)
1377 OM_uint32 major, minor;
1378 gss_OID_desc desired_oids[3];
1379 gss_OID_set_desc desired_attrs;
1380 gss_OID_desc except_oids[3];
1381 gss_OID_set_desc except_attrs;
1383 if (gs2_mechs != GSS_C_NO_OID_SET)
1386 desired_oids[0] = *GSS_C_MA_AUTH_INIT;
1387 desired_oids[1] = *GSS_C_MA_AUTH_TARG;
1388 desired_oids[2] = *GSS_C_MA_CBINDINGS;
1389 desired_attrs.count = sizeof(desired_oids)/sizeof(desired_oids[0]);
1390 desired_attrs.elements = desired_oids;
1392 except_oids[0] = *GSS_C_MA_MECH_NEGO;
1393 except_oids[1] = *GSS_C_MA_NOT_MECH;
1394 except_oids[2] = *GSS_C_MA_DEPRECATED;
1396 except_attrs.count = sizeof(except_oids)/sizeof(except_oids[0]);
1397 except_attrs.elements = except_oids;
1399 major = gss_indicate_mechs_by_attrs(&minor,
1404 if (GSS_ERROR(major)) {
1405 utils->seterror(utils->conn, SASL_NOLOG,
1406 "GS2 Failure: gss_indicate_mechs_by_attrs");
1410 return (gs2_mechs->count > 0) ? SASL_OK : SASL_NOMECH;
1414 * Map SASL mechanism name to OID
1417 gs2_map_sasl_name(const sasl_utils_t *utils,
1421 OM_uint32 major, minor;
1422 gss_buffer_desc buf;
1424 buf.length = strlen(mech);
1425 buf.value = (void *)mech;
1427 major = gss_inquire_mech_for_saslname(&minor, &buf, oid);
1428 if (GSS_ERROR(major)) {
1429 utils->seterror(utils->conn, SASL_NOLOG,
1430 "GS2 Failure: gss_inquire_mech_for_saslname");
1438 gs2_duplicate_buffer(const sasl_utils_t *utils,
1439 const gss_buffer_t src,
1442 dst->value = utils->malloc(src->length + 1);
1443 if (dst->value == NULL)
1446 memcpy(dst->value, src->value, src->length);
1447 ((char *)dst->value)[src->length] = '\0';
1448 dst->length = src->length;
1454 gs2_unescape_authzid(const sasl_utils_t *utils,
1460 size_t i, len, inlen = *remain;
1465 for (i = 0, len = 0; i < inlen; i++) {
1470 } else if (in[i] == '=') {
1472 return SASL_BADPROT;
1478 if (len == 0 || *endp == NULL)
1479 return SASL_BADPROT;
1481 p = *authzid = utils->malloc(len + 1);
1482 if (*authzid == NULL)
1485 for (i = 0; i < inlen; i++) {
1488 else if (in[i] == '=') {
1489 if (memcmp(&in[i + 1], "2C", 2) == 0)
1491 else if (memcmp(&in[i + 1], "3D", 2) == 0)
1494 utils->free(*authzid);
1496 return SASL_BADPROT;
1509 gs2_escape_authzid(const sasl_utils_t *utils,
1517 p = *authzid = utils->malloc((inlen * 3) + 1);
1518 if (*authzid == NULL)
1521 for (i = 0; i < inlen; i++) {
1523 memcpy(p, "=2C", 3);
1525 } else if (in[i] == '=') {
1526 memcpy(p, "=3D", 3);
1539 gs2_ask_user_info(context_t *text,
1540 sasl_client_params_t *params,
1541 char **realms __attribute__((unused)),
1542 int nrealm __attribute__((unused)),
1543 sasl_interact_t **prompt_need,
1544 sasl_out_params_t *oparams)
1546 int result = SASL_OK;
1547 const char *authid = NULL, *userid = NULL;
1548 int user_result = SASL_OK;
1549 int auth_result = SASL_OK;
1550 int pass_result = SASL_OK;
1552 /* try to get the authid */
1553 if (oparams->authid == NULL) {
1554 auth_result = _plug_get_authid(params->utils, &authid, prompt_need);
1556 if (auth_result != SASL_OK && auth_result != SASL_INTERACT) {
1561 /* try to get the userid */
1562 if (oparams->user == NULL) {
1563 user_result = _plug_get_userid(params->utils, &userid, prompt_need);
1565 if (user_result != SASL_OK && user_result != SASL_INTERACT) {
1570 /* try to get the password */
1571 if (text->password == NULL) {
1572 pass_result = _plug_get_password(params->utils, &text->password,
1573 &text->free_password, prompt_need);
1574 if (pass_result != SASL_OK && pass_result != SASL_INTERACT) {
1579 /* free prompts we got */
1580 if (prompt_need && *prompt_need) {
1581 params->utils->free(*prompt_need);
1582 *prompt_need = NULL;
1585 /* if there are prompts not filled in */
1586 if (user_result == SASL_INTERACT || auth_result == SASL_INTERACT ||
1587 pass_result == SASL_INTERACT) {
1589 /* make the prompt list */
1591 _plug_make_prompts(params->utils, prompt_need,
1592 user_result == SASL_INTERACT ?
1593 "Please enter your authorization name" : NULL,
1595 auth_result == SASL_INTERACT ?
1596 "Please enter your authentication name" : NULL,
1598 pass_result == SASL_INTERACT ?
1599 "Please enter your password" : NULL, NULL,
1603 if (result == SASL_OK) return SASL_INTERACT;
1608 if (oparams->authid == NULL) {
1609 if (userid == NULL || userid[0] == '\0') {
1610 result = params->canon_user(params->utils->conn, authid, 0,
1611 SASL_CU_AUTHID | SASL_CU_AUTHZID,
1614 result = params->canon_user(params->utils->conn,
1615 authid, 0, SASL_CU_AUTHID, oparams);
1616 if (result != SASL_OK) return result;
1618 result = params->canon_user(params->utils->conn,
1619 userid, 0, SASL_CU_AUTHZID, oparams);
1621 if (result != SASL_OK)
1629 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
1632 OM_uint32 maj_stat, min_stat;
1633 gss_buffer_desc msg;
1637 unsigned int len, curlen = 0;
1638 const char prefix[] = "GSSAPI Error: ";
1640 len = sizeof(prefix);
1641 ret = _plug_buf_alloc(utils, &out, &curlen, 256);
1645 strcpy(out, prefix);
1649 maj_stat = gss_display_status(&min_stat, maj,
1650 GSS_C_GSS_CODE, GSS_C_NULL_OID,
1653 if (GSS_ERROR(maj_stat)) {
1655 utils->log(utils->conn, SASL_LOG_FAIL,
1656 "GS2 Failure: (could not get major error message)");
1658 utils->seterror(utils->conn, 0,
1660 "(could not get major error message)");
1666 len += len + msg.length;
1667 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1668 if (ret != SASL_OK) {
1673 strcat(out, msg.value);
1675 gss_release_buffer(&min_stat, &msg);
1681 /* Now get the minor status */
1684 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1685 if (ret != SASL_OK) {
1694 maj_stat = gss_display_status(&min_stat, min,
1695 GSS_C_MECH_CODE, GSS_C_NULL_OID,
1698 if (GSS_ERROR(maj_stat)) {
1700 utils->log(utils->conn, SASL_LOG_FAIL,
1701 "GS2 Failure: (could not get minor error message)");
1703 utils->seterror(utils->conn, 0,
1705 "(could not get minor error message)");
1711 len += len + msg.length;
1713 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1714 if (ret != SASL_OK) {
1719 strcat(out, msg.value);
1721 gss_release_buffer(&min_stat, &msg);
1728 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1729 if (ret != SASL_OK) {
1737 utils->log(utils->conn, SASL_LOG_FAIL, out);
1739 utils->seterror(utils->conn, 0, out);