File: client.c

package info (click to toggle)
gsasl 2.2.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,600 kB
  • sloc: ansic: 16,551; sh: 1,739; makefile: 676; xml: 301; php: 172; perl: 4
file content (349 lines) | stat: -rw-r--r-- 8,581 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* client.c --- DIGEST-MD5 mechanism from RFC 2831, client side.
 * Copyright (C) 2002-2025 Simon Josefsson
 *
 * This file is part of GNU SASL Library.
 *
 * GNU SASL Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * GNU SASL Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GNU SASL Library; if not, see
 * <https://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

/* Get specification. */
#include "digest-md5.h"

/* Get malloc, free. */
#include <stdlib.h>

/* Get memcpy, strlen. */
#include <string.h>

#include "gc.h"

/* Get tools. */
#include "nonascii.h"
#include "tokens.h"
#include "parser.h"
#include "printer.h"
#include "free.h"
#include "session.h"
#include "digesthmac.h"
#include "qop.h"

#include "mechtools.h"

#define CNONCE_ENTROPY_BYTES 16

struct _Gsasl_digest_md5_client_state
{
  int step;
  unsigned long readseqnum, sendseqnum;
  char secret[DIGEST_MD5_LENGTH];
  char kic[DIGEST_MD5_LENGTH];
  char kcc[DIGEST_MD5_LENGTH];
  char kis[DIGEST_MD5_LENGTH];
  char kcs[DIGEST_MD5_LENGTH];
  digest_md5_challenge challenge;
  digest_md5_response response;
  digest_md5_finish finish;
};
typedef struct _Gsasl_digest_md5_client_state _Gsasl_digest_md5_client_state;

int
_gsasl_digest_md5_client_start (Gsasl_session *sctx _GL_UNUSED,
				void **mech_data)
{
  _Gsasl_digest_md5_client_state *state;
  char nonce[CNONCE_ENTROPY_BYTES];
  char *p;
  int rc;

  rc = gsasl_nonce (nonce, CNONCE_ENTROPY_BYTES);
  if (rc != GSASL_OK)
    return rc;

  rc = gsasl_base64_to (nonce, CNONCE_ENTROPY_BYTES, &p, NULL);
  if (rc != GSASL_OK)
    return rc;

  state = calloc (1, sizeof (*state));
  if (state == NULL)
    {
      free (p);
      return GSASL_MALLOC_ERROR;
    }

  state->response.cnonce = p;
  state->response.nc = 1;

  *mech_data = state;

  return GSASL_OK;
}

