1*0a6a1f1dSLionel Sambuc /* $NetBSD: digest.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
9ebfedea0SLionel Sambuc *
10ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc * are met:
13ebfedea0SLionel Sambuc *
14ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc *
17ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
19ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
22ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
23ebfedea0SLionel Sambuc * without specific prior written permission.
24ebfedea0SLionel Sambuc *
25ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35ebfedea0SLionel Sambuc * SUCH DAMAGE.
36ebfedea0SLionel Sambuc */
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc #include <sys/types.h>
39ebfedea0SLionel Sambuc #include <stdio.h>
40ebfedea0SLionel Sambuc #include <unistd.h>
41ebfedea0SLionel Sambuc #include <CommonCrypto/CommonDigest.h>
42ebfedea0SLionel Sambuc #include <CommonCrypto/CommonHMAC.h>
43ebfedea0SLionel Sambuc #include <assert.h>
44ebfedea0SLionel Sambuc #include <krb5/roken.h>
45ebfedea0SLionel Sambuc #include <krb5/hex.h>
46ebfedea0SLionel Sambuc #include "heim-auth.h"
47ebfedea0SLionel Sambuc #include <krb5/ntlm_err.h>
48ebfedea0SLionel Sambuc
49ebfedea0SLionel Sambuc struct heim_digest_desc {
50ebfedea0SLionel Sambuc int server;
51ebfedea0SLionel Sambuc int type;
52ebfedea0SLionel Sambuc char *password;
53ebfedea0SLionel Sambuc uint8_t SecretHash[CC_MD5_DIGEST_LENGTH];
54ebfedea0SLionel Sambuc char *serverNonce;
55ebfedea0SLionel Sambuc char *serverRealm;
56ebfedea0SLionel Sambuc char *serverQOP;
57ebfedea0SLionel Sambuc char *serverMethod;
58ebfedea0SLionel Sambuc char *clientUsername;
59ebfedea0SLionel Sambuc char *clientResponse;
60ebfedea0SLionel Sambuc char *clientURI;
61ebfedea0SLionel Sambuc char *clientRealm;
62ebfedea0SLionel Sambuc char *clientNonce;
63ebfedea0SLionel Sambuc char *clientQOP;
64ebfedea0SLionel Sambuc char *clientNC;
65ebfedea0SLionel Sambuc char *serverAlgorithm;
66ebfedea0SLionel Sambuc char *auth_id;
67ebfedea0SLionel Sambuc };
68ebfedea0SLionel Sambuc
69ebfedea0SLionel Sambuc #define FREE_AND_CLEAR(x) do { if ((x)) { free((x)); (x) = NULL; } } while(0)
70ebfedea0SLionel Sambuc #define MEMSET_FREE_AND_CLEAR(x) do { if ((x)) { memset(x, 0, strlen(x)); free((x)); (x) = NULL; } } while(0)
71ebfedea0SLionel Sambuc
72ebfedea0SLionel Sambuc static void
clear_context(heim_digest_t context)73ebfedea0SLionel Sambuc clear_context(heim_digest_t context)
74ebfedea0SLionel Sambuc {
75ebfedea0SLionel Sambuc MEMSET_FREE_AND_CLEAR(context->password);
76ebfedea0SLionel Sambuc memset(context->SecretHash, 0, sizeof(context->SecretHash));
77ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->serverNonce);
78ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->serverRealm);
79ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->serverQOP);
80ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->serverMethod);
81ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->clientUsername);
82ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->clientResponse);
83ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->clientURI);
84ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->clientRealm);
85ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->clientNonce);
86ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->clientQOP);
87ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->clientNC);
88ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->serverAlgorithm);
89ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->auth_id);
90ebfedea0SLionel Sambuc }
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc static char *
build_A1_hash(int type,const char * username,const char * password,const char * realm,const char * serverNonce,const char * clientNonce,const char * auth_id)93ebfedea0SLionel Sambuc build_A1_hash(int type,
94ebfedea0SLionel Sambuc const char *username, const char *password,
95ebfedea0SLionel Sambuc const char *realm, const char *serverNonce,
96ebfedea0SLionel Sambuc const char *clientNonce,
97ebfedea0SLionel Sambuc const char *auth_id)
98ebfedea0SLionel Sambuc {
99ebfedea0SLionel Sambuc unsigned char md[CC_MD5_DIGEST_LENGTH];
100ebfedea0SLionel Sambuc CC_MD5_CTX ctx;
101ebfedea0SLionel Sambuc char *A1;
102ebfedea0SLionel Sambuc
103ebfedea0SLionel Sambuc CC_MD5_Init(&ctx);
104ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, username, strlen(username));
105ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
106ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, realm, strlen(realm));
107ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
108ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, password, strlen(password));
109ebfedea0SLionel Sambuc CC_MD5_Final(md, &ctx);
110ebfedea0SLionel Sambuc
111ebfedea0SLionel Sambuc if (type != HEIM_DIGEST_TYPE_RFC2069) {
112ebfedea0SLionel Sambuc CC_MD5_Init(&ctx);
113ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, md, sizeof(md));
114ebfedea0SLionel Sambuc memset(md, 0, sizeof(md));
115ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
116ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, serverNonce, strlen(serverNonce));
117ebfedea0SLionel Sambuc if (clientNonce) {
118ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
119ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, clientNonce, strlen(clientNonce));
120ebfedea0SLionel Sambuc }
121ebfedea0SLionel Sambuc if (auth_id) {
122ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
123ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, auth_id, strlen(auth_id));
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc CC_MD5_Final(md, &ctx);
126ebfedea0SLionel Sambuc }
127ebfedea0SLionel Sambuc hex_encode(md, sizeof(md), &A1);
128ebfedea0SLionel Sambuc if (A1)
129ebfedea0SLionel Sambuc strlwr(A1);
130ebfedea0SLionel Sambuc
131ebfedea0SLionel Sambuc return A1;
132ebfedea0SLionel Sambuc }
133ebfedea0SLionel Sambuc
134ebfedea0SLionel Sambuc static char *
build_A2_hash(heim_digest_t context,const char * method)135ebfedea0SLionel Sambuc build_A2_hash(heim_digest_t context, const char *method)
136ebfedea0SLionel Sambuc {
137ebfedea0SLionel Sambuc unsigned char md[CC_MD5_DIGEST_LENGTH];
138ebfedea0SLionel Sambuc CC_MD5_CTX ctx;
139ebfedea0SLionel Sambuc char *A2;
140ebfedea0SLionel Sambuc
141ebfedea0SLionel Sambuc CC_MD5_Init(&ctx);
142ebfedea0SLionel Sambuc if (method)
143ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, method, strlen(method));
144ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
145ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, context->clientURI, strlen(context->clientURI));
146ebfedea0SLionel Sambuc
147ebfedea0SLionel Sambuc /* conf|int */
148ebfedea0SLionel Sambuc if (context->type != HEIM_DIGEST_TYPE_RFC2069) {
149ebfedea0SLionel Sambuc if (strcmp(context->clientQOP, "auth") != 0) {
150ebfedea0SLionel Sambuc /* XXX if we have a body hash, use that */
151ebfedea0SLionel Sambuc static char conf_zeros[] = ":00000000000000000000000000000000";
152ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, conf_zeros, sizeof(conf_zeros) - 1);
153ebfedea0SLionel Sambuc }
154ebfedea0SLionel Sambuc } else {
155ebfedea0SLionel Sambuc /* support auth-int ? */
156ebfedea0SLionel Sambuc }
157ebfedea0SLionel Sambuc
158ebfedea0SLionel Sambuc CC_MD5_Final(md, &ctx);
159ebfedea0SLionel Sambuc
160ebfedea0SLionel Sambuc hex_encode(md, sizeof(md), &A2);
161ebfedea0SLionel Sambuc if (A2)
162ebfedea0SLionel Sambuc strlwr(A2);
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc return A2;
165ebfedea0SLionel Sambuc }
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc /*
168ebfedea0SLionel Sambuc *
169ebfedea0SLionel Sambuc */
170ebfedea0SLionel Sambuc
171ebfedea0SLionel Sambuc struct md5_value {
172ebfedea0SLionel Sambuc char *mv_name;
173ebfedea0SLionel Sambuc char *mv_value;
174ebfedea0SLionel Sambuc struct md5_value *mv_next;
175ebfedea0SLionel Sambuc };
176ebfedea0SLionel Sambuc
177ebfedea0SLionel Sambuc static void
free_values(struct md5_value * val)178ebfedea0SLionel Sambuc free_values(struct md5_value *val)
179ebfedea0SLionel Sambuc {
180ebfedea0SLionel Sambuc struct md5_value *v;
181ebfedea0SLionel Sambuc while(val) {
182ebfedea0SLionel Sambuc v = val->mv_next;
183ebfedea0SLionel Sambuc if (val->mv_name)
184ebfedea0SLionel Sambuc free(val->mv_name);
185ebfedea0SLionel Sambuc if (val->mv_value)
186ebfedea0SLionel Sambuc free(val->mv_value);
187ebfedea0SLionel Sambuc free(val);
188ebfedea0SLionel Sambuc val = v;
189ebfedea0SLionel Sambuc }
190ebfedea0SLionel Sambuc }
191ebfedea0SLionel Sambuc
192ebfedea0SLionel Sambuc /*
193ebfedea0SLionel Sambuc * Search for entry, if found, remove entry and return string to be freed.
194ebfedea0SLionel Sambuc */
195ebfedea0SLionel Sambuc
196ebfedea0SLionel Sambuc static char *
values_find(struct md5_value ** val,const char * v)197ebfedea0SLionel Sambuc values_find(struct md5_value **val, const char *v)
198ebfedea0SLionel Sambuc {
199ebfedea0SLionel Sambuc struct md5_value *cur = *val;
200ebfedea0SLionel Sambuc char *str;
201ebfedea0SLionel Sambuc
202ebfedea0SLionel Sambuc while (*val != NULL) {
203ebfedea0SLionel Sambuc if (strcasecmp(v, (*val)->mv_name) == 0)
204ebfedea0SLionel Sambuc break;
205ebfedea0SLionel Sambuc val = &(*val)->mv_next;
206ebfedea0SLionel Sambuc }
207ebfedea0SLionel Sambuc if (*val == NULL)
208ebfedea0SLionel Sambuc return NULL;
209ebfedea0SLionel Sambuc cur = *val;
210ebfedea0SLionel Sambuc *val = (*val)->mv_next;
211ebfedea0SLionel Sambuc
212ebfedea0SLionel Sambuc str = cur->mv_value;
213ebfedea0SLionel Sambuc free(cur->mv_name);
214ebfedea0SLionel Sambuc free(cur);
215ebfedea0SLionel Sambuc
216ebfedea0SLionel Sambuc return str;
217ebfedea0SLionel Sambuc }
218ebfedea0SLionel Sambuc
219ebfedea0SLionel Sambuc static int
parse_values(const char * string,struct md5_value ** val)220ebfedea0SLionel Sambuc parse_values(const char *string, struct md5_value **val)
221ebfedea0SLionel Sambuc {
222ebfedea0SLionel Sambuc struct md5_value *v;
223ebfedea0SLionel Sambuc size_t size;
224ebfedea0SLionel Sambuc char *str, *p1, *p2;
225ebfedea0SLionel Sambuc size_t sz;
226ebfedea0SLionel Sambuc
227ebfedea0SLionel Sambuc *val = NULL;
228ebfedea0SLionel Sambuc
229ebfedea0SLionel Sambuc if ((str = strdup(string)) == NULL)
230ebfedea0SLionel Sambuc return ENOMEM;
231ebfedea0SLionel Sambuc
232ebfedea0SLionel Sambuc size = strlen(str);
233ebfedea0SLionel Sambuc
234ebfedea0SLionel Sambuc p1 = str;
235ebfedea0SLionel Sambuc
236ebfedea0SLionel Sambuc while (p1 - str < size) {
237ebfedea0SLionel Sambuc sz = strspn(p1, " \t\n\r,");
238ebfedea0SLionel Sambuc if (p1[sz] == '\0')
239ebfedea0SLionel Sambuc break;
240ebfedea0SLionel Sambuc p1 += sz;
241ebfedea0SLionel Sambuc sz = strcspn(p1, " \t\n\r=");
242ebfedea0SLionel Sambuc if (sz == 0 || p1[sz] == '\0')
243ebfedea0SLionel Sambuc goto error;
244ebfedea0SLionel Sambuc p2 = p1 + sz;
245ebfedea0SLionel Sambuc
246ebfedea0SLionel Sambuc if ((v = malloc(sizeof(*v))) == NULL)
247ebfedea0SLionel Sambuc goto nomem;
248ebfedea0SLionel Sambuc v->mv_name = v->mv_value = NULL;
249ebfedea0SLionel Sambuc v->mv_next = *val;
250ebfedea0SLionel Sambuc *val = v;
251ebfedea0SLionel Sambuc if ((v->mv_name = malloc(p2 - p1 + 1)) == NULL)
252ebfedea0SLionel Sambuc goto nomem;
253ebfedea0SLionel Sambuc strncpy(v->mv_name, p1, p2 - p1);
254ebfedea0SLionel Sambuc v->mv_name[p2 - p1] = '\0';
255ebfedea0SLionel Sambuc
256ebfedea0SLionel Sambuc sz = strspn(p2, " \t\n\r");
257ebfedea0SLionel Sambuc if (p2[sz] == '\0')
258ebfedea0SLionel Sambuc goto error;
259ebfedea0SLionel Sambuc p2 += sz;
260ebfedea0SLionel Sambuc
261ebfedea0SLionel Sambuc if (*p2 != '=')
262ebfedea0SLionel Sambuc goto error;
263ebfedea0SLionel Sambuc p2++;
264ebfedea0SLionel Sambuc
265ebfedea0SLionel Sambuc sz = strspn(p2, " \t\n\r");
266ebfedea0SLionel Sambuc if (p2[sz] == '\0')
267ebfedea0SLionel Sambuc goto error;
268ebfedea0SLionel Sambuc p2 += sz;
269ebfedea0SLionel Sambuc p1 = p2;
270ebfedea0SLionel Sambuc
271ebfedea0SLionel Sambuc if (*p2 == '"') {
272ebfedea0SLionel Sambuc p1++;
273ebfedea0SLionel Sambuc while (*p2 == '"') {
274ebfedea0SLionel Sambuc p2++;
275ebfedea0SLionel Sambuc p2 = strchr(p2, '\"');
276ebfedea0SLionel Sambuc if (p2 == NULL)
277ebfedea0SLionel Sambuc goto error;
278ebfedea0SLionel Sambuc if (p2[0] == '\0')
279ebfedea0SLionel Sambuc goto error;
280ebfedea0SLionel Sambuc if (p2[-1] != '\\')
281ebfedea0SLionel Sambuc break;
282ebfedea0SLionel Sambuc }
283ebfedea0SLionel Sambuc } else {
284ebfedea0SLionel Sambuc sz = strcspn(p2, " \t\n\r=,");
285ebfedea0SLionel Sambuc p2 += sz;
286ebfedea0SLionel Sambuc }
287ebfedea0SLionel Sambuc
288ebfedea0SLionel Sambuc #if 0 /* allow empty values */
289ebfedea0SLionel Sambuc if (p1 == p2)
290ebfedea0SLionel Sambuc goto error;
291ebfedea0SLionel Sambuc #endif
292ebfedea0SLionel Sambuc
293ebfedea0SLionel Sambuc if ((v->mv_value = malloc(p2 - p1 + 1)) == NULL)
294ebfedea0SLionel Sambuc goto nomem;
295ebfedea0SLionel Sambuc strncpy(v->mv_value, p1, p2 - p1);
296ebfedea0SLionel Sambuc v->mv_value[p2 - p1] = '\0';
297ebfedea0SLionel Sambuc
298ebfedea0SLionel Sambuc if (p2[0] == '\0')
299ebfedea0SLionel Sambuc break;
300ebfedea0SLionel Sambuc if (p2[0] == '"')
301ebfedea0SLionel Sambuc p2++;
302ebfedea0SLionel Sambuc
303ebfedea0SLionel Sambuc sz = strspn(p2, " \t\n\r");
304ebfedea0SLionel Sambuc if (p2[sz] == '\0')
305ebfedea0SLionel Sambuc break;
306ebfedea0SLionel Sambuc p2 += sz;
307ebfedea0SLionel Sambuc
308ebfedea0SLionel Sambuc if (p2[0] == '\0')
309ebfedea0SLionel Sambuc break;
310ebfedea0SLionel Sambuc if (p2[0] != ',')
311ebfedea0SLionel Sambuc goto error;
312ebfedea0SLionel Sambuc p1 = p2;
313ebfedea0SLionel Sambuc }
314ebfedea0SLionel Sambuc
315ebfedea0SLionel Sambuc free(str);
316ebfedea0SLionel Sambuc
317ebfedea0SLionel Sambuc return 0;
318ebfedea0SLionel Sambuc error:
319ebfedea0SLionel Sambuc free_values(*val);
320ebfedea0SLionel Sambuc *val = NULL;
321ebfedea0SLionel Sambuc free(str);
322ebfedea0SLionel Sambuc return EINVAL;
323ebfedea0SLionel Sambuc nomem:
324ebfedea0SLionel Sambuc free_values(*val);
325ebfedea0SLionel Sambuc *val = NULL;
326ebfedea0SLionel Sambuc free(str);
327ebfedea0SLionel Sambuc return ENOMEM;
328ebfedea0SLionel Sambuc }
329ebfedea0SLionel Sambuc
330ebfedea0SLionel Sambuc /*
331ebfedea0SLionel Sambuc *
332ebfedea0SLionel Sambuc */
333ebfedea0SLionel Sambuc
334ebfedea0SLionel Sambuc heim_digest_t
heim_digest_create(int server,int type)335ebfedea0SLionel Sambuc heim_digest_create(int server, int type)
336ebfedea0SLionel Sambuc {
337ebfedea0SLionel Sambuc heim_digest_t context;
338ebfedea0SLionel Sambuc
339ebfedea0SLionel Sambuc context = calloc(1, sizeof(*context));
340ebfedea0SLionel Sambuc if (context == NULL)
341ebfedea0SLionel Sambuc return NULL;
342ebfedea0SLionel Sambuc context->server = server;
343ebfedea0SLionel Sambuc context->type = type;
344ebfedea0SLionel Sambuc
345ebfedea0SLionel Sambuc return context;
346ebfedea0SLionel Sambuc }
347ebfedea0SLionel Sambuc
348ebfedea0SLionel Sambuc const char *
heim_digest_generate_challenge(heim_digest_t context)349ebfedea0SLionel Sambuc heim_digest_generate_challenge(heim_digest_t context)
350ebfedea0SLionel Sambuc {
351ebfedea0SLionel Sambuc return NULL;
352ebfedea0SLionel Sambuc }
353ebfedea0SLionel Sambuc
354ebfedea0SLionel Sambuc int
heim_digest_parse_challenge(heim_digest_t context,const char * challenge)355ebfedea0SLionel Sambuc heim_digest_parse_challenge(heim_digest_t context, const char *challenge)
356ebfedea0SLionel Sambuc {
357ebfedea0SLionel Sambuc struct md5_value *val = NULL;
358ebfedea0SLionel Sambuc int ret, type;
359ebfedea0SLionel Sambuc
360ebfedea0SLionel Sambuc ret = parse_values(challenge, &val);
361ebfedea0SLionel Sambuc if (ret)
362ebfedea0SLionel Sambuc goto out;
363ebfedea0SLionel Sambuc
364ebfedea0SLionel Sambuc ret = 1;
365ebfedea0SLionel Sambuc
366ebfedea0SLionel Sambuc context->serverNonce = values_find(&val, "nonce");
367ebfedea0SLionel Sambuc if (context->serverNonce == NULL) goto out;
368ebfedea0SLionel Sambuc
369ebfedea0SLionel Sambuc context->serverRealm = values_find(&val, "realm");
370ebfedea0SLionel Sambuc if (context->serverRealm == NULL) goto out;
371ebfedea0SLionel Sambuc
372ebfedea0SLionel Sambuc context->serverQOP = values_find(&val, "qop");
373ebfedea0SLionel Sambuc if (context->serverQOP == NULL)
374ebfedea0SLionel Sambuc context->serverQOP = strdup("auth");
375ebfedea0SLionel Sambuc if (context->serverQOP == NULL) goto out;
376ebfedea0SLionel Sambuc
377ebfedea0SLionel Sambuc /* check alg */
378ebfedea0SLionel Sambuc
379ebfedea0SLionel Sambuc context->serverAlgorithm = values_find(&val, "algorithm");
380ebfedea0SLionel Sambuc if (context->serverAlgorithm == NULL || strcasecmp(context->serverAlgorithm, "md5") == 0) {
381ebfedea0SLionel Sambuc type = HEIM_DIGEST_TYPE_RFC2069;
382ebfedea0SLionel Sambuc } else if (strcasecmp(context->serverAlgorithm, "md5-sess") == 0) {
383ebfedea0SLionel Sambuc type = HEIM_DIGEST_TYPE_MD5_SESS;
384ebfedea0SLionel Sambuc } else {
385ebfedea0SLionel Sambuc goto out;
386ebfedea0SLionel Sambuc }
387ebfedea0SLionel Sambuc
388ebfedea0SLionel Sambuc if (context->type != HEIM_DIGEST_TYPE_AUTO && context->type != type)
389ebfedea0SLionel Sambuc goto out;
390ebfedea0SLionel Sambuc else
391ebfedea0SLionel Sambuc context->type = type;
392ebfedea0SLionel Sambuc
393ebfedea0SLionel Sambuc
394ebfedea0SLionel Sambuc
395ebfedea0SLionel Sambuc ret = 0;
396ebfedea0SLionel Sambuc out:
397ebfedea0SLionel Sambuc free_values(val);
398ebfedea0SLionel Sambuc if (ret)
399ebfedea0SLionel Sambuc clear_context(context);
400ebfedea0SLionel Sambuc return ret;
401ebfedea0SLionel Sambuc }
402ebfedea0SLionel Sambuc
403ebfedea0SLionel Sambuc int
heim_digest_parse_response(heim_digest_t context,const char * response)404ebfedea0SLionel Sambuc heim_digest_parse_response(heim_digest_t context, const char *response)
405ebfedea0SLionel Sambuc {
406ebfedea0SLionel Sambuc struct md5_value *val = NULL;
407ebfedea0SLionel Sambuc char *nonce;
408ebfedea0SLionel Sambuc int ret;
409ebfedea0SLionel Sambuc
410ebfedea0SLionel Sambuc ret = parse_values(response, &val);
411ebfedea0SLionel Sambuc if (ret)
412ebfedea0SLionel Sambuc goto out;
413ebfedea0SLionel Sambuc
414ebfedea0SLionel Sambuc ret = 1;
415ebfedea0SLionel Sambuc
416ebfedea0SLionel Sambuc if (context->type == HEIM_DIGEST_TYPE_AUTO)
417ebfedea0SLionel Sambuc goto out;
418ebfedea0SLionel Sambuc
419ebfedea0SLionel Sambuc context->clientUsername = values_find(&val, "username");
420ebfedea0SLionel Sambuc if (context->clientUsername == NULL) goto out;
421ebfedea0SLionel Sambuc
422ebfedea0SLionel Sambuc context->clientRealm = values_find(&val, "realm");
423ebfedea0SLionel Sambuc
424ebfedea0SLionel Sambuc context->clientResponse = values_find(&val, "response");
425ebfedea0SLionel Sambuc if (context->clientResponse == NULL) goto out;
426ebfedea0SLionel Sambuc
427ebfedea0SLionel Sambuc nonce = values_find(&val, "nonce");
428ebfedea0SLionel Sambuc if (nonce == NULL) goto out;
429ebfedea0SLionel Sambuc
430ebfedea0SLionel Sambuc if (strcmp(nonce, context->serverNonce) != 0) {
431ebfedea0SLionel Sambuc free(nonce);
432ebfedea0SLionel Sambuc goto out;
433ebfedea0SLionel Sambuc }
434ebfedea0SLionel Sambuc free(nonce);
435ebfedea0SLionel Sambuc
436ebfedea0SLionel Sambuc context->clientQOP = values_find(&val, "qop");
437ebfedea0SLionel Sambuc if (context->clientQOP == NULL)
438ebfedea0SLionel Sambuc context->clientQOP = strdup("auth");
439ebfedea0SLionel Sambuc if (context->clientQOP == NULL) goto out;
440ebfedea0SLionel Sambuc
441ebfedea0SLionel Sambuc
442ebfedea0SLionel Sambuc if (context->type != HEIM_DIGEST_TYPE_RFC2069) {
443ebfedea0SLionel Sambuc context->clientNC = values_find(&val, "nc");
444ebfedea0SLionel Sambuc if (context->clientNC == NULL) goto out;
445ebfedea0SLionel Sambuc
446ebfedea0SLionel Sambuc context->clientNonce = values_find(&val, "cnonce");
447ebfedea0SLionel Sambuc if (context->clientNonce == NULL) goto out;
448ebfedea0SLionel Sambuc }
449ebfedea0SLionel Sambuc
450ebfedea0SLionel Sambuc if (context->type == HEIM_DIGEST_TYPE_RFC2069)
451ebfedea0SLionel Sambuc context->clientURI = values_find(&val, "uri");
452ebfedea0SLionel Sambuc else
453ebfedea0SLionel Sambuc context->clientURI = values_find(&val, "digest-uri");
454ebfedea0SLionel Sambuc if (context->clientURI == NULL) goto out;
455ebfedea0SLionel Sambuc
456ebfedea0SLionel Sambuc ret = 0;
457ebfedea0SLionel Sambuc out:
458ebfedea0SLionel Sambuc free_values(val);
459ebfedea0SLionel Sambuc return ret;
460ebfedea0SLionel Sambuc }
461ebfedea0SLionel Sambuc
462ebfedea0SLionel Sambuc const char *
heim_digest_get_key(heim_digest_t context,const char * key)463ebfedea0SLionel Sambuc heim_digest_get_key(heim_digest_t context, const char *key)
464ebfedea0SLionel Sambuc {
465ebfedea0SLionel Sambuc if (strcmp(key, "username") == 0) {
466ebfedea0SLionel Sambuc return context->clientUsername;
467ebfedea0SLionel Sambuc } else if (strcmp(key, "realm") == 0) {
468ebfedea0SLionel Sambuc return context->clientRealm;
469ebfedea0SLionel Sambuc } else {
470ebfedea0SLionel Sambuc return NULL;
471ebfedea0SLionel Sambuc }
472ebfedea0SLionel Sambuc }
473ebfedea0SLionel Sambuc
474ebfedea0SLionel Sambuc int
heim_digest_set_key(heim_digest_t context,const char * key,const char * value)475ebfedea0SLionel Sambuc heim_digest_set_key(heim_digest_t context, const char *key, const char *value)
476ebfedea0SLionel Sambuc {
477ebfedea0SLionel Sambuc if (strcmp(key, "password") == 0) {
478ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->password);
479ebfedea0SLionel Sambuc if ((context->password = strdup(value)) == NULL)
480ebfedea0SLionel Sambuc return ENOMEM;
481ebfedea0SLionel Sambuc } else if (strcmp(key, "method") == 0) {
482ebfedea0SLionel Sambuc FREE_AND_CLEAR(context->serverMethod);
483ebfedea0SLionel Sambuc if ((context->serverMethod = strdup(value)) != NULL)
484ebfedea0SLionel Sambuc return ENOMEM;
485ebfedea0SLionel Sambuc } else {
486ebfedea0SLionel Sambuc return EINVAL;
487ebfedea0SLionel Sambuc }
488ebfedea0SLionel Sambuc return 0;
489ebfedea0SLionel Sambuc }
490ebfedea0SLionel Sambuc
491ebfedea0SLionel Sambuc static char *
build_digest(heim_digest_t context,const char * a1,const char * method)492ebfedea0SLionel Sambuc build_digest(heim_digest_t context, const char *a1, const char *method)
493ebfedea0SLionel Sambuc {
494ebfedea0SLionel Sambuc CC_MD5_CTX ctx;
495ebfedea0SLionel Sambuc uint8_t md[CC_MD5_DIGEST_LENGTH];
496ebfedea0SLionel Sambuc char *a2, *str = NULL;
497ebfedea0SLionel Sambuc
498ebfedea0SLionel Sambuc a2 = build_A2_hash(context, method);
499ebfedea0SLionel Sambuc if (a2 == NULL)
500ebfedea0SLionel Sambuc return NULL;
501ebfedea0SLionel Sambuc
502ebfedea0SLionel Sambuc CC_MD5_Init(&ctx);
503ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, a1, strlen(a1));
504ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
505ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, context->serverNonce, strlen(context->serverNonce));
506ebfedea0SLionel Sambuc if (context->type != HEIM_DIGEST_TYPE_RFC2069) {
507ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
508ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, context->clientNC, strlen(context->clientNC));
509ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
510ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, context->clientNonce, strlen(context->clientNonce));
511ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
512ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, context->clientQOP, strlen(context->clientQOP));
513ebfedea0SLionel Sambuc }
514ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, ":", 1);
515ebfedea0SLionel Sambuc CC_MD5_Update(&ctx, a2, strlen(a2));
516ebfedea0SLionel Sambuc CC_MD5_Final(md, &ctx);
517ebfedea0SLionel Sambuc
518ebfedea0SLionel Sambuc free(a2);
519ebfedea0SLionel Sambuc
520ebfedea0SLionel Sambuc hex_encode(md, sizeof(md), &str);
521ebfedea0SLionel Sambuc if (str)
522ebfedea0SLionel Sambuc strlwr(str);
523ebfedea0SLionel Sambuc
524ebfedea0SLionel Sambuc return str;
525ebfedea0SLionel Sambuc }
526ebfedea0SLionel Sambuc
527ebfedea0SLionel Sambuc const char *
heim_digest_create_response(heim_digest_t context)528ebfedea0SLionel Sambuc heim_digest_create_response(heim_digest_t context)
529ebfedea0SLionel Sambuc {
530ebfedea0SLionel Sambuc return NULL;
531ebfedea0SLionel Sambuc }
532ebfedea0SLionel Sambuc
533ebfedea0SLionel Sambuc int
heim_digest_verify(heim_digest_t context,char ** response)534ebfedea0SLionel Sambuc heim_digest_verify(heim_digest_t context, char **response)
535ebfedea0SLionel Sambuc {
536ebfedea0SLionel Sambuc CC_MD5_CTX ctx;
537ebfedea0SLionel Sambuc char *a1, *a2;
538ebfedea0SLionel Sambuc uint8_t md[CC_MD5_DIGEST_LENGTH];
539ebfedea0SLionel Sambuc char *str;
540ebfedea0SLionel Sambuc int res;
541ebfedea0SLionel Sambuc
542ebfedea0SLionel Sambuc if (response)
543ebfedea0SLionel Sambuc *response = NULL;
544ebfedea0SLionel Sambuc
545ebfedea0SLionel Sambuc if (context->serverMethod == NULL) {
546ebfedea0SLionel Sambuc if (context->type != HEIM_DIGEST_TYPE_RFC2069)
547ebfedea0SLionel Sambuc context->serverMethod = strdup("AUTHENTICATE");
548ebfedea0SLionel Sambuc else
549ebfedea0SLionel Sambuc context->serverMethod = strdup("GET");
550ebfedea0SLionel Sambuc }
551ebfedea0SLionel Sambuc
552ebfedea0SLionel Sambuc a1 = build_A1_hash(context->type,
553ebfedea0SLionel Sambuc context->clientUsername, context->password,
554ebfedea0SLionel Sambuc context->serverRealm, context->serverNonce,
555ebfedea0SLionel Sambuc context->clientNonce, context->auth_id);
556ebfedea0SLionel Sambuc if (a1 == NULL)
557ebfedea0SLionel Sambuc return ENOMEM;
558ebfedea0SLionel Sambuc
559ebfedea0SLionel Sambuc str = build_digest(context, a1, context->serverMethod);
560ebfedea0SLionel Sambuc if (str == NULL) {
561ebfedea0SLionel Sambuc MEMSET_FREE_AND_CLEAR(a1);
562ebfedea0SLionel Sambuc return ENOMEM;
563ebfedea0SLionel Sambuc }
564ebfedea0SLionel Sambuc
565ebfedea0SLionel Sambuc res = (strcmp(str, context->clientResponse) == 0) ? 0 : EINVAL;
566ebfedea0SLionel Sambuc free(str);
567ebfedea0SLionel Sambuc if (res) {
568ebfedea0SLionel Sambuc MEMSET_FREE_AND_CLEAR(a1);
569ebfedea0SLionel Sambuc return res;
570ebfedea0SLionel Sambuc }
571ebfedea0SLionel Sambuc
572ebfedea0SLionel Sambuc /* build server_response */
573ebfedea0SLionel Sambuc if (response) {
574ebfedea0SLionel Sambuc str = build_digest(context, a1, NULL);
575ebfedea0SLionel Sambuc if (str == NULL) {
576ebfedea0SLionel Sambuc MEMSET_FREE_AND_CLEAR(a1);
577ebfedea0SLionel Sambuc return ENOMEM;
578ebfedea0SLionel Sambuc }
579ebfedea0SLionel Sambuc
580ebfedea0SLionel Sambuc asprintf(response, "rspauth=%s", str);
581ebfedea0SLionel Sambuc free(str);
582ebfedea0SLionel Sambuc }
583ebfedea0SLionel Sambuc MEMSET_FREE_AND_CLEAR(a1);
584ebfedea0SLionel Sambuc
585ebfedea0SLionel Sambuc return 0;
586ebfedea0SLionel Sambuc }
587ebfedea0SLionel Sambuc
588ebfedea0SLionel Sambuc void
heim_digest_get_session_key(heim_digest_t context,void ** key,size_t * keySize)589ebfedea0SLionel Sambuc heim_digest_get_session_key(heim_digest_t context, void **key, size_t *keySize)
590ebfedea0SLionel Sambuc {
591ebfedea0SLionel Sambuc }
592ebfedea0SLionel Sambuc
593ebfedea0SLionel Sambuc void
heim_digest_release(heim_digest_t context)594ebfedea0SLionel Sambuc heim_digest_release(heim_digest_t context)
595ebfedea0SLionel Sambuc {
596ebfedea0SLionel Sambuc clear_context(context);
597ebfedea0SLionel Sambuc free(context);
598ebfedea0SLionel Sambuc }
599ebfedea0SLionel Sambuc
600