3 ** Copyright 2000-2008 Double Precision, Inc. See COPYING for
4 ** distribution information.
7 #include "courier_auth_config.h"
8 #include "courierauthsasl.h"
9 #include "libhmac/hmac.h"
10 #include "authsaslclient.h"
17 int authsaslclient_cram(const struct authsaslclientinfo
*info
,
18 const char *challenge
,
19 const struct hmac_hashinfo
*hashinfo
)
21 char *base64buf
=malloc(strlen(challenge
)+1);
22 unsigned char *keybuf
;
24 const char *userid
=info
->userid
? info
->userid
:"";
25 const char *password
=info
->password
? info
->password
:"";
31 return (AUTHSASL_ERROR
);
33 strcpy(base64buf
, challenge
);
35 if ( (i
=authsasl_frombase64(base64buf
))<0 ||
36 (keybuf
=(unsigned char *)malloc(hashinfo
->hh_L
*3)) == 0)
40 return (AUTHSASL_ERROR
);
43 hmac_hashkey( hashinfo
, password
, strlen(password
),
44 keybuf
, keybuf
+hashinfo
->hh_L
);
46 hmac_hashtext( hashinfo
, base64buf
, i
,
47 keybuf
, keybuf
+hashinfo
->hh_L
,
48 keybuf
+hashinfo
->hh_L
*2);
51 base64buf
=malloc(strlen(userid
)+2+hashinfo
->hh_L
*2);
56 return (AUTHSASL_ERROR
);
58 strcat(strcpy(base64buf
, userid
), " ");
59 p
=base64buf
+strlen(base64buf
);
60 for (i
=0; i
<hashinfo
->hh_L
; i
++)
62 static const char xdigit
[]="0123456789abcdef";
63 int c
=keybuf
[hashinfo
->hh_L
*2+i
];
65 *p
++ = xdigit
[ (c
>> 4) & 0x0F ];
66 *p
++ = xdigit
[c
& 0x0F];
70 keybuf
=(unsigned char *)authsasl_tobase64(base64buf
, -1);
77 return (AUTHSASL_ERROR
);
79 i
= (*info
->final_conv_func
)((char *)keybuf
, info
->conv_func_arg
);