xref: /netbsd-src/crypto/external/bsd/libsaslc/dist/src/mech_plain.c (revision beea8b97d4a59f9d0f17f6dc5a9e6bda59e0bc43)
1*beea8b97Schristos /* $NetBSD: mech_plain.c,v 1.4 2011/02/12 23:21:32 christos Exp $ */
2231558cbSagc 
3231558cbSagc /* Copyright (c) 2010 The NetBSD Foundation, Inc.
4231558cbSagc  * All rights reserved.
5231558cbSagc  *
6231558cbSagc  * This code is derived from software contributed to The NetBSD Foundation
7231558cbSagc  * by Mateusz Kocielski.
8231558cbSagc  *
9231558cbSagc  * Redistribution and use in source and binary forms, with or without
10231558cbSagc  * modification, are permitted provided that the following conditions
11231558cbSagc  * are met:
12231558cbSagc  * 1. Redistributions of source code must retain the above copyright
13231558cbSagc  *    notice, this list of conditions and the following disclaimer.
14231558cbSagc  * 2. Redistributions in binary form must reproduce the above copyright
15231558cbSagc  *    notice, this list of conditions and the following disclaimer in the
16231558cbSagc  *    documentation and/or other materials provided with the distribution.
17231558cbSagc  * 3. All advertising materials mentioning features or use of this software
18231558cbSagc  *    must display the following acknowledgement:
19231558cbSagc  *        This product includes software developed by the NetBSD
20231558cbSagc  *        Foundation, Inc. and its contributors.
21231558cbSagc  * 4. Neither the name of The NetBSD Foundation nor the names of its
22231558cbSagc  *    contributors may be used to endorse or promote products derived
23231558cbSagc  *    from this software without specific prior written permission.
24231558cbSagc  *
25231558cbSagc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26231558cbSagc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27231558cbSagc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28231558cbSagc  * PURPOSE ARE DISCLAIMED.	IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29231558cbSagc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30231558cbSagc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31231558cbSagc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32231558cbSagc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33231558cbSagc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34231558cbSagc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35231558cbSagc  * POSSIBILITY OF SUCH DAMAGE.
36231558cbSagc  */
3719c14409Schristos #include <sys/cdefs.h>
38*beea8b97Schristos __RCSID("$NetBSD: mech_plain.c,v 1.4 2011/02/12 23:21:32 christos Exp $");
39231558cbSagc 
40231558cbSagc #include <saslc.h>
41231558cbSagc #include <stdio.h>
42231558cbSagc #include <string.h>
43231558cbSagc 
4419c14409Schristos #include "error.h"
4519c14409Schristos #include "mech.h"
4619c14409Schristos #include "msg.h"
4719c14409Schristos #include "saslc_private.h"
4819c14409Schristos 
4919c14409Schristos 
5019c14409Schristos /* See RFC 2595. */
51231558cbSagc 
52231558cbSagc /* properties */
5319c14409Schristos #define SASLC_PLAIN_AUTHCID	SASLC_PROP_AUTHCID	/* username key */
5419c14409Schristos #define SASLC_PLAIN_AUTHZID	SASLC_PROP_AUTHZID	/* authorization id */
5519c14409Schristos #define SASLC_PLAIN_PASSWD	SASLC_PROP_PASSWD	/* password key */
56231558cbSagc 
57231558cbSagc #define NUL_DELIM	'\x00'
58231558cbSagc #define CRED_MAX_LEN	255
59231558cbSagc 
60231558cbSagc /**
61231558cbSagc  * @brief doing one step of the sasl authentication
62231558cbSagc  * @param sess sasl session
63231558cbSagc  * @param in input data
64231558cbSagc  * @param inlen input data length
65231558cbSagc  * @param out place to store output data
66231558cbSagc  * @param outlen output data length
67231558cbSagc  * @return MECH_OK - success,
68231558cbSagc  * MECH_STEP - more steps are needed,
69231558cbSagc  * MECH_ERROR - error
70231558cbSagc  */
71231558cbSagc /*ARGSUSED*/
72231558cbSagc static int
saslc__mech_plain_cont(saslc_sess_t * sess,const void * in __unused,size_t inlen __unused,void ** out,size_t * outlen)7319c14409Schristos saslc__mech_plain_cont(saslc_sess_t *sess, const void *in __unused,
7419c14409Schristos     size_t inlen __unused, void **out, size_t *outlen)
75231558cbSagc {
76231558cbSagc 	const char *authzid, *authcid, *passwd;
77231558cbSagc 	char *outstr;
78231558cbSagc 	int len;
79231558cbSagc 
80231558cbSagc 	authzid = saslc_sess_getprop(sess, SASLC_PLAIN_AUTHZID);
81231558cbSagc 	if (authzid != NULL && strlen(authzid) > CRED_MAX_LEN) {
82231558cbSagc 		saslc__error_set(ERR(sess), ERROR_MECH,
83231558cbSagc 		    "authzid should be shorter than 256 characters");
84231558cbSagc 		return MECH_ERROR;
85231558cbSagc 	}
86231558cbSagc 
87231558cbSagc 	if ((authcid = saslc_sess_getprop(sess, SASLC_PLAIN_AUTHCID))
88231558cbSagc 	    == NULL) {
89231558cbSagc 		saslc__error_set(ERR(sess), ERROR_MECH,
90231558cbSagc 			"authcid is required for an authentication");
91231558cbSagc 		return MECH_ERROR;
92231558cbSagc 	}
93231558cbSagc 	if (strlen(authcid) > CRED_MAX_LEN) {
94231558cbSagc 		saslc__error_set(ERR(sess), ERROR_MECH,
95231558cbSagc 		    "authcid should be shorter than 256 characters");
96231558cbSagc 		return MECH_ERROR;
97231558cbSagc 	}
98231558cbSagc 
9919c14409Schristos 	if ((passwd = saslc_sess_getprop(sess, SASLC_PLAIN_PASSWD))
100231558cbSagc 	    == NULL) {
101231558cbSagc 		saslc__error_set(ERR(sess), ERROR_MECH,
102231558cbSagc 			"passwd is required for an authentication");
103231558cbSagc 		return MECH_ERROR;
104231558cbSagc 	}
105231558cbSagc 	if (strlen(passwd) > CRED_MAX_LEN) {
106231558cbSagc 		saslc__error_set(ERR(sess), ERROR_MECH,
107231558cbSagc 		    "passwd should be shorter than 256 characters");
108231558cbSagc 		return MECH_ERROR;
109231558cbSagc 	}
110231558cbSagc 
11119c14409Schristos 	len = asprintf(&outstr, "%s%c%s%c%s", authzid != NULL ? authzid : "",
11219c14409Schristos 	    NUL_DELIM, authcid, NUL_DELIM, passwd);
1136b638291Sagc 	if (len == -1) {
1146b638291Sagc 		saslc__error_set_errno(ERR(sess), ERROR_NOMEM);
115231558cbSagc 		return MECH_ERROR;
1166b638291Sagc 	}
117231558cbSagc 	*out = outstr;
11819c14409Schristos 	*outlen = len;
11919c14409Schristos 
12019c14409Schristos 	saslc__msg_dbg("saslc__mech_plain_cont: "
12119c14409Schristos 	    "authzid='%s' authcid='%s' passwd='%s'\n",
12219c14409Schristos 	    authzid != NULL ? authzid : "", authcid, passwd);
123231558cbSagc 
124231558cbSagc 	return MECH_OK;
125231558cbSagc }
126231558cbSagc 
127231558cbSagc /* mechanism definition */
128231558cbSagc const saslc__mech_t saslc__mech_plain = {
12919c14409Schristos 	.name	 = "PLAIN",
13019c14409Schristos 	.flags	 = FLAG_PLAINTEXT,
13119c14409Schristos 	.create	 = saslc__mech_generic_create,
13219c14409Schristos 	.cont	 = saslc__mech_plain_cont,
13319c14409Schristos 	.encode	 = NULL,
13419c14409Schristos 	.decode	 = NULL,
13519c14409Schristos 	.destroy = saslc__mech_generic_destroy
136231558cbSagc };
137