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 if ((out_flags & GSS_C_SEQUENCE_FLAG) == 0) {
448 maj_stat = gss_display_name(&min_stat, text->client_name,
450 if (GSS_ERROR(maj_stat))
453 ret = gs2_duplicate_buffer(params->utils, &name_buf, &short_name_buf);
457 p = (char *)memchr(name_buf.value, '@', name_buf.length);
459 short_name_buf.length = (p - (char *)name_buf.value);
461 maj_stat = gss_import_name(&min_stat,
465 if (GSS_ERROR(maj_stat)) {
470 maj_stat = gss_compare_name(&min_stat, text->client_name,
472 if (GSS_ERROR(maj_stat)) {
478 ((char *)short_name_buf.value)[short_name_buf.length] = '\0';
481 text->authid = (char *)short_name_buf.value;
482 short_name_buf.value = NULL;
483 short_name_buf.length = 0;
485 if (text->authzid != NULL) {
486 ret = params->canon_user(params->utils->conn,
488 SASL_CU_AUTHZID, oparams);
493 ret = params->canon_user(params->utils->conn,
495 text->authzid == NULL
496 ? (SASL_CU_AUTHZID | SASL_CU_AUTHID)
502 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
504 oparams->cbindingdisp = SASL_CB_DISP_NONE;
507 oparams->cbindingdisp = SASL_CB_DISP_USED;
508 oparams->cbindingname = text->cbindingname;
511 oparams->cbindingdisp = SASL_CB_DISP_WANT;
515 if (text->client_creds != GSS_C_NO_CREDENTIAL)
516 oparams->client_creds = &text->client_creds;
518 oparams->client_creds = NULL;
520 oparams->gss_peer_name = text->client_name;
521 oparams->gss_local_name = text->server_name;
522 oparams->maxoutbuf = 0xFFFFFF;
523 oparams->encode = NULL;
524 oparams->decode = NULL;
525 oparams->mech_ssf = 0;
526 oparams->doneflag = 1;
531 if (initialContextToken)
532 gss_release_buffer(&min_stat, &input_token);
533 gss_release_buffer(&min_stat, &name_buf);
534 gss_release_buffer(&min_stat, &short_name_buf);
535 gss_release_buffer(&min_stat, &output_token);
536 gss_release_name(&min_stat, &without);
538 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
539 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
543 sasl_gs2_free_context_contents(text);
549 gs2_common_plug_init(const sasl_utils_t *utils,
551 int (*plug_alloc)(const sasl_utils_t *,
558 OM_uint32 major, minor;
565 if (gs2_indicate_mechs(utils) != SASL_OK) {
569 plugs = utils->malloc(gs2_mechs->count * plugsize);
574 memset(plugs, 0, gs2_mechs->count * plugsize);
576 for (i = 0; i < gs2_mechs->count; i++) {
577 gss_buffer_desc sasl_mech_name = GSS_C_EMPTY_BUFFER;
579 major = gss_inquire_saslname_for_mech(&minor,
580 &gs2_mechs->elements[i],
584 if (GSS_ERROR(major))
587 #define PLUG_AT(index) (void *)((unsigned char *)plugs + (count * plugsize))
589 if (plug_alloc(utils, PLUG_AT(count), &sasl_mech_name,
590 &gs2_mechs->elements[i]) == SASL_OK)
593 gss_release_buffer(&minor, &sasl_mech_name);
608 gs2_server_plug_alloc(const sasl_utils_t *utils,
610 gss_buffer_t sasl_name,
614 sasl_server_plug_t *splug = (sasl_server_plug_t *)plug;
617 memset(splug, 0, sizeof(*splug));
619 ret = gs2_get_mech_attrs(utils, mech,
620 &splug->security_flags,
626 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
630 splug->mech_name = (char *)buf.value;
631 splug->glob_context = plug;
632 splug->mech_new = gs2_server_mech_new;
633 splug->mech_step = gs2_server_mech_step;
634 splug->mech_dispose = gs2_common_mech_dispose;
635 splug->mech_free = gs2_common_mech_free;
640 static sasl_server_plug_t *gs2_server_plugins;
641 static int gs2_server_plugcount;
644 gs2_server_plug_init(const sasl_utils_t *utils,
647 sasl_server_plug_t **pluglist,
655 if (maxversion < SASL_SERVER_PLUG_VERSION)
658 *outversion = SASL_SERVER_PLUG_VERSION;
660 if (gs2_server_plugins == NULL) {
661 ret = gs2_common_plug_init(utils,
662 sizeof(sasl_server_plug_t),
663 gs2_server_plug_alloc,
664 (void **)&gs2_server_plugins,
665 &gs2_server_plugcount);
670 *pluglist = gs2_server_plugins;
671 *plugcount = gs2_server_plugcount;
676 /***************************** Client Section *****************************/
678 static int gs2_client_mech_step(void *conn_context,
679 sasl_client_params_t *params,
680 const char *serverin,
681 unsigned serverinlen,
682 sasl_interact_t **prompt_need,
683 const char **clientout,
684 unsigned *clientoutlen,
685 sasl_out_params_t *oparams)
687 context_t *text = (context_t *)conn_context;
688 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
689 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
690 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
691 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
692 OM_uint32 req_flags, ret_flags;
694 int initialContextToken;
699 if (text->gss_ctx == GSS_C_NO_CONTEXT) {
700 ret = gs2_get_init_creds(text, params, prompt_need, oparams);
704 initialContextToken = 1;
706 initialContextToken = 0;
708 if (text->server_name == GSS_C_NO_NAME) { /* only once */
709 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
710 name_buf.value = params->utils->malloc(name_buf.length + 1);
711 if (name_buf.value == NULL) {
715 if (params->serverFQDN == NULL ||
716 strlen(params->serverFQDN) == 0) {
717 SETERROR(text->utils, "GS2 Failure: no serverFQDN");
722 snprintf(name_buf.value, name_buf.length + 1,
723 "%s@%s", params->service, params->serverFQDN);
725 maj_stat = gss_import_name(&min_stat,
727 GSS_C_NT_HOSTBASED_SERVICE,
729 params->utils->free(name_buf.value);
730 name_buf.value = NULL;
732 if (GSS_ERROR(maj_stat))
736 /* From GSSAPI plugin: apparently this is for some IMAP bug workaround */
737 if (serverinlen == 0 && text->gss_ctx != GSS_C_NO_CONTEXT) {
738 gss_delete_sec_context(&min_stat, &text->gss_ctx, GSS_C_NO_BUFFER);
739 text->gss_ctx = GSS_C_NO_CONTEXT;
742 input_token.value = (void *)serverin;
743 input_token.length = serverinlen;
745 if (initialContextToken) {
746 if ((text->plug.client->features & SASL_FEAT_GSS_FRAMING) == 0)
747 text->gs2_flags |= GS2_NONSTD_FLAG;
749 switch (params->cbindingdisp) {
750 case SASL_CB_DISP_NONE:
751 text->gs2_flags |= GS2_CB_FLAG_N;
753 case SASL_CB_DISP_USED:
754 text->gs2_flags |= GS2_CB_FLAG_P;
756 case SASL_CB_DISP_WANT:
757 text->gs2_flags |= GS2_CB_FLAG_Y;
761 ret = gs2_make_header(text, params,
762 strcmp(oparams->user, oparams->authid) ?
763 (char *) oparams->user : NULL,
764 &text->out_buf, &text->out_buf_len);
769 req_flags = GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG;
771 maj_stat = gss_init_sec_context(&min_stat,
772 (params->gss_creds != GSS_C_NO_CREDENTIAL)
773 ? (gss_cred_id_t)params->gss_creds
774 : text->client_creds,
777 (gss_OID)text->mechanism,
780 &text->gss_cbindings,
781 serverinlen ? &input_token : GSS_C_NO_BUFFER,
786 if (GSS_ERROR(maj_stat))
789 ret = gs2_make_message(text, params, initialContextToken, &output_token,
790 &text->out_buf, &text->out_buf_len);
794 *clientout = text->out_buf;
795 *clientoutlen = text->out_buf_len;
797 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
802 if (text->client_name != GSS_C_NO_NAME)
803 gss_release_name(&min_stat, &text->client_name);
805 maj_stat = gss_inquire_context(&min_stat,
811 &ret_flags, /* flags */
814 if (GSS_ERROR(maj_stat))
817 if ((ret_flags & req_flags) != req_flags) {
818 maj_stat = SASL_BADAUTH;
822 maj_stat = gss_display_name(&min_stat,
826 if (GSS_ERROR(maj_stat))
829 oparams->gss_peer_name = text->server_name;
830 oparams->gss_local_name = text->client_name;
831 oparams->encode = NULL;
832 oparams->decode = NULL;
833 oparams->mech_ssf = 0;
834 oparams->maxoutbuf = 0xFFFFFF;
835 oparams->doneflag = 1;
838 gss_release_buffer(&min_stat, &output_token);
839 gss_release_buffer(&min_stat, &name_buf);
841 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
842 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
846 sasl_gs2_free_context_contents(text);
851 static int gs2_client_mech_new(void *glob_context,
852 sasl_client_params_t *params,
858 text = sasl_gs2_new_context(params->utils);
860 MEMERROR(params->utils);
864 text->gss_ctx = GSS_C_NO_CONTEXT;
865 text->client_name = GSS_C_NO_NAME;
866 text->server_creds = GSS_C_NO_CREDENTIAL;
867 text->client_creds = GSS_C_NO_CREDENTIAL;
868 text->plug.client = glob_context;
870 ret = gs2_map_sasl_name(params->utils, text->plug.client->mech_name,
872 if (ret != SASL_OK) {
873 gs2_common_mech_dispose(text, params->utils);
877 *conn_context = text;
883 gs2_client_plug_alloc(const sasl_utils_t *utils,
885 gss_buffer_t sasl_name,
889 sasl_client_plug_t *cplug = (sasl_client_plug_t *)plug;
892 memset(cplug, 0, sizeof(*cplug));
894 ret = gs2_get_mech_attrs(utils, mech,
895 &cplug->security_flags,
897 &cplug->required_prompts);
901 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
905 cplug->mech_name = (char *)buf.value;
906 cplug->features |= SASL_FEAT_NEEDSERVERFQDN;
907 cplug->glob_context = plug;
908 cplug->mech_new = gs2_client_mech_new;
909 cplug->mech_step = gs2_client_mech_step;
910 cplug->mech_dispose = gs2_common_mech_dispose;
911 cplug->mech_free = gs2_common_mech_free;
916 static sasl_client_plug_t *gs2_client_plugins;
917 static int gs2_client_plugcount;
920 gs2_client_plug_init(const sasl_utils_t *utils,
923 sasl_client_plug_t **pluglist,
931 if (maxversion < SASL_CLIENT_PLUG_VERSION)
934 *outversion = SASL_CLIENT_PLUG_VERSION;
936 if (gs2_client_plugins == NULL) {
937 ret = gs2_common_plug_init(utils,
938 sizeof(sasl_client_plug_t),
939 gs2_client_plug_alloc,
940 (void **)&gs2_client_plugins,
941 &gs2_client_plugcount);
946 *pluglist = gs2_client_plugins;
947 *plugcount = gs2_client_plugcount;
953 * Copy header and application channel bindings to GSS channel bindings
954 * structure in context.
957 gs2_save_cbindings(context_t *text,
959 const sasl_channel_binding_t *cbinding)
961 gss_buffer_t gss_cbindings = &text->gss_cbindings.application_data;
965 assert(gss_cbindings->value == NULL);
968 * The application-data field MUST be set to the gs2-header, excluding
969 * the initial [gs2-nonstd-flag ","] part, concatenated with, when a
970 * gs2-cb-flag of "p" is used, the application's channel binding data.
972 len = header->length;
973 if (text->gs2_flags & GS2_NONSTD_FLAG) {
977 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
979 len += cbinding->len;
982 gss_cbindings->length = len;
983 gss_cbindings->value = text->utils->malloc(len);
984 if (gss_cbindings->value == NULL)
987 p = (unsigned char *)gss_cbindings->value;
988 if (text->gs2_flags & GS2_NONSTD_FLAG) {
989 memcpy(p, (unsigned char *)header->value + 2, header->length - 2);
990 p += header->length - 2;
992 memcpy(p, header->value, header->length);
996 if ((text->gs2_flags & GS2_CB_FLAG_MASK) == GS2_CB_FLAG_P &&
998 memcpy(p, cbinding->data, cbinding->len);
1004 #define CHECK_REMAIN(n) do { if (remain < (n)) return SASL_BADPROT; } while (0)
1007 * Verify gs2-header, save authzid and channel bindings to context.
1010 gs2_verify_initial_message(context_t *text,
1011 sasl_server_params_t *sparams,
1016 OM_uint32 major, minor;
1017 char *p = (char *)in;
1018 unsigned remain = inlen;
1020 gss_buffer_desc buf = GSS_C_EMPTY_BUFFER;
1022 assert(text->cbindingname == NULL);
1023 assert(text->authzid == NULL);
1026 token->value = NULL;
1028 /* minimum header includes CB flag and non-zero GSS token */
1029 CHECK_REMAIN(4); /* [pny],,. */
1031 /* non-standard GSS framing flag */
1032 if (remain > 1 && memcmp(p, "F,", 2) == 0) {
1033 text->gs2_flags |= GS2_NONSTD_FLAG;
1038 /* SASL channel bindings */
1039 CHECK_REMAIN(1); /* [pny] */
1043 CHECK_REMAIN(1); /* = */
1046 return SASL_BADPROT;
1048 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->cbindingname);
1052 text->gs2_flags |= GS2_CB_FLAG_P;
1055 text->gs2_flags |= GS2_CB_FLAG_N;
1058 text->gs2_flags |= GS2_CB_FLAG_Y;
1062 CHECK_REMAIN(1); /* , */
1065 return SASL_BADPROT;
1067 /* authorization identity */
1068 if (remain > 1 && memcmp(p, "a=", 2) == 0) {
1073 ret = gs2_unescape_authzid(text->utils, &p, &remain, &text->authzid);
1079 CHECK_REMAIN(1); /* , */
1082 return SASL_BADPROT;
1084 buf.length = inlen - remain;
1085 buf.value = (void *)in;
1087 /* stash channel bindings to pass into gss_accept_sec_context() */
1088 ret = gs2_save_cbindings(text, &buf, sparams->cbinding);
1092 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1093 buf.length = remain;
1096 gss_buffer_desc tmp;
1098 tmp.length = remain;
1101 major = gss_encapsulate_token(&tmp, text->mechanism, &buf);
1102 if (GSS_ERROR(major))
1106 token->value = text->utils->malloc(buf.length);
1107 if (token->value == NULL)
1110 token->length = buf.length;
1111 memcpy(token->value, buf.value, buf.length);
1113 if ((text->gs2_flags & GS2_NONSTD_FLAG) == 0)
1114 gss_release_buffer(&minor, &buf);
1120 * Create gs2-header, save channel bindings to context.
1123 gs2_make_header(context_t *text,
1124 sasl_client_params_t *cparams,
1125 const char *authzid,
1129 size_t required = 0;
1130 size_t wire_authzid_len = 0, cbnamelen = 0;
1131 char *wire_authzid = NULL;
1134 gss_buffer_desc buf;
1139 /* non-standard GSS framing flag */
1140 if (text->gs2_flags & GS2_NONSTD_FLAG)
1141 required += 2; /* F, */
1143 /* SASL channel bindings */
1144 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1146 if (!SASL_CB_PRESENT(cparams))
1147 return SASL_BADPARAM;
1148 cbnamelen = strlen(cparams->cbinding->name);
1149 required += 1 /*=*/ + cbnamelen;
1153 required += 2; /* [pny], */
1156 return SASL_BADPARAM;
1159 /* authorization identity */
1160 if (authzid != NULL) {
1161 ret = gs2_escape_authzid(text->utils, authzid,
1162 strlen(authzid), &wire_authzid);
1166 wire_authzid_len = strlen(wire_authzid);
1167 required += 2 /* a= */ + wire_authzid_len;
1170 required += 1; /* trailing comma */
1172 ret = _plug_buf_alloc(text->utils, out, outlen, required);
1173 if (ret != SASL_OK) {
1174 text->utils->free(wire_authzid);
1178 *out = text->out_buf;
1181 p = (char *)text->out_buf;
1182 if (text->gs2_flags & GS2_NONSTD_FLAG) {
1186 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
1189 memcpy(p + 2, cparams->cbinding->name, cbnamelen);
1200 if (wire_authzid != NULL) {
1202 memcpy(p + 2, wire_authzid, wire_authzid_len);
1203 text->utils->free(wire_authzid);
1204 p += 2 + wire_authzid_len;
1208 assert(p == (char *)text->out_buf + required);
1210 buf.length = required;
1213 ret = gs2_save_cbindings(text, &buf, cparams->cbinding);
1221 * Convert a GSS token to a GS2 one
1224 gs2_make_message(context_t *text,
1225 sasl_client_params_t *cparams __attribute__((unused)),
1226 int initialContextToken,
1231 OM_uint32 major, minor;
1233 unsigned header_len = 0;
1234 gss_buffer_desc decap_token = GSS_C_EMPTY_BUFFER;
1236 if (initialContextToken) {
1237 header_len = *outlen;
1239 major = gss_decapsulate_token(token, text->mechanism, &decap_token);
1240 if ((major == GSS_S_DEFECTIVE_TOKEN &&
1241 (text->plug.client->features & SASL_FEAT_GSS_FRAMING)) ||
1245 token = &decap_token;
1248 ret = _plug_buf_alloc(text->utils, out, outlen,
1249 header_len + token->length);
1253 memcpy(*out + header_len, token->value, token->length);
1254 *outlen = header_len + token->length;
1256 if (initialContextToken)
1257 gss_release_buffer(&minor, &decap_token);
1262 static const unsigned long gs2_required_prompts[] = {
1267 * Map GSS mechanism attributes to SASL ones
1270 gs2_get_mech_attrs(const sasl_utils_t *utils,
1272 unsigned int *security_flags,
1273 unsigned int *features,
1274 const unsigned long **prompts)
1276 OM_uint32 major, minor;
1278 gss_OID_set attrs = GSS_C_NO_OID_SET;
1280 major = gss_inquire_attrs_for_mech(&minor, mech, &attrs, NULL);
1281 if (GSS_ERROR(major)) {
1282 utils->seterror(utils->conn, SASL_NOLOG,
1283 "GS2 Failure: gss_inquire_attrs_for_mech");
1287 *security_flags = SASL_SEC_NOPLAINTEXT | SASL_SEC_NOACTIVE;
1288 *features = SASL_FEAT_WANT_CLIENT_FIRST | SASL_FEAT_CHANNEL_BINDING;
1289 if (prompts != NULL)
1290 *prompts = gs2_required_prompts;
1292 #define MA_PRESENT(a) (gss_test_oid_set_member(&minor, (gss_OID)(a), \
1293 attrs, &present) == GSS_S_COMPLETE && \
1296 if (MA_PRESENT(GSS_C_MA_PFS))
1297 *security_flags |= SASL_SEC_FORWARD_SECRECY;
1298 if (!MA_PRESENT(GSS_C_MA_AUTH_INIT_ANON))
1299 *security_flags |= SASL_SEC_NOANONYMOUS;
1300 if (MA_PRESENT(GSS_C_MA_DELEG_CRED))
1301 *security_flags |= SASL_SEC_PASS_CREDENTIALS;
1302 if (MA_PRESENT(GSS_C_MA_AUTH_TARG))
1303 *security_flags |= SASL_SEC_MUTUAL_AUTH;
1304 if (MA_PRESENT(GSS_C_MA_AUTH_INIT_INIT) && prompts != NULL)
1306 if (MA_PRESENT(GSS_C_MA_ITOK_FRAMED))
1307 *features |= SASL_FEAT_GSS_FRAMING;
1309 gss_release_oid_set(&minor, &attrs);
1315 * Enumerate GSS mechanisms that can be used for GS2
1317 static int gs2_indicate_mechs(const sasl_utils_t *utils)
1319 OM_uint32 major, minor;
1320 gss_OID_desc desired_oids[3];
1321 gss_OID_set_desc desired_attrs;
1322 gss_OID_desc except_oids[3];
1323 gss_OID_set_desc except_attrs;
1325 if (gs2_mechs != GSS_C_NO_OID_SET)
1328 desired_oids[0] = *GSS_C_MA_AUTH_INIT;
1329 desired_oids[1] = *GSS_C_MA_AUTH_TARG;
1330 desired_oids[2] = *GSS_C_MA_CBINDINGS;
1331 desired_attrs.count = sizeof(desired_oids)/sizeof(desired_oids[0]);
1332 desired_attrs.elements = desired_oids;
1334 except_oids[0] = *GSS_C_MA_MECH_NEGO;
1335 except_oids[1] = *GSS_C_MA_NOT_MECH;
1336 except_oids[2] = *GSS_C_MA_DEPRECATED;
1338 except_attrs.count = sizeof(except_oids)/sizeof(except_oids[0]);
1339 except_attrs.elements = except_oids;
1341 major = gss_indicate_mechs_by_attrs(&minor,
1346 if (GSS_ERROR(major)) {
1347 utils->seterror(utils->conn, SASL_NOLOG,
1348 "GS2 Failure: gss_indicate_mechs_by_attrs");
1352 return (gs2_mechs->count > 0) ? SASL_OK : SASL_NOMECH;
1356 * Map SASL mechanism name to OID
1359 gs2_map_sasl_name(const sasl_utils_t *utils,
1363 OM_uint32 major, minor;
1364 gss_buffer_desc buf;
1366 buf.length = strlen(mech);
1367 buf.value = (void *)mech;
1369 major = gss_inquire_mech_for_saslname(&minor, &buf, oid);
1370 if (GSS_ERROR(major)) {
1371 utils->seterror(utils->conn, SASL_NOLOG,
1372 "GS2 Failure: gss_inquire_mech_for_saslname");
1380 gs2_duplicate_buffer(const sasl_utils_t *utils,
1381 const gss_buffer_t src,
1384 dst->value = utils->malloc(src->length + 1);
1385 if (dst->value == NULL)
1388 memcpy(dst->value, src->value, src->length);
1389 ((char *)dst->value)[src->length] = '\0';
1390 dst->length = src->length;
1396 gs2_unescape_authzid(const sasl_utils_t *utils,
1402 size_t i, len, inlen = *remain;
1407 for (i = 0, len = 0; i < inlen; i++) {
1412 } else if (in[i] == '=') {
1414 return SASL_BADPROT;
1420 if (len == 0 || *endp == NULL)
1421 return SASL_BADPROT;
1423 p = *authzid = utils->malloc(len + 1);
1424 if (*authzid == NULL)
1427 for (i = 0; i < inlen; i++) {
1430 else if (in[i] == '=') {
1431 if (memcmp(&in[i + 1], "2C", 2) == 0)
1433 else if (memcmp(&in[i + 1], "3D", 2) == 0)
1436 utils->free(*authzid);
1438 return SASL_BADPROT;
1451 gs2_escape_authzid(const sasl_utils_t *utils,
1459 p = *authzid = utils->malloc((inlen * 3) + 1);
1460 if (*authzid == NULL)
1463 for (i = 0; i < inlen; i++) {
1465 memcpy(p, "=2C", 3);
1467 } else if (in[i] == '=') {
1468 memcpy(p, "=3D", 3);
1480 #define GOT_CREDS(text, params) ((text)->client_creds != NULL || (params)->gss_creds != NULL)
1481 #define CRED_ERROR(status) ((status) == GSS_S_CRED_UNAVAIL || (status) == GSS_S_NO_CRED)
1484 * Determine the authentication identity from the application supplied
1485 * GSS credential, the application supplied identity, and the default
1486 * GSS credential, in that order. Then, acquire credentials.
1489 gs2_get_init_creds(context_t *text,
1490 sasl_client_params_t *params,
1491 sasl_interact_t **prompt_need,
1492 sasl_out_params_t *oparams)
1494 int result = SASL_OK;
1495 const char *authid = NULL, *userid = NULL;
1496 int user_result = SASL_OK;
1497 int auth_result = SASL_OK;
1498 int pass_result = SASL_OK;
1499 OM_uint32 maj_stat = GSS_S_COMPLETE, min_stat = 0;
1500 gss_OID_set_desc mechs;
1501 gss_buffer_desc cred_authid = GSS_C_EMPTY_BUFFER;
1502 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
1505 mechs.elements = (gss_OID)text->mechanism;
1508 * Get the authentication identity from the application.
1510 if (oparams->authid == NULL) {
1511 auth_result = _plug_get_authid(params->utils, &authid, prompt_need);
1512 if (auth_result != SASL_OK && auth_result != SASL_INTERACT) {
1513 result = auth_result;
1519 * Get the authorization identity from the application.
1521 if (oparams->user == NULL) {
1522 user_result = _plug_get_userid(params->utils, &userid, prompt_need);
1523 if (user_result != SASL_OK && user_result != SASL_INTERACT) {
1524 result = user_result;
1530 * Canonicalize the authentication and authorization identities before
1531 * calling GSS_Import_name.
1533 if (auth_result == SASL_OK && user_result == SASL_OK &&
1534 oparams->authid == NULL) {
1535 if (userid == NULL || userid[0] == '\0') {
1536 result = params->canon_user(params->utils->conn, authid, 0,
1537 SASL_CU_AUTHID | SASL_CU_AUTHZID,
1540 result = params->canon_user(params->utils->conn,
1541 authid, 0, SASL_CU_AUTHID, oparams);
1542 if (result != SASL_OK)
1545 result = params->canon_user(params->utils->conn,
1546 userid, 0, SASL_CU_AUTHZID, oparams);
1547 if (result != SASL_OK)
1551 if (oparams->authid != NULL) {
1552 name_buf.length = strlen(oparams->authid);
1553 name_buf.value = (void *)oparams->authid;
1555 assert(text->client_name == GSS_C_NO_NAME);
1557 maj_stat = gss_import_name(&min_stat,
1560 &text->client_name);
1561 if (GSS_ERROR(maj_stat))
1567 * If application didn't provide an authid, then use the default
1568 * credential. If that doesn't work, give up.
1570 if (!GOT_CREDS(text, params) && oparams->authid == NULL) {
1571 maj_stat = gss_acquire_cred(&min_stat,
1576 &text->client_creds,
1579 if (GSS_ERROR(maj_stat))
1582 assert(text->client_name == GSS_C_NO_NAME);
1584 maj_stat = gss_inquire_cred(&min_stat,
1586 ? (gss_cred_id_t)params->gss_creds
1587 : text->client_creds,
1592 if (GSS_ERROR(maj_stat))
1595 maj_stat = gss_display_name(&min_stat,
1599 if (GSS_ERROR(maj_stat))
1602 if (userid == NULL || userid[0] == '\0') {
1603 result = params->canon_user(params->utils->conn,
1604 cred_authid.value, cred_authid.length,
1605 SASL_CU_AUTHID | SASL_CU_AUTHZID,
1608 result = params->canon_user(params->utils->conn,
1609 cred_authid.value, cred_authid.length,
1610 SASL_CU_AUTHID, oparams);
1611 if (result != SASL_OK)
1614 result = params->canon_user(params->utils->conn,
1615 cred_authid.value, cred_authid.length,
1616 SASL_CU_AUTHZID, oparams);
1617 if (result != SASL_OK)
1623 * Armed with the authentication identity, try to get a credential without
1626 if (!GOT_CREDS(text, params) && text->client_name != GSS_C_NO_NAME) {
1627 maj_stat = gss_acquire_cred(&min_stat,
1632 &text->client_creds,
1635 if (GSS_ERROR(maj_stat) && !CRED_ERROR(maj_stat))
1640 * If that failed, try to get a credential with a password.
1642 if (!GOT_CREDS(text, params)) {
1643 if (text->password == NULL) {
1644 pass_result = _plug_get_password(params->utils, &text->password,
1645 &text->free_password, prompt_need);
1646 if (pass_result != SASL_OK && pass_result != SASL_INTERACT) {
1647 result = pass_result;
1652 if (text->password != NULL) {
1653 gss_buffer_desc password_buf;
1655 password_buf.length = text->password->len;
1656 password_buf.value = text->password->data;
1658 maj_stat = gss_acquire_cred_with_password(&min_stat,
1664 &text->client_creds,
1667 if (GSS_ERROR(maj_stat))
1672 maj_stat = GSS_S_COMPLETE;
1674 /* free prompts we got */
1675 if (prompt_need && *prompt_need) {
1676 params->utils->free(*prompt_need);
1677 *prompt_need = NULL;
1680 /* if there are prompts not filled in */
1681 if (user_result == SASL_INTERACT || auth_result == SASL_INTERACT ||
1682 pass_result == SASL_INTERACT) {
1683 /* make the prompt list */
1685 _plug_make_prompts(params->utils, prompt_need,
1686 user_result == SASL_INTERACT ?
1687 "Please enter your authorization name" : NULL,
1689 auth_result == SASL_INTERACT ?
1690 "Please enter your authentication name" : NULL,
1692 pass_result == SASL_INTERACT ?
1693 "Please enter your password" : NULL, NULL,
1697 if (result == SASL_OK)
1698 result = SASL_INTERACT;
1702 if (result == SASL_OK && maj_stat != GSS_S_COMPLETE) {
1703 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
1707 gss_release_buffer(&min_stat, &cred_authid);
1713 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
1716 OM_uint32 maj_stat, min_stat;
1717 gss_buffer_desc msg;
1721 unsigned int len, curlen = 0;
1722 const char prefix[] = "GS2 Error: ";
1724 len = sizeof(prefix);
1725 ret = _plug_buf_alloc(utils, &out, &curlen, 256);
1729 strcpy(out, prefix);
1733 maj_stat = gss_display_status(&min_stat, maj,
1734 GSS_C_GSS_CODE, GSS_C_NULL_OID,
1737 if (GSS_ERROR(maj_stat)) {
1739 utils->log(utils->conn, SASL_LOG_FAIL,
1740 "GS2 Failure: (could not get major error message)");
1742 utils->seterror(utils->conn, 0,
1744 "(could not get major error message)");
1750 len += len + msg.length;
1751 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1752 if (ret != SASL_OK) {
1757 strcat(out, msg.value);
1759 gss_release_buffer(&min_stat, &msg);
1765 /* Now get the minor status */
1768 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1769 if (ret != SASL_OK) {
1778 maj_stat = gss_display_status(&min_stat, min,
1779 GSS_C_MECH_CODE, GSS_C_NULL_OID,
1782 if (GSS_ERROR(maj_stat)) {
1784 utils->log(utils->conn, SASL_LOG_FAIL,
1785 "GS2 Failure: (could not get minor error message)");
1787 utils->seterror(utils->conn, 0,
1789 "(could not get minor error message)");
1795 len += len + msg.length;
1797 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1798 if (ret != SASL_OK) {
1803 strcat(out, msg.value);
1805 gss_release_buffer(&min_stat, &msg);
1812 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1813 if (ret != SASL_OK) {
1821 utils->log(utils->conn, SASL_LOG_FAIL, out);
1823 utils->seterror(utils->conn, 0, out);