Path: blob/main/crypto/heimdal/kdc/digest-service.c
34860 views
/*1* Copyright (c) 2006 - 2007 Kungliga Tekniska H�gskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#define HC_DEPRECATED_CRYPTO3637#include "headers.h"38#include <digest_asn1.h>39#include <heimntlm.h>40#include <heim-ipc.h>41#include <getarg.h>4243typedef struct pk_client_params pk_client_params;44struct DigestREQ;45struct Kx509Request;46#include <kdc-private.h>4748krb5_kdc_configuration *config;4950static void51ntlm_service(void *ctx, const heim_idata *req,52const heim_icred cred,53heim_ipc_complete complete,54heim_sipc_call cctx)55{56NTLMRequest2 ntq;57unsigned char sessionkey[16];58heim_idata rep = { 0, NULL };59krb5_context context = ctx;60hdb_entry_ex *user = NULL;61Key *key = NULL;62NTLMReply ntp;63size_t size;64int ret;65const char *domain;6667kdc_log(context, config, 1, "digest-request: uid=%d",68(int)heim_ipc_cred_get_uid(cred));6970if (heim_ipc_cred_get_uid(cred) != 0) {71(*complete)(cctx, EPERM, NULL);72return;73}7475ntp.success = 0;76ntp.flags = 0;77ntp.sessionkey = NULL;7879ret = decode_NTLMRequest2(req->data, req->length, &ntq, NULL);80if (ret)81goto failed;8283/* XXX forward to NetrLogonSamLogonEx() if not a local domain */84if (strcmp(ntq.loginDomainName, "BUILTIN") == 0) {85domain = ntq.loginDomainName;86} else if (strcmp(ntq.loginDomainName, "") == 0) {87domain = "BUILTIN";88} else {89ret = EINVAL;90goto failed;91}9293kdc_log(context, config, 1, "digest-request: user=%s/%s",94ntq.loginUserName, domain);9596if (ntq.lmchallenge.length != 8)97goto failed;9899if (ntq.ntChallengeResponce.length == 0)100goto failed;101102{103krb5_principal client;104105ret = krb5_make_principal(context, &client, domain,106ntq.loginUserName, NULL);107if (ret)108goto failed;109110krb5_principal_set_type(context, client, KRB5_NT_NTLM);111112ret = _kdc_db_fetch(context, config, client,113HDB_F_GET_CLIENT, NULL, NULL, &user);114krb5_free_principal(context, client);115if (ret)116goto failed;117118ret = hdb_enctype2key(context, &user->entry,119ETYPE_ARCFOUR_HMAC_MD5, &key);120if (ret) {121krb5_set_error_message(context, ret, "NTLM missing arcfour key");122goto failed;123}124}125126kdc_log(context, config, 2,127"digest-request: found user, processing ntlm request", ret);128129if (ntq.ntChallengeResponce.length != 24) {130struct ntlm_buf infotarget, answer;131132answer.length = ntq.ntChallengeResponce.length;133answer.data = ntq.ntChallengeResponce.data;134135ret = heim_ntlm_verify_ntlm2(key->key.keyvalue.data,136key->key.keyvalue.length,137ntq.loginUserName,138ntq.loginDomainName,1390,140ntq.lmchallenge.data,141&answer,142&infotarget,143sessionkey);144if (ret) {145goto failed;146}147148free(infotarget.data);149/* XXX verify info target */150151} else {152struct ntlm_buf answer;153154if (ntq.flags & NTLM_NEG_NTLM2_SESSION) {155unsigned char sessionhash[MD5_DIGEST_LENGTH];156EVP_MD_CTX *md5ctx;157158/* the first first 8 bytes is the challenge, what is the other 16 bytes ? */159if (ntq.lmChallengeResponce.length != 24)160goto failed;161162md5ctx = EVP_MD_CTX_create();163EVP_DigestInit_ex(md5ctx, EVP_md5(), NULL);164EVP_DigestUpdate(md5ctx, ntq.lmchallenge.data, 8);165EVP_DigestUpdate(md5ctx, ntq.lmChallengeResponce.data, 8);166EVP_DigestFinal_ex(md5ctx, sessionhash, NULL);167EVP_MD_CTX_destroy(md5ctx);168memcpy(ntq.lmchallenge.data, sessionhash, ntq.lmchallenge.length);169}170171ret = heim_ntlm_calculate_ntlm1(key->key.keyvalue.data,172key->key.keyvalue.length,173ntq.lmchallenge.data, &answer);174if (ret)175goto failed;176177if (ntq.ntChallengeResponce.length != answer.length ||178memcmp(ntq.ntChallengeResponce.data, answer.data, answer.length) != 0) {179free(answer.data);180ret = EINVAL;181goto failed;182}183free(answer.data);184185{186EVP_MD_CTX *ctxp;187188ctxp = EVP_MD_CTX_create();189EVP_DigestInit_ex(ctxp, EVP_md4(), NULL);190EVP_DigestUpdate(ctxp, key->key.keyvalue.data, key->key.keyvalue.length);191EVP_DigestFinal_ex(ctxp, sessionkey, NULL);192EVP_MD_CTX_destroy(ctxp);193}194}195196ntp.success = 1;197198ASN1_MALLOC_ENCODE(NTLMReply, rep.data, rep.length, &ntp, &size, ret);199if (ret)200goto failed;201if (rep.length != size)202abort();203204failed:205kdc_log(context, config, 1, "digest-request: %d", ret);206207(*complete)(cctx, ret, &rep);208209free(rep.data);210211free_NTLMRequest2(&ntq);212if (user)213_kdc_free_ent (context, user);214}215216static int help_flag;217static int version_flag;218219static struct getargs args[] = {220{ "help", 'h', arg_flag, &help_flag, NULL, NULL },221{ "version", 'v', arg_flag, &version_flag, NULL, NULL }222};223224static int num_args = sizeof(args) / sizeof(args[0]);225226static void227usage(int ret)228{229arg_printusage (args, num_args, NULL, "");230exit (ret);231}232233int234main(int argc, char **argv)235{236krb5_context context;237int ret, optidx = 0;238239setprogname(argv[0]);240241if (getarg(args, num_args, argc, argv, &optidx))242usage(1);243244if (help_flag)245usage(0);246247if (version_flag) {248print_version(NULL);249exit(0);250}251252ret = krb5_init_context(&context);253if (ret)254krb5_errx(context, 1, "krb5_init_context");255256ret = krb5_kdc_get_config(context, &config);257if (ret)258krb5_err(context, 1, ret, "krb5_kdc_default_config");259260kdc_openlog(context, "digest-service", config);261262ret = krb5_kdc_set_dbinfo(context, config);263if (ret)264krb5_err(context, 1, ret, "krb5_kdc_set_dbinfo");265266#if __APPLE__267{268heim_sipc mach;269heim_sipc_launchd_mach_init("org.h5l.ntlm-service",270ntlm_service, context, &mach);271heim_sipc_timeout(60);272}273#endif274{275heim_sipc un;276heim_sipc_service_unix("org.h5l.ntlm-service", ntlm_service, NULL, &un);277}278279heim_ipc_main();280return 0;281}282283284