xref: /netbsd-src/crypto/external/bsd/libsaslc/dist/src/mech.c (revision 9674b81ed919245c6e325e26eb1c2397bd3de117)
1 /* $NetBSD: mech.c,v 1.7 2011/02/22 05:45:05 joerg Exp $ */
2 
3 /* Copyright (c) 2010 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Mateusz Kocielski.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: mech.c,v 1.7 2011/02/22 05:45:05 joerg Exp $");
39 
40 #include <sys/queue.h>
41 
42 #include <saslc.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "dict.h"
48 #include "error.h"
49 #include "mech.h"
50 #include "msg.h"
51 #include "saslc_private.h"
52 
53 /* mechanisms */
54 __weakref_visible const saslc__mech_t weak_saslc__mech_anonymous
55     __weak_reference(saslc__mech_anonymous);
56 __weakref_visible const saslc__mech_t weak_saslc__mech_crammd5
57     __weak_reference(saslc__mech_crammd5);
58 __weakref_visible const saslc__mech_t weak_saslc__mech_digestmd5
59     __weak_reference(saslc__mech_digestmd5);
60 __weakref_visible const saslc__mech_t weak_saslc__mech_external
61     __weak_reference(saslc__mech_external);
62 __weakref_visible const saslc__mech_t weak_saslc__mech_gssapi
63     __weak_reference(saslc__mech_gssapi);
64 __weakref_visible const saslc__mech_t weak_saslc__mech_login
65     __weak_reference(saslc__mech_login);
66 __weakref_visible const saslc__mech_t weak_saslc__mech_plain
67     __weak_reference(saslc__mech_plain);
68 
69 static const saslc__mech_t *saslc__mechanisms[] = {
70 	&weak_saslc__mech_anonymous,
71 	&weak_saslc__mech_crammd5,
72 	&weak_saslc__mech_digestmd5,
73 	&weak_saslc__mech_external,
74 	&weak_saslc__mech_gssapi,
75 	&weak_saslc__mech_login,
76 	&weak_saslc__mech_plain,
77 };
78 
79 /*
80  * This table is used by the inline functions in mech.h, which are
81  * used in mech_digestmd5.c and mech_gssapi.c.
82  *
83  * NB: This is indexed by saslc__mech_sess_qop_t values and must be
84  * NULL terminated for use in saslc__list_flags().
85  */
86 const named_flag_t saslc__mech_qop_tbl[] = {
87 	{ "auth",	F_QOP_NONE },
88 	{ "auth-int",	F_QOP_INT },
89 	{ "auth-conf",	F_QOP_CONF },
90 	{ NULL,		0 }
91 };
92 
93 /**
94  * @brief creates a list of supported mechanisms and their resources.
95  * @param ctx context
96  * @return pointer to head of the list, NULL if allocation failed
97  */
98 saslc__mech_list_t *
saslc__mech_list_create(saslc_t * ctx)99 saslc__mech_list_create(saslc_t *ctx)
100 {
101 	saslc__mech_list_t *head = NULL;
102 	saslc__mech_list_node_t *node = NULL;
103 	size_t i;
104 
105 	if ((head = calloc(1, sizeof(*head))) == NULL) {
106 		saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
107 		return NULL;
108 	}
109 	for (i = 0; i < __arraycount(saslc__mechanisms); i++) {
110 		if (saslc__mechanisms[i] == NULL)
111 			continue;
112 		if ((node = calloc(1, sizeof(*node))) == NULL)
113 			goto error;
114 
115 		if ((node->prop = saslc__dict_create()) == NULL) {
116 			free(node);
117 			goto error;
118 		}
119 
120 		node->mech = saslc__mechanisms[i];
121 		LIST_INSERT_HEAD(head, node, nodes);
122 	}
123 	return head;
124 
125  error:
126 	saslc__error_set_errno(ERR(ctx), ERROR_NOMEM);
127 	saslc__mech_list_destroy(head);
128 	return NULL;
129 }
130 
131 /**
132  * @brief gets mechanism from the list using name
133  * @param list mechanisms list
134  * @param mech_name mechanism name
135  * @return pointer to the mechanism, NULL if mechanism was not found
136  */
137 saslc__mech_list_node_t *
saslc__mech_list_get(saslc__mech_list_t * list,const char * mech_name)138 saslc__mech_list_get(saslc__mech_list_t *list, const char *mech_name)
139 {
140 	saslc__mech_list_node_t *node;
141 
142 	LIST_FOREACH(node, list, nodes) {
143 		if (strcasecmp(node->mech->name, mech_name) == 0)
144 			return node;
145 	}
146 	return NULL;
147 }
148 
149 /**
150  * @brief destroys and deallocates mechanism list
151  * @param list mechanisms list
152  */
153 void
saslc__mech_list_destroy(saslc__mech_list_t * list)154 saslc__mech_list_destroy(saslc__mech_list_t *list)
155 {
156 	saslc__mech_list_node_t *node;
157 
158 	while (!LIST_EMPTY(list)) {
159 		node = LIST_FIRST(list);
160 		LIST_REMOVE(node, nodes);
161 		saslc__dict_destroy(node->prop);
162 		free(node);
163 	}
164 	free(list);
165 }
166 
167 /**
168  * @brief doing copy of the session property, on error sets
169  * error message for the session. Copied data is located in *out and *outlen
170  * @param sess sasl session
171  * @param out out buffer for the session property copy
172  * @param outlen length of the session property copy
173  * @param name name of the property
174  * @param error_msg error messages set on failure
175  * @return MECH_OK - on success
176  * MECH_ERROR - on failure
177  */
178 int
saslc__mech_strdup(saslc_sess_t * sess,char ** out,size_t * outlen,const char * name,const char * error_msg)179 saslc__mech_strdup(saslc_sess_t *sess, char **out, size_t *outlen,
180     const char *name, const char *error_msg)
181 {
182 	const char *value; /* property value */
183 
184 	/* get value */
185 	if ((value = saslc_sess_getprop(sess, name)) == NULL) {
186 		saslc__error_set(ERR(sess), ERROR_MECH, error_msg);
187 		return MECH_ERROR;
188 	}
189 	saslc__msg_dbg("saslc__mech_strdup: value='%s'\n", value);
190 
191 	/* copy value */
192 	if ((*out = strdup(value)) == NULL) {
193 		saslc__error_set_errno(ERR(sess), ERROR_NOMEM);
194 		return MECH_ERROR;
195 	}
196 	if (outlen != NULL)
197 		*outlen = strlen(value);
198 
199 	return MECH_OK;
200 }
201 
202 /**
203  * @brief generic session create function, this
204  * function is suitable for the most mechanisms.
205  * @return 0 on success, -1 on failure
206  */
207 int
saslc__mech_generic_create(saslc_sess_t * sess)208 saslc__mech_generic_create(saslc_sess_t *sess)
209 {
210 	saslc__mech_sess_t *ms;
211 
212 	if ((ms = calloc(1, sizeof(*ms))) == NULL) {
213 		saslc__error_set(ERR(sess), ERROR_NOMEM, NULL);
214 		return -1;
215 	}
216 	sess->mech_sess = ms;
217 	return 0;
218 }
219 
220 /**
221  * @brief generic session destroy function, this
222  * function is suitable for the most mechanisms.
223  * @return function always returns 0
224  */
225 int
saslc__mech_generic_destroy(saslc_sess_t * sess)226 saslc__mech_generic_destroy(saslc_sess_t *sess)
227 {
228 
229 	free(sess->mech_sess);
230 	sess->mech_sess = NULL;
231 	return 0;
232 }
233