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,
160 const unsigned long **prompts);
162 static int gs2_indicate_mechs(const sasl_utils_t *utils);
164 static int gs2_map_sasl_name(const sasl_utils_t *utils,
168 static int gs2_duplicate_buffer(const sasl_utils_t *utils,
169 const gss_buffer_t src,
172 static int gs2_unescape_authzid(const sasl_utils_t *utils,
177 static int gs2_escape_authzid(const sasl_utils_t *utils,
182 /* sasl_gs_log: only logs status string returned from gss_display_status() */
183 #define sasl_gs2_log(x,y,z) sasl_gs2_seterror_(x,y,z,1)
184 #define sasl_gs2_seterror(x,y,z) sasl_gs2_seterror_(x,y,z,0)
187 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
191 sasl_gs2_new_context(const sasl_utils_t *utils)
195 ret = utils->malloc(sizeof(context_t));
199 memset(ret, 0, sizeof(context_t));
206 sasl_gs2_free_context_contents(context_t *text)
213 if (text->gss_ctx != GSS_C_NO_CONTEXT) {
214 gss_delete_sec_context(&min_stat,&text->gss_ctx,
216 text->gss_ctx = GSS_C_NO_CONTEXT;
219 if (text->client_name != GSS_C_NO_NAME) {
220 gss_release_name(&min_stat,&text->client_name);
221 text->client_name = GSS_C_NO_NAME;
224 if (text->server_name != GSS_C_NO_NAME) {
225 gss_release_name(&min_stat,&text->server_name);
226 text->server_name = GSS_C_NO_NAME;
229 if (text->server_creds != GSS_C_NO_CREDENTIAL) {
230 gss_release_cred(&min_stat, &text->server_creds);
231 text->server_creds = GSS_C_NO_CREDENTIAL;
234 if (text->client_creds != GSS_C_NO_CREDENTIAL) {
235 gss_release_cred(&min_stat, &text->client_creds);
236 text->client_creds = GSS_C_NO_CREDENTIAL;
239 if (text->authid != NULL) {
240 text->utils->free(text->authid);
244 if (text->authzid != NULL) {
245 text->utils->free(text->authzid);
246 text->authzid = NULL;
249 gss_release_buffer(&min_stat, &text->gss_cbindings.application_data);
251 if (text->out_buf != NULL) {
252 text->utils->free(text->out_buf);
253 text->out_buf = NULL;
256 text->out_buf_len = 0;
258 if (text->cbindingname != NULL) {
259 text->utils->free(text->cbindingname);
260 text->cbindingname = NULL;
263 if (text->free_password)
264 _plug_free_secret(text->utils, &text->password);
266 memset(text, 0, sizeof(*text));
272 gs2_common_mech_dispose(void *conn_context, const sasl_utils_t *utils)
274 sasl_gs2_free_context_contents((context_t *)(conn_context));
275 utils->free(conn_context);
279 gs2_common_mech_free(void *global_context __attribute__((unused)),
280 const sasl_utils_t *utils)
284 if (gs2_mechs != GSS_C_NO_OID_SET) {
285 gss_release_oid_set(&minor, &gs2_mechs);
286 gs2_mechs = GSS_C_NO_OID_SET;
290 /***************************** Server Section *****************************/
293 gs2_server_mech_new(void *glob_context,
294 sasl_server_params_t *params,
295 const char *challenge __attribute__((unused)),
296 unsigned challen __attribute__((unused)),
302 text = sasl_gs2_new_context(params->utils);
304 MEMERROR(params->utils);
308 text->gss_ctx = GSS_C_NO_CONTEXT;
309 text->client_name = GSS_C_NO_NAME;
310 text->server_name = GSS_C_NO_NAME;
311 text->server_creds = GSS_C_NO_CREDENTIAL;
312 text->client_creds = GSS_C_NO_CREDENTIAL;
313 text->plug.server = glob_context;
315 ret = gs2_map_sasl_name(params->utils, text->plug.server->mech_name,
317 if (ret != SASL_OK) {
318 gs2_common_mech_dispose(text, params->utils);
322 *conn_context = text;
328 gs2_server_mech_step(void *conn_context,
329 sasl_server_params_t *params,
330 const char *clientin,
331 unsigned clientinlen,
332 const char **serverout,
333 unsigned *serveroutlen,
334 sasl_out_params_t *oparams)
336 context_t *text = (context_t *)conn_context;
337 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
338 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
339 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
340 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
341 gss_buffer_desc short_name_buf = GSS_C_EMPTY_BUFFER;
342 gss_name_t without = GSS_C_NO_NAME;
343 gss_OID_set_desc mechs;
344 gss_OID actual_mech = GSS_C_NO_OID;
345 OM_uint32 out_flags = 0;
346 int ret = 0, equal = 0;
347 int initialContextToken = (text->gss_ctx == GSS_C_NO_CONTEXT);
350 if (serverout == NULL) {
351 PARAMERROR(text->utils);
352 return SASL_BADPARAM;
358 if (initialContextToken) {
359 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
360 name_buf.value = params->utils->malloc(name_buf.length + 1);
361 if (name_buf.value == NULL) {
362 MEMERROR(text->utils);
366 snprintf(name_buf.value, name_buf.length + 1,
367 "%s@%s", params->service, params->serverFQDN);
368 maj_stat = gss_import_name(&min_stat,
370 GSS_C_NT_HOSTBASED_SERVICE,
372 params->utils->free(name_buf.value);
373 name_buf.value = NULL;
375 if (GSS_ERROR(maj_stat))
378 assert(text->server_creds == GSS_C_NO_CREDENTIAL);
381 mechs.elements = (gss_OID)text->mechanism;
383 if (params->gss_creds == GSS_C_NO_CREDENTIAL) {
384 maj_stat = gss_acquire_cred(&min_stat,
392 if (GSS_ERROR(maj_stat))
396 ret = gs2_verify_initial_message(text,
404 input_token.value = (void *)clientin;
405 input_token.length = clientinlen;
408 maj_stat = gss_accept_sec_context(&min_stat,
410 (params->gss_creds != GSS_C_NO_CREDENTIAL)
411 ? (gss_cred_id_t)params->gss_creds
412 : text->server_creds,
414 &text->gss_cbindings,
420 &text->client_creds);
421 if (GSS_ERROR(maj_stat)) {
422 sasl_gs2_log(text->utils, maj_stat, min_stat);
423 text->utils->seterror(text->utils->conn, SASL_NOLOG,
424 "GS2 Failure: gss_accept_sec_context");
425 ret = (maj_stat == GSS_S_BAD_BINDINGS) ? SASL_BADBINDING : SASL_BADAUTH;
429 *serveroutlen = output_token.length;
430 if (output_token.value != NULL) {
431 ret = _plug_buf_alloc(text->utils, &text->out_buf,
432 &text->out_buf_len, *serveroutlen);
435 memcpy(text->out_buf, output_token.value, *serveroutlen);
436 *serverout = text->out_buf;
438 /* No output token, send an empty string */
443 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
448 assert(maj_stat == GSS_S_COMPLETE);
451 if (!g_OID_equal(text->mechanism, actual_mech)) {
452 ret = SASL_WRONGMECH;
456 if ((out_flags & GSS_C_SEQUENCE_FLAG) == 0) {
461 maj_stat = gss_display_name(&min_stat, text->client_name,
463 if (GSS_ERROR(maj_stat))
466 ret = gs2_duplicate_buffer(params->utils, &name_buf, &short_name_buf);
470 p = (char *)memchr(name_buf.value, '@', name_buf.length);
472 short_name_buf.length = (p - (char *)name_buf.value);
474 maj_stat = gss_import_name(&min_stat,
478 if (GSS_ERROR(maj_stat)) {
483 maj_stat = gss_compare_name(&min_stat, text->client_name,
485 if (GSS_ERROR(maj_stat)) {
491 ((char *)short_name_buf.value)[short_name_buf.length] = '\0';
494 text->authid = (char *)short_name_buf.value;
495 short_name_buf.value = NULL;
496 short_name_buf.length = 0;
498 if (text->authzid != NULL) {
499 ret = params->canon_user(params->utils->conn,
501 SASL_CU_AUTHZID, oparams);
506 ret = params->canon_user(params->utils->conn,
508 text->authzid == NULL
509 ? (SASL_CU_AUTHZID | SASL_CU_AUTHID)
515 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
517 oparams->cbindingdisp = SASL_CB_DISP_NONE;
520 oparams->cbindingdisp = SASL_CB_DISP_USED;
521 oparams->cbindingname = text->cbindingname;
524 oparams->cbindingdisp = SASL_CB_DISP_WANT;
528 if (text->client_creds != GSS_C_NO_CREDENTIAL)
529 oparams->client_creds = &text->client_creds;
531 oparams->client_creds = NULL;
533 oparams->gss_peer_name = text->client_name;
534 oparams->gss_local_name = text->server_name;
535 oparams->maxoutbuf = 0xFFFFFF;
536 oparams->encode = NULL;
537 oparams->decode = NULL;
538 oparams->mech_ssf = 0;
539 oparams->doneflag = 1;
544 if (initialContextToken)
545 gss_release_buffer(&min_stat, &input_token);
546 gss_release_buffer(&min_stat, &name_buf);
547 gss_release_buffer(&min_stat, &short_name_buf);
548 gss_release_buffer(&min_stat, &output_token);
549 gss_release_name(&min_stat, &without);
551 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
552 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
555 if (ret != SASL_OK && ret != SASL_CONTINUE)
556 sasl_gs2_free_context_contents(text);
562 gs2_common_plug_init(const sasl_utils_t *utils,
564 int (*plug_alloc)(const sasl_utils_t *,
571 OM_uint32 major, minor;
578 if (gs2_indicate_mechs(utils) != SASL_OK) {
582 plugs = utils->malloc(2 * gs2_mechs->count * plugsize);
587 memset(plugs, 0, 2 * gs2_mechs->count * plugsize);
589 for (i = 0; i < gs2_mechs->count; i++) {
590 gss_buffer_desc sasl_mech_name = GSS_C_EMPTY_BUFFER;
592 major = gss_inquire_saslname_for_mech(&minor,
593 &gs2_mechs->elements[i],
597 if (GSS_ERROR(major))
600 #define PLUG_AT(index) (void *)((unsigned char *)plugs + (count * plugsize))
602 if (plug_alloc(utils, PLUG_AT(count), &sasl_mech_name,
603 &gs2_mechs->elements[i]) == SASL_OK)
606 gss_release_buffer(&minor, &sasl_mech_name);
621 gs2_server_plug_alloc(const sasl_utils_t *utils,
623 gss_buffer_t sasl_name,
627 sasl_server_plug_t *splug = (sasl_server_plug_t *)plug;
630 memset(splug, 0, sizeof(*splug));
632 ret = gs2_get_mech_attrs(utils, mech,
633 &splug->security_flags,
639 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
643 splug->mech_name = (char *)buf.value;
644 splug->glob_context = plug;
645 splug->mech_new = gs2_server_mech_new;
646 splug->mech_step = gs2_server_mech_step;
647 splug->mech_dispose = gs2_common_mech_dispose;
648 splug->mech_free = gs2_common_mech_free;
653 static sasl_server_plug_t *gs2_server_plugins;
654 static int gs2_server_plugcount;
657 gs2_server_plug_init(const sasl_utils_t *utils,
660 sasl_server_plug_t **pluglist,
668 if (maxversion < SASL_SERVER_PLUG_VERSION)
671 *outversion = SASL_SERVER_PLUG_VERSION;
673 if (gs2_server_plugins == NULL) {
674 ret = gs2_common_plug_init(utils,
675 sizeof(sasl_server_plug_t),
676 gs2_server_plug_alloc,
677 (void **)&gs2_server_plugins,
678 &gs2_server_plugcount);
683 *pluglist = gs2_server_plugins;
684 *plugcount = gs2_server_plugcount;
689 /***************************** Client Section *****************************/
691 static int gs2_client_mech_step(void *conn_context,
692 sasl_client_params_t *params,
693 const char *serverin,
694 unsigned serverinlen,
695 sasl_interact_t **prompt_need,
696 const char **clientout,
697 unsigned *clientoutlen,
698 sasl_out_params_t *oparams)
700 context_t *text = (context_t *)conn_context;
701 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
702 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
703 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
704 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
705 OM_uint32 req_flags, ret_flags;
706 gss_OID actual_mech = GSS_C_NO_OID;
708 int initialContextToken;
713 if (text->gss_ctx == GSS_C_NO_CONTEXT) {
714 ret = gs2_ask_user_info(text, params, NULL, 0, prompt_need, oparams);
718 if (params->gss_creds == GSS_C_NO_CREDENTIAL &&
719 text->password != NULL && text->password->len != 0) {
720 gss_buffer_desc password_buf;
721 gss_buffer_desc name_buf;
722 gss_OID_set_desc mechs;
724 name_buf.length = strlen(oparams->authid);
725 name_buf.value = (void *)oparams->authid;
727 password_buf.length = text->password->len;
728 password_buf.value = text->password->data;
731 mechs.elements = (gss_OID)text->mechanism;
733 maj_stat = gss_import_name(&min_stat,
737 if (GSS_ERROR(maj_stat))
740 maj_stat = gss_acquire_cred_with_password(&min_stat,
749 if (GSS_ERROR(maj_stat))
753 initialContextToken = 1;
755 initialContextToken = 0;
757 if (text->server_name == GSS_C_NO_NAME) { /* only once */
758 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
759 name_buf.value = params->utils->malloc(name_buf.length + 1);
760 if (name_buf.value == NULL) {
764 if (params->serverFQDN == NULL ||
765 strlen(params->serverFQDN) == 0) {
766 SETERROR(text->utils, "GS2 Failure: no serverFQDN");
771 snprintf(name_buf.value, name_buf.length + 1,
772 "%s@%s", params->service, params->serverFQDN);
774 maj_stat = gss_import_name (&min_stat,
776 GSS_C_NT_HOSTBASED_SERVICE,
778 params->utils->free(name_buf.value);
779 name_buf.value = NULL;
781 if (GSS_ERROR(maj_stat))
785 /* From GSSAPI plugin: apparently this is for some IMAP bug workaround */
786 if (serverinlen == 0 && text->gss_ctx != GSS_C_NO_CONTEXT) {
787 gss_delete_sec_context(&min_stat, &text->gss_ctx, GSS_C_NO_BUFFER);
788 text->gss_ctx = GSS_C_NO_CONTEXT;
791 input_token.value = (void *)serverin;
792 input_token.length = serverinlen;
794 if (initialContextToken) {
795 if ((text->plug.client->features & SASL_FEAT_GSS_FRAMING) == 0)
796 text->gs2_flags |= GS2_NONSTD_FLAG;
798 switch (params->cbindingdisp) {
799 case SASL_CB_DISP_NONE:
800 text->gs2_flags |= GS2_CB_FLAG_N;
802 case SASL_CB_DISP_USED:
803 text->gs2_flags |= GS2_CB_FLAG_P;
805 case SASL_CB_DISP_WANT:
806 text->gs2_flags |= GS2_CB_FLAG_Y;
810 ret = gs2_make_header(text, params,
811 strcmp(oparams->user, oparams->authid) ?
812 (char *) oparams->user : NULL,
813 &text->out_buf, &text->out_buf_len);
818 req_flags = GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG;
820 maj_stat = gss_init_sec_context(&min_stat,
821 (params->gss_creds != GSS_C_NO_CREDENTIAL)
822 ? (gss_cred_id_t)params->gss_creds
823 : text->client_creds,
826 (gss_OID)text->mechanism,
829 &text->gss_cbindings,
830 serverinlen ? &input_token : GSS_C_NO_BUFFER,
835 if (GSS_ERROR(maj_stat))
838 ret = gs2_make_message(text, params, initialContextToken, &output_token,
839 &text->out_buf, &text->out_buf_len);
843 *clientout = text->out_buf;
844 *clientoutlen = text->out_buf_len;
846 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
851 if (text->client_name != GSS_C_NO_NAME)
852 gss_release_name(&min_stat, &text->client_name);
854 maj_stat = gss_inquire_context(&min_stat,
860 &ret_flags, /* flags */
863 if (GSS_ERROR(maj_stat))
866 if (!g_OID_equal(text->mechanism, actual_mech)) {
867 ret = SASL_WRONGMECH;
870 if ((ret_flags & req_flags) != req_flags) {
871 maj_stat = SASL_BADAUTH;
875 maj_stat = gss_display_name(&min_stat,
879 if (GSS_ERROR(maj_stat))
882 oparams->gss_peer_name = text->server_name;
883 oparams->gss_local_name = text->client_name;
884 oparams->encode = NULL;
885 oparams->decode = NULL;
886 oparams->mech_ssf = 0;
887 oparams->maxoutbuf = 0xFFFFFF;
888 oparams->doneflag = 1;
891 gss_release_buffer(&min_stat, &output_token);
892 gss_release_buffer(&min_stat, &name_buf);
894 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
895 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
898 if (ret != SASL_OK && ret != SASL_CONTINUE)
899 sasl_gs2_free_context_contents(text);
904 static int gs2_client_mech_new(void *glob_context,
905 sasl_client_params_t *params,
911 text = sasl_gs2_new_context(params->utils);
913 MEMERROR(params->utils);
917 text->gss_ctx = GSS_C_NO_CONTEXT;
918 text->client_name = GSS_C_NO_NAME;
919 text->server_creds = GSS_C_NO_CREDENTIAL;
920 text->client_creds = GSS_C_NO_CREDENTIAL;
921 text->plug.client = glob_context;
923 ret = gs2_map_sasl_name(params->utils, text->plug.client->mech_name,
925 if (ret != SASL_OK) {
926 gs2_common_mech_dispose(text, params->utils);
930 *conn_context = text;
936 gs2_client_plug_alloc(const sasl_utils_t *utils,
938 gss_buffer_t sasl_name,
942 sasl_client_plug_t *cplug = (sasl_client_plug_t *)plug;
945 memset(cplug, 0, sizeof(*cplug));
947 ret = gs2_get_mech_attrs(utils, mech,
948 &cplug->security_flags,
950 &cplug->required_prompts);
954 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
958 cplug->mech_name = (char *)buf.value;
959 cplug->features |= SASL_FEAT_NEEDSERVERFQDN;
960 cplug->glob_context = plug;
961 cplug->mech_new = gs2_client_mech_new;
962 cplug->mech_step = gs2_client_mech_step;
963 cplug->mech_dispose = gs2_common_mech_dispose;
964 cplug->mech_free = gs2_common_mech_free;
969 static sasl_client_plug_t *gs2_client_plugins;
970 static int gs2_client_plugcount;
973 gs2_client_plug_init(const sasl_utils_t *utils,
976 sasl_client_plug_t **pluglist,
984 if (maxversion < SASL_CLIENT_PLUG_VERSION)
987 *outversion = SASL_CLIENT_PLUG_VERSION;
989 if (gs2_client_plugins == NULL) {
990 ret = gs2_common_plug_init(utils,
991 sizeof(sasl_client_plug_t),
992 gs2_client_plug_alloc,
993 (void **)&gs2_client_plugins,
994 &gs2_client_plugcount);
999 *pluglist = gs2_client_plugins;
1000 *plugcount = gs2_client_plugcount;
1006 * Copy header and application channel bindings to GSS channel bindings
1007 * structure in context.
1010 gs2_save_cbindings(context_t *text,
1011 gss_buffer_t header,
1012 const sasl_channel_binding_t *cbinding)
1014 gss_buffer_t gss_cbindings = &text->gss_cbindings.application_data;
1018 assert(gss_cbindings->value == NULL);
1021 * The application-data field MUST be set to the gs2-header, excluding
1022 * the initial [gs2-nonstd-flag ","] part, concatenated with, when a
1023 * gs2-cb-flag of "p" is used, the application's channel binding data.
1025 len = header->length;
1026 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1030 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
1032 len += cbinding->len;
1035 gss_cbindings->length = len;
1036 gss_cbindings->value = text->utils->malloc(len);
1037 if (gss_cbindings->value == NULL)
1040 p = (unsigned char *)gss_cbindings->value;
1041 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1042 memcpy(p, (unsigned char *)header->value + 2, header->length - 2);
1043 p += header->length - 2;
1045 memcpy(p, header->value, header->length);
1046 p += header->length;
1049 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
1051 memcpy(p, cbinding->data, cbinding->len);
1057 #define CHECK_REMAIN(n) do { if (remain < (n)) return SASL_BADPROT; } while (0)
1060 * Verify gs2-header, save authzid and channel bindings to context.
1063 gs2_verify_initial_message(context_t *text,
1064 sasl_server_params_t *sparams,
1069 char *p = (char *)in;
1070 unsigned remain = inlen;
1072 gss_buffer_desc buf = GSS_C_EMPTY_BUFFER;
1074 assert(text->cbindingname == NULL);
1075 assert(text->authzid == NULL);
1078 token->value = NULL;
1080 /* minimum header includes CB flag and non-zero GSS token */
1081 CHECK_REMAIN(4); /* [pny],,. */
1083 /* non-standard GSS framing flag */
1084 if (remain > 1 && memcmp(p, "F,", 2) == 0) {
1085 text->gs2_flags |= GS2_NONSTD_FLAG;
1090 /* SASL channel bindings */
1091 CHECK_REMAIN(1); /* [pny] */
1095 CHECK_REMAIN(1); /* = */
1098 return SASL_BADPROT;
1100 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->cbindingname);
1104 text->gs2_flags |= GS2_CB_FLAG_P;
1107 text->gs2_flags |= GS2_CB_FLAG_N;
1110 text->gs2_flags |= GS2_CB_FLAG_Y;
1114 CHECK_REMAIN(1); /* , */
1117 return SASL_BADPROT;
1119 /* authorization identity */
1120 if (remain > 1 && memcmp(p, "a=", 2) == 0) {
1125 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->authzid);
1131 CHECK_REMAIN(1); /* , */
1134 return SASL_BADPROT;
1136 buf.length = inlen - remain;
1137 buf.value = (void *)in;
1139 /* stash channel bindings to pass into gss_accept_sec_context() */
1140 ret = gs2_save_cbindings(text, &buf, sparams->cbinding);
1144 buf.length = remain;
1147 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1148 token->value = text->utils->malloc(buf.length);
1149 if (token->value == NULL)
1152 token->length = buf.length;
1153 memcpy(token->value, buf.value, buf.length);
1155 unsigned int token_size;
1157 /* create a properly formed GSS token */
1158 token_size = gs2_token_size(text->mechanism, buf.length);
1159 token->value = text->utils->malloc(token_size);
1160 if (token->value == NULL)
1163 token->length = token_size;
1165 p = (char *)token->value;
1166 gs2_make_token_header(text->mechanism, buf.length,
1167 (unsigned char **)&p);
1168 memcpy(p, buf.value, buf.length);
1175 * Create gs2-header, save channel bindings to context.
1178 gs2_make_header(context_t *text,
1179 sasl_client_params_t *cparams,
1180 const char *authzid,
1184 size_t required = 0;
1185 size_t wire_authzid_len = 0, cbnamelen = 0;
1186 char *wire_authzid = NULL;
1189 gss_buffer_desc buf;
1194 /* non-standard GSS framing flag */
1195 if (text->gs2_flags & GS2_NONSTD_FLAG)
1196 required += 2; /* F, */
1198 /* SASL channel bindings */
1199 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1201 if (!SASL_CB_PRESENT(cparams))
1202 return SASL_BADPARAM;
1203 cbnamelen = strlen(cparams->cbinding->name);
1204 required += 1 /*=*/ + cbnamelen;
1208 required += 2; /* [pny], */
1211 return SASL_BADPARAM;
1214 /* authorization identity */
1215 if (authzid != NULL) {
1216 ret = gs2_escape_authzid(text->utils, authzid,
1217 strlen(authzid), &wire_authzid);
1221 wire_authzid_len = strlen(wire_authzid);
1222 required += 2 /* a= */ + wire_authzid_len;
1225 required += 1; /* trailing comma */
1227 ret = _plug_buf_alloc(text->utils, out, outlen, required);
1228 if (ret != SASL_OK) {
1229 text->utils->free(wire_authzid);
1233 *out = text->out_buf;
1236 p = (char *)text->out_buf;
1237 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1241 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1244 memcpy(p + 2, cparams->cbinding->name, cbnamelen);
1255 if (wire_authzid != NULL) {
1257 memcpy(p + 2, wire_authzid, wire_authzid_len);
1258 text->utils->free(wire_authzid);
1259 p += 2 + wire_authzid_len;
1263 assert(p == (char *)text->out_buf + required);
1265 buf.length = required;
1268 ret = gs2_save_cbindings(text, &buf, cparams->cbinding);
1276 * Convert a GSS token to a GS2 one
1279 gs2_make_message(context_t *text,
1280 sasl_client_params_t *cparams __attribute__((unused)),
1281 int initialContextToken,
1286 OM_uint32 major, minor;
1287 unsigned char *mech_token_data;
1288 size_t mech_token_size;
1290 unsigned header_len = 0;
1293 mech_token_size = token->length;
1294 mech_token_data = (unsigned char *)token->value;
1296 if (initialContextToken) {
1297 header_len = *outlen;
1299 major = gs2_verify_token_header(&minor, text->mechanism,
1300 &mech_token_size, &mech_token_data,
1302 if ((major == GSS_S_DEFECTIVE_TOKEN &&
1303 (text->plug.client->features & SASL_FEAT_GSS_FRAMING)) ||
1308 ret = _plug_buf_alloc(text->utils, out, outlen,
1309 header_len + mech_token_size);
1313 p = *out + header_len;
1314 memcpy(p, mech_token_data, mech_token_size);
1316 *outlen = header_len + mech_token_size;
1321 static const unsigned long gs2_required_prompts[] = {
1326 * Map GSS mechanism attributes to SASL ones
1329 gs2_get_mech_attrs(const sasl_utils_t *utils,
1331 unsigned int *security_flags,
1332 unsigned int *features,
1333 const unsigned long **prompts)
1335 OM_uint32 major, minor;
1337 gss_OID_set attrs = GSS_C_NO_OID_SET;
1339 major = gss_inquire_attrs_for_mech(&minor, mech, &attrs, NULL);
1340 if (GSS_ERROR(major)) {
1341 utils->seterror(utils->conn, SASL_NOLOG,
1342 "GS2 Failure: gss_inquire_attrs_for_mech");
1346 *security_flags = SASL_SEC_NOPLAINTEXT | SASL_SEC_NOACTIVE;
1347 *features = SASL_FEAT_WANT_CLIENT_FIRST | SASL_FEAT_CHANNEL_BINDING;
1348 if (prompts != NULL)
1349 *prompts = gs2_required_prompts;
1351 #define MA_PRESENT(a) (gss_test_oid_set_member(&minor, (gss_OID)(a), \
1352 attrs, &present) == GSS_S_COMPLETE && \
1355 if (MA_PRESENT(GSS_C_MA_PFS))
1356 *security_flags |= SASL_SEC_FORWARD_SECRECY;
1357 if (!MA_PRESENT(GSS_C_MA_AUTH_INIT_ANON))
1358 *security_flags |= SASL_SEC_NOANONYMOUS;
1359 if (MA_PRESENT(GSS_C_MA_DELEG_CRED))
1360 *security_flags |= SASL_SEC_PASS_CREDENTIALS;
1361 if (MA_PRESENT(GSS_C_MA_AUTH_TARG))
1362 *security_flags |= SASL_SEC_MUTUAL_AUTH;
1363 if (MA_PRESENT(GSS_C_MA_AUTH_INIT_INIT) && prompts != NULL)
1365 if (MA_PRESENT(GSS_C_MA_ITOK_FRAMED))
1366 *features |= SASL_FEAT_GSS_FRAMING;
1368 gss_release_oid_set(&minor, &attrs);
1374 * Enumerate GSS mechanisms that can be used for GS2
1376 static int gs2_indicate_mechs(const sasl_utils_t *utils)
1378 OM_uint32 major, minor;
1379 gss_OID_desc desired_oids[3];
1380 gss_OID_set_desc desired_attrs;
1381 gss_OID_desc except_oids[3];
1382 gss_OID_set_desc except_attrs;
1384 if (gs2_mechs != GSS_C_NO_OID_SET)
1387 desired_oids[0] = *GSS_C_MA_AUTH_INIT;
1388 desired_oids[1] = *GSS_C_MA_AUTH_TARG;
1389 desired_oids[2] = *GSS_C_MA_CBINDINGS;
1390 desired_attrs.count = sizeof(desired_oids)/sizeof(desired_oids[0]);
1391 desired_attrs.elements = desired_oids;
1393 except_oids[0] = *GSS_C_MA_MECH_NEGO;
1394 except_oids[1] = *GSS_C_MA_NOT_MECH;
1395 except_oids[2] = *GSS_C_MA_DEPRECATED;
1397 except_attrs.count = sizeof(except_oids)/sizeof(except_oids[0]);
1398 except_attrs.elements = except_oids;
1400 major = gss_indicate_mechs_by_attrs(&minor,
1405 if (GSS_ERROR(major)) {
1406 utils->seterror(utils->conn, SASL_NOLOG,
1407 "GS2 Failure: gss_indicate_mechs_by_attrs");
1411 return (gs2_mechs->count > 0) ? SASL_OK : SASL_NOMECH;
1415 * Map SASL mechanism name to OID
1418 gs2_map_sasl_name(const sasl_utils_t *utils,
1422 OM_uint32 major, minor;
1423 gss_buffer_desc buf;
1425 buf.length = strlen(mech);
1426 buf.value = (void *)mech;
1428 major = gss_inquire_mech_for_saslname(&minor, &buf, oid);
1429 if (GSS_ERROR(major)) {
1430 utils->seterror(utils->conn, SASL_NOLOG,
1431 "GS2 Failure: gss_inquire_mech_for_saslname");
1439 gs2_duplicate_buffer(const sasl_utils_t *utils,
1440 const gss_buffer_t src,
1443 dst->value = utils->malloc(src->length + 1);
1444 if (dst->value == NULL)
1447 memcpy(dst->value, src->value, src->length);
1448 ((char *)dst->value)[src->length] = '\0';
1449 dst->length = src->length;
1455 gs2_unescape_authzid(const sasl_utils_t *utils,
1461 size_t i, len, inlen = *remain;
1466 for (i = 0, len = 0; i < inlen; i++) {
1471 } else if (in[i] == '=') {
1473 return SASL_BADPROT;
1479 if (len == 0 || *endp == NULL)
1480 return SASL_BADPROT;
1482 p = *authzid = utils->malloc(len + 1);
1483 if (*authzid == NULL)
1486 for (i = 0; i < inlen; i++) {
1489 else if (in[i] == '=') {
1490 if (memcmp(&in[i + 1], "2C", 2) == 0)
1492 else if (memcmp(&in[i + 1], "3D", 2) == 0)
1495 utils->free(*authzid);
1497 return SASL_BADPROT;
1510 gs2_escape_authzid(const sasl_utils_t *utils,
1518 p = *authzid = utils->malloc((inlen * 3) + 1);
1519 if (*authzid == NULL)
1522 for (i = 0; i < inlen; i++) {
1524 memcpy(p, "=2C", 3);
1526 } else if (in[i] == '=') {
1527 memcpy(p, "=3D", 3);
1540 gs2_ask_user_info(context_t *text,
1541 sasl_client_params_t *params,
1542 char **realms __attribute__((unused)),
1543 int nrealm __attribute__((unused)),
1544 sasl_interact_t **prompt_need,
1545 sasl_out_params_t *oparams)
1547 int result = SASL_OK;
1548 const char *authid = NULL, *userid = NULL;
1549 int user_result = SASL_OK;
1550 int auth_result = SASL_OK;
1551 int pass_result = SASL_OK;
1553 /* try to get the authid */
1554 if (oparams->authid == NULL) {
1555 auth_result = _plug_get_authid(params->utils, &authid, prompt_need);
1557 if (auth_result != SASL_OK && auth_result != SASL_INTERACT) {
1562 /* try to get the userid */
1563 if (oparams->user == NULL) {
1564 user_result = _plug_get_userid(params->utils, &userid, prompt_need);
1566 if (user_result != SASL_OK && user_result != SASL_INTERACT) {
1571 /* try to get the password */
1572 if (text->password == NULL) {
1573 pass_result = _plug_get_password(params->utils, &text->password,
1574 &text->free_password, prompt_need);
1575 if (pass_result != SASL_OK && pass_result != SASL_INTERACT) {
1580 /* free prompts we got */
1581 if (prompt_need && *prompt_need) {
1582 params->utils->free(*prompt_need);
1583 *prompt_need = NULL;
1586 /* if there are prompts not filled in */
1587 if (user_result == SASL_INTERACT || auth_result == SASL_INTERACT ||
1588 pass_result == SASL_INTERACT) {
1590 /* make the prompt list */
1592 _plug_make_prompts(params->utils, prompt_need,
1593 user_result == SASL_INTERACT ?
1594 "Please enter your authorization name" : NULL,
1596 auth_result == SASL_INTERACT ?
1597 "Please enter your authentication name" : NULL,
1599 pass_result == SASL_INTERACT ?
1600 "Please enter your password" : NULL, NULL,
1604 if (result == SASL_OK) return SASL_INTERACT;
1609 if (oparams->authid == NULL) {
1610 if (userid == NULL || userid[0] == '\0') {
1611 result = params->canon_user(params->utils->conn, authid, 0,
1612 SASL_CU_AUTHID | SASL_CU_AUTHZID,
1615 result = params->canon_user(params->utils->conn,
1616 authid, 0, SASL_CU_AUTHID, oparams);
1617 if (result != SASL_OK) return result;
1619 result = params->canon_user(params->utils->conn,
1620 userid, 0, SASL_CU_AUTHZID, oparams);
1622 if (result != SASL_OK)
1630 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
1633 OM_uint32 maj_stat, min_stat;
1634 gss_buffer_desc msg;
1638 unsigned int len, curlen = 0;
1639 const char prefix[] = "GSSAPI Error: ";
1641 len = sizeof(prefix);
1642 ret = _plug_buf_alloc(utils, &out, &curlen, 256);
1646 strcpy(out, prefix);
1650 maj_stat = gss_display_status(&min_stat, maj,
1651 GSS_C_GSS_CODE, GSS_C_NULL_OID,
1654 if (GSS_ERROR(maj_stat)) {
1656 utils->log(utils->conn, SASL_LOG_FAIL,
1657 "GS2 Failure: (could not get major error message)");
1659 utils->seterror(utils->conn, 0,
1661 "(could not get major error message)");
1667 len += len + msg.length;
1668 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1669 if (ret != SASL_OK) {
1674 strcat(out, msg.value);
1676 gss_release_buffer(&min_stat, &msg);
1682 /* Now get the minor status */
1685 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1686 if (ret != SASL_OK) {
1695 maj_stat = gss_display_status(&min_stat, min,
1696 GSS_C_MECH_CODE, GSS_C_NULL_OID,
1699 if (GSS_ERROR(maj_stat)) {
1701 utils->log(utils->conn, SASL_LOG_FAIL,
1702 "GS2 Failure: (could not get minor error message)");
1704 utils->seterror(utils->conn, 0,
1706 "(could not get minor error message)");
1712 len += len + msg.length;
1714 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1715 if (ret != SASL_OK) {
1720 strcat(out, msg.value);
1722 gss_release_buffer(&min_stat, &msg);
1729 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1730 if (ret != SASL_OK) {
1738 utils->log(utils->conn, SASL_LOG_FAIL, out);
1740 utils->seterror(utils->conn, 0, out);