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