2 * Copyright (c) 2010 PADL Software Pty Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Redistributions in any form must be accompanied by information on
17 * how to obtain complete source code for the GS2 software and any
18 * accompanying software that uses the GS2 software. The source code
19 * must either be included in the distribution or be available for no
20 * more than the cost of distribution plus a nominal fee, and must be
21 * freely redistributable under reasonable conditions. For an
22 * executable file, complete source code means the source code for all
23 * modules it contains. It does not include source code for modules or
24 * files that typically accompany the major components of the operating
25 * system on which the executable file runs.
27 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE ``AS IS'' AND ANY EXPRESS
28 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
30 * NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
37 * THE POSSIBILITY OF SUCH DAMAGE.
40 * Copyright (c) 1998-2003 Carnegie Mellon University.
41 * All rights reserved.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in
52 * the documentation and/or other materials provided with the
55 * 3. The name "Carnegie Mellon University" must not be used to
56 * endorse or promote products derived from this software without
57 * prior written permission. For permission or any other legal
58 * details, please contact
59 * Office of Technology Transfer
60 * Carnegie Mellon University
62 * Pittsburgh, PA 15213-3890
63 * (412) 268-4387, fax: (412) 268-7395
64 * tech-transfer@andrew.cmu.edu
66 * 4. Redistributions of any form whatsoever must retain the following
68 * "This product includes software developed by Computing Services
69 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
71 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
72 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
73 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
74 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
75 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
76 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
77 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
81 #include <gssapi/gssapi.h>
82 #include <gssapi/gssapi_ext.h>
89 #include "plugin_common.h"
97 #include "gs2_token.h"
99 #define GS2_CB_FLAG_MASK 0x0F
100 #define GS2_CB_FLAG_N 0x00
101 #define GS2_CB_FLAG_P 0x01
102 #define GS2_CB_FLAG_Y 0x02
103 #define GS2_NONSTD_FLAG 0x10
105 typedef struct context {
106 gss_ctx_id_t gss_ctx;
107 gss_name_t client_name;
108 gss_name_t server_name;
109 gss_cred_id_t server_creds;
110 gss_cred_id_t client_creds;
112 unsigned out_buf_len;
113 const sasl_utils_t *utils;
117 sasl_client_plug_t *client;
118 sasl_server_plug_t *server;
123 struct gss_channel_bindings_struct gss_cbindings;
124 sasl_secret_t *password;
125 unsigned int free_password;
129 static gss_OID_set gs2_mechs = GSS_C_NO_OID_SET;
131 static int gs2_ask_user_info(context_t *context,
132 sasl_client_params_t *params,
133 char **realms, int nrealm,
134 sasl_interact_t **prompt_need,
135 sasl_out_params_t *oparams);
137 static int gs2_verify_initial_message(context_t *text,
138 sasl_server_params_t *sparams,
143 static int gs2_make_header(context_t *text,
144 sasl_client_params_t *cparams,
149 static int gs2_make_message(context_t *text,
150 sasl_client_params_t *cparams,
151 int initialContextToken,
156 static int gs2_get_mech_attrs(const sasl_utils_t *utils,
158 unsigned int *security_flags,
159 unsigned int *features);
161 static int gs2_indicate_mechs(const sasl_utils_t *utils);
163 static int gs2_map_sasl_name(const sasl_utils_t *utils,
167 static int gs2_duplicate_buffer(const sasl_utils_t *utils,
168 const gss_buffer_t src,
171 static int gs2_unescape_authzid(const sasl_utils_t *utils,
176 static int gs2_escape_authzid(const sasl_utils_t *utils,
181 /* sasl_gs_log: only logs status string returned from gss_display_status() */
182 #define sasl_gs2_log(x,y,z) sasl_gs2_seterror_(x,y,z,1)
183 #define sasl_gs2_seterror(x,y,z) sasl_gs2_seterror_(x,y,z,0)
186 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
190 sasl_gs2_new_context(const sasl_utils_t *utils)
194 ret = utils->malloc(sizeof(context_t));
198 memset(ret, 0, sizeof(context_t));
205 sasl_gs2_free_context_contents(context_t *text)
212 if (text->gss_ctx != GSS_C_NO_CONTEXT) {
213 gss_delete_sec_context(&min_stat,&text->gss_ctx,
215 text->gss_ctx = GSS_C_NO_CONTEXT;
218 if (text->client_name != GSS_C_NO_NAME) {
219 gss_release_name(&min_stat,&text->client_name);
220 text->client_name = GSS_C_NO_NAME;
223 if (text->server_name != GSS_C_NO_NAME) {
224 gss_release_name(&min_stat,&text->server_name);
225 text->server_name = GSS_C_NO_NAME;
228 if (text->server_creds != GSS_C_NO_CREDENTIAL) {
229 gss_release_cred(&min_stat, &text->server_creds);
230 text->server_creds = GSS_C_NO_CREDENTIAL;
233 if (text->client_creds != GSS_C_NO_CREDENTIAL) {
234 gss_release_cred(&min_stat, &text->client_creds);
235 text->client_creds = GSS_C_NO_CREDENTIAL;
238 if (text->authid != NULL) {
239 text->utils->free(text->authid);
243 if (text->authzid != NULL) {
244 text->utils->free(text->authzid);
245 text->authzid = NULL;
248 gss_release_buffer(&min_stat, &text->gss_cbindings.application_data);
250 if (text->out_buf != NULL) {
251 text->utils->free(text->out_buf);
252 text->out_buf = NULL;
255 text->out_buf_len = 0;
257 if (text->cbindingname != NULL) {
258 text->utils->free(text->cbindingname);
259 text->cbindingname = NULL;
262 if (text->free_password)
263 _plug_free_secret(text->utils, &text->password);
265 memset(text, 0, sizeof(*text));
271 gs2_common_mech_dispose(void *conn_context, const sasl_utils_t *utils)
273 sasl_gs2_free_context_contents((context_t *)(conn_context));
274 utils->free(conn_context);
278 gs2_common_mech_free(void *global_context __attribute__((unused)),
279 const sasl_utils_t *utils)
283 if (gs2_mechs != GSS_C_NO_OID_SET) {
284 gss_release_oid_set(&minor, &gs2_mechs);
285 gs2_mechs = GSS_C_NO_OID_SET;
289 /***************************** Server Section *****************************/
292 gs2_server_mech_new(void *glob_context,
293 sasl_server_params_t *params,
294 const char *challenge __attribute__((unused)),
295 unsigned challen __attribute__((unused)),
301 text = sasl_gs2_new_context(params->utils);
303 MEMERROR(params->utils);
307 text->gss_ctx = GSS_C_NO_CONTEXT;
308 text->client_name = GSS_C_NO_NAME;
309 text->server_name = GSS_C_NO_NAME;
310 text->server_creds = GSS_C_NO_CREDENTIAL;
311 text->client_creds = GSS_C_NO_CREDENTIAL;
312 text->plug.server = glob_context;
314 ret = gs2_map_sasl_name(params->utils, text->plug.server->mech_name,
316 if (ret != SASL_OK) {
317 gs2_common_mech_dispose(text, params->utils);
321 *conn_context = text;
327 gs2_server_mech_step(void *conn_context,
328 sasl_server_params_t *params,
329 const char *clientin,
330 unsigned clientinlen,
331 const char **serverout,
332 unsigned *serveroutlen,
333 sasl_out_params_t *oparams)
335 context_t *text = (context_t *)conn_context;
336 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
337 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
338 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
339 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
340 gss_buffer_desc short_name_buf = GSS_C_EMPTY_BUFFER;
341 gss_name_t without = GSS_C_NO_NAME;
342 gss_OID_set_desc mechs;
343 gss_OID actual_mech = GSS_C_NO_OID;
344 OM_uint32 out_flags = 0;
345 int ret = 0, equal = 0;
346 int initialContextToken = (text->gss_ctx == GSS_C_NO_CONTEXT);
349 if (serverout == NULL) {
350 PARAMERROR(text->utils);
351 return SASL_BADPARAM;
357 if (initialContextToken) {
358 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
359 name_buf.value = params->utils->malloc(name_buf.length + 1);
360 if (name_buf.value == NULL) {
361 MEMERROR(text->utils);
365 snprintf(name_buf.value, name_buf.length + 1,
366 "%s@%s", params->service, params->serverFQDN);
367 maj_stat = gss_import_name(&min_stat,
369 GSS_C_NT_HOSTBASED_SERVICE,
371 params->utils->free(name_buf.value);
372 name_buf.value = NULL;
374 if (GSS_ERROR(maj_stat))
377 assert(text->server_creds == GSS_C_NO_CREDENTIAL);
380 mechs.elements = (gss_OID)text->mechanism;
382 if (params->gss_creds == GSS_C_NO_CREDENTIAL) {
383 maj_stat = gss_acquire_cred(&min_stat,
391 if (GSS_ERROR(maj_stat))
395 ret = gs2_verify_initial_message(text,
403 input_token.value = (void *)clientin;
404 input_token.length = clientinlen;
407 maj_stat = gss_accept_sec_context(&min_stat,
409 (params->gss_creds != GSS_C_NO_CREDENTIAL)
410 ? (gss_cred_id_t)params->gss_creds
411 : text->server_creds,
413 &text->gss_cbindings,
419 &text->client_creds);
420 if (GSS_ERROR(maj_stat)) {
421 sasl_gs2_log(text->utils, maj_stat, min_stat);
422 text->utils->seterror(text->utils->conn, SASL_NOLOG,
423 "GS2 Failure: gss_accept_sec_context");
424 ret = (maj_stat == GSS_S_BAD_BINDINGS) ? SASL_BADBINDING : SASL_BADAUTH;
428 *serveroutlen = output_token.length;
429 if (output_token.value != NULL) {
430 ret = _plug_buf_alloc(text->utils, &text->out_buf,
431 &text->out_buf_len, *serveroutlen);
434 memcpy(text->out_buf, output_token.value, *serveroutlen);
435 *serverout = text->out_buf;
437 /* No output token, send an empty string */
442 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
447 assert(maj_stat == GSS_S_COMPLETE);
449 if (!g_OID_equal(text->mechanism, actual_mech)) {
450 ret = SASL_WRONGMECH;
453 if ((out_flags & GSS_C_SEQUENCE_FLAG) == 0) {
458 maj_stat = gss_display_name(&min_stat, text->client_name,
460 if (GSS_ERROR(maj_stat))
463 ret = gs2_duplicate_buffer(params->utils, &name_buf, &short_name_buf);
467 p = (char *)memchr(name_buf.value, '@', name_buf.length);
469 short_name_buf.length = (p - (char *)name_buf.value);
471 maj_stat = gss_import_name(&min_stat,
475 if (GSS_ERROR(maj_stat)) {
480 maj_stat = gss_compare_name(&min_stat, text->client_name,
482 if (GSS_ERROR(maj_stat)) {
488 ((char *)short_name_buf.value)[short_name_buf.length] = '\0';
491 text->authid = (char *)short_name_buf.value;
492 short_name_buf.value = NULL;
493 short_name_buf.length = 0;
495 if (text->authzid != NULL) {
496 ret = params->canon_user(params->utils->conn,
498 SASL_CU_AUTHZID, oparams);
503 ret = params->canon_user(params->utils->conn,
505 text->authzid == NULL
506 ? (SASL_CU_AUTHZID | SASL_CU_AUTHID)
512 switch (text->gs2_flags & GS2_CB_FLAG_MASK) {
514 oparams->cbindingdisp = SASL_CB_DISP_NONE;
517 oparams->cbindingdisp = SASL_CB_DISP_USED;
518 oparams->cbindingname = text->cbindingname;
521 oparams->cbindingdisp = SASL_CB_DISP_WANT;
525 if (text->client_creds != GSS_C_NO_CREDENTIAL)
526 oparams->client_creds = &text->client_creds;
528 oparams->client_creds = NULL;
530 oparams->gss_peer_name = text->client_name;
531 oparams->gss_local_name = text->server_name;
532 oparams->maxoutbuf = 0xFFFFFF;
533 oparams->encode = NULL;
534 oparams->decode = NULL;
535 oparams->mech_ssf = 0;
536 oparams->doneflag = 1;
541 if (initialContextToken)
542 gss_release_buffer(&min_stat, &input_token);
543 gss_release_buffer(&min_stat, &name_buf);
544 gss_release_buffer(&min_stat, &short_name_buf);
545 gss_release_buffer(&min_stat, &output_token);
546 gss_release_name(&min_stat, &without);
548 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
549 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
552 if (ret != SASL_OK && ret != SASL_CONTINUE)
553 sasl_gs2_free_context_contents(text);
559 gs2_common_plug_init(const sasl_utils_t *utils,
561 int (*plug_alloc)(const sasl_utils_t *,
568 OM_uint32 major, minor;
575 if (gs2_indicate_mechs(utils) != SASL_OK) {
579 plugs = utils->malloc(2 * gs2_mechs->count * plugsize);
584 memset(plugs, 0, 2 * gs2_mechs->count * plugsize);
586 for (i = 0; i < gs2_mechs->count; i++) {
587 gss_buffer_desc sasl_mech_name = GSS_C_EMPTY_BUFFER;
589 major = gss_inquire_saslname_for_mech(&minor,
590 &gs2_mechs->elements[i],
594 if (GSS_ERROR(major))
597 #define PLUG_AT(index) (void *)((unsigned char *)plugs + (count * plugsize))
599 if (plug_alloc(utils, PLUG_AT(count), &sasl_mech_name,
600 &gs2_mechs->elements[i]) == SASL_OK)
603 gss_release_buffer(&minor, &sasl_mech_name);
618 gs2_server_plug_alloc(const sasl_utils_t *utils,
620 gss_buffer_t sasl_name,
624 sasl_server_plug_t *splug = (sasl_server_plug_t *)plug;
627 memset(splug, 0, sizeof(*splug));
629 ret = gs2_get_mech_attrs(utils, mech,
630 &splug->security_flags,
635 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
639 splug->mech_name = (char *)buf.value;
640 splug->glob_context = plug;
641 splug->mech_new = gs2_server_mech_new;
642 splug->mech_step = gs2_server_mech_step;
643 splug->mech_dispose = gs2_common_mech_dispose;
644 splug->mech_free = gs2_common_mech_free;
649 static sasl_server_plug_t *gs2_server_plugins;
650 static int gs2_server_plugcount;
653 gs2_server_plug_init(const sasl_utils_t *utils,
656 sasl_server_plug_t **pluglist,
664 if (maxversion < SASL_SERVER_PLUG_VERSION)
667 *outversion = SASL_SERVER_PLUG_VERSION;
669 if (gs2_server_plugins == NULL) {
670 ret = gs2_common_plug_init(utils,
671 sizeof(sasl_server_plug_t),
672 gs2_server_plug_alloc,
673 (void **)&gs2_server_plugins,
674 &gs2_server_plugcount);
679 *pluglist = gs2_server_plugins;
680 *plugcount = gs2_server_plugcount;
685 /***************************** Client Section *****************************/
687 static int gs2_client_mech_step(void *conn_context,
688 sasl_client_params_t *params,
689 const char *serverin,
690 unsigned serverinlen,
691 sasl_interact_t **prompt_need,
692 const char **clientout,
693 unsigned *clientoutlen,
694 sasl_out_params_t *oparams)
696 context_t *text = (context_t *)conn_context;
697 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
698 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
699 gss_buffer_desc name_buf = GSS_C_EMPTY_BUFFER;
700 OM_uint32 maj_stat = GSS_S_FAILURE, min_stat = 0;
701 OM_uint32 req_flags, ret_flags;
702 gss_OID actual_mech = GSS_C_NO_OID;
704 int initialContextToken;
709 if (text->gss_ctx == GSS_C_NO_CONTEXT) {
710 ret = gs2_ask_user_info(text, params, NULL, 0, prompt_need, oparams);
714 if (params->gss_creds == GSS_C_NO_CREDENTIAL &&
715 text->password != NULL && text->password->len != 0) {
716 gss_buffer_desc password_buf;
717 gss_buffer_desc name_buf;
718 gss_OID_set_desc mechs;
720 name_buf.length = strlen(oparams->authid);
721 name_buf.value = (void *)oparams->authid;
723 password_buf.length = text->password->len;
724 password_buf.value = text->password->data;
727 mechs.elements = (gss_OID)text->mechanism;
729 maj_stat = gss_import_name(&min_stat,
733 if (GSS_ERROR(maj_stat))
736 maj_stat = gss_acquire_cred_with_password(&min_stat,
745 if (GSS_ERROR(maj_stat))
749 initialContextToken = 1;
751 initialContextToken = 0;
753 if (text->server_name == GSS_C_NO_NAME) { /* only once */
754 name_buf.length = strlen(params->service) + 1 + strlen(params->serverFQDN);
755 name_buf.value = params->utils->malloc(name_buf.length + 1);
756 if (name_buf.value == NULL) {
760 if (params->serverFQDN == NULL ||
761 strlen(params->serverFQDN) == 0) {
762 SETERROR(text->utils, "GS2 Failure: no serverFQDN");
767 snprintf(name_buf.value, name_buf.length + 1,
768 "%s@%s", params->service, params->serverFQDN);
770 maj_stat = gss_import_name (&min_stat,
772 GSS_C_NT_HOSTBASED_SERVICE,
774 params->utils->free(name_buf.value);
775 name_buf.value = NULL;
777 if (GSS_ERROR(maj_stat))
781 /* From GSSAPI plugin: apparently this is for some IMAP bug workaround */
782 if (serverinlen == 0 && text->gss_ctx != GSS_C_NO_CONTEXT) {
783 gss_delete_sec_context(&min_stat, &text->gss_ctx, GSS_C_NO_BUFFER);
784 text->gss_ctx = GSS_C_NO_CONTEXT;
787 input_token.value = (void *)serverin;
788 input_token.length = serverinlen;
790 if (initialContextToken) {
791 if ((text->plug.client->features & SASL_FEAT_GSS_FRAMING) == 0)
792 text->gs2_flags |= GS2_NONSTD_FLAG;
794 switch (params->cbindingdisp) {
795 case SASL_CB_DISP_NONE:
796 text->gs2_flags |= GS2_CB_FLAG_N;
798 case SASL_CB_DISP_USED:
799 text->gs2_flags |= GS2_CB_FLAG_P;
801 case SASL_CB_DISP_WANT:
802 text->gs2_flags |= GS2_CB_FLAG_Y;
806 ret = gs2_make_header(text, params,
807 strcmp(oparams->user, oparams->authid) ?
808 (char *) oparams->user : NULL,
809 &text->out_buf, &text->out_buf_len);
814 req_flags = GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG;
816 maj_stat = gss_init_sec_context(&min_stat,
817 (params->gss_creds != GSS_C_NO_CREDENTIAL)
818 ? (gss_cred_id_t)params->gss_creds
819 : text->client_creds,
822 (gss_OID)text->mechanism,
825 &text->gss_cbindings,
826 serverinlen ? &input_token : GSS_C_NO_BUFFER,
831 if (GSS_ERROR(maj_stat))
834 ret = gs2_make_message(text, params, initialContextToken, &output_token,
835 &text->out_buf, &text->out_buf_len);
839 *clientout = text->out_buf;
840 *clientoutlen = text->out_buf_len;
842 if (maj_stat == GSS_S_CONTINUE_NEEDED) {
847 if (text->client_name != GSS_C_NO_NAME)
848 gss_release_name(&min_stat, &text->client_name);
850 maj_stat = gss_inquire_context(&min_stat,
856 &ret_flags, /* flags */
859 if (GSS_ERROR(maj_stat))
862 if (!g_OID_equal(text->mechanism, actual_mech)) {
863 ret = SASL_WRONGMECH;
866 if ((ret_flags & req_flags) != req_flags) {
867 maj_stat = SASL_BADAUTH;
871 maj_stat = gss_display_name(&min_stat,
875 if (GSS_ERROR(maj_stat))
878 oparams->gss_peer_name = text->server_name;
879 oparams->gss_local_name = text->client_name;
880 oparams->encode = NULL;
881 oparams->decode = NULL;
882 oparams->mech_ssf = 0;
883 oparams->maxoutbuf = 0xFFFFFF;
884 oparams->doneflag = 1;
887 gss_release_buffer(&min_stat, &output_token);
888 gss_release_buffer(&min_stat, &name_buf);
890 if (ret == SASL_OK && maj_stat != GSS_S_COMPLETE) {
891 sasl_gs2_seterror(text->utils, maj_stat, min_stat);
894 if (ret != SASL_OK && ret != SASL_CONTINUE)
895 sasl_gs2_free_context_contents(text);
900 static int gs2_client_mech_new(void *glob_context,
901 sasl_client_params_t *params,
907 text = sasl_gs2_new_context(params->utils);
909 MEMERROR(params->utils);
913 text->gss_ctx = GSS_C_NO_CONTEXT;
914 text->client_name = GSS_C_NO_NAME;
915 text->server_creds = GSS_C_NO_CREDENTIAL;
916 text->client_creds = GSS_C_NO_CREDENTIAL;
917 text->plug.client = glob_context;
919 ret = gs2_map_sasl_name(params->utils, text->plug.client->mech_name,
921 if (ret != SASL_OK) {
922 gs2_common_mech_dispose(text, params->utils);
926 *conn_context = text;
931 static const unsigned long gs2_required_prompts[] = {
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,
953 ret = gs2_duplicate_buffer(utils, sasl_name, &buf);
957 cplug->mech_name = (char *)buf.value;
958 cplug->features |= SASL_FEAT_NEEDSERVERFQDN;
959 cplug->glob_context = plug;
960 cplug->mech_new = gs2_client_mech_new;
961 cplug->mech_step = gs2_client_mech_step;
962 cplug->mech_dispose = gs2_common_mech_dispose;
963 cplug->mech_free = gs2_common_mech_free;
964 cplug->required_prompts = gs2_required_prompts;
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;
1322 * Map GSS mechanism attributes to SASL ones
1325 gs2_get_mech_attrs(const sasl_utils_t *utils,
1327 unsigned int *security_flags,
1328 unsigned int *features)
1330 OM_uint32 major, minor;
1332 gss_OID_set attrs = GSS_C_NO_OID_SET;
1334 major = gss_inquire_attrs_for_mech(&minor, mech, &attrs, NULL);
1335 if (GSS_ERROR(major)) {
1336 utils->seterror(utils->conn, SASL_NOLOG,
1337 "GS2 Failure: gss_inquire_attrs_for_mech");
1341 *security_flags = SASL_SEC_NOPLAINTEXT | SASL_SEC_NOACTIVE;
1342 *features = SASL_FEAT_WANT_CLIENT_FIRST | SASL_FEAT_CHANNEL_BINDING;
1344 #define MA_PRESENT(a) (gss_test_oid_set_member(&minor, (gss_OID)(a), \
1345 attrs, &present) == GSS_S_COMPLETE && \
1350 if (MA_PRESENT(GSS_C_MA_PFS))
1351 *security_flags |= SASL_SEC_FORWARD_SECRECY;
1352 if (!MA_PRESENT(GSS_C_MA_AUTH_INIT_ANON))
1353 *security_flags |= SASL_SEC_NOANONYMOUS;
1354 if (MA_PRESENT(GSS_C_MA_DELEG_CRED))
1355 *security_flags |= SASL_SEC_PASS_CREDENTIALS;
1356 if (MA_PRESENT(GSS_C_MA_AUTH_TARG))
1357 *security_flags |= SASL_SEC_MUTUAL_AUTH;
1358 if (MA_PRESENT(GSS_C_MA_ITOK_FRAMED))
1359 *features |= SASL_FEAT_GSS_FRAMING;
1361 gss_release_oid_set(&minor, &attrs);
1366 * Enumerate GSS mechanisms that can be used for GS2
1368 static int gs2_indicate_mechs(const sasl_utils_t *utils)
1370 OM_uint32 major, minor;
1371 gss_OID_desc desired_oids[3];
1372 gss_OID_set_desc desired_attrs;
1373 gss_OID_desc except_oids[3];
1374 gss_OID_set_desc except_attrs;
1376 if (gs2_mechs != GSS_C_NO_OID_SET)
1379 desired_oids[0] = *GSS_C_MA_AUTH_INIT;
1380 desired_oids[1] = *GSS_C_MA_AUTH_TARG;
1381 desired_oids[2] = *GSS_C_MA_CBINDINGS;
1382 desired_attrs.count = sizeof(desired_oids)/sizeof(desired_oids[0]);
1383 desired_attrs.elements = desired_oids;
1385 except_oids[0] = *GSS_C_MA_MECH_NEGO;
1386 except_oids[1] = *GSS_C_MA_NOT_MECH;
1387 except_oids[2] = *GSS_C_MA_DEPRECATED;
1389 except_attrs.count = sizeof(except_oids)/sizeof(except_oids[0]);
1390 except_attrs.elements = except_oids;
1392 major = gss_indicate_mechs_by_attrs(&minor,
1397 if (GSS_ERROR(major)) {
1398 utils->seterror(utils->conn, SASL_NOLOG,
1399 "GS2 Failure: gss_indicate_mechs_by_attrs");
1403 return (gs2_mechs->count > 0) ? SASL_OK : SASL_NOMECH;
1407 * Map SASL mechanism name to OID
1410 gs2_map_sasl_name(const sasl_utils_t *utils,
1414 OM_uint32 major, minor;
1415 gss_buffer_desc buf;
1417 buf.length = strlen(mech);
1418 buf.value = (void *)mech;
1420 major = gss_inquire_mech_for_saslname(&minor, &buf, oid);
1421 if (GSS_ERROR(major)) {
1422 utils->seterror(utils->conn, SASL_NOLOG,
1423 "GS2 Failure: gss_inquire_mech_for_saslname");
1431 gs2_duplicate_buffer(const sasl_utils_t *utils,
1432 const gss_buffer_t src,
1435 dst->value = utils->malloc(src->length + 1);
1436 if (dst->value == NULL)
1439 memcpy(dst->value, src->value, src->length);
1440 ((char *)dst->value)[src->length] = '\0';
1441 dst->length = src->length;
1447 gs2_unescape_authzid(const sasl_utils_t *utils,
1453 size_t i, len, inlen = *remain;
1458 for (i = 0, len = 0; i < inlen; i++) {
1463 } else if (in[i] == '=') {
1465 return SASL_BADPROT;
1471 if (len == 0 || *endp == NULL)
1472 return SASL_BADPROT;
1474 p = *authzid = utils->malloc(len + 1);
1475 if (*authzid == NULL)
1478 for (i = 0; i < inlen; i++) {
1481 else if (in[i] == '=') {
1482 if (memcmp(&in[i + 1], "2C", 2) == 0)
1484 else if (memcmp(&in[i + 1], "3D", 2) == 0)
1487 utils->free(*authzid);
1489 return SASL_BADPROT;
1502 gs2_escape_authzid(const sasl_utils_t *utils,
1510 p = *authzid = utils->malloc((inlen * 3) + 1);
1511 if (*authzid == NULL)
1514 for (i = 0; i < inlen; i++) {
1516 memcpy(p, "=2C", 3);
1518 } else if (in[i] == '=') {
1519 memcpy(p, "=3D", 3);
1532 gs2_ask_user_info(context_t *text,
1533 sasl_client_params_t *params,
1534 char **realms __attribute__((unused)),
1535 int nrealm __attribute__((unused)),
1536 sasl_interact_t **prompt_need,
1537 sasl_out_params_t *oparams)
1539 int result = SASL_OK;
1540 const char *authid = NULL, *userid = NULL;
1541 int user_result = SASL_OK;
1542 int auth_result = SASL_OK;
1543 int pass_result = SASL_OK;
1545 /* try to get the authid */
1546 if (oparams->authid == NULL) {
1547 auth_result = _plug_get_authid(params->utils, &authid, prompt_need);
1549 if (auth_result != SASL_OK && auth_result != SASL_INTERACT) {
1554 /* try to get the userid */
1555 if (oparams->user == NULL) {
1556 user_result = _plug_get_userid(params->utils, &userid, prompt_need);
1558 if (user_result != SASL_OK && user_result != SASL_INTERACT) {
1563 /* try to get the password */
1564 if (text->password == NULL) {
1565 pass_result = _plug_get_password(params->utils, &text->password,
1566 &text->free_password, prompt_need);
1567 if (pass_result != SASL_OK && pass_result != SASL_INTERACT) {
1572 /* free prompts we got */
1573 if (prompt_need && *prompt_need) {
1574 params->utils->free(*prompt_need);
1575 *prompt_need = NULL;
1578 /* if there are prompts not filled in */
1579 if (user_result == SASL_INTERACT || auth_result == SASL_INTERACT ||
1580 pass_result == SASL_INTERACT) {
1582 /* make the prompt list */
1584 _plug_make_prompts(params->utils, prompt_need,
1585 user_result == SASL_INTERACT ?
1586 "Please enter your authorization name" : NULL,
1588 auth_result == SASL_INTERACT ?
1589 "Please enter your authentication name" : NULL,
1591 pass_result == SASL_INTERACT ?
1592 "Please enter your password" : NULL, NULL,
1596 if (result == SASL_OK) return SASL_INTERACT;
1601 if (oparams->authid == NULL) {
1602 if (userid == NULL || userid[0] == '\0') {
1603 result = params->canon_user(params->utils->conn, authid, 0,
1604 SASL_CU_AUTHID | SASL_CU_AUTHZID,
1607 result = params->canon_user(params->utils->conn,
1608 authid, 0, SASL_CU_AUTHID, oparams);
1609 if (result != SASL_OK) return result;
1611 result = params->canon_user(params->utils->conn,
1612 userid, 0, SASL_CU_AUTHZID, oparams);
1614 if (result != SASL_OK)
1622 sasl_gs2_seterror_(const sasl_utils_t *utils, OM_uint32 maj, OM_uint32 min,
1625 OM_uint32 maj_stat, min_stat;
1626 gss_buffer_desc msg;
1630 unsigned int len, curlen = 0;
1631 const char prefix[] = "GSSAPI Error: ";
1633 len = sizeof(prefix);
1634 ret = _plug_buf_alloc(utils, &out, &curlen, 256);
1638 strcpy(out, prefix);
1642 maj_stat = gss_display_status(&min_stat, maj,
1643 GSS_C_GSS_CODE, GSS_C_NULL_OID,
1646 if (GSS_ERROR(maj_stat)) {
1648 utils->log(utils->conn, SASL_LOG_FAIL,
1649 "GS2 Failure: (could not get major error message)");
1651 utils->seterror(utils->conn, 0,
1653 "(could not get major error message)");
1659 len += len + msg.length;
1660 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1661 if (ret != SASL_OK) {
1666 strcat(out, msg.value);
1668 gss_release_buffer(&min_stat, &msg);
1674 /* Now get the minor status */
1677 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1678 if (ret != SASL_OK) {
1687 maj_stat = gss_display_status(&min_stat, min,
1688 GSS_C_MECH_CODE, GSS_C_NULL_OID,
1691 if (GSS_ERROR(maj_stat)) {
1693 utils->log(utils->conn, SASL_LOG_FAIL,
1694 "GS2 Failure: (could not get minor error message)");
1696 utils->seterror(utils->conn, 0,
1698 "(could not get minor error message)");
1704 len += len + msg.length;
1706 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1707 if (ret != SASL_OK) {
1712 strcat(out, msg.value);
1714 gss_release_buffer(&min_stat, &msg);
1721 ret = _plug_buf_alloc(utils, &out, &curlen, len);
1722 if (ret != SASL_OK) {
1730 utils->log(utils->conn, SASL_LOG_FAIL, out);
1732 utils->seterror(utils->conn, 0, out);