Add.
[gsasl.git] / tests / old-cram-md5.c
blob0c928567a0e91911804c8609a9854382d1f9b3ed
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/>.
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "utils.h"
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" */
37 static int
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;
48 *keylen = needlen;
49 if (key)
50 memcpy (key, PASSWORD, *keylen);
52 return GSASL_OK;
55 static int
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;
64 *outlen = needlen;
65 if (out)
66 memcpy (out, USERNAME, *outlen);
68 return GSASL_OK;
71 static int
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;
79 *outlen = needlen;
80 if (out)
81 memcpy (out, PASSWORD, *outlen);
83 return GSASL_OK;
86 void
87 doit (void)
89 Gsasl_ctx *ctx = NULL;
90 Gsasl_session_ctx *server = NULL, *client = NULL;
91 char *s1, *s2;
92 size_t s1len, s2len;
93 size_t i;
94 int res;
96 res = gsasl_init (&ctx);
97 if (res != GSASL_OK)
99 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
100 return;
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);
113 if (res != GSASL_OK)
115 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
116 return;
118 res = gsasl_client_start (ctx, "CRAM-MD5", &client);
119 if (res != GSASL_OK)
121 fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
122 return;
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));
129 return;
132 if (debug)
133 printf ("S: %.*s\n", s1len, s1);
135 res = gsasl_step (client, s1, s1len, &s2, &s2len);
136 free (s1);
137 if (res != GSASL_OK)
139 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
140 return;
143 if (debug)
144 printf ("C: %.*s\n", s2len, s2);
146 res = gsasl_step (server, s2, s2len, &s1, &s1len);
147 free (s2);
148 if (res != GSASL_OK)
150 fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
151 return;
154 if (s1len != 0)
156 fail ("gsasl_step() failed, additional length=%d:\n", s1len);
157 fail ("%s\n", s1);
158 return;
161 free (s1);
163 if (debug)
164 printf ("\n");
166 gsasl_client_finish (client);
167 gsasl_server_finish (server);
170 gsasl_done (ctx);