Branch data Line data Source code
1 : : /* cram-md5.c --- Test the CRAM-MD5 mechanism, using old callback API.
2 : : * Copyright (C) 2002-2012 Simon Josefsson
3 : : *
4 : : * This file is part of GNU SASL.
5 : : *
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.
10 : : *
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.
15 : : *
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/>.
18 : : *
19 : : */
20 : :
21 : : #ifdef HAVE_CONFIG_H
22 : : #include "config.h"
23 : : #endif
24 : :
25 : : #include <stdio.h>
26 : : #include <stdarg.h>
27 : : #include <stdlib.h>
28 : : #include <string.h>
29 : :
30 : : #include "utils.h"
31 : :
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" */
36 : :
37 : : static int
38 : 10 : 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)
42 : : {
43 : 10 : size_t needlen = strlen (PASSWORD);
44 : :
45 [ + - ][ - + ]: 10 : if (key && *keylen < needlen)
46 : 0 : return GSASL_TOO_SMALL_BUFFER;
47 : :
48 : 10 : *keylen = needlen;
49 [ + - ]: 10 : if (key)
50 : 10 : memcpy (key, PASSWORD, *keylen);
51 : :
52 : 10 : return GSASL_OK;
53 : : }
54 : :
55 : : static int
56 : 5 : client_cb_authentication_id (Gsasl_session_ctx * xctx,
57 : : char *out, size_t * outlen)
58 : : {
59 : 5 : size_t needlen = strlen (USERNAME);
60 : :
61 [ + - ][ - + ]: 5 : if (out && *outlen < needlen)
62 : 0 : return GSASL_TOO_SMALL_BUFFER;
63 : :
64 : 5 : *outlen = needlen;
65 [ + - ]: 5 : if (out)
66 : 5 : memcpy (out, USERNAME, *outlen);
67 : :
68 : 5 : return GSASL_OK;
69 : : }
70 : :
71 : : static int
72 : 0 : client_cb_password (Gsasl_session_ctx * xctx, char *out, size_t * outlen)
73 : : {
74 : 0 : size_t needlen = strlen (PASSWORD);
75 : :
76 [ # # ][ # # ]: 0 : if (out && *outlen < needlen)
77 : 0 : return GSASL_TOO_SMALL_BUFFER;
78 : :
79 : 0 : *outlen = needlen;
80 [ # # ]: 0 : if (out)
81 : 0 : memcpy (out, PASSWORD, *outlen);
82 : :
83 : 0 : return GSASL_OK;
84 : : }
85 : :
86 : : void
87 : 1 : doit (void)
88 : : {
89 : 1 : Gsasl_ctx *ctx = NULL;
90 : 1 : Gsasl_session_ctx *server = NULL, *client = NULL;
91 : : char *s1, *s2;
92 : : size_t s1len, s2len;
93 : : size_t i;
94 : : int res;
95 : :
96 : 1 : res = gsasl_init (&ctx);
97 [ - + ]: 1 : if (res != GSASL_OK)
98 : : {
99 : 0 : fail ("gsasl_init() failed (%d):\n%s\n", res, gsasl_strerror (res));
100 : 0 : return;
101 : : }
102 : :
103 [ + - ]: 1 : if (!gsasl_client_support_p (ctx, "CRAM-MD5")
104 [ - + ]: 1 : || !gsasl_server_support_p (ctx, "CRAM-MD5"))
105 : : {
106 : 0 : gsasl_done (ctx);
107 : 0 : fail ("No support for CRAM-MD5.\n");
108 : 0 : exit (77);
109 : : }
110 : :
111 : 1 : gsasl_server_callback_retrieve_set (ctx, server_cb_retrieve);
112 : :
113 : 1 : gsasl_client_callback_authentication_id_set (ctx,
114 : : client_cb_authentication_id);
115 : 1 : gsasl_client_callback_password_set (ctx, client_cb_password);
116 : :
117 : :
118 [ + + ]: 6 : for (i = 0; i < 5; i++)
119 : : {
120 : 5 : res = gsasl_server_start (ctx, "CRAM-MD5", &server);
121 [ - + ]: 5 : if (res != GSASL_OK)
122 : : {
123 : 0 : fail ("gsasl_server_start() failed (%d):\n%s\n",
124 : : res, gsasl_strerror (res));
125 : 0 : return;
126 : : }
127 : 5 : res = gsasl_client_start (ctx, "CRAM-MD5", &client);
128 [ - + ]: 5 : if (res != GSASL_OK)
129 : : {
130 : 0 : fail ("gsasl_client_start() failed (%d):\n%s\n",
131 : : res, gsasl_strerror (res));
132 : 0 : return;
133 : : }
134 : :
135 : 5 : res = gsasl_step (server, NULL, 0, &s1, &s1len);
136 [ - + ]: 5 : if (res != GSASL_NEEDS_MORE)
137 : : {
138 : 0 : fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
139 : 0 : return;
140 : : }
141 : :
142 [ + - ]: 5 : if (debug)
143 : 5 : printf ("S: %.*s\n", (int) s1len, s1);
144 : :
145 : 5 : res = gsasl_step (client, s1, s1len, &s2, &s2len);
146 : 5 : free (s1);
147 [ - + ]: 5 : if (res != GSASL_OK)
148 : : {
149 : 0 : fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
150 : 0 : return;
151 : : }
152 : :
153 [ + - ]: 5 : if (debug)
154 : 5 : printf ("C: %.*s\n", (int) s2len, s2);
155 : :
156 : 5 : res = gsasl_step (server, s2, s2len, &s1, &s1len);
157 : 5 : free (s2);
158 [ - + ]: 5 : if (res != GSASL_OK)
159 : : {
160 : 0 : fail ("gsasl_step() failed (%d):\n%s\n", res, gsasl_strerror (res));
161 : 0 : return;
162 : : }
163 : :
164 [ - + ]: 5 : if (s1len != 0)
165 : : {
166 : 0 : fail ("gsasl_step() failed, additional length=%lu:\n",
167 : : (unsigned long) s1len);
168 : 0 : fail ("%s\n", s1);
169 : 0 : return;
170 : : }
171 : :
172 : 5 : free (s1);
173 : :
174 [ + - ]: 5 : if (debug)
175 : 5 : printf ("\n");
176 : :
177 : 5 : gsasl_client_finish (client);
178 : 5 : gsasl_server_finish (server);
179 : : }
180 : :
181 : 1 : gsasl_done (ctx);
182 : : }
|