Path: blob/main/crypto/heimdal/lib/gssapi/test_ntlm.c
34889 views
/*1* Copyright (c) 2006 - 2008 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of KTH nor the names of its contributors may be17* used to endorse or promote products derived from this software without18* specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR23* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR27* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,28* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR29* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF30* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233#include "config.h"3435#include <roken.h>36#include <stdio.h>37#include <gssapi.h>38#include <err.h>39#include <getarg.h>40#include "test_common.h"4142#include <krb5.h>43#include <heimntlm.h>4445static int46test_libntlm_v1(int flags)47{48const char *user = "foo",49*domain = "mydomain",50*password = "digestpassword";51OM_uint32 maj_stat, min_stat;52gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;53gss_buffer_desc input, output;54struct ntlm_type1 type1;55struct ntlm_type2 type2;56struct ntlm_type3 type3;57struct ntlm_buf data;58krb5_error_code ret;59gss_name_t src_name = GSS_C_NO_NAME;6061memset(&type1, 0, sizeof(type1));62memset(&type2, 0, sizeof(type2));63memset(&type3, 0, sizeof(type3));6465type1.flags = NTLM_NEG_UNICODE|NTLM_NEG_TARGET|NTLM_NEG_NTLM|flags;66type1.domain = strdup(domain);67type1.hostname = NULL;68type1.os[0] = 0;69type1.os[1] = 0;7071ret = heim_ntlm_encode_type1(&type1, &data);72if (ret)73errx(1, "heim_ntlm_encode_type1");7475input.value = data.data;76input.length = data.length;7778output.length = 0;79output.value = NULL;8081maj_stat = gss_accept_sec_context(&min_stat,82&ctx,83GSS_C_NO_CREDENTIAL,84&input,85GSS_C_NO_CHANNEL_BINDINGS,86NULL,87NULL,88&output,89NULL,90NULL,91NULL);92free(data.data);93if (GSS_ERROR(maj_stat))94errx(1, "accept_sec_context v1: %s",95gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));9697if (output.length == 0)98errx(1, "output.length == 0");99100data.data = output.value;101data.length = output.length;102103ret = heim_ntlm_decode_type2(&data, &type2);104if (ret)105errx(1, "heim_ntlm_decode_type2");106107gss_release_buffer(&min_stat, &output);108109type3.flags = type2.flags;110type3.username = rk_UNCONST(user);111type3.targetname = type2.targetname;112type3.ws = rk_UNCONST("workstation");113114{115struct ntlm_buf key;116117heim_ntlm_nt_key(password, &key);118119heim_ntlm_calculate_ntlm1(key.data, key.length,120type2.challenge,121&type3.ntlm);122123if (flags & NTLM_NEG_KEYEX) {124struct ntlm_buf sessionkey;125heim_ntlm_build_ntlm1_master(key.data, key.length,126&sessionkey,127&type3.sessionkey);128free(sessionkey.data);129}130free(key.data);131}132133ret = heim_ntlm_encode_type3(&type3, &data);134if (ret)135errx(1, "heim_ntlm_encode_type3");136137input.length = data.length;138input.value = data.data;139140maj_stat = gss_accept_sec_context(&min_stat,141&ctx,142GSS_C_NO_CREDENTIAL,143&input,144GSS_C_NO_CHANNEL_BINDINGS,145&src_name,146NULL,147&output,148NULL,149NULL,150NULL);151free(input.value);152if (maj_stat != GSS_S_COMPLETE)153errx(1, "accept_sec_context v1 2 %s",154gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));155156gss_release_buffer(&min_stat, &output);157gss_delete_sec_context(&min_stat, &ctx, NULL);158159if (src_name == GSS_C_NO_NAME)160errx(1, "no source name!");161162gss_display_name(&min_stat, src_name, &output, NULL);163164printf("src_name: %.*s\n", (int)output.length, (char*)output.value);165166gss_release_name(&min_stat, &src_name);167gss_release_buffer(&min_stat, &output);168169return 0;170}171172static int173test_libntlm_v2(int flags)174{175const char *user = "foo",176*domain = "mydomain",177*password = "digestpassword";178OM_uint32 maj_stat, min_stat;179gss_ctx_id_t ctx = GSS_C_NO_CONTEXT;180gss_buffer_desc input, output;181struct ntlm_type1 type1;182struct ntlm_type2 type2;183struct ntlm_type3 type3;184struct ntlm_buf data;185krb5_error_code ret;186187memset(&type1, 0, sizeof(type1));188memset(&type2, 0, sizeof(type2));189memset(&type3, 0, sizeof(type3));190191type1.flags = NTLM_NEG_UNICODE|NTLM_NEG_NTLM|flags;192type1.domain = strdup(domain);193type1.hostname = NULL;194type1.os[0] = 0;195type1.os[1] = 0;196197ret = heim_ntlm_encode_type1(&type1, &data);198if (ret)199errx(1, "heim_ntlm_encode_type1");200201input.value = data.data;202input.length = data.length;203204output.length = 0;205output.value = NULL;206207maj_stat = gss_accept_sec_context(&min_stat,208&ctx,209GSS_C_NO_CREDENTIAL,210&input,211GSS_C_NO_CHANNEL_BINDINGS,212NULL,213NULL,214&output,215NULL,216NULL,217NULL);218free(data.data);219if (GSS_ERROR(maj_stat))220errx(1, "accept_sec_context v2 %s",221gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));222223if (output.length == 0)224errx(1, "output.length == 0");225226data.data = output.value;227data.length = output.length;228229ret = heim_ntlm_decode_type2(&data, &type2);230if (ret)231errx(1, "heim_ntlm_decode_type2");232233type3.flags = type2.flags;234type3.username = rk_UNCONST(user);235type3.targetname = type2.targetname;236type3.ws = rk_UNCONST("workstation");237238{239struct ntlm_buf key;240unsigned char ntlmv2[16];241242heim_ntlm_nt_key(password, &key);243244heim_ntlm_calculate_ntlm2(key.data, key.length,245user,246type2.targetname,247type2.challenge,248&type2.targetinfo,249ntlmv2,250&type3.ntlm);251free(key.data);252253if (flags & NTLM_NEG_KEYEX) {254struct ntlm_buf sessionkey;255heim_ntlm_build_ntlm1_master(ntlmv2, sizeof(ntlmv2),256&sessionkey,257&type3.sessionkey);258free(sessionkey.data);259}260}261262ret = heim_ntlm_encode_type3(&type3, &data);263if (ret)264errx(1, "heim_ntlm_encode_type3");265266input.length = data.length;267input.value = data.data;268269maj_stat = gss_accept_sec_context(&min_stat,270&ctx,271GSS_C_NO_CREDENTIAL,272&input,273GSS_C_NO_CHANNEL_BINDINGS,274NULL,275NULL,276&output,277NULL,278NULL,279NULL);280free(input.value);281if (maj_stat != GSS_S_COMPLETE)282errx(1, "accept_sec_context v2 2 %s",283gssapi_err(maj_stat, min_stat, GSS_C_NO_OID));284285gss_delete_sec_context(&min_stat, &ctx, NULL);286287return 0;288}289290291292static int version_flag = 0;293static int help_flag = 0;294295static struct getargs args[] = {296{"version", 0, arg_flag, &version_flag, "print version", NULL },297{"help", 0, arg_flag, &help_flag, NULL, NULL }298};299300static void301usage (int ret)302{303arg_printusage (args, sizeof(args)/sizeof(*args),304NULL, "");305exit (ret);306}307308int309main(int argc, char **argv)310{311int ret = 0, optind = 0;312313setprogname(argv[0]);314315if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))316usage(1);317318if (help_flag)319usage (0);320321if(version_flag){322print_version(NULL);323exit(0);324}325326argc -= optind;327argv += optind;328329ret += test_libntlm_v1(0);330ret += test_libntlm_v1(NTLM_NEG_KEYEX);331332ret += test_libntlm_v2(0);333ret += test_libntlm_v2(NTLM_NEG_KEYEX);334335return 0;336}337338339