xref: /netbsd-src/crypto/external/bsd/libsaslc/dist/src/mech_crammd5.c (revision beea8b97d4a59f9d0f17f6dc5a9e6bda59e0bc43)
1*beea8b97Schristos /* $NetBSD: mech_crammd5.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_crammd5.c,v 1.4 2011/02/12 23:21:32 christos Exp $");
39*beea8b97Schristos 
40*beea8b97Schristos #include <sys/param.h>
41231558cbSagc 
42231558cbSagc #include <saslc.h>
43231558cbSagc #include <stdio.h>
44231558cbSagc #include <stdlib.h>
45231558cbSagc #include <string.h>
46231558cbSagc 
4719c14409Schristos #include "crypto.h"
4819c14409Schristos #include "mech.h"
4919c14409Schristos #include "saslc_private.h"
5019c14409Schristos 
5119c14409Schristos 
5219c14409Schristos /* See RFC 2195. */
53231558cbSagc 
54231558cbSagc /* properties */
5519c14409Schristos #define SASLC_CRAMMD5_AUTHCID		SASLC_PROP_AUTHCID
5619c14409Schristos #define SASLC_CRAMMD5_PASSWD		SASLC_PROP_PASSWD
57231558cbSagc 
58231558cbSagc /**
5919c14409Schristos  * @brief do one step of the sasl authentication
60231558cbSagc  * @param sess sasl session
61231558cbSagc  * @param in input data
62231558cbSagc  * @param inlen input data length
63231558cbSagc  * @param out place to store output data
64231558cbSagc  * @param outlen output data length
65231558cbSagc  * @return MECH_OK - success,
66231558cbSagc  * MECH_STEP - more steps are needed,
67231558cbSagc  * MECH_ERROR - error
68231558cbSagc  */
69231558cbSagc static int
saslc__mech_crammd5_cont(saslc_sess_t * sess,const void * in,size_t inlen,void ** out,size_t * outlen)70231558cbSagc saslc__mech_crammd5_cont(saslc_sess_t *sess, const void *in, size_t inlen,
71231558cbSagc     void **out, size_t *outlen)
72231558cbSagc {
7319c14409Schristos 	const char *authcid, *passwd;
7419c14409Schristos 	char *digest, *name;
7519c14409Schristos 	int len, rv;
76231558cbSagc 
7719c14409Schristos 	/* in case we are called before getting challenge from server */
78231558cbSagc 	if (inlen == 0) {
79231558cbSagc 		*out = NULL;
80231558cbSagc 		*outlen = 0;
81231558cbSagc 		return MECH_STEP;
82231558cbSagc 	}
8319c14409Schristos 	if ((authcid = saslc_sess_getprop(sess, SASLC_CRAMMD5_AUTHCID))
8419c14409Schristos 	    == NULL) {
8519c14409Schristos 		saslc__error_set(ERR(sess), ERROR_MECH,
8619c14409Schristos 		    "authcid is required for an authentication");
8719c14409Schristos 		return MECH_ERROR;
8819c14409Schristos 	}
8919c14409Schristos 	if ((passwd = saslc_sess_getprop(sess, SASLC_CRAMMD5_PASSWD))
9019c14409Schristos 	    == NULL) {
9119c14409Schristos 		saslc__error_set(ERR(sess), ERROR_MECH,
9219c14409Schristos 		    "passwd is required for an authentication");
9319c14409Schristos 		return MECH_ERROR;
9419c14409Schristos 	}
9519c14409Schristos 	digest = saslc__crypto_hmac_md5_hex((const unsigned char *)passwd,
96231558cbSagc 					    strlen(passwd), in, inlen);
976b638291Sagc 	if (digest == NULL) {
98231558cbSagc 		saslc__error_set_errno(ERR(sess), ERROR_NOMEM);
99231558cbSagc 		return MECH_ERROR;
100231558cbSagc 	}
10119c14409Schristos 	if ((len = asprintf(&name, "%s %s", authcid, digest)) == -1) {
1026b638291Sagc 		saslc__error_set_errno(ERR(sess), ERROR_NOMEM);
10319c14409Schristos 		rv = MECH_ERROR;
1046b638291Sagc 	}
10519c14409Schristos 	else {
10619c14409Schristos 		*out = name;
10719c14409Schristos 		*outlen = len;	/* don't count the '\0' byte */
10819c14409Schristos 		rv = MECH_OK;
10919c14409Schristos 	}
11019c14409Schristos 	memset(digest, 0, HMAC_MD5_DIGEST_LENGTH);
11119c14409Schristos 	free(digest);
11219c14409Schristos 	return rv;
113231558cbSagc }
114231558cbSagc 
115231558cbSagc /* mechanism definition */
116231558cbSagc const saslc__mech_t saslc__mech_crammd5 = {
11719c14409Schristos 	.name	 = "CRAM-MD5",
11819c14409Schristos 	.flags	 = FLAG_DICTIONARY,
11919c14409Schristos 	.create	 = saslc__mech_generic_create,
12019c14409Schristos 	.cont	 = saslc__mech_crammd5_cont,
12119c14409Schristos 	.encode	 = NULL,
12219c14409Schristos 	.decode	 = NULL,
12319c14409Schristos 	.destroy = saslc__mech_generic_destroy
124231558cbSagc };
125