Allow KDC to always return the salt in the PA-ETYPE-INFO[2]
[heimdal.git] / kdc / digest-service.c
blob6608d0a06bb0c79cff953495b2b5d17fd3ddbe41
1 /*
2 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #define HC_DEPRECATED_CRYPTO
38 #include "headers.h"
39 #include <digest_asn1.h>
40 #include <heimntlm.h>
41 #include <heim-ipc.h>
42 #include <getarg.h>
44 typedef struct pk_client_params pk_client_params;
45 struct DigestREQ;
46 struct Kx509Request;
48 #include <kdc-private.h>
50 krb5_kdc_configuration *config;
52 static void
53 ntlm_service(void *ctx, const heim_idata *req,
54 const heim_icred cred,
55 heim_ipc_complete complete,
56 heim_sipc_call cctx)
58 NTLMRequest2 ntq;
59 unsigned char sessionkey[16];
60 heim_idata rep = { 0, NULL };
61 krb5_context context = ctx;
62 hdb_entry_ex *user = NULL;
63 Key *key = NULL;
64 NTLMReply ntp;
65 size_t size;
66 int ret;
67 const char *domain;
69 kdc_log(context, config, 4, "digest-request: uid=%d",
70 (int)heim_ipc_cred_get_uid(cred));
72 if (heim_ipc_cred_get_uid(cred) != 0) {
73 (*complete)(cctx, EPERM, NULL);
74 return;
77 ntp.success = 0;
78 ntp.flags = 0;
79 ntp.sessionkey = NULL;
81 ret = decode_NTLMRequest2(req->data, req->length, &ntq, NULL);
82 if (ret)
83 goto failed;
85 /* XXX forward to NetrLogonSamLogonEx() if not a local domain */
86 if (strcmp(ntq.loginDomainName, "BUILTIN") == 0) {
87 domain = ntq.loginDomainName;
88 } else if (strcmp(ntq.loginDomainName, "") == 0) {
89 domain = "BUILTIN";
90 } else {
91 ret = EINVAL;
92 goto failed;
95 kdc_log(context, config, 4, "digest-request: user=%s/%s",
96 ntq.loginUserName, domain);
98 if (ntq.lmchallenge.length != 8)
99 goto failed;
101 if (ntq.ntChallengeResponce.length == 0)
102 goto failed;
105 krb5_principal client;
107 ret = krb5_make_principal(context, &client, domain,
108 ntq.loginUserName, NULL);
109 if (ret)
110 goto failed;
112 krb5_principal_set_type(context, client, KRB5_NT_NTLM);
114 ret = _kdc_db_fetch(context, config, client,
115 HDB_F_GET_CLIENT, NULL, NULL, &user);
116 krb5_free_principal(context, client);
117 if (ret)
118 goto failed;
120 ret = hdb_enctype2key(context, &user->entry, NULL,
121 ETYPE_ARCFOUR_HMAC_MD5, &key);
122 if (ret) {
123 krb5_set_error_message(context, ret, "NTLM missing arcfour key");
124 goto failed;
128 kdc_log(context, config, 5,
129 "digest-request: found user, processing ntlm request");
131 if (ntq.ntChallengeResponce.length != 24) {
132 struct ntlm_buf infotarget, answer;
134 answer.length = ntq.ntChallengeResponce.length;
135 answer.data = ntq.ntChallengeResponce.data;
137 ret = heim_ntlm_verify_ntlm2(key->key.keyvalue.data,
138 key->key.keyvalue.length,
139 ntq.loginUserName,
140 ntq.loginDomainName,
142 ntq.lmchallenge.data,
143 &answer,
144 &infotarget,
145 sessionkey);
146 if (ret) {
147 goto failed;
150 free(infotarget.data);
151 /* XXX verify info target */
153 } else {
154 struct ntlm_buf answer;
156 if (ntq.flags & NTLM_NEG_NTLM2_SESSION) {
157 unsigned char sessionhash[MD5_DIGEST_LENGTH];
158 EVP_MD_CTX *md5ctx;
160 /* the first first 8 bytes is the challenge, what is the other 16 bytes ? */
161 if (ntq.lmChallengeResponce.length != 24)
162 goto failed;
164 md5ctx = EVP_MD_CTX_create();
165 EVP_DigestInit_ex(md5ctx, EVP_md5(), NULL);
166 EVP_DigestUpdate(md5ctx, ntq.lmchallenge.data, 8);
167 EVP_DigestUpdate(md5ctx, ntq.lmChallengeResponce.data, 8);
168 EVP_DigestFinal_ex(md5ctx, sessionhash, NULL);
169 EVP_MD_CTX_destroy(md5ctx);
170 memcpy(ntq.lmchallenge.data, sessionhash, ntq.lmchallenge.length);
173 ret = heim_ntlm_calculate_ntlm1(key->key.keyvalue.data,
174 key->key.keyvalue.length,
175 ntq.lmchallenge.data, &answer);
176 if (ret)
177 goto failed;
179 if (ntq.ntChallengeResponce.length != answer.length ||
180 memcmp(ntq.ntChallengeResponce.data, answer.data, answer.length) != 0) {
181 free(answer.data);
182 ret = EINVAL;
183 goto failed;
185 free(answer.data);
188 EVP_MD_CTX *ctxp;
190 ctxp = EVP_MD_CTX_create();
191 EVP_DigestInit_ex(ctxp, EVP_md4(), NULL);
192 EVP_DigestUpdate(ctxp, key->key.keyvalue.data, key->key.keyvalue.length);
193 EVP_DigestFinal_ex(ctxp, sessionkey, NULL);
194 EVP_MD_CTX_destroy(ctxp);
198 ntp.success = 1;
200 ASN1_MALLOC_ENCODE(NTLMReply, rep.data, rep.length, &ntp, &size, ret);
201 if (ret)
202 goto failed;
203 if (rep.length != size)
204 abort();
206 failed:
207 kdc_log(context, config, 4, "digest-request: %d", ret);
209 (*complete)(cctx, ret, &rep);
211 free(rep.data);
213 free_NTLMRequest2(&ntq);
214 if (user)
215 _kdc_free_ent (context, user);
218 static int help_flag;
219 static int version_flag;
221 static struct getargs args[] = {
222 { "help", 'h', arg_flag, &help_flag, NULL, NULL },
223 { "version", 'v', arg_flag, &version_flag, NULL, NULL }
226 static int num_args = sizeof(args) / sizeof(args[0]);
228 static void
229 usage(int ret)
231 arg_printusage (args, num_args, NULL, "");
232 exit (ret);
236 main(int argc, char **argv)
238 krb5_context context;
239 int ret, optidx = 0;
241 setprogname(argv[0]);
243 if (getarg(args, num_args, argc, argv, &optidx))
244 usage(1);
246 if (help_flag)
247 usage(0);
249 if (version_flag) {
250 print_version(NULL);
251 exit(0);
254 ret = krb5_init_context(&context);
255 if (ret)
256 krb5_errx(context, 1, "krb5_init_context");
258 ret = krb5_kdc_get_config(context, &config);
259 if (ret)
260 krb5_err(context, 1, ret, "krb5_kdc_default_config");
262 kdc_openlog(context, "digest-service", config);
264 ret = krb5_kdc_set_dbinfo(context, config);
265 if (ret)
266 krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");
268 #if __APPLE__
270 heim_sipc mach;
271 heim_sipc_launchd_mach_init("org.h5l.ntlm-service",
272 ntlm_service, context, &mach);
273 heim_sipc_timeout(60);
275 #endif
276 #ifdef HAVE_DOOR_CREATE
278 heim_sipc door;
279 heim_sipc_service_door("org.h5l.ntlm-service", ntlm_service, NULL, &door);
281 #endif
283 heim_sipc un;
284 heim_sipc_service_unix("org.h5l.ntlm-service", ntlm_service, NULL, &un);
287 heim_ipc_main();
288 return 0;