1 /* cram-md5.c --- Test the CRAM-MD5 mechanism, using old callback API.
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
4 * This file is part of GNU SASL.
6 * GNU SASL 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 2 of the License, or
9 * (at your option) any later version.
11 * GNU SASL 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 GNU SASL; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #define PASSWORD "Open, Sesame"
34 #define USERNAME "Ali Baba"
35 /* "Ali " "\xC2\xAD" "Bab" "\xC2\xAA" */
36 /* "Al\xC2\xAA""dd\xC2\xAD""in\xC2\xAE" */
39 server_cb_retrieve (Gsasl_session_ctx
* xctx
,
40 const char *authentication_id
,
41 const char *authorization_id
,
42 const char *realm
, char *key
, size_t * keylen
)
44 size_t needlen
= strlen (PASSWORD
);
46 if (key
&& *keylen
< needlen
)
47 return GSASL_TOO_SMALL_BUFFER
;
51 memcpy (key
, PASSWORD
, *keylen
);
57 client_cb_authentication_id (Gsasl_session_ctx
* xctx
,
58 char *out
, size_t * outlen
)
60 size_t needlen
= strlen (USERNAME
);
62 if (out
&& *outlen
< needlen
)
63 return GSASL_TOO_SMALL_BUFFER
;
67 memcpy (out
, USERNAME
, *outlen
);
73 client_cb_password (Gsasl_session_ctx
* xctx
, char *out
, size_t * outlen
)
75 size_t needlen
= strlen (PASSWORD
);
77 if (out
&& *outlen
< needlen
)
78 return GSASL_TOO_SMALL_BUFFER
;
82 memcpy (out
, PASSWORD
, *outlen
);
90 Gsasl_ctx
*ctx
= NULL
;
91 Gsasl_session_ctx
*server
= NULL
, *client
= NULL
;
97 res
= gsasl_init (&ctx
);
100 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
104 gsasl_server_callback_retrieve_set (ctx
, server_cb_retrieve
);
106 gsasl_client_callback_authentication_id_set (ctx
,
107 client_cb_authentication_id
);
108 gsasl_client_callback_password_set (ctx
, client_cb_password
);
111 for (i
= 0; i
< 5; i
++)
113 res
= gsasl_server_start (ctx
, "CRAM-MD5", &server
);
116 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
119 res
= gsasl_client_start (ctx
, "CRAM-MD5", &client
);
122 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
126 res
= gsasl_step (server
, NULL
, 0, &s1
, &s1len
);
127 if (res
!= GSASL_NEEDS_MORE
)
129 fail ("gsasl_step() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
134 printf ("S: %.*s\n", s1len
, s1
);
136 res
= gsasl_step (client
, s1
, s1len
, &s2
, &s2len
);
140 fail ("gsasl_step() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
145 printf ("C: %.*s\n", s2len
, s2
);
147 res
= gsasl_step (server
, s2
, s2len
, &s1
, &s1len
);
151 fail ("gsasl_step() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
157 fail ("gsasl_step() failed, additional length=%d:\n", s1len
);
167 gsasl_client_finish (client
);
168 gsasl_server_finish (server
);