gsasl  2.2.1
challenge.c
Go to the documentation of this file.
1 /* challenge.c --- Generate a CRAM-MD5 challenge string.
2  * Copyright (C) 2002-2024 Simon Josefsson
3  *
4  * This file is part of GNU SASL Library.
5  *
6  * GNU SASL Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GNU SASL Library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with GNU SASL Library; if not, write to the Free
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include <config.h>
24 
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28 
29 /* Get prototype. */
30 #include "challenge.h"
31 
32 /* Get gc_nonce. */
33 #include <gc.h>
34 
35 /*
36  * From draft-ietf-sasl-crammd5-02.txt:
37  *
38  * The data encoded in the challenge contains a presumptively
39  * arbitrary string of random digits, a time-stamp, and the
40  * fully-qualified primary host name of the server.
41  * ...
42  * challenge = "<" 1*DIGIT "." 1*DIGIT "@" hostname ">"
43  * hostname = 1*(ALPHA / DIGIT) *("." / "-" / ALPHA / DIGIT)
44  *
45  * This implementation avoid the information leakage by always using 0
46  * as the time-stamp and a fixed host name. This should be
47  * unproblematic, as any client that try to validate the challenge
48  * string somehow, would violate the same specification:
49  *
50  * The client MUST NOT interpret or attempt to validate the
51  * contents of the challenge in any way.
52  *
53  */
54 
55 /* The sequence of X in TEMPLATE must be twice as long as NONCELEN. */
56 #define NONCELEN 10
57 #define TEMPLATE "<XXXXXXXXXXXXXXXXXXXX.0@localhost>"
58 
59 /* The probabilities for each digit are skewed (0-5 is more likely to
60  occur than 6-9), but it is just used as a nonce anyway. */
61 #define DIGIT(c) (((c) & 0x0F) > 9 ? \
62  '0' + ((c) & 0x0F) - 10 : \
63  '0' + ((c) & 0x0F))
64 
65 int
67 {
68  char nonce[NONCELEN];
69  size_t i;
70  int rc;
71 
72  assert (strlen (TEMPLATE) == CRAM_MD5_CHALLENGE_LEN - 1);
73 
74  memcpy (challenge, TEMPLATE, CRAM_MD5_CHALLENGE_LEN);
75 
76  rc = gc_nonce (nonce, sizeof (nonce));
77  if (rc != GC_OK)
78  return -1;
79 
80  for (i = 0; i < sizeof (nonce); i++)
81  {
82  challenge[1 + i] = DIGIT (nonce[i]);
83  challenge[11 + i] = DIGIT (nonce[i] >> 4);
84  }
85 
86  return 0;
87 }
#define NONCELEN
Definition: challenge.c:56
#define TEMPLATE
Definition: challenge.c:57
#define DIGIT(c)
Definition: challenge.c:61
int cram_md5_challenge(char challenge[CRAM_MD5_CHALLENGE_LEN])
Definition: challenge.c:66
#define CRAM_MD5_CHALLENGE_LEN
Definition: challenge.h:26
int rc
Definition: error.c:37