gsasl  2.2.1
digesthmac.c
Go to the documentation of this file.
1 /* digesthmac.c --- Compute DIGEST-MD5 response value.
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 /* Get specification. */
26 #include "digesthmac.h"
27 
28 /* Get malloc, free. */
29 #include <stdlib.h>
30 
31 /* Get memcpy, strlen. */
32 #include <string.h>
33 
34 /* Get sprintf. */
35 #include <stdio.h>
36 
37 /* Get gc_md5. */
38 #include <gc.h>
39 
40 #define HEXCHAR(c) ((c & 0x0F) > 9 ? 'a' + (c & 0x0F) - 10 : '0' + (c & 0x0F))
41 
42 #define QOP_AUTH "auth"
43 #define QOP_AUTH_INT "auth-int"
44 #define QOP_AUTH_CONF "auth-conf"
45 
46 #define A2_PRE "AUTHENTICATE:"
47 #define A2_POST ":00000000000000000000000000000000"
48 #define COLON ":"
49 #define MD5LEN 16
50 #define DERIVE_CLIENT_INTEGRITY_KEY_STRING \
51  "Digest session key to client-to-server signing key magic constant"
52 #define DERIVE_CLIENT_INTEGRITY_KEY_STRING_LEN 65
53 #define DERIVE_SERVER_INTEGRITY_KEY_STRING \
54  "Digest session key to server-to-client signing key magic constant"
55 #define DERIVE_SERVER_INTEGRITY_KEY_STRING_LEN 65
56 #define DERIVE_CLIENT_CONFIDENTIALITY_KEY_STRING \
57  "Digest H(A1) to client-to-server sealing key magic constant"
58 #define DERIVE_CLIENT_CONFIDENTIALITY_KEY_STRING_LEN 59
59 #define DERIVE_SERVER_CONFIDENTIALITY_KEY_STRING \
60  "Digest H(A1) to server-to-client sealing key magic constant"
61 #define DERIVE_SERVER_CONFIDENTIALITY_KEY_STRING_LEN 59
62 
63 /* Compute in 33 bytes large array OUTPUT the DIGEST-MD5 response
64  value. SECRET holds the 16 bytes MD5 hash SS, i.e.,
65  H(username:realm:passwd). NONCE is a zero terminated string with
66  the server nonce. NC is the nonce-count, typically 1 for initial
67  authentication. CNONCE is a zero terminated string with the client
68  nonce. QOP is the quality of protection to use. AUTHZID is a zero
69  terminated string with the authorization identity. DIGESTURI is a
70  zero terminated string with the server principal (e.g.,
71  imap/mail.example.org). RSPAUTH is a boolean which indicate
72  whether to compute a value for the RSPAUTH response or the "real"
73  authentication. CIPHER is the cipher to use. KIC, KIS, KCC, KCS
74  are either NULL, or points to 16 byte arrays that will hold the
75  computed keys on output. Returns 0 on success. */
76 int
77 digest_md5_hmac (char *output, char secret[MD5LEN], const char *nonce,
78  unsigned long nc, const char *cnonce, digest_md5_qop qop,
79  const char *authzid, const char *digesturi, int rspauth,
80  digest_md5_cipher cipher,
81  char *kic, char *kis, char *kcc, char *kcs)
82 {
83  const char *a2string = rspauth ? COLON : A2_PRE;
84  char nchex[17]; /* really 9 but 17 for -Werror=format-overflow= */
85  char a1hexhash[2 * MD5LEN];
86  char a2hexhash[2 * MD5LEN];
87  char hash[MD5LEN];
88  char *tmp, *p;
89  size_t tmplen;
90  int rc;
91  int i;
92 
93  /* A1 */
94 
95  tmplen = MD5LEN + strlen (COLON) + strlen (nonce) +
96  strlen (COLON) + strlen (cnonce);
97  if (authzid && strlen (authzid) > 0)
98  tmplen += strlen (COLON) + strlen (authzid);
99 
100  p = tmp = malloc (tmplen);
101  if (tmp == NULL)
102  return -1;
103 
104  memcpy (p, secret, MD5LEN);
105  p += MD5LEN;
106  memcpy (p, COLON, strlen (COLON));
107  p += strlen (COLON);
108  memcpy (p, nonce, strlen (nonce));
109  p += strlen (nonce);
110  memcpy (p, COLON, strlen (COLON));
111  p += strlen (COLON);
112  memcpy (p, cnonce, strlen (cnonce));
113  p += strlen (cnonce);
114  if (authzid && strlen (authzid) > 0)
115  {
116  memcpy (p, COLON, strlen (COLON));
117  p += strlen (COLON);
118  memcpy (p, authzid, strlen (authzid));
119  }
120 
121  rc = gc_md5 (tmp, tmplen, hash);
122  free (tmp);
123  if (rc)
124  return rc;
125 
126  if (kic)
127  {
128  char hash2[MD5LEN];
131 
132  memcpy (q, hash, MD5LEN);
135 
136  rc = gc_md5 (q, qlen, hash2);
137  if (rc)
138  return rc;
139 
140  memcpy (kic, hash2, MD5LEN);
141  }
142 
143  if (kis)
144  {
145  char hash2[MD5LEN];
148 
149  memcpy (q, hash, MD5LEN);
152 
153  rc = gc_md5 (q, qlen, hash2);
154  if (rc)
155  return rc;
156 
157  memcpy (kis, hash2, MD5LEN);
158  }
159 
160  if (kcc)
161  {
162  char hash2[MD5LEN];
163  int n;
165 
166  if (cipher == DIGEST_MD5_CIPHER_RC4_40)
167  n = 5;
168  else if (cipher == DIGEST_MD5_CIPHER_RC4_56)
169  n = 7;
170  else
171  n = MD5LEN;
172 
173  memcpy (q, hash, n);
176 
178  hash2);
179  if (rc)
180  return rc;
181 
182  memcpy (kcc, hash2, MD5LEN);
183  }
184 
185  if (kcs)
186  {
187  char hash2[MD5LEN];
188  int n;
190 
191  if (cipher == DIGEST_MD5_CIPHER_RC4_40)
192  n = 5;
193  else if (cipher == DIGEST_MD5_CIPHER_RC4_56)
194  n = 7;
195  else
196  n = MD5LEN;
197 
198  memcpy (q, hash, n);
201 
203  hash2);
204  if (rc)
205  return rc;
206 
207  memcpy (kcs, hash2, MD5LEN);
208  }
209 
210  for (i = 0; i < MD5LEN; i++)
211  {
212  a1hexhash[2 * i + 1] = HEXCHAR (hash[i]);
213  a1hexhash[2 * i + 0] = HEXCHAR (hash[i] >> 4);
214  }
215 
216  /* A2 */
217 
218  tmplen = strlen (a2string) + strlen (digesturi);
220  tmplen += strlen (A2_POST);
221 
222  p = tmp = malloc (tmplen);
223  if (tmp == NULL)
224  return -1;
225 
226  memcpy (p, a2string, strlen (a2string));
227  p += strlen (a2string);
228  memcpy (p, digesturi, strlen (digesturi));
229  p += strlen (digesturi);
231  memcpy (p, A2_POST, strlen (A2_POST));
232 
233  rc = gc_md5 (tmp, tmplen, hash);
234  free (tmp);
235  if (rc)
236  return rc;
237 
238  for (i = 0; i < MD5LEN; i++)
239  {
240  a2hexhash[2 * i + 1] = HEXCHAR (hash[i]);
241  a2hexhash[2 * i + 0] = HEXCHAR (hash[i] >> 4);
242  }
243 
244  /* response_value */
245 
246  sprintf (nchex, "%08lx", nc);
247 
248  tmplen = 2 * MD5LEN + strlen (COLON) + strlen (nonce) + strlen (COLON) +
249  strlen (nchex) + strlen (COLON) + strlen (cnonce) + strlen (COLON);
250  if (qop & DIGEST_MD5_QOP_AUTH_CONF)
251  tmplen += strlen (QOP_AUTH_CONF);
252  else if (qop & DIGEST_MD5_QOP_AUTH_INT)
253  tmplen += strlen (QOP_AUTH_INT);
254  else if (qop & DIGEST_MD5_QOP_AUTH)
255  tmplen += strlen (QOP_AUTH);
256  tmplen += strlen (COLON) + 2 * MD5LEN;
257 
258  p = tmp = malloc (tmplen);
259  if (tmp == NULL)
260  return -1;
261 
262  memcpy (p, a1hexhash, 2 * MD5LEN);
263  p += 2 * MD5LEN;
264  memcpy (p, COLON, strlen (COLON));
265  p += strlen (COLON);
266  memcpy (p, nonce, strlen (nonce));
267  p += strlen (nonce);
268  memcpy (p, COLON, strlen (COLON));
269  p += strlen (COLON);
270  memcpy (p, nchex, strlen (nchex));
271  p += strlen (nchex);
272  memcpy (p, COLON, strlen (COLON));
273  p += strlen (COLON);
274  memcpy (p, cnonce, strlen (cnonce));
275  p += strlen (cnonce);
276  memcpy (p, COLON, strlen (COLON));
277  p += strlen (COLON);
278  if (qop & DIGEST_MD5_QOP_AUTH_CONF)
279  {
280  memcpy (p, QOP_AUTH_CONF, strlen (QOP_AUTH_CONF));
281  p += strlen (QOP_AUTH_CONF);
282  }
283  else if (qop & DIGEST_MD5_QOP_AUTH_INT)
284  {
285  memcpy (p, QOP_AUTH_INT, strlen (QOP_AUTH_INT));
286  p += strlen (QOP_AUTH_INT);
287  }
288  else if (qop & DIGEST_MD5_QOP_AUTH)
289  {
290  memcpy (p, QOP_AUTH, strlen (QOP_AUTH));
291  p += strlen (QOP_AUTH);
292  }
293  memcpy (p, COLON, strlen (COLON));
294  p += strlen (COLON);
295  memcpy (p, a2hexhash, 2 * MD5LEN);
296 
297  rc = gc_md5 (tmp, tmplen, hash);
298  free (tmp);
299  if (rc)
300  return rc;
301 
302  for (i = 0; i < MD5LEN; i++)
303  {
304  output[2 * i + 1] = HEXCHAR (hash[i]);
305  output[2 * i + 0] = HEXCHAR (hash[i] >> 4);
306  }
307  output[32] = '\0';
308 
309  return 0;
310 }
digest_md5_qop
@ DIGEST_MD5_QOP_AUTH_INT
@ DIGEST_MD5_QOP_AUTH_CONF
@ DIGEST_MD5_QOP_AUTH
digest_md5_cipher
@ DIGEST_MD5_CIPHER_RC4_56
@ DIGEST_MD5_CIPHER_RC4_40
#define DERIVE_SERVER_CONFIDENTIALITY_KEY_STRING_LEN
Definition: digesthmac.c:61
#define MD5LEN
Definition: digesthmac.c:49
int digest_md5_hmac(char *output, char secret[MD5LEN], const char *nonce, unsigned long nc, const char *cnonce, digest_md5_qop qop, const char *authzid, const char *digesturi, int rspauth, digest_md5_cipher cipher, char *kic, char *kis, char *kcc, char *kcs)
Definition: digesthmac.c:77
#define HEXCHAR(c)
Definition: digesthmac.c:40
#define A2_PRE
Definition: digesthmac.c:46
#define DERIVE_CLIENT_CONFIDENTIALITY_KEY_STRING_LEN
Definition: digesthmac.c:58
#define DERIVE_CLIENT_CONFIDENTIALITY_KEY_STRING
Definition: digesthmac.c:56
#define COLON
Definition: digesthmac.c:48
#define DERIVE_SERVER_CONFIDENTIALITY_KEY_STRING
Definition: digesthmac.c:59
#define QOP_AUTH_INT
Definition: digesthmac.c:43
#define DERIVE_CLIENT_INTEGRITY_KEY_STRING
Definition: digesthmac.c:50
#define DERIVE_SERVER_INTEGRITY_KEY_STRING
Definition: digesthmac.c:53
#define A2_POST
Definition: digesthmac.c:47
#define DERIVE_SERVER_INTEGRITY_KEY_STRING_LEN
Definition: digesthmac.c:55
#define QOP_AUTH
Definition: digesthmac.c:42
#define DERIVE_CLIENT_INTEGRITY_KEY_STRING_LEN
Definition: digesthmac.c:52
#define QOP_AUTH_CONF
Definition: digesthmac.c:44
int rc
Definition: error.c:37