1*0a6a1f1dSLionel Sambuc /*	$NetBSD: apop.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2010 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 <krb5/roken.h>
44ebfedea0SLionel Sambuc #include <krb5/hex.h>
45ebfedea0SLionel Sambuc #include "heim-auth.h"
46ebfedea0SLionel Sambuc #include <krb5/ntlm_err.h>
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc char *
heim_generate_challenge(const char * hostname)49ebfedea0SLionel Sambuc heim_generate_challenge(const char *hostname)
50ebfedea0SLionel Sambuc {
51ebfedea0SLionel Sambuc     char host[MAXHOSTNAMELEN], *str = NULL;
52ebfedea0SLionel Sambuc     uint32_t num, t;
53ebfedea0SLionel Sambuc 
54ebfedea0SLionel Sambuc     if (hostname == NULL) {
55ebfedea0SLionel Sambuc 	if (gethostname(host, sizeof(host)))
56ebfedea0SLionel Sambuc 	    return NULL;
57ebfedea0SLionel Sambuc 	hostname = host;
58ebfedea0SLionel Sambuc     }
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc     t = time(NULL);
61ebfedea0SLionel Sambuc     num = rk_random();
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc     asprintf(&str, "<%lu%lu@%s>", (unsigned long)t,
64ebfedea0SLionel Sambuc 	     (unsigned long)num, hostname);
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc     return str;
67ebfedea0SLionel Sambuc }
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc char *
heim_apop_create(const char * challenge,const char * password)70ebfedea0SLionel Sambuc heim_apop_create(const char *challenge, const char *password)
71ebfedea0SLionel Sambuc {
72ebfedea0SLionel Sambuc     char *str = NULL;
73ebfedea0SLionel Sambuc     uint8_t hash[CC_MD5_DIGEST_LENGTH];
74ebfedea0SLionel Sambuc     CC_MD5_CTX ctx;
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc     CC_MD5_Init(&ctx);
77ebfedea0SLionel Sambuc     CC_MD5_Update(&ctx, challenge, strlen(challenge));
78ebfedea0SLionel Sambuc     CC_MD5_Update(&ctx, password, strlen(password));
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc     CC_MD5_Final(hash, &ctx);
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc     hex_encode(hash, sizeof(hash), &str);
83ebfedea0SLionel Sambuc     if (str)
84ebfedea0SLionel Sambuc       strlwr(str);
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc     return str;
87ebfedea0SLionel Sambuc }
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc int
heim_apop_verify(const char * challenge,const char * password,const char * response)90ebfedea0SLionel Sambuc heim_apop_verify(const char *challenge, const char *password, const char *response)
91ebfedea0SLionel Sambuc {
92ebfedea0SLionel Sambuc     char *str;
93ebfedea0SLionel Sambuc     int res;
94ebfedea0SLionel Sambuc 
95ebfedea0SLionel Sambuc     str = heim_apop_create(challenge, password);
96ebfedea0SLionel Sambuc     if (str == NULL)
97ebfedea0SLionel Sambuc 	return ENOMEM;
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc     res = (strcasecmp(str, response) != 0);
100ebfedea0SLionel Sambuc     free(str);
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc     if (res)
103ebfedea0SLionel Sambuc 	return HNTLM_ERR_INVALID_APOP;
104ebfedea0SLionel Sambuc     return 0;
105ebfedea0SLionel Sambuc }
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc struct heim_cram_md5 {
108ebfedea0SLionel Sambuc     CC_MD5_CTX ipad;
109ebfedea0SLionel Sambuc     CC_MD5_CTX opad;
110ebfedea0SLionel Sambuc };
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc void
heim_cram_md5_export(const char * password,heim_CRAM_MD5_STATE * state)114ebfedea0SLionel Sambuc heim_cram_md5_export(const char *password, heim_CRAM_MD5_STATE *state)
115ebfedea0SLionel Sambuc {
116ebfedea0SLionel Sambuc     size_t keylen = strlen(password);
117ebfedea0SLionel Sambuc     uint8_t key[CC_MD5_BLOCK_BYTES];
118ebfedea0SLionel Sambuc     uint8_t pad[CC_MD5_BLOCK_BYTES];
119ebfedea0SLionel Sambuc     struct heim_cram_md5 ctx;
120ebfedea0SLionel Sambuc     size_t n;
121ebfedea0SLionel Sambuc 
122ebfedea0SLionel Sambuc     memset(&ctx, 0, sizeof(ctx));
123ebfedea0SLionel Sambuc 
124ebfedea0SLionel Sambuc     if (keylen > CC_MD5_BLOCK_BYTES) {
125ebfedea0SLionel Sambuc 	CC_MD5(password, keylen, key);
126ebfedea0SLionel Sambuc 	keylen = sizeof(keylen);
127ebfedea0SLionel Sambuc     } else {
128ebfedea0SLionel Sambuc 	memcpy(key, password, keylen);
129ebfedea0SLionel Sambuc     }
130ebfedea0SLionel Sambuc 
131ebfedea0SLionel Sambuc     memset(pad, 0x36, sizeof(pad));
132ebfedea0SLionel Sambuc     for (n = 0; n < keylen; n++)
133ebfedea0SLionel Sambuc 	pad[n] ^= key[n];
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc     CC_MD5_Init(&ctx.ipad);
136ebfedea0SLionel Sambuc     CC_MD5_Init(&ctx.opad);
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc     CC_MD5_Update(&ctx.ipad, pad, sizeof(pad));
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc     memset(pad, 0x5c, sizeof(pad));
141ebfedea0SLionel Sambuc     for (n = 0; n < keylen; n++)
142ebfedea0SLionel Sambuc 	pad[n] ^= key[n];
143ebfedea0SLionel Sambuc 
144ebfedea0SLionel Sambuc     CC_MD5_Update(&ctx.opad, pad, sizeof(pad));
145ebfedea0SLionel Sambuc 
146ebfedea0SLionel Sambuc     memset(pad, 0, sizeof(pad));
147ebfedea0SLionel Sambuc     memset(key, 0, sizeof(key));
148ebfedea0SLionel Sambuc 
149ebfedea0SLionel Sambuc     state->istate[0] = htonl(ctx.ipad.A);
150ebfedea0SLionel Sambuc     state->istate[1] = htonl(ctx.ipad.B);
151ebfedea0SLionel Sambuc     state->istate[2] = htonl(ctx.ipad.C);
152ebfedea0SLionel Sambuc     state->istate[3] = htonl(ctx.ipad.D);
153ebfedea0SLionel Sambuc 
154ebfedea0SLionel Sambuc     state->ostate[0] = htonl(ctx.opad.A);
155ebfedea0SLionel Sambuc     state->ostate[1] = htonl(ctx.opad.B);
156ebfedea0SLionel Sambuc     state->ostate[2] = htonl(ctx.opad.C);
157ebfedea0SLionel Sambuc     state->ostate[3] = htonl(ctx.opad.D);
158ebfedea0SLionel Sambuc 
159ebfedea0SLionel Sambuc     memset(&ctx, 0, sizeof(ctx));
160ebfedea0SLionel Sambuc }
161ebfedea0SLionel Sambuc 
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc heim_cram_md5
heim_cram_md5_import(void * data,size_t len)164ebfedea0SLionel Sambuc heim_cram_md5_import(void *data, size_t len)
165ebfedea0SLionel Sambuc {
166ebfedea0SLionel Sambuc     heim_CRAM_MD5_STATE state;
167ebfedea0SLionel Sambuc     heim_cram_md5 ctx;
168ebfedea0SLionel Sambuc     unsigned n;
169ebfedea0SLionel Sambuc 
170ebfedea0SLionel Sambuc     if (len != sizeof(state))
171ebfedea0SLionel Sambuc 	return NULL;
172ebfedea0SLionel Sambuc 
173ebfedea0SLionel Sambuc     ctx = calloc(1, sizeof(*ctx));
174ebfedea0SLionel Sambuc     if (ctx == NULL)
175ebfedea0SLionel Sambuc 	return NULL;
176ebfedea0SLionel Sambuc 
177ebfedea0SLionel Sambuc     memcpy(&state, data, sizeof(state));
178ebfedea0SLionel Sambuc 
179ebfedea0SLionel Sambuc     ctx->ipad.A = ntohl(state.istate[0]);
180ebfedea0SLionel Sambuc     ctx->ipad.B = ntohl(state.istate[1]);
181ebfedea0SLionel Sambuc     ctx->ipad.C = ntohl(state.istate[2]);
182ebfedea0SLionel Sambuc     ctx->ipad.D = ntohl(state.istate[3]);
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc     ctx->opad.A = ntohl(state.ostate[0]);
185ebfedea0SLionel Sambuc     ctx->opad.B = ntohl(state.ostate[1]);
186ebfedea0SLionel Sambuc     ctx->opad.C = ntohl(state.ostate[2]);
187ebfedea0SLionel Sambuc     ctx->opad.D = ntohl(state.ostate[3]);
188ebfedea0SLionel Sambuc 
189ebfedea0SLionel Sambuc     ctx->ipad.Nl = ctx->opad.Nl = 512;
190ebfedea0SLionel Sambuc     ctx->ipad.Nh = ctx->opad.Nh = 0;
191ebfedea0SLionel Sambuc     ctx->ipad.num = ctx->opad.num = 0;
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc     return ctx;
194ebfedea0SLionel Sambuc }
195ebfedea0SLionel Sambuc 
196ebfedea0SLionel Sambuc int
heim_cram_md5_verify_ctx(heim_cram_md5 ctx,const char * challenge,const char * response)197ebfedea0SLionel Sambuc heim_cram_md5_verify_ctx(heim_cram_md5 ctx, const char *challenge, const char *response)
198ebfedea0SLionel Sambuc {
199ebfedea0SLionel Sambuc     uint8_t hash[CC_MD5_DIGEST_LENGTH];
200ebfedea0SLionel Sambuc     char *str = NULL;
201ebfedea0SLionel Sambuc     int res;
202ebfedea0SLionel Sambuc 
203ebfedea0SLionel Sambuc     CC_MD5_Update(&ctx->ipad, challenge, strlen(challenge));
204ebfedea0SLionel Sambuc     CC_MD5_Final(hash, &ctx->ipad);
205ebfedea0SLionel Sambuc 
206ebfedea0SLionel Sambuc     CC_MD5_Update(&ctx->opad, hash, sizeof(hash));
207ebfedea0SLionel Sambuc     CC_MD5_Final(hash, &ctx->opad);
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc     hex_encode(hash, sizeof(hash), &str);
210ebfedea0SLionel Sambuc     if (str == NULL)
211ebfedea0SLionel Sambuc 	return ENOMEM;
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc     res = (strcasecmp(str, response) != 0);
214ebfedea0SLionel Sambuc     free(str);
215ebfedea0SLionel Sambuc 
216ebfedea0SLionel Sambuc     if (res)
217ebfedea0SLionel Sambuc 	return HNTLM_ERR_INVALID_CRAM_MD5;
218ebfedea0SLionel Sambuc     return 0;
219ebfedea0SLionel Sambuc }
220ebfedea0SLionel Sambuc 
221ebfedea0SLionel Sambuc void
heim_cram_md5_free(heim_cram_md5 ctx)222ebfedea0SLionel Sambuc heim_cram_md5_free(heim_cram_md5 ctx)
223ebfedea0SLionel Sambuc {
224ebfedea0SLionel Sambuc     memset(ctx, 0, sizeof(*ctx));
225ebfedea0SLionel Sambuc     free(ctx);
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc 
228ebfedea0SLionel Sambuc 
229ebfedea0SLionel Sambuc char *
heim_cram_md5_create(const char * challenge,const char * password)230ebfedea0SLionel Sambuc heim_cram_md5_create(const char *challenge, const char *password)
231ebfedea0SLionel Sambuc {
232ebfedea0SLionel Sambuc     CCHmacContext ctx;
233ebfedea0SLionel Sambuc     uint8_t hash[CC_MD5_DIGEST_LENGTH];
234ebfedea0SLionel Sambuc     char *str = NULL;
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc     CCHmacInit(&ctx, kCCHmacAlgMD5, password, strlen(password));
237ebfedea0SLionel Sambuc     CCHmacUpdate(&ctx, challenge, strlen(challenge));
238ebfedea0SLionel Sambuc     CCHmacFinal(&ctx, hash);
239ebfedea0SLionel Sambuc 
240ebfedea0SLionel Sambuc     memset(&ctx, 0, sizeof(ctx));
241ebfedea0SLionel Sambuc 
242ebfedea0SLionel Sambuc     hex_encode(hash, sizeof(hash), &str);
243ebfedea0SLionel Sambuc     if (str)
244ebfedea0SLionel Sambuc       strlwr(str);
245ebfedea0SLionel Sambuc 
246ebfedea0SLionel Sambuc     return str;
247ebfedea0SLionel Sambuc }
248ebfedea0SLionel Sambuc 
249ebfedea0SLionel Sambuc  int
heim_cram_md5_verify(const char * challenge,const char * password,const char * response)250ebfedea0SLionel Sambuc heim_cram_md5_verify(const char *challenge, const char *password, const char *response)
251ebfedea0SLionel Sambuc {
252ebfedea0SLionel Sambuc     char *str;
253ebfedea0SLionel Sambuc     int res;
254ebfedea0SLionel Sambuc 
255ebfedea0SLionel Sambuc     str = heim_cram_md5_create(challenge, password);
256ebfedea0SLionel Sambuc     if (str == NULL)
257ebfedea0SLionel Sambuc 	return ENOMEM;
258ebfedea0SLionel Sambuc 
259ebfedea0SLionel Sambuc     res = (strcasecmp(str, response) != 0);
260ebfedea0SLionel Sambuc     free(str);
261ebfedea0SLionel Sambuc 
262ebfedea0SLionel Sambuc     if (res)
263ebfedea0SLionel Sambuc 	return HNTLM_ERR_INVALID_CRAM_MD5;
264ebfedea0SLionel Sambuc     return 0;
265ebfedea0SLionel Sambuc }
266ebfedea0SLionel Sambuc 
267