1 /* cram-md5.c --- Test the CRAM-MD5 mechanism, using old callback API.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Simon Josefsson
4 * This file is part of GNU SASL.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #define PASSWORD "Open, Sesame"
33 #define USERNAME "Ali Baba"
34 /* "Ali " "\xC2\xAD" "Bab" "\xC2\xAA" */
35 /* "Al\xC2\xAA""dd\xC2\xAD""in\xC2\xAE" */
38 server_cb_retrieve (Gsasl_session_ctx
* xctx
,
39 const char *authentication_id
,
40 const char *authorization_id
,
41 const char *realm
, char *key
, size_t * keylen
)
43 size_t needlen
= strlen (PASSWORD
);
45 if (key
&& *keylen
< needlen
)
46 return GSASL_TOO_SMALL_BUFFER
;
50 memcpy (key
, PASSWORD
, *keylen
);
56 client_cb_authentication_id (Gsasl_session_ctx
* xctx
,
57 char *out
, size_t * outlen
)
59 size_t needlen
= strlen (USERNAME
);
61 if (out
&& *outlen
< needlen
)
62 return GSASL_TOO_SMALL_BUFFER
;
66 memcpy (out
, USERNAME
, *outlen
);
72 client_cb_password (Gsasl_session_ctx
* xctx
, char *out
, size_t * outlen
)
74 size_t needlen
= strlen (PASSWORD
);
76 if (out
&& *outlen
< needlen
)
77 return GSASL_TOO_SMALL_BUFFER
;
81 memcpy (out
, PASSWORD
, *outlen
);
89 Gsasl_ctx
*ctx
= NULL
;
90 Gsasl_session_ctx
*server
= NULL
, *client
= NULL
;
96 res
= gsasl_init (&ctx
);
99 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
103 gsasl_server_callback_retrieve_set (ctx
, server_cb_retrieve
);
105 gsasl_client_callback_authentication_id_set (ctx
,
106 client_cb_authentication_id
);
107 gsasl_client_callback_password_set (ctx
, client_cb_password
);
110 for (i
= 0; i
< 5; i
++)
112 res
= gsasl_server_start (ctx
, "CRAM-MD5", &server
);
115 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
118 res
= gsasl_client_start (ctx
, "CRAM-MD5", &client
);
121 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
125 res
= gsasl_step (server
, NULL
, 0, &s1
, &s1len
);
126 if (res
!= GSASL_NEEDS_MORE
)
128 fail ("gsasl_step() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
133 printf ("S: %.*s\n", s1len
, s1
);
135 res
= gsasl_step (client
, s1
, s1len
, &s2
, &s2len
);
139 fail ("gsasl_step() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
144 printf ("C: %.*s\n", s2len
, s2
);
146 res
= gsasl_step (server
, s2
, s2len
, &s1
, &s1len
);
150 fail ("gsasl_step() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
156 fail ("gsasl_step() failed, additional length=%d:\n", s1len
);
166 gsasl_client_finish (client
);
167 gsasl_server_finish (server
);