Path: blob/main/crypto/heimdal/lib/ntlm/test_ntlm.c
34869 views
/*1* Copyright (c) 2006 - 2007 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 <stdio.h>36#include <err.h>37#include <roken.h>38#include <getarg.h>3940#include <krb5-types.h> /* or <inttypes.h> */41#include <heimntlm.h>4243static int44test_parse(void)45{46const char *user = "foo",47*domain = "mydomain",48*password = "digestpassword",49*target = "DOMAIN";50struct ntlm_type1 type1;51struct ntlm_type2 type2;52struct ntlm_type3 type3;53struct ntlm_buf data;54int ret, flags;5556memset(&type1, 0, sizeof(type1));5758type1.flags = NTLM_NEG_UNICODE|NTLM_NEG_TARGET|NTLM_NEG_NTLM;59type1.domain = rk_UNCONST(domain);60type1.hostname = NULL;61type1.os[0] = 0;62type1.os[1] = 0;6364ret = heim_ntlm_encode_type1(&type1, &data);65if (ret)66errx(1, "heim_ntlm_encode_type1");6768memset(&type1, 0, sizeof(type1));6970ret = heim_ntlm_decode_type1(&data, &type1);71free(data.data);72if (ret)73errx(1, "heim_ntlm_encode_type1");7475heim_ntlm_free_type1(&type1);7677/*78*79*/8081memset(&type2, 0, sizeof(type2));8283flags = NTLM_NEG_UNICODE | NTLM_NEG_NTLM | NTLM_TARGET_DOMAIN;84type2.flags = flags;8586memset(type2.challenge, 0x7f, sizeof(type2.challenge));87type2.targetname = rk_UNCONST(target);88type2.targetinfo.data = NULL;89type2.targetinfo.length = 0;9091ret = heim_ntlm_encode_type2(&type2, &data);92if (ret)93errx(1, "heim_ntlm_encode_type2");9495memset(&type2, 0, sizeof(type2));9697ret = heim_ntlm_decode_type2(&data, &type2);98free(data.data);99if (ret)100errx(1, "heim_ntlm_decode_type2");101102heim_ntlm_free_type2(&type2);103104/*105*106*/107108memset(&type3, 0, sizeof(type3));109110type3.flags = flags;111type3.username = rk_UNCONST(user);112type3.targetname = rk_UNCONST(target);113type3.ws = rk_UNCONST("workstation");114115{116struct ntlm_buf key;117heim_ntlm_nt_key(password, &key);118119heim_ntlm_calculate_ntlm1(key.data, key.length,120type2.challenge,121&type3.ntlm);122free(key.data);123}124125ret = heim_ntlm_encode_type3(&type3, &data);126if (ret)127errx(1, "heim_ntlm_encode_type3");128129free(type3.ntlm.data);130131memset(&type3, 0, sizeof(type3));132133ret = heim_ntlm_decode_type3(&data, 1, &type3);134free(data.data);135if (ret)136errx(1, "heim_ntlm_decode_type3");137138if (strcmp("workstation", type3.ws) != 0)139errx(1, "type3 ws wrong");140141if (strcmp(target, type3.targetname) != 0)142errx(1, "type3 targetname wrong");143144if (strcmp(user, type3.username) != 0)145errx(1, "type3 username wrong");146147148heim_ntlm_free_type3(&type3);149150/*151* NTLMv2152*/153154memset(&type2, 0, sizeof(type2));155156flags = NTLM_NEG_UNICODE | NTLM_NEG_NTLM | NTLM_TARGET_DOMAIN;157type2.flags = flags;158159memset(type2.challenge, 0x7f, sizeof(type2.challenge));160type2.targetname = rk_UNCONST(target);161type2.targetinfo.data = "\x00\x00";162type2.targetinfo.length = 2;163164ret = heim_ntlm_encode_type2(&type2, &data);165if (ret)166errx(1, "heim_ntlm_encode_type2");167168memset(&type2, 0, sizeof(type2));169170ret = heim_ntlm_decode_type2(&data, &type2);171free(data.data);172if (ret)173errx(1, "heim_ntlm_decode_type2");174175heim_ntlm_free_type2(&type2);176177return 0;178}179180static int181test_keys(void)182{183const char184*username = "test",185*password = "test1234",186*target = "TESTNT";187const unsigned char188serverchallenge[8] = "\x67\x7f\x1c\x55\x7a\x5e\xe9\x6c";189struct ntlm_buf infotarget, infotarget2, answer, key;190unsigned char ntlmv2[16], ntlmv2_1[16];191int ret;192193infotarget.length = 70;194infotarget.data =195"\x02\x00\x0c\x00\x54\x00\x45\x00\x53\x00\x54\x00\x4e\x00\x54\x00"196"\x01\x00\x0c\x00\x4d\x00\x45\x00\x4d\x00\x42\x00\x45\x00\x52\x00"197"\x03\x00\x1e\x00\x6d\x00\x65\x00\x6d\x00\x62\x00\x65\x00\x72\x00"198"\x2e\x00\x74\x00\x65\x00\x73\x00\x74\x00\x2e\x00\x63\x00\x6f"199"\x00\x6d\x00"200"\x00\x00\x00\x00";201202answer.length = 0;203answer.data = NULL;204205heim_ntlm_nt_key(password, &key);206207ret = heim_ntlm_calculate_ntlm2(key.data,208key.length,209username,210target,211serverchallenge,212&infotarget,213ntlmv2,214&answer);215if (ret)216errx(1, "heim_ntlm_calculate_ntlm2");217218ret = heim_ntlm_verify_ntlm2(key.data,219key.length,220username,221target,2220,223serverchallenge,224&answer,225&infotarget2,226ntlmv2_1);227if (ret)228errx(1, "heim_ntlm_verify_ntlm2");229230if (memcmp(ntlmv2, ntlmv2_1, sizeof(ntlmv2)) != 0)231errx(1, "ntlm master key not same");232233if (infotarget.length > infotarget2.length)234errx(1, "infotarget length");235236if (memcmp(infotarget.data, infotarget2.data, infotarget.length) != 0)237errx(1, "infotarget not the same");238239free(key.data);240free(answer.data);241free(infotarget2.data);242243return 0;244}245246static int247test_ntlm2_session_resp(void)248{249int ret;250struct ntlm_buf lm, ntlm;251252const unsigned char lm_resp[24] =253"\xff\xff\xff\x00\x11\x22\x33\x44"254"\x00\x00\x00\x00\x00\x00\x00\x00"255"\x00\x00\x00\x00\x00\x00\x00\x00";256const unsigned char ntlm2_sess_resp[24] =257"\x10\xd5\x50\x83\x2d\x12\xb2\xcc"258"\xb7\x9d\x5a\xd1\xf4\xee\xd3\xdf"259"\x82\xac\xa4\xc3\x68\x1d\xd4\x55";260261const unsigned char client_nonce[8] =262"\xff\xff\xff\x00\x11\x22\x33\x44";263const unsigned char server_challenge[8] =264"\x01\x23\x45\x67\x89\xab\xcd\xef";265266const unsigned char ntlm_hash[16] =267"\xcd\x06\xca\x7c\x7e\x10\xc9\x9b"268"\x1d\x33\xb7\x48\x5a\x2e\xd8\x08";269270ret = heim_ntlm_calculate_ntlm2_sess(client_nonce,271server_challenge,272ntlm_hash,273&lm,274&ntlm);275if (ret)276errx(1, "heim_ntlm_calculate_ntlm2_sess_resp");277278if (lm.length != 24 || memcmp(lm.data, lm_resp, 24) != 0)279errx(1, "lm_resp wrong");280if (ntlm.length != 24 || memcmp(ntlm.data, ntlm2_sess_resp, 24) != 0)281errx(1, "ntlm2_sess_resp wrong");282283free(lm.data);284free(ntlm.data);285286287return 0;288}289290static int291test_targetinfo(void)292{293struct ntlm_targetinfo ti;294struct ntlm_buf buf;295const char *dnsservername = "dnsservername";296int ret;297298memset(&ti, 0, sizeof(ti));299300ti.dnsservername = rk_UNCONST(dnsservername);301ti.avflags = 1;302ret = heim_ntlm_encode_targetinfo(&ti, 1, &buf);303if (ret)304return ret;305306memset(&ti, 0, sizeof(ti));307308ret = heim_ntlm_decode_targetinfo(&buf, 1, &ti);309if (ret)310return ret;311312if (ti.dnsservername == NULL ||313strcmp(ti.dnsservername, dnsservername) != 0)314errx(1, "ti.dnshostname != %s", dnsservername);315if (ti.avflags != 1)316errx(1, "ti.avflags != 1");317318heim_ntlm_free_targetinfo(&ti);319320return 0;321}322323static int verbose_flag = 0;324static int version_flag = 0;325static int help_flag = 0;326327static struct getargs args[] = {328{"verbose", 0, arg_flag, &verbose_flag, "verbose printing", NULL },329{"version", 0, arg_flag, &version_flag, "print version", NULL },330{"help", 0, arg_flag, &help_flag, NULL, NULL }331};332333static void334usage (int ret)335{336arg_printusage (args, sizeof(args)/sizeof(*args),337NULL, "");338exit (ret);339}340341int342main(int argc, char **argv)343{344int ret = 0, optind = 0;345346setprogname(argv[0]);347348if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optind))349usage(1);350351if (help_flag)352usage (0);353354if(version_flag){355print_version(NULL);356exit(0);357}358359argc -= optind;360argv += optind;361362if (verbose_flag)363printf("test_parse\n");364365ret += test_parse();366if (verbose_flag)367printf("test_keys\n");368369ret += test_keys();370if (verbose_flag)371printf("test_ntlm2_session_resp\n");372ret += test_ntlm2_session_resp();373374if (verbose_flag)375printf("test_targetinfo\n");376ret += test_targetinfo();377378return ret;379}380381382