1 /* digest-md5.c --- Test the DIGEST-MD5 mechanism.
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_callback_service (Gsasl_session_ctx
* ctx
,
61 size_t * hostlen
, char *srvname
, size_t * srvnamelen
)
67 client_cb_authentication_id (Gsasl_session_ctx
* xctx
,
68 char *out
, size_t * outlen
)
70 size_t needlen
= strlen (USERNAME
);
72 if (out
&& *outlen
< needlen
)
73 return GSASL_TOO_SMALL_BUFFER
;
77 memcpy (out
, USERNAME
, *outlen
);
83 client_cb_password (Gsasl_session_ctx
* xctx
, char *out
, size_t * outlen
)
85 size_t needlen
= strlen (PASSWORD
);
87 if (out
&& *outlen
< needlen
)
88 return GSASL_TOO_SMALL_BUFFER
;
92 memcpy (out
, PASSWORD
, *outlen
);
100 Gsasl_ctx
*ctx
= NULL
;
101 Gsasl_session_ctx
*server
= NULL
, *client
= NULL
;
107 res
= gsasl_init (&ctx
);
110 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
114 if (!gsasl_client_support_p (ctx
, "DIGEST-MD5") ||
115 !gsasl_server_support_p (ctx
, "DIGEST-MD5"))
117 printf ("No support for DIGEST-MD5...\n");
121 gsasl_server_callback_retrieve_set (ctx
, server_cb_retrieve
);
123 gsasl_client_callback_service_set (ctx
, client_callback_service
);
125 gsasl_client_callback_authentication_id_set (ctx
,
126 client_cb_authentication_id
);
127 gsasl_client_callback_password_set (ctx
, client_cb_password
);
130 for (i
= 0; i
< 5; i
++)
132 res
= gsasl_server_start (ctx
, "DIGEST-MD5", &server
);
135 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
138 res
= gsasl_client_start (ctx
, "DIGEST-MD5", &client
);
141 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
145 /* Server begins... */
147 res
= gsasl_step (server
, NULL
, 0, &s1
, &s1len
);
148 if (res
!= GSASL_NEEDS_MORE
)
150 fail ("gsasl_step(1) failed (%d):\n%s\n", res
,
151 gsasl_strerror (res
));
156 printf ("S: %.*s\n", s1len
, s1
);
158 /* Client respond... */
160 res
= gsasl_step (client
, s1
, s1len
, &s2
, &s2len
);
162 if (res
!= GSASL_NEEDS_MORE
)
164 fail ("gsasl_step(2) failed (%d):\n%s\n", res
,
165 gsasl_strerror (res
));
170 printf ("C: %.*s\n", s2len
, s2
);
172 /* Server finishes... */
174 res
= gsasl_step (server
, s2
, s2len
, &s1
, &s1len
);
176 if (res
!= GSASL_NEEDS_MORE
)
178 fail ("gsasl_step(3) failed (%d):\n%s\n", res
,
179 gsasl_strerror (res
));
184 printf ("S: %.*s\n", s1len
, s1
);
186 /* Client finishes... */
188 res
= gsasl_step (client
, s1
, s1len
, &s2
, &s2len
);
192 fail ("gsasl_step(4) failed (%d):\n%s\n", res
,
193 gsasl_strerror (res
));
199 /* Solaris x86 crashes here if s2 is NULL, even when s2len
202 printf ("C: %.*s\n", s2len
, s2
);
207 /* Server is done. */
209 res
= gsasl_step (server
, s2
, s2len
, &s1
, &s1len
);
213 fail ("gsasl_step(5) failed (%d):\n%s\n", res
,
214 gsasl_strerror (res
));
220 fail ("gsasl_step() failed, additional length=%d:\n", s1len
);
230 gsasl_client_finish (client
);
231 gsasl_server_finish (server
);