mech-cram-md5.c revision e80203675151ef9d4f3f850cf02041042eb13096
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (C) 2002,2003 Timo Sirainen / Joshua Goodall */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* CRAM-MD5 SASL authentication, see RFC-2195
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen Joshua Goodall <joshua@roughtrade.net> */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /* requested: */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /* received: */
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen unsigned long maxbuf;
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic const char *get_cram_challenge(void)
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen return t_strdup_printf("<%s.%s@%s>", (const char *)buf,
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic int verify_credentials(struct cram_auth_request *request,
13a8c553f293349248b161ff851743498916e26eTimo Sirainen if (hex_to_binary(credentials, context_digest_buf) < 0) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_error("cram-md5(%s): passdb credentials are not in hex",
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen hmac_md5_set_cram_context(&ctx, context_digest);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen hmac_md5_update(&ctx, request->challenge, strlen(request->challenge));
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (memcmp(response_hex, request->response, 32) != 0) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainenstatic int parse_cram_response(struct cram_auth_request *request,
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen const char **error_r)
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen /* <username> SPACE <response>. Username may contain spaces, so assume
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen the rightmost space is the response separator. */
0ae010139a1bb3b29fbf117c5da1a6a6c6b7b5a0Timo Sirainen request->username = p_strndup(request->pool, data, space);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen p_strndup(request->pool, data + space, size - space);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic void credentials_callback(enum passdb_result result,
1ac19c5c2b66a12f5598792aad15114ee3eb62e2Timo Sirainenmech_cram_md5_auth_continue(struct auth_request *auth_request,
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (parse_cram_response(request, data, data_size, &error)) {
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen p_strdup(auth_request->pool, request->username);
dc9de21d4375faeedbe5b7e941502ac578650da9Timo Sirainen if (mech_fix_username(auth_request->user, &error)) {
6b2738c39a868ff9291867138c55029fc40cf105Timo Sirainen i_info("cram-md5(%s): %s", get_log_prefix(auth_request), error);