Update.
[gsasl.git] / tests / old-digest-md5.c
blob9c62bbf992f4c2adbfd577166fd2ded1a8b5aedf
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.
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "utils.h"
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" */
38 static int
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;
49 *keylen = needlen;
50 if (key)
51 memcpy (key, PASSWORD, *keylen);
53 return GSASL_OK;
56 static int
57 client_callback_service (Gsasl_session_ctx * ctx,
58 char *srv,
59 size_t * srvlen,
60 char *host,
61 size_t * hostlen, char *srvname, size_t * srvnamelen)
63 return GSASL_OK;
66 static int
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;
75 *outlen = needlen;
76 if (out)
77 memcpy (out, USERNAME, *outlen);
79 return GSASL_OK;
82 static int
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;
90 *outlen = needlen;
91 if (out)
92 memcpy (out, PASSWORD, *outlen);
94 return GSASL_OK;
97 void
98 doit (void)
100 Gsasl_ctx *ctx = NULL;
101 Gsasl_session_ctx *server = NULL, *client = NULL;
102 char *s1, *s2;
103 size_t s1len, s2len;
104 size_t i;
105 int res;
107 res = gsasl_init (&ctx);
108 if (res != GSASL_OK)
110 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
111 return;
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");
118 return;
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);
133 if (res != GSASL_OK)
135 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
136 return;
138 res = gsasl_client_start (ctx, "DIGEST-MD5", &client);
139 if (res != GSASL_OK)
141 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
142 return;
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));
152 return;
155 if (debug)
156 printf ("S: %.*s\n", s1len, s1);
158 /* Client respond... */
160 res = gsasl_step (client, s1, s1len, &s2, &s2len);
161 free (s1);
162 if (res != GSASL_NEEDS_MORE)
164 fail ("gsasl_step(2) failed (%d):\n%s\n", res,
165 gsasl_strerror (res));
166 return;
169 if (debug)
170 printf ("C: %.*s\n", s2len, s2);
172 /* Server finishes... */
174 res = gsasl_step (server, s2, s2len, &s1, &s1len);
175 free (s2);
176 if (res != GSASL_NEEDS_MORE)
178 fail ("gsasl_step(3) failed (%d):\n%s\n", res,
179 gsasl_strerror (res));
180 return;
183 if (debug)
184 printf ("S: %.*s\n", s1len, s1);
186 /* Client finishes... */
188 res = gsasl_step (client, s1, s1len, &s2, &s2len);
189 free (s1);
190 if (res != GSASL_OK)
192 fail ("gsasl_step(4) failed (%d):\n%s\n", res,
193 gsasl_strerror (res));
194 return;
197 if (debug)
199 /* Solaris x86 crashes here if s2 is NULL, even when s2len
200 is 0. */
201 if (s2len)
202 printf ("C: %.*s\n", s2len, s2);
203 else
204 printf ("C: \n");
207 /* Server is done. */
209 res = gsasl_step (server, s2, s2len, &s1, &s1len);
210 free (s2);
211 if (res != GSASL_OK)
213 fail ("gsasl_step(5) failed (%d):\n%s\n", res,
214 gsasl_strerror (res));
215 return;
218 if (s1len != 0)
220 fail ("gsasl_step() failed, additional length=%d:\n", s1len);
221 fail ("%s\n", s1);
222 return;
225 free (s1);
227 if (debug)
228 printf ("\n");
230 gsasl_client_finish (client);
231 gsasl_server_finish (server);
234 gsasl_done (ctx);