1 /* CRAM-MD5 SASL plugin
4 * $Id: cram.c,v 1.85 2004/09/08 10:57:56 mel Exp $
7 * Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. The name "Carnegie Mellon University" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For permission or any other legal
24 * details, please contact
25 * Office of Technology Transfer
26 * Carnegie Mellon University
28 * Pittsburgh, PA 15213-3890
29 * (412) 268-4387, fax: (412) 268-7395
30 * tech-transfer@andrew.cmu.edu
32 * 4. Redistributions of any form whatsoever must retain the following
34 * "This product includes software developed by Computing Services
35 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
60 #include "plugin_common.h"
63 #include <sasl_cram_plugin_decl.h>
66 /***************************** Common Section *****************************/
68 static const char plugin_id[] = "$Id: cram.c,v 1.85 2004/09/08 10:57:56 mel Exp $";
70 /* convert a string of 8bit chars to it's representation in hex
71 * using lowercase letters
73 static char *convert16(unsigned char *in, int inlen, const sasl_utils_t *utils)
75 static char hex[]="0123456789abcdef";
79 out = utils->malloc(inlen*2+1);
80 if (out == NULL) return NULL;
82 for (lup=0; lup < inlen; lup++) {
83 out[lup*2] = hex[in[lup] >> 4];
84 out[lup*2+1] = hex[in[lup] & 15];
92 /***************************** Server Section *****************************/
94 typedef struct server_context {
101 crammd5_server_mech_new(void *glob_context __attribute__((unused)),
102 sasl_server_params_t *sparams,
103 const char *challenge __attribute__((unused)),
104 unsigned challen __attribute__((unused)),
107 server_context_t *text;
109 /* holds state are in */
110 text = sparams->utils->malloc(sizeof(server_context_t));
112 MEMERROR( sparams->utils );
116 memset(text, 0, sizeof(server_context_t));
120 *conn_context = text;
126 * Returns the current time (or part of it) in string form
129 static char *gettime(sasl_server_params_t *sparams)
135 ret= sparams->utils->malloc(15);
136 if (ret==NULL) return NULL;
138 /* the bottom bits are really the only random ones so if
139 we overflow we don't want to loose them */
140 snprintf(ret,15,"%lu",t%(0xFFFFFF));
145 static char *randomdigits(sasl_server_params_t *sparams)
149 unsigned char temp[5]; /* random 32-bit number */
151 sparams->utils->rand(sparams->utils->rpool,(char *) temp,4);
152 num=(temp[0] * 256 * 256 * 256) +
153 (temp[1] * 256 * 256) +
157 ret = sparams->utils->malloc(15); /* there's no way an unsigned can be longer than this right? */
158 if (ret == NULL) return NULL;
159 sprintf(ret, "%u", num);
165 crammd5_server_mech_step1(server_context_t *text,
166 sasl_server_params_t *sparams,
167 const char *clientin __attribute__((unused)),
168 unsigned clientinlen,
169 const char **serverout,
170 unsigned *serveroutlen,
171 sasl_out_params_t *oparams __attribute__((unused)))
173 char *time, *randdigits;
175 /* we shouldn't have received anything */
176 if (clientinlen != 0) {
177 SETERROR(sparams->utils, "CRAM-MD5 does not accpet inital data");
181 /* get time and a random number for the nonce */
182 time = gettime(sparams);
183 randdigits = randomdigits(sparams);
184 if ((time == NULL) || (randdigits == NULL)) {
185 MEMERROR( sparams->utils );
189 /* allocate some space for the challenge */
190 text->challenge = sparams->utils->malloc(200 + 1);
191 if (text->challenge == NULL) {
192 MEMERROR(sparams->utils);
196 /* create the challenge */
197 snprintf(text->challenge, 200, "<%s.%s@%s>", randdigits, time,
198 sparams->serverFQDN);
200 *serverout = text->challenge;
201 *serveroutlen = (unsigned) strlen(text->challenge);
204 sparams->utils->free(time);
205 sparams->utils->free(randdigits);
209 return SASL_CONTINUE;
213 crammd5_server_mech_step2(server_context_t *text,
214 sasl_server_params_t *sparams,
215 const char *clientin,
216 unsigned clientinlen,
217 const char **serverout __attribute__((unused)),
218 unsigned *serveroutlen __attribute__((unused)),
219 sasl_out_params_t *oparams)
222 sasl_secret_t *sec = NULL;
225 int result = SASL_FAIL;
226 const char *password_request[] = { SASL_AUX_PASSWORD,
227 "*cmusaslsecretCRAM-MD5",
229 struct propval auxprop_values[3];
230 HMAC_MD5_CTX tmphmac;
231 HMAC_MD5_STATE md5state;
232 int clear_md5state = 0;
233 char *digest_str = NULL;
236 /* extract userid; everything before last space */
238 while ((pos > 0) && (clientin[pos] != ' ')) pos--;
241 SETERROR( sparams->utils,"need authentication name");
245 userid = (char *) sparams->utils->malloc(pos+1);
246 if (userid == NULL) {
247 MEMERROR( sparams->utils);
251 /* copy authstr out */
252 memcpy(userid, clientin, pos);
255 result = sparams->utils->prop_request(sparams->propctx, password_request);
256 if (result != SASL_OK) goto done;
258 /* this will trigger the getting of the aux properties */
259 result = sparams->canon_user(sparams->utils->conn,
260 userid, 0, SASL_CU_AUTHID | SASL_CU_AUTHZID,
262 if (result != SASL_OK) goto done;
264 result = sparams->utils->prop_getnames(sparams->propctx,
268 ((!auxprop_values[0].name || !auxprop_values[0].values) &&
269 (!auxprop_values[1].name || !auxprop_values[1].values))) {
270 /* We didn't find this username */
271 sparams->utils->seterror(sparams->utils->conn,0,
272 "no secret in database");
273 result = sparams->transition ? SASL_TRANS : SASL_NOUSER;
277 if (auxprop_values[0].name && auxprop_values[0].values) {
278 len = strlen(auxprop_values[0].values[0]);
280 sparams->utils->seterror(sparams->utils->conn,0,
286 sec = sparams->utils->malloc(sizeof(sasl_secret_t) + len);
289 sec->len = (unsigned) len;
290 strncpy((char *)sec->data, auxprop_values[0].values[0], len + 1);
293 /* Do precalculation on plaintext secret */
294 sparams->utils->hmac_md5_precalc(&md5state, /* OUT */
297 } else if (auxprop_values[1].name && auxprop_values[1].values) {
298 /* We have a precomputed secret */
299 memcpy(&md5state, auxprop_values[1].values[0],
300 sizeof(HMAC_MD5_STATE));
302 sparams->utils->seterror(sparams->utils->conn, 0,
303 "Have neither type of secret");
307 /* erase the plaintext password */
308 sparams->utils->prop_erase(sparams->propctx, password_request[0]);
310 /* ok this is annoying:
311 so we have this half-way hmac transform instead of the plaintext
312 that means we half to:
313 -import it back into a md5 context
314 -do an md5update with the nonce
317 sparams->utils->hmac_md5_import(&tmphmac, (HMAC_MD5_STATE *) &md5state);
318 sparams->utils->MD5Update(&(tmphmac.ictx),
319 (const unsigned char *) text->challenge,
320 (unsigned) strlen(text->challenge));
321 sparams->utils->hmac_md5_final((unsigned char *) &digest, &tmphmac);
323 /* convert to base 16 with lower case letters */
324 digest_str = convert16((unsigned char *) digest, 16, sparams->utils);
326 /* if same then verified
327 * - we know digest_str is null terminated but clientin might not be
328 * - verify the length of clientin anyway!
330 len = strlen(digest_str);
331 if (clientinlen-pos-1 < len ||
332 strncmp(digest_str, clientin+pos+1, len) != 0) {
333 sparams->utils->seterror(sparams->utils->conn, 0,
334 "incorrect digest response");
335 result = SASL_BADAUTH;
340 oparams->doneflag = 1;
341 oparams->mech_ssf = 0;
342 oparams->maxoutbuf = 0;
343 oparams->encode_context = NULL;
344 oparams->encode = NULL;
345 oparams->decode_context = NULL;
346 oparams->decode = NULL;
347 oparams->param_version = 0;
352 if (userid) sparams->utils->free(userid);
353 if (sec) _plug_free_secret(sparams->utils, &sec);
355 if (digest_str) sparams->utils->free(digest_str);
356 if (clear_md5state) memset(&md5state, 0, sizeof(md5state));
361 static int crammd5_server_mech_step(void *conn_context,
362 sasl_server_params_t *sparams,
363 const char *clientin,
364 unsigned clientinlen,
365 const char **serverout,
366 unsigned *serveroutlen,
367 sasl_out_params_t *oparams)
369 server_context_t *text = (server_context_t *) conn_context;
374 /* this should be well more than is ever needed */
375 if (clientinlen > 1024) {
376 SETERROR(sparams->utils, "CRAM-MD5 input longer than 1024 bytes");
380 switch (text->state) {
383 return crammd5_server_mech_step1(text, sparams,
384 clientin, clientinlen,
385 serverout, serveroutlen,
389 return crammd5_server_mech_step2(text, sparams,
390 clientin, clientinlen,
391 serverout, serveroutlen,
394 default: /* should never get here */
395 sparams->utils->log(NULL, SASL_LOG_ERR,
396 "Invalid CRAM-MD5 server step %d\n", text->state);
400 return SASL_FAIL; /* should never get here */
403 static void crammd5_server_mech_dispose(void *conn_context,
404 const sasl_utils_t *utils)
406 server_context_t *text = (server_context_t *) conn_context;
410 if (text->challenge) _plug_free_string(utils,&(text->challenge));
415 static sasl_server_plug_t crammd5_server_plugins[] =
418 "CRAM-MD5", /* mech_name */
421 | SASL_SEC_NOANONYMOUS, /* security_flags */
422 SASL_FEAT_SERVER_FIRST, /* features */
423 NULL, /* glob_context */
424 &crammd5_server_mech_new, /* mech_new */
425 &crammd5_server_mech_step, /* mech_step */
426 &crammd5_server_mech_dispose, /* mech_dispose */
427 NULL, /* mech_free */
429 NULL, /* user_query */
431 NULL, /* mech avail */
436 int crammd5_server_plug_init(const sasl_utils_t *utils,
439 sasl_server_plug_t **pluglist,
442 if (maxversion < SASL_SERVER_PLUG_VERSION) {
443 SETERROR( utils, "CRAM version mismatch");
447 *out_version = SASL_SERVER_PLUG_VERSION;
448 *pluglist = crammd5_server_plugins;
454 /***************************** Client Section *****************************/
456 typedef struct client_context {
458 unsigned out_buf_len;
461 static int crammd5_client_mech_new(void *glob_context __attribute__((unused)),
462 sasl_client_params_t *params,
465 client_context_t *text;
467 /* holds state are in */
468 text = params->utils->malloc(sizeof(client_context_t));
470 MEMERROR(params->utils);
474 memset(text, 0, sizeof(client_context_t));
476 *conn_context = text;
481 static char *make_hashed(sasl_secret_t *sec, char *nonce, int noncelen,
482 const sasl_utils_t *utils)
484 unsigned char digest[24];
487 if (sec == NULL) return NULL;
489 /* do the hmac md5 hash output 128 bits */
490 utils->hmac_md5((unsigned char *) nonce, noncelen,
491 sec->data, sec->len, digest);
493 /* convert that to hex form */
494 in16 = convert16(digest, 16, utils);
495 if (in16 == NULL) return NULL;
500 static int crammd5_client_mech_step(void *conn_context,
501 sasl_client_params_t *params,
502 const char *serverin,
503 unsigned serverinlen,
504 sasl_interact_t **prompt_need,
505 const char **clientout,
506 unsigned *clientoutlen,
507 sasl_out_params_t *oparams)
509 client_context_t *text = (client_context_t *) conn_context;
510 const char *authid = NULL;
511 sasl_secret_t *password = NULL;
512 unsigned int free_password = 0; /* set if we need to free password */
513 int auth_result = SASL_OK;
514 int pass_result = SASL_OK;
522 /* First check for absurd lengths */
523 if (serverinlen > 1024) {
524 params->utils->seterror(params->utils->conn, 0,
525 "CRAM-MD5 input longer than 1024 bytes");
529 /* check if sec layer strong enough */
530 if (params->props.min_ssf > params->external_ssf) {
531 SETERROR( params->utils, "SSF requested of CRAM-MD5 plugin");
535 /* try to get the userid */
536 if (oparams->authid == NULL) {
537 auth_result=_plug_get_authid(params->utils, &authid, prompt_need);
539 if ((auth_result != SASL_OK) && (auth_result != SASL_INTERACT))
543 /* try to get the password */
544 if (password == NULL) {
545 pass_result=_plug_get_password(params->utils, &password,
546 &free_password, prompt_need);
548 if ((pass_result != SASL_OK) && (pass_result != SASL_INTERACT))
552 /* free prompts we got */
553 if (prompt_need && *prompt_need) {
554 params->utils->free(*prompt_need);
558 /* if there are prompts not filled in */
559 if ((auth_result == SASL_INTERACT) || (pass_result == SASL_INTERACT)) {
560 /* make the prompt list */
562 _plug_make_prompts(params->utils, prompt_need,
564 auth_result == SASL_INTERACT ?
565 "Please enter your authentication name" : NULL,
567 pass_result == SASL_INTERACT ?
568 "Please enter your password" : NULL, NULL,
571 if (result != SASL_OK) goto cleanup;
573 return SASL_INTERACT;
577 PARAMERROR(params->utils);
578 return SASL_BADPARAM;
581 result = params->canon_user(params->utils->conn, authid, 0,
582 SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
583 if (result != SASL_OK) goto cleanup;
586 * username SP digest (keyed md5 where key is passwd)
589 in16 = make_hashed(password, (char *) serverin, serverinlen,
593 SETERROR(params->utils, "whoops, make_hashed failed us this time");
598 maxsize = 32+1+strlen(oparams->authid)+30;
599 result = _plug_buf_alloc(params->utils, &(text->out_buf),
600 &(text->out_buf_len), (unsigned) maxsize);
601 if (result != SASL_OK) goto cleanup;
603 snprintf(text->out_buf, maxsize, "%s %s", oparams->authid, in16);
605 *clientout = text->out_buf;
606 *clientoutlen = (unsigned) strlen(*clientout);
609 oparams->doneflag = 1;
610 oparams->mech_ssf = 0;
611 oparams->maxoutbuf = 0;
612 oparams->encode_context = NULL;
613 oparams->encode = NULL;
614 oparams->decode_context = NULL;
615 oparams->decode = NULL;
616 oparams->param_version = 0;
621 /* get rid of private information */
622 if (in16) _plug_free_string(params->utils, &in16);
624 /* get rid of all sensitive info */
625 if (free_password) _plug_free_secret(params-> utils, &password);
630 static void crammd5_client_mech_dispose(void *conn_context,
631 const sasl_utils_t *utils)
633 client_context_t *text = (client_context_t *) conn_context;
637 if (text->out_buf) utils->free(text->out_buf);
642 static sasl_client_plug_t crammd5_client_plugins[] =
645 "CRAM-MD5", /* mech_name */
648 | SASL_SEC_NOANONYMOUS, /* security_flags */
649 SASL_FEAT_SERVER_FIRST, /* features */
650 NULL, /* required_prompts */
651 NULL, /* glob_context */
652 &crammd5_client_mech_new, /* mech_new */
653 &crammd5_client_mech_step, /* mech_step */
654 &crammd5_client_mech_dispose, /* mech_dispose */
655 NULL, /* mech_free */
662 int crammd5_client_plug_init(const sasl_utils_t *utils,
665 sasl_client_plug_t **pluglist,
668 if (maxversion < SASL_CLIENT_PLUG_VERSION) {
669 SETERROR( utils, "CRAM version mismatch");
673 *out_version = SASL_CLIENT_PLUG_VERSION;
674 *pluglist = crammd5_client_plugins;