int
_gsasl_digest_md5_client_step (Gsasl_session *sctx,
			       void *mech_data,
			       const char *input,
			       size_t input_len,
			       char **output, size_t *output_len)
{
  _Gsasl_digest_md5_client_state *state = mech_data;
  int rc, res;

  *output = NULL;
  *output_len = 0;

  if (state->step == 0)
    {
      state->step++;
      if (input_len == 0)
	return GSASL_NEEDS_MORE;
    }

  switch (state->step)
    {
    case 1:
      {
	if (digest_md5_parse_challenge (input, input_len,
					&state->challenge) < 0)
	  return GSASL_MECHANISM_PARSE_ERROR;

	/* FIXME: How to let application know of remaining realms?
	   One idea, add a GSASL_REALM_COUNT property, and have the
	   GSASL_REALM be that many concatenated zero terminated realm
	   strings.  Slightly hackish, though.  Another cleaner
	   approach would be to add gsasl_property_set_array and
	   gsasl_property_get_array APIs, for those properties that
	   may be used multiple times. */
	if (state->challenge.nrealms > 0)
	  res = gsasl_property_set (sctx, GSASL_REALM,
				    state->challenge.realms[0]);
	else
	  res = gsasl_property_set (sctx, GSASL_REALM, NULL);
	if (res != GSASL_OK)
	  return res;

	/* FIXME: cipher, maxbuf. */

	/* Create response token. */
	state->response.utf8 = 1;

	res = gsasl_property_set (sctx, GSASL_QOPS,
				  digest_md5_qops2qopstr (state->
							  challenge.qops));
	if (res != GSASL_OK)
	  return res;

	{
	  const char *qop = gsasl_property_get (sctx, GSASL_QOP);

	  if (!qop)
	    state->response.qop = DIGEST_MD5_QOP_AUTH;
	  else if (strcmp (qop, "qop-int") == 0)
	    state->response.qop = DIGEST_MD5_QOP_AUTH_INT;
	  else if (strcmp (qop, "qop-auth") == 0)
	    state->response.qop = DIGEST_MD5_QOP_AUTH;
	  else
	    /* We don't support confidentiality or unknown
	       keywords. */
	    return GSASL_AUTHENTICATION_ERROR;
	}

	state->response.nonce = strdup (state->challenge.nonce);
	if (!state->response.nonce)
	  return GSASL_MALLOC_ERROR;

	{
	  const char *service = gsasl_property_get (sctx, GSASL_SERVICE);
	  const char *hostname = gsasl_property_get (sctx, GSASL_HOSTNAME);
	  if (!service)
	    return GSASL_NO_SERVICE;
	  if (!hostname)
	    return GSASL_NO_HOSTNAME;
	  if (asprintf (&state->response.digesturi, "%s/%s",
			service, hostname) < 0)
	    return GSASL_MALLOC_ERROR;
	}

	{
	  const char *c;
	  char *tmp, *tmp2;

	  c = gsasl_property_get (sctx, GSASL_AUTHID);
	  if (!c)
	    return GSASL_NO_AUTHID;

	  state->response.username = strdup (c);
	  if (!state->response.username)
	    return GSASL_MALLOC_ERROR;

	  c = gsasl_property_get (sctx, GSASL_AUTHZID);
	  if (c)
	    {
	      state->response.authzid = strdup (c);
	      if (!state->response.authzid)
		return GSASL_MALLOC_ERROR;
	    }

	  gsasl_callback (NULL, sctx, GSASL_REALM);
	  c = gsasl_property_fast (sctx, GSASL_REALM);
	  if (c)
	    {
	      state->response.realm = strdup (c);
	      if (!state->response.realm)
		return GSASL_MALLOC_ERROR;
	    }

	  c = gsasl_property_get (sctx, GSASL_PASSWORD);
	  if (!c)
	    return GSASL_NO_PASSWORD;

	  tmp2 = utf8tolatin1ifpossible (c);

	  rc = asprintf (&tmp, "%s:%s:%s", state->response.username,
			 state->response.realm ?
			 state->response.realm : "", tmp2);
	  free (tmp2);
	  if (rc < 0)
	    return GSASL_MALLOC_ERROR;

	  rc = gc_md5 (tmp, strlen (tmp), state->secret);
	  free (tmp);
	  if (rc != GC_OK)
	    return GSASL_CRYPTO_ERROR;
	}

	rc = digest_md5_hmac (state->response.response,
			      state->secret,
			      state->response.nonce,
			      state->response.nc,
			      state->response.cnonce,
			      state->response.qop,
			      state->response.authzid,
			      state->response.digesturi,
			      0,
			      state->response.cipher,
			      state->kic, state->kis, state->kcc, state->kcs);
	if (rc)
	  return GSASL_CRYPTO_ERROR;

	*output = digest_md5_print_response (&state->response);
	if (!*output)
	  return GSASL_AUTHENTICATION_ERROR;

	*output_len = strlen (*output);

	state->step++;
	res = GSASL_NEEDS_MORE;
      }
      break;

    case 2:
      {
	char check[DIGEST_MD5_RESPONSE_LENGTH + 1];

	if (digest_md5_parse_finish (input, input_len, &state->finish) < 0)
	  return GSASL_MECHANISM_PARSE_ERROR;

	res = digest_md5_hmac (check, state->secret,
			       state->response.nonce, state->response.nc,
			       state->response.cnonce, state->response.qop,
			       state->response.authzid,
			       state->response.digesturi, 1,
			       state->response.cipher, NULL, NULL, NULL,
			       NULL);
	if (res != GSASL_OK)
	  break;

	if (strcmp (state->finish.rspauth, check) == 0)
	  res = GSASL_OK;
	else
	  res = GSASL_AUTHENTICATION_ERROR;
	state->step++;
      }
      break;

    default:
      res = GSASL_MECHANISM_CALLED_TOO_MANY_TIMES;
      break;
    }

  return res;
}

void
_gsasl_digest_md5_client_finish (Gsasl_session *sctx _GL_UNUSED,
				 void *mech_data)
{
  _Gsasl_digest_md5_client_state *state = mech_data;

  if (!state)
    return;

  digest_md5_free_challenge (&state->challenge);
  digest_md5_free_response (&state->response);
  digest_md5_free_finish (&state->finish);

  free (state);
}

int
_gsasl_digest_md5_client_encode (Gsasl_session *sctx _GL_UNUSED,
				 void *mech_data,
				 const char *input,
				 size_t input_len,
				 char **output, size_t *output_len)
{
  _Gsasl_digest_md5_client_state *state = mech_data;
  int res;

  res = digest_md5_encode (input, input_len, output, output_len,
			   state->response.qop,
			   state->sendseqnum, state->kic);
  if (res)
    return res == -2 ? GSASL_NEEDS_MORE : GSASL_INTEGRITY_ERROR;

  if (state->sendseqnum == 4294967295UL)
    state->sendseqnum = 0;
  else
    state->sendseqnum++;

  return GSASL_OK;
}

int
_gsasl_digest_md5_client_decode (Gsasl_session *sctx _GL_UNUSED,
				 void *mech_data,
				 const char *input,
				 size_t input_len,
				 char **output, size_t *output_len)
{
  _Gsasl_digest_md5_client_state *state = mech_data;
  int res;

  res = digest_md5_decode (input, input_len, output, output_len,
			   state->response.qop,
			   state->readseqnum, state->kis);
  if (res)
    return res == -2 ? GSASL_NEEDS_MORE : GSASL_INTEGRITY_ERROR;

  if (state->readseqnum == 4294967295UL)
    state->readseqnum = 0;
  else
    state->readseqnum++;

  return GSASL_OK;
}