2 * Copyright (c) 2010, JANET(UK)
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. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1998-2003 Carnegie Mellon University.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
48 * 3. The name "Carnegie Mellon University" must not be used to
49 * endorse or promote products derived from this software without
50 * prior written permission. For permission or any other legal
51 * details, please contact
52 * Office of Technology Transfer
53 * Carnegie Mellon University
55 * Pittsburgh, PA 15213-3890
56 * (412) 268-4387, fax: (412) 268-7395
57 * tech-transfer@andrew.cmu.edu
59 * 4. Redistributions of any form whatsoever must retain the following
61 * "This product includes software developed by Computing Services
62 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
64 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
65 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
66 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
67 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
69 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
70 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
74 #include <gssapi/gssapi.h>
75 #ifdef HAVE_GSSAPI_GSSAPI_EXT_H
76 #include <gssapi/gssapi_ext.h>
84 #include "plugin_common.h"
92 #include "gs2_token.h"
94 #define GS2_CB_FLAG_MASK 0x0F
95 #define GS2_CB_FLAG_N 0x00
96 #define GS2_CB_FLAG_P 0x01
97 #define GS2_CB_FLAG_Y 0x02
98 #define GS2_NONSTD_FLAG 0x10
100 typedef struct context {
101 gss_ctx_id_t gss_ctx;
102 gss_name_t client_name;
103 gss_name_t server_name;
104 gss_cred_id_t server_creds;
105 gss_cred_id_t client_creds;
107 unsigned out_buf_len;
108 const sasl_utils_t *utils;
112 sasl_client_plug_t *client;
113 sasl_server_plug_t *server;
118 struct gss_channel_bindings_struct gss_cbindings;
119 sasl_secret_t *password;
120 unsigned int free_password;
124 static gss_OID_set gs2_mechs = GSS_C_NO_OID_SET;
126 static int gs2_get_init_creds(context_t *context,
127 sasl_client_params_t *params,
128 sasl_interact_t **prompt_need,
129 sasl_out_params_t *oparams);
131 static int gs2_verify_initial_message(context_t *text,
132 sasl_server_params_t *sparams,
137 static int gs2_make_header(context_t *text,
138 sasl_client_params_t *cparams,
143 static int gs2_make_message(context_t *text,
144 sasl_client_params_t *cparams,
145 int initialContextToken,
150 static int gs2_get_mech_attrs(const sasl_utils_t *utils,
152 unsigned int *security_flags,
153 unsigned int *features,
154 const unsigned long **prompts);
156 static int gs2_indicate_mechs(const sasl_utils_t *utils);
158 static int gs2_map_sasl_name(const sasl_utils_t *utils,
162 static int gs2_duplicate_buffer(const sasl_utils_t *utils,
163 const gss_buffer_t src,
166 static int gs2_unescape_authzid(const sasl_utils_t *utils,
171 static int gs2_escape_authzid(const sasl_utils_t *utils,
176 /* sasl_gs_log: only logs status string returned from gss_display_status() */
177 #define sasl_gs2_log(x,y,z) sasl_gs2_seterror_(x,y,z,1)
178 #define sasl_gs2_seterror(x,y,z) sasl_gs2_seterror_(x,y,z,0)
181 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
185 sasl_gs2_new_context(const sasl_utils_t *utils)
189 ret = utils->malloc(sizeof(context_t));
193 memset(ret, 0, sizeof(context_t));
200 sasl_gs2_free_context_contents(context_t *text)
207 if (text->gss_ctx != GSS_C_NO_CONTEXT) {
208 gss_delete_sec_context(&min_stat,&text->gss_ctx,
210 text->gss_ctx = GSS_C_NO_CONTEXT;
213 if (text->client_name != GSS_C_NO_NAME) {
214 gss_release_name(&min_stat,&text->client_name);
215 text->client_name = GSS_C_NO_NAME;
218 if (text->server_name != GSS_C_NO_NAME) {
219 gss_release_name(&min_stat,&text->server_name);
220 text->server_name = GSS_C_NO_NAME;
223 if (text->server_creds != GSS_C_NO_CREDENTIAL) {
224 gss_release_cred(&min_stat, &text->server_creds);
225 text->server_creds = GSS_C_NO_CREDENTIAL;
228 if (text->client_creds != GSS_C_NO_CREDENTIAL) {
229 gss_release_cred(&min_stat, &text->client_creds);
230 text->client_creds = GSS_C_NO_CREDENTIAL;
233 if (text->authid != NULL) {
234 text->utils->free(text->authid);
238 if (text->authzid != NULL) {
239 text->utils->free(text->authzid);
240 text->authzid = NULL;
243 gss_release_buffer(&min_stat, &text->gss_cbindings.application_data);
245 if (text->out_buf != NULL) {
246 text->utils->free(text->out_buf);
247 text->out_buf = NULL;
250 text->out_buf_len = 0;
252 if (text->cbindingname != NULL) {
253 text->utils->free(text->cbindingname);
254 text->cbindingname = NULL;
257 if (text->free_password)
258 _plug_free_secret(text->utils, &text->password);
260 memset(text, 0, sizeof(*text));
266 gs2_common_mech_dispose(void *conn_context, const sasl_utils_t *utils)
268 sasl_gs2_free_context_contents((context_t *)(conn_context));
269 utils->free(conn_context);
273 gs2_common_mech_free(void *global_context __attribute__((unused)),
274 const sasl_utils_t *utils)
278 if (gs2_mechs != GSS_C_NO_OID_SET) {
279 gss_release_oid_set(&minor, &gs2_mechs);
280 gs2_mechs = GSS_C_NO_OID_SET;
284 /***************************** Server Section *****************************/
287 gs2_server_mech_new(void *glob_context,
288 sasl_server_params_t *params,
289 const char *challenge __attribute__((unused)),
290 unsigned challen __attribute__((unused)),
296 text = sasl_gs2_new_context(params->utils);
298 MEMERROR(params->utils);
302 text->gss_ctx = GSS_C_NO_CONTEXT;
303 text->client_name = GSS_C_NO_NAME;
304 text->server_name = GSS_C_NO_NAME;
305 text->server_creds = GSS_C_NO_CREDENTIAL;
306 text->client_creds = GSS_C_NO_CREDENTIAL;
307 text->plug.server = glob_context;
309 ret = gs2_map_sasl_name(params->utils, text->plug.server->mech_name,
311 if (ret != SASL_OK) {
312 gs2_common_mech_dispose(text, params->utils);
316 *conn_context = text;
322 gs2_server_mech_step(void *conn_context,
323 sasl_server_params_t *params,
324 const char *clientin,
325 unsigned clientinlen,
326 const char **serverout,
327 unsigned *serveroutlen,
328 sasl_out_params_t *oparams)
330 context_t *text = (context_t *)conn_context;
331 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
332 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
333 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
334 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
335 gss_buffer_desc short_name_buf = GSS_C_EMPTY_BUFFER;
336 gss_name_t without = GSS_C_NO_NAME;
337 gss_OID_set_desc mechs;
338 OM_uint32 out_flags = 0;
339 int ret = 0, equal = 0;
340 int initialContextToken = (text->gss_ctx == GSS_C_NO_CONTEXT);
343 if (serverout == NULL) {
344 PARAMERROR(text->utils);
345 return SASL_BADPARAM;
351 if (initialContextToken) {
352 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
353 name_buf.value = params->utils->malloc(name_buf.length + 1);
354 if (name_buf.value == NULL) {
355 MEMERROR(text->utils);
359 snprintf(name_buf.value, name_buf.length + 1,
360 "%s@%s", params->service, params->serverFQDN);
361 maj_stat = gss_import_name(&min_stat,
363 GSS_C_NT_HOSTBASED_SERVICE,
365 params->utils->free(name_buf.value);
366 name_buf.value = NULL;
368 if (GSS_ERROR(maj_stat))
371 assert(text->server_creds == GSS_C_NO_CREDENTIAL);
374 mechs.elements = (gss_OID)text->mechanism;
376 if (params->gss_creds == GSS_C_NO_CREDENTIAL) {
377 maj_stat = gss_acquire_cred(&min_stat,
385 if (GSS_ERROR(maj_stat))
389 ret = gs2_verify_initial_message(text,
397 input_token.value = (void *)clientin;
398 input_token.length = clientinlen;
401 maj_stat = gss_accept_sec_context(&min_stat,
403 (params->gss_creds != GSS_C_NO_CREDENTIAL)
404 ? (gss_cred_id_t)params->gss_creds
405 : text->server_creds,
407 &text->gss_cbindings,
413 &text->client_creds);
414 if (GSS_ERROR(maj_stat)) {
415 sasl_gs2_log(text->utils, maj_stat, min_stat);
416 text->utils->seterror(text->utils->conn, SASL_NOLOG,
417 "GS2 Failure: gss_accept_sec_context");
418 ret = (maj_stat == GSS_S_BAD_BINDINGS) ? SASL_BADBINDING : SASL_BADAUTH;
422 *serveroutlen = output_token.length;
423 if (output_token.value != NULL) {
424 ret = _plug_buf_alloc(text->utils, &text->out_buf,
425 &text->out_buf_len, *serveroutlen);
428 memcpy(text->out_buf, output_token.value, *serveroutlen);
429 *serverout = text->out_buf;
431 /* No output token, send an empty string */
436 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
441 assert(maj_stat == GSS_S_COMPLETE);
443 maj_stat = gss_display_name(&min_stat, text->client_name,
445 if (GSS_ERROR(maj_stat))
448 ret = gs2_duplicate_buffer(params->utils, &name_buf, &short_name_buf);
452 p = (char *)memchr(name_buf.value, '@', name_buf.length);
454 short_name_buf.length = (p - (char *)name_buf.value);
456 maj_stat = gss_import_name(&min_stat,
460 if (GSS_ERROR(maj_stat)) {
465 maj_stat = gss_compare_name(&min_stat, text->client_name,
467 if (GSS_ERROR(maj_stat)) {
473 ((char *)short_name_buf.value)[short_name_buf.length] = '\0';
476 text->authid = (char *)short_name_buf.value;
477 short_name_buf.value = NULL;
478 short_name_buf.length = 0;
480 if (text->authzid != NULL) {
481 ret = params->canon_user(params->utils->conn,
483 SASL_CU_AUTHZID, oparams);
488 ret = params->canon_user(params->utils->conn,
490 text->authzid == NULL
491 ? (SASL_CU_AUTHZID | SASL_CU_AUTHID)
497 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
499 oparams->cbindingdisp = SASL_CB_DISP_NONE;
502 oparams->cbindingdisp = SASL_CB_DISP_USED;
503 oparams->cbindingname = text->cbindingname;
506 oparams->cbindingdisp = SASL_CB_DISP_WANT;
510 if (text->client_creds != GSS_C_NO_CREDENTIAL)
511 oparams->client_creds = &text->client_creds;
513 oparams->client_creds = NULL;
515 oparams->gss_peer_name = text->client_name;
516 oparams->gss_local_name = text->server_name;
517 oparams->maxoutbuf = 0xFFFFFF;
518 oparams->encode = NULL;
519 oparams->decode = NULL;
520 oparams->mech_ssf = 0;
521 oparams->doneflag = 1;
526 if (initialContextToken)
527 gss_release_buffer(&min_stat, &input_token);
528 gss_release_buffer(&min_stat, &name_buf);
529 gss_release_buffer(&min_stat, &short_name_buf);
530 gss_release_buffer(&min_stat, &output_token);
531 gss_release_name(&min_stat, &without);
533 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
534 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
538 sasl_gs2_free_context_contents(text);
544 gs2_common_plug_init(const sasl_utils_t *utils,
546 int (*plug_alloc)(const sasl_utils_t *,
553 OM_uint32 major, minor;
560 if (gs2_indicate_mechs(utils) != SASL_OK) {
564 plugs = utils->malloc(gs2_mechs->count * plugsize);
569 memset(plugs, 0, gs2_mechs->count * plugsize);
571 for (i = 0; i < gs2_mechs->count; i++) {
572 gss_buffer_desc sasl_mech_name = GSS_C_EMPTY_BUFFER;
574 major = gss_inquire_saslname_for_mech(&minor,
575 &gs2_mechs->elements[i],
579 if (GSS_ERROR(major))
582 #define PLUG_AT(index) (void *)((unsigned char *)plugs + (count * plugsize))
584 if (plug_alloc(utils, PLUG_AT(count), &sasl_mech_name,
585 &gs2_mechs->elements[i]) == SASL_OK)
588 gss_release_buffer(&minor, &sasl_mech_name);
603 gs2_server_plug_alloc(const sasl_utils_t *utils,
605 gss_buffer_t sasl_name,
609 sasl_server_plug_t *splug = (sasl_server_plug_t *)plug;
612 memset(splug, 0, sizeof(*splug));
614 ret = gs2_get_mech_attrs(utils, mech,
615 &splug->security_flags,
621 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
625 splug->mech_name = (char *)buf.value;
626 splug->glob_context = plug;
627 splug->mech_new = gs2_server_mech_new;
628 splug->mech_step = gs2_server_mech_step;
629 splug->mech_dispose = gs2_common_mech_dispose;
630 splug->mech_free = gs2_common_mech_free;
635 static sasl_server_plug_t *gs2_server_plugins;
636 static int gs2_server_plugcount;
639 gs2_server_plug_init(const sasl_utils_t *utils,
642 sasl_server_plug_t **pluglist,
650 if (maxversion < SASL_SERVER_PLUG_VERSION)
653 *outversion = SASL_SERVER_PLUG_VERSION;
655 if (gs2_server_plugins == NULL) {
656 ret = gs2_common_plug_init(utils,
657 sizeof(sasl_server_plug_t),
658 gs2_server_plug_alloc,
659 (void **)&gs2_server_plugins,
660 &gs2_server_plugcount);
665 *pluglist = gs2_server_plugins;
666 *plugcount = gs2_server_plugcount;
671 /***************************** Client Section *****************************/
673 static int gs2_client_mech_step(void *conn_context,
674 sasl_client_params_t *params,
675 const char *serverin,
676 unsigned serverinlen,
677 sasl_interact_t **prompt_need,
678 const char **clientout,
679 unsigned *clientoutlen,
680 sasl_out_params_t *oparams)
682 context_t *text = (context_t *)conn_context;
683 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
684 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
685 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
686 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
689 int initialContextToken;
694 if (text->gss_ctx == GSS_C_NO_CONTEXT) {
695 ret = gs2_get_init_creds(text, params, prompt_need, oparams);
699 initialContextToken = 1;
701 initialContextToken = 0;
703 if (text->server_name == GSS_C_NO_NAME) { /* only once */
704 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
705 name_buf.value = params->utils->malloc(name_buf.length + 1);
706 if (name_buf.value == NULL) {
710 if (params->serverFQDN == NULL ||
711 strlen(params->serverFQDN) == 0) {
712 SETERROR(text->utils, "GS2 Failure: no serverFQDN");
717 snprintf(name_buf.value, name_buf.length + 1,
718 "%s@%s", params->service, params->serverFQDN);
720 maj_stat = gss_import_name(&min_stat,
722 GSS_C_NT_HOSTBASED_SERVICE,
724 params->utils->free(name_buf.value);
725 name_buf.value = NULL;
727 if (GSS_ERROR(maj_stat))
731 /* From GSSAPI plugin: apparently this is for some IMAP bug workaround */
732 if (serverinlen == 0 && text->gss_ctx != GSS_C_NO_CONTEXT) {
733 gss_delete_sec_context(&min_stat, &text->gss_ctx, GSS_C_NO_BUFFER);
734 text->gss_ctx = GSS_C_NO_CONTEXT;
737 input_token.value = (void *)serverin;
738 input_token.length = serverinlen;
740 if (initialContextToken) {
741 if ((text->plug.client->features & SASL_FEAT_GSS_FRAMING) == 0)
742 text->gs2_flags |= GS2_NONSTD_FLAG;
744 switch (params->cbindingdisp) {
745 case SASL_CB_DISP_NONE:
746 text->gs2_flags |= GS2_CB_FLAG_N;
748 case SASL_CB_DISP_USED:
749 text->gs2_flags |= GS2_CB_FLAG_P;
751 case SASL_CB_DISP_WANT:
752 text->gs2_flags |= GS2_CB_FLAG_Y;
756 ret = gs2_make_header(text, params,
757 strcmp(oparams->user, oparams->authid) ?
758 (char *) oparams->user : NULL,
759 &text->out_buf, &text->out_buf_len);
764 maj_stat = gss_init_sec_context(&min_stat,
765 (params->gss_creds != GSS_C_NO_CREDENTIAL)
766 ? (gss_cred_id_t)params->gss_creds
767 : text->client_creds,
770 (gss_OID)text->mechanism,
773 &text->gss_cbindings,
774 serverinlen ? &input_token : GSS_C_NO_BUFFER,
779 if (GSS_ERROR(maj_stat))
782 ret = gs2_make_message(text, params, initialContextToken, &output_token,
783 &text->out_buf, &text->out_buf_len);
787 *clientout = text->out_buf;
788 *clientoutlen = text->out_buf_len;
790 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
795 if (text->client_name != GSS_C_NO_NAME)
796 gss_release_name(&min_stat, &text->client_name);
798 maj_stat = gss_inquire_context(&min_stat,
804 &ret_flags, /* flags */
807 if (GSS_ERROR(maj_stat))
810 if (params->cbindingdisp != SASL_CB_DISP_NONE &&
811 (ret_flags & GSS_C_MUTUAL_FLAG) == 0) {
812 maj_stat = SASL_BADAUTH;
816 maj_stat = gss_display_name(&min_stat,
820 if (GSS_ERROR(maj_stat))
823 oparams->gss_peer_name = text->server_name;
824 oparams->gss_local_name = text->client_name;
825 oparams->encode = NULL;
826 oparams->decode = NULL;
827 oparams->mech_ssf = 0;
828 oparams->maxoutbuf = 0xFFFFFF;
829 oparams->doneflag = 1;
832 gss_release_buffer(&min_stat, &output_token);
833 gss_release_buffer(&min_stat, &name_buf);
835 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
836 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
840 sasl_gs2_free_context_contents(text);
845 static int gs2_client_mech_new(void *glob_context,
846 sasl_client_params_t *params,
852 text = sasl_gs2_new_context(params->utils);
854 MEMERROR(params->utils);
858 text->gss_ctx = GSS_C_NO_CONTEXT;
859 text->client_name = GSS_C_NO_NAME;
860 text->server_creds = GSS_C_NO_CREDENTIAL;
861 text->client_creds = GSS_C_NO_CREDENTIAL;
862 text->plug.client = glob_context;
864 ret = gs2_map_sasl_name(params->utils, text->plug.client->mech_name,
866 if (ret != SASL_OK) {
867 gs2_common_mech_dispose(text, params->utils);
871 *conn_context = text;
877 gs2_client_plug_alloc(const sasl_utils_t *utils,
879 gss_buffer_t sasl_name,
883 sasl_client_plug_t *cplug = (sasl_client_plug_t *)plug;
886 memset(cplug, 0, sizeof(*cplug));
888 ret = gs2_get_mech_attrs(utils, mech,
889 &cplug->security_flags,
891 &cplug->required_prompts);
895 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
899 cplug->mech_name = (char *)buf.value;
900 cplug->features |= SASL_FEAT_NEEDSERVERFQDN;
901 cplug->glob_context = plug;
902 cplug->mech_new = gs2_client_mech_new;
903 cplug->mech_step = gs2_client_mech_step;
904 cplug->mech_dispose = gs2_common_mech_dispose;
905 cplug->mech_free = gs2_common_mech_free;
910 static sasl_client_plug_t *gs2_client_plugins;
911 static int gs2_client_plugcount;
914 gs2_client_plug_init(const sasl_utils_t *utils,
917 sasl_client_plug_t **pluglist,
925 if (maxversion < SASL_CLIENT_PLUG_VERSION)
928 *outversion = SASL_CLIENT_PLUG_VERSION;
930 if (gs2_client_plugins == NULL) {
931 ret = gs2_common_plug_init(utils,
932 sizeof(sasl_client_plug_t),
933 gs2_client_plug_alloc,
934 (void **)&gs2_client_plugins,
935 &gs2_client_plugcount);
940 *pluglist = gs2_client_plugins;
941 *plugcount = gs2_client_plugcount;
947 * Copy header and application channel bindings to GSS channel bindings
948 * structure in context.
951 gs2_save_cbindings(context_t *text,
953 const sasl_channel_binding_t *cbinding)
955 gss_buffer_t gss_cbindings = &text->gss_cbindings.application_data;
959 assert(gss_cbindings->value == NULL);
962 * The application-data field MUST be set to the gs2-header, excluding
963 * the initial [gs2-nonstd-flag ","] part, concatenated with, when a
964 * gs2-cb-flag of "p" is used, the application's channel binding data.
966 len = header->length;
967 if (text->gs2_flags & GS2_NONSTD_FLAG) {
971 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
973 len += cbinding->len;
976 gss_cbindings->length = len;
977 gss_cbindings->value = text->utils->malloc(len);
978 if (gss_cbindings->value == NULL)
981 p = (unsigned char *)gss_cbindings->value;
982 if (text->gs2_flags & GS2_NONSTD_FLAG) {
983 memcpy(p, (unsigned char *)header->value + 2, header->length - 2);
984 p += header->length - 2;
986 memcpy(p, header->value, header->length);
990 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
992 memcpy(p, cbinding->data, cbinding->len);
998 #define CHECK_REMAIN(n) do { if (remain < (n)) return SASL_BADPROT; } while (0)
1001 * Verify gs2-header, save authzid and channel bindings to context.
1004 gs2_verify_initial_message(context_t *text,
1005 sasl_server_params_t *sparams,
1010 OM_uint32 major, minor;
1011 char *p = (char *)in;
1012 unsigned remain = inlen;
1014 gss_buffer_desc buf = GSS_C_EMPTY_BUFFER;
1016 assert(text->cbindingname == NULL);
1017 assert(text->authzid == NULL);
1020 token->value = NULL;
1022 /* minimum header includes CB flag and non-zero GSS token */
1023 CHECK_REMAIN(4); /* [pny],,. */
1025 /* non-standard GSS framing flag */
1026 if (remain > 1 && memcmp(p, "F,", 2) == 0) {
1027 text->gs2_flags |= GS2_NONSTD_FLAG;
1032 /* SASL channel bindings */
1033 CHECK_REMAIN(1); /* [pny] */
1037 CHECK_REMAIN(1); /* = */
1040 return SASL_BADPROT;
1042 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->cbindingname);
1046 text->gs2_flags |= GS2_CB_FLAG_P;
1049 text->gs2_flags |= GS2_CB_FLAG_N;
1052 text->gs2_flags |= GS2_CB_FLAG_Y;
1056 CHECK_REMAIN(1); /* , */
1059 return SASL_BADPROT;
1061 /* authorization identity */
1062 if (remain > 1 && memcmp(p, "a=", 2) == 0) {
1067 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->authzid);
1073 CHECK_REMAIN(1); /* , */
1076 return SASL_BADPROT;
1078 buf.length = inlen - remain;
1079 buf.value = (void *)in;
1081 /* stash channel bindings to pass into gss_accept_sec_context() */
1082 ret = gs2_save_cbindings(text, &buf, sparams->cbinding);
1086 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1087 buf.length = remain;
1090 gss_buffer_desc tmp;
1092 tmp.length = remain;
1095 major = gss_encapsulate_token(&tmp, text->mechanism, &buf);
1096 if (GSS_ERROR(major))
1100 token->value = text->utils->malloc(buf.length);
1101 if (token->value == NULL)
1104 token->length = buf.length;
1105 memcpy(token->value, buf.value, buf.length);
1107 if ((text->gs2_flags & GS2_NONSTD_FLAG) == 0)
1108 gss_release_buffer(&minor, &buf);
1114 * Create gs2-header, save channel bindings to context.
1117 gs2_make_header(context_t *text,
1118 sasl_client_params_t *cparams,
1119 const char *authzid,
1123 size_t required = 0;
1124 size_t wire_authzid_len = 0, cbnamelen = 0;
1125 char *wire_authzid = NULL;
1128 gss_buffer_desc buf;
1133 /* non-standard GSS framing flag */
1134 if (text->gs2_flags & GS2_NONSTD_FLAG)
1135 required += 2; /* F, */
1137 /* SASL channel bindings */
1138 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1140 if (!SASL_CB_PRESENT(cparams))
1141 return SASL_BADPARAM;
1142 cbnamelen = strlen(cparams->cbinding->name);
1143 required += 1 /*=*/ + cbnamelen;
1147 required += 2; /* [pny], */
1150 return SASL_BADPARAM;
1153 /* authorization identity */
1154 if (authzid != NULL) {
1155 ret = gs2_escape_authzid(text->utils, authzid,
1156 strlen(authzid), &wire_authzid);
1160 wire_authzid_len = strlen(wire_authzid);
1161 required += 2 /* a= */ + wire_authzid_len;
1164 required += 1; /* trailing comma */
1166 ret = _plug_buf_alloc(text->utils, out, outlen, required);
1167 if (ret != SASL_OK) {
1168 text->utils->free(wire_authzid);
1172 *out = text->out_buf;
1175 p = (char *)text->out_buf;
1176 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1180 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1183 memcpy(p + 2, cparams->cbinding->name, cbnamelen);
1194 if (wire_authzid != NULL) {
1196 memcpy(p + 2, wire_authzid, wire_authzid_len);
1197 text->utils->free(wire_authzid);
1198 p += 2 + wire_authzid_len;
1202 assert(p == (char *)text->out_buf + required);
1204 buf.length = required;
1207 ret = gs2_save_cbindings(text, &buf, cparams->cbinding);
1215 * Convert a GSS token to a GS2 one
1218 gs2_make_message(context_t *text,
1219 sasl_client_params_t *cparams __attribute__((unused)),
1220 int initialContextToken,
1225 OM_uint32 major, minor;
1227 unsigned header_len = 0;
1228 gss_buffer_desc decap_token = GSS_C_EMPTY_BUFFER;
1230 if (initialContextToken) {
1231 header_len = *outlen;
1233 major = gss_decapsulate_token(token, text->mechanism, &decap_token);
1234 if ((major == GSS_S_DEFECTIVE_TOKEN &&
1235 (text->plug.client->features & SASL_FEAT_GSS_FRAMING)) ||
1239 token = &decap_token;
1242 ret = _plug_buf_alloc(text->utils, out, outlen,
1243 header_len + token->length);
1247 memcpy(*out + header_len, token->value, token->length);
1248 *outlen = header_len + token->length;
1250 if (initialContextToken)
1251 gss_release_buffer(&minor, &decap_token);
1256 static const unsigned long gs2_required_prompts[] = {
1261 * Map GSS mechanism attributes to SASL ones
1264 gs2_get_mech_attrs(const sasl_utils_t *utils,
1266 unsigned int *security_flags,
1267 unsigned int *features,
1268 const unsigned long **prompts)
1270 OM_uint32 major, minor;
1272 gss_OID_set attrs = GSS_C_NO_OID_SET;
1274 major = gss_inquire_attrs_for_mech(&minor, mech, &attrs, NULL);
1275 if (GSS_ERROR(major)) {
1276 utils->seterror(utils->conn, SASL_NOLOG,
1277 "GS2 Failure: gss_inquire_attrs_for_mech");
1281 *security_flags = SASL_SEC_NOPLAINTEXT | SASL_SEC_NOACTIVE;
1282 *features = SASL_FEAT_WANT_CLIENT_FIRST;
1283 if (prompts != NULL)
1284 *prompts = gs2_required_prompts;
1286 #define MA_PRESENT(a) (gss_test_oid_set_member(&minor, (gss_OID)(a), \
1287 attrs, &present) == GSS_S_COMPLETE && \
1290 if (MA_PRESENT(GSS_C_MA_PFS))
1291 *security_flags |= SASL_SEC_FORWARD_SECRECY;
1292 if (!MA_PRESENT(GSS_C_MA_AUTH_INIT_ANON))
1293 *security_flags |= SASL_SEC_NOANONYMOUS;
1294 if (MA_PRESENT(GSS_C_MA_DELEG_CRED))
1295 *security_flags |= SASL_SEC_PASS_CREDENTIALS;
1296 if (MA_PRESENT(GSS_C_MA_AUTH_TARG)) {
1297 *features |= SASL_FEAT_CHANNEL_BINDING;
1298 *security_flags |= SASL_SEC_MUTUAL_AUTH;
1300 if (MA_PRESENT(GSS_C_MA_AUTH_INIT_INIT) && prompts != NULL)
1302 if (MA_PRESENT(GSS_C_MA_ITOK_FRAMED))
1303 *features |= SASL_FEAT_GSS_FRAMING;
1305 gss_release_oid_set(&minor, &attrs);
1311 * Enumerate GSS mechanisms that can be used for GS2
1313 static int gs2_indicate_mechs(const sasl_utils_t *utils)
1315 OM_uint32 major, minor;
1316 gss_OID_desc desired_oids[2];
1317 gss_OID_set_desc desired_attrs;
1318 gss_OID_desc except_oids[3];
1319 gss_OID_set_desc except_attrs;
1321 if (gs2_mechs != GSS_C_NO_OID_SET)
1324 desired_oids[0] = *GSS_C_MA_AUTH_INIT;
1325 desired_oids[1] = *GSS_C_MA_CBINDINGS;
1326 desired_attrs.count = sizeof(desired_oids)/sizeof(desired_oids[0]);
1327 desired_attrs.elements = desired_oids;
1329 except_oids[0] = *GSS_C_MA_MECH_NEGO;
1330 except_oids[1] = *GSS_C_MA_NOT_MECH;
1331 except_oids[2] = *GSS_C_MA_DEPRECATED;
1333 except_attrs.count = sizeof(except_oids)/sizeof(except_oids[0]);
1334 except_attrs.elements = except_oids;
1336 major = gss_indicate_mechs_by_attrs(&minor,
1341 if (GSS_ERROR(major)) {
1342 utils->seterror(utils->conn, SASL_NOLOG,
1343 "GS2 Failure: gss_indicate_mechs_by_attrs");
1347 return (gs2_mechs->count > 0) ? SASL_OK : SASL_NOMECH;
1351 * Map SASL mechanism name to OID
1354 gs2_map_sasl_name(const sasl_utils_t *utils,
1358 OM_uint32 major, minor;
1359 gss_buffer_desc buf;
1361 buf.length = strlen(mech);
1362 buf.value = (void *)mech;
1364 major = gss_inquire_mech_for_saslname(&minor, &buf, oid);
1365 if (GSS_ERROR(major)) {
1366 utils->seterror(utils->conn, SASL_NOLOG,
1367 "GS2 Failure: gss_inquire_mech_for_saslname");
1375 gs2_duplicate_buffer(const sasl_utils_t *utils,
1376 const gss_buffer_t src,
1379 dst->value = utils->malloc(src->length + 1);
1380 if (dst->value == NULL)
1383 memcpy(dst->value, src->value, src->length);
1384 ((char *)dst->value)[src->length] = '\0';
1385 dst->length = src->length;
1391 gs2_unescape_authzid(const sasl_utils_t *utils,
1397 size_t i, len, inlen = *remain;
1402 for (i = 0, len = 0; i < inlen; i++) {
1407 } else if (in[i] == '=') {
1409 return SASL_BADPROT;
1415 if (len == 0 || *endp == NULL)
1416 return SASL_BADPROT;
1418 p = *authzid = utils->malloc(len + 1);
1419 if (*authzid == NULL)
1422 for (i = 0; i < inlen; i++) {
1425 else if (in[i] == '=') {
1426 if (memcmp(&in[i + 1], "2C", 2) == 0)
1428 else if (memcmp(&in[i + 1], "3D", 2) == 0)
1431 utils->free(*authzid);
1433 return SASL_BADPROT;
1446 gs2_escape_authzid(const sasl_utils_t *utils,
1454 p = *authzid = utils->malloc((inlen * 3) + 1);
1455 if (*authzid == NULL)
1458 for (i = 0; i < inlen; i++) {
1460 memcpy(p, "=2C", 3);
1462 } else if (in[i] == '=') {
1463 memcpy(p, "=3D", 3);
1475 #define GOT_CREDS(text, params) ((text)->client_creds != NULL || (params)->gss_creds != NULL)
1476 #define CRED_ERROR(status) ((status) == GSS_S_CRED_UNAVAIL || (status) == GSS_S_NO_CRED)
1479 * Determine the authentication identity from the application supplied
1480 * GSS credential, the application supplied identity, and the default
1481 * GSS credential, in that order. Then, acquire credentials.
1484 gs2_get_init_creds(context_t *text,
1485 sasl_client_params_t *params,
1486 sasl_interact_t **prompt_need,
1487 sasl_out_params_t *oparams)
1489 int result = SASL_OK;
1490 const char *authid = NULL, *userid = NULL;
1491 int user_result = SASL_OK;
1492 int auth_result = SASL_OK;
1493 int pass_result = SASL_OK;
1494 OM_uint32 maj_stat = GSS_S_COMPLETE, min_stat = 0;
1495 gss_OID_set_desc mechs;
1496 gss_buffer_desc cred_authid = GSS_C_EMPTY_BUFFER;
1497 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
1500 mechs.elements = (gss_OID)text->mechanism;
1503 * Get the authentication identity from the application.
1505 if (oparams->authid == NULL) {
1506 auth_result = _plug_get_authid(params->utils, &authid, prompt_need);
1507 if (auth_result != SASL_OK && auth_result != SASL_INTERACT) {
1508 result = auth_result;
1514 * Get the authorization identity from the application.
1516 if (oparams->user == NULL) {
1517 user_result = _plug_get_userid(params->utils, &userid, prompt_need);
1518 if (user_result != SASL_OK && user_result != SASL_INTERACT) {
1519 result = user_result;
1525 * Canonicalize the authentication and authorization identities before
1526 * calling GSS_Import_name.
1528 if (auth_result == SASL_OK && user_result == SASL_OK &&
1529 oparams->authid == NULL) {
1530 if (userid == NULL || userid[0] == '\0') {
1531 result = params->canon_user(params->utils->conn, authid, 0,
1532 SASL_CU_AUTHID | SASL_CU_AUTHZID,
1535 result = params->canon_user(params->utils->conn,
1536 authid, 0, SASL_CU_AUTHID, oparams);
1537 if (result != SASL_OK)
1540 result = params->canon_user(params->utils->conn,
1541 userid, 0, SASL_CU_AUTHZID, oparams);
1542 if (result != SASL_OK)
1546 if (oparams->authid != NULL) {
1547 name_buf.length = strlen(oparams->authid);
1548 name_buf.value = (void *)oparams->authid;
1550 assert(text->client_name == GSS_C_NO_NAME);
1552 maj_stat = gss_import_name(&min_stat,
1555 &text->client_name);
1556 if (GSS_ERROR(maj_stat))
1559 /* The authid may have changed after prompting, so free any creds */
1560 gss_release_cred(&min_stat, &text->client_creds);
1565 * If application didn't provide an authid, then use the default
1566 * credential. If that doesn't work, give up.
1568 if (!GOT_CREDS(text, params) && oparams->authid == NULL) {
1569 maj_stat = gss_acquire_cred(&min_stat,
1574 &text->client_creds,
1577 if (GSS_ERROR(maj_stat))
1580 assert(text->client_name == GSS_C_NO_NAME);
1582 maj_stat = gss_inquire_cred(&min_stat,
1584 ? (gss_cred_id_t)params->gss_creds
1585 : text->client_creds,
1590 if (GSS_ERROR(maj_stat)) {
1591 /* Maybe there was no default credential */
1592 auth_result = SASL_INTERACT;
1596 maj_stat = gss_display_name(&min_stat,
1600 if (GSS_ERROR(maj_stat))
1603 if (userid == NULL || userid[0] == '\0') {
1604 result = params->canon_user(params->utils->conn,
1605 cred_authid.value, cred_authid.length,
1606 SASL_CU_AUTHID | SASL_CU_AUTHZID,
1609 result = params->canon_user(params->utils->conn,
1610 cred_authid.value, cred_authid.length,
1611 SASL_CU_AUTHID, oparams);
1612 if (result != SASL_OK)
1615 result = params->canon_user(params->utils->conn,
1616 cred_authid.value, cred_authid.length,
1617 SASL_CU_AUTHZID, oparams);
1618 if (result != SASL_OK)
1624 * Armed with the authentication identity, try to get a credential without
1627 if (!GOT_CREDS(text, params) && text->client_name != GSS_C_NO_NAME) {
1628 maj_stat = gss_acquire_cred(&min_stat,
1633 &text->client_creds,
1636 if (GSS_ERROR(maj_stat) && !CRED_ERROR(maj_stat))
1641 * If that failed, try to get a credential with a password.
1643 if (!GOT_CREDS(text, params)) {
1644 if (text->password == NULL) {
1645 pass_result = _plug_get_password(params->utils, &text->password,
1646 &text->free_password, prompt_need);
1647 if (pass_result != SASL_OK && pass_result != SASL_INTERACT) {
1648 result = pass_result;
1653 if (text->password != NULL) {
1654 gss_buffer_desc password_buf;
1656 password_buf.length = text->password->len;
1657 password_buf.value = text->password->data;
1659 maj_stat = gss_acquire_cred_with_password(&min_stat,
1665 &text->client_creds,
1668 if (GSS_ERROR(maj_stat))
1673 maj_stat = GSS_S_COMPLETE;
1677 /* free prompts we got */
1678 if (prompt_need && *prompt_need) {
1679 params->utils->free(*prompt_need);
1680 *prompt_need = NULL;
1683 /* if there are prompts not filled in */
1684 if (user_result == SASL_INTERACT || auth_result == SASL_INTERACT ||
1685 pass_result == SASL_INTERACT) {
1686 /* make the prompt list */
1688 _plug_make_prompts(params->utils, prompt_need,
1689 user_result == SASL_INTERACT ?
1690 "Please enter your authorization name" : NULL,
1692 auth_result == SASL_INTERACT ?
1693 "Please enter your authentication name" : NULL,
1695 pass_result == SASL_INTERACT ?
1696 "Please enter your password" : NULL, NULL,
1700 if (result == SASL_OK)
1701 result = SASL_INTERACT;
1705 if (result == SASL_OK && maj_stat != GSS_S_COMPLETE) {
1706 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
1710 gss_release_buffer(&min_stat, &cred_authid);
1716 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
1719 OM_uint32 maj_stat, min_stat;
1720 gss_buffer_desc msg;
1724 unsigned int len, curlen = 0;
1725 const char prefix[] = "GS2 Error: ";
1727 len = sizeof(prefix);
1728 ret = _plug_buf_alloc(utils, &out, &curlen, 256);
1732 strcpy(out, prefix);
1736 maj_stat = gss_display_status(&min_stat, maj,
1737 GSS_C_GSS_CODE, GSS_C_NULL_OID,
1740 if (GSS_ERROR(maj_stat)) {
1742 utils->log(utils->conn, SASL_LOG_FAIL,
1743 "GS2 Failure: (could not get major error message)");
1745 utils->seterror(utils->conn, 0,
1747 "(could not get major error message)");
1753 len += len + msg.length;
1754 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1755 if (ret != SASL_OK) {
1760 strcat(out, msg.value);
1762 gss_release_buffer(&min_stat, &msg);
1768 /* Now get the minor status */
1771 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1772 if (ret != SASL_OK) {
1781 maj_stat = gss_display_status(&min_stat, min,
1782 GSS_C_MECH_CODE, GSS_C_NULL_OID,
1785 if (GSS_ERROR(maj_stat)) {
1787 utils->log(utils->conn, SASL_LOG_FAIL,
1788 "GS2 Failure: (could not get minor error message)");
1790 utils->seterror(utils->conn, 0,
1792 "(could not get minor error message)");
1798 len += len + msg.length;
1800 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1801 if (ret != SASL_OK) {
1806 strcat(out, msg.value);
1808 gss_release_buffer(&min_stat, &msg);
1815 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1816 if (ret != SASL_OK) {
1824 utils->log(utils->conn, SASL_LOG_FAIL, out);
1826 utils->seterror(utils->conn, 0, out);