1 /* digest-md5.c --- Test the DIGEST-MD5 mechanism.
2 * Copyright (C) 2002, 2003, 2004, 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" */
37 #define SERVICE "imap"
38 #define HOSTNAME "hostname"
42 callback (Gsasl
* ctx
, Gsasl_session
* sctx
, Gsasl_property prop
)
46 int rc
= GSASL_NO_CALLBACK
;
48 /* Get user info from user. */
53 gsasl_property_set (sctx
, prop
, PASSWORD
);
58 gsasl_property_set (sctx
, prop
, USERNAME
);
64 gsasl_property_set (sctx
, prop
, AUTHZID
);
66 gsasl_property_set (sctx
, prop
, NULL
);
72 gsasl_property_set (sctx
, prop
, SERVICE
);
78 gsasl_property_set (sctx
, prop
, REALM
);
80 gsasl_property_set (sctx
, prop
, NULL
);
88 gsasl_property_set (sctx
, prop
, HOSTNAME
);
93 fail ("Unknown callback property %d\n", prop
);
104 Gsasl_session
*server
= NULL
, *client
= NULL
;
110 res
= gsasl_init (&ctx
);
113 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
117 gsasl_callback_set (ctx
, callback
);
119 for (i
= 0; i
< 5; i
++)
121 res
= gsasl_server_start (ctx
, "DIGEST-MD5", &server
);
124 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
127 res
= gsasl_client_start (ctx
, "DIGEST-MD5", &client
);
130 fail ("gsasl_init() failed (%d):\n%s\n", res
, gsasl_strerror (res
));
134 /* Server begins... */
136 res
= gsasl_step (server
, NULL
, 0, &s1
, &s1len
);
137 if (res
!= GSASL_NEEDS_MORE
)
139 fail ("gsasl_step(1) failed (%d):\n%s\n", res
,
140 gsasl_strerror (res
));
145 printf ("S: %.*s\n", s1len
, s1
);
147 /* Client respond... */
149 res
= gsasl_step (client
, s1
, s1len
, &s2
, &s2len
);
151 if (res
!= GSASL_NEEDS_MORE
)
153 fail ("gsasl_step(2) failed (%d):\n%s\n", res
,
154 gsasl_strerror (res
));
159 printf ("C: %.*s\n", s2len
, s2
);
161 /* Server finishes... */
163 res
= gsasl_step (server
, s2
, s2len
, &s1
, &s1len
);
165 if (res
!= GSASL_NEEDS_MORE
)
167 fail ("gsasl_step(3) failed (%d):\n%s\n", res
,
168 gsasl_strerror (res
));
173 printf ("S: %.*s\n", s1len
, s1
);
175 /* Client finishes... */
177 res
= gsasl_step (client
, s1
, s1len
, &s2
, &s2len
);
181 fail ("gsasl_step(4) failed (%d):\n%s\n", res
,
182 gsasl_strerror (res
));
188 /* Solaris x86 crashes here if s2 is NULL, even when s2len
191 printf ("C: %.*s\n", s2len
, s2
);
196 /* Server is done. */
198 res
= gsasl_step (server
, s2
, s2len
, &s1
, &s1len
);
202 fail ("gsasl_step(5) failed (%d):\n%s\n", res
,
203 gsasl_strerror (res
));
209 fail ("gsasl_step() failed, additional length=%d:\n", s1len
);
219 gsasl_finish (client
);
220 gsasl_finish (server
);