Home | History | Annotate | Line # | Download | only in ntlm
      1  1.1     elric /*	$NetBSD: test_ntlm.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 2006 - 2007 Kungliga Tekniska Hgskolan
      5  1.1     elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1     elric  * All rights reserved.
      7  1.1     elric  *
      8  1.1     elric  * Redistribution and use in source and binary forms, with or without
      9  1.1     elric  * modification, are permitted provided that the following conditions
     10  1.1     elric  * are met:
     11  1.1     elric  *
     12  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     13  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     14  1.1     elric  *
     15  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     elric  *    documentation and/or other materials provided with the distribution.
     18  1.1     elric  *
     19  1.1     elric  * 3. Neither the name of KTH nor the names of its contributors may be
     20  1.1     elric  *    used to endorse or promote products derived from this software without
     21  1.1     elric  *    specific prior written permission.
     22  1.1     elric  *
     23  1.1     elric  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
     24  1.1     elric  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.1     elric  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
     27  1.1     elric  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.1     elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.1     elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     30  1.1     elric  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     31  1.1     elric  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     32  1.1     elric  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     33  1.1     elric  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  1.1     elric  */
     35  1.1     elric 
     36  1.1     elric #include "config.h"
     37  1.1     elric 
     38  1.1     elric #include <stdio.h>
     39  1.1     elric #include <err.h>
     40  1.1     elric #include <krb5/roken.h>
     41  1.1     elric #include <krb5/getarg.h>
     42  1.1     elric 
     43  1.1     elric #include <krb5/krb5-types.h> /* or <inttypes.h> */
     44  1.1     elric #include <krb5/heimntlm.h>
     45  1.1     elric 
     46  1.2  christos static int dumpdata_flag;
     47  1.2  christos 
     48  1.1     elric static int
     49  1.1     elric test_parse(void)
     50  1.1     elric {
     51  1.1     elric     const char *user = "foo",
     52  1.1     elric 	*domain = "mydomain",
     53  1.2  christos 	*hostname = "myhostname",
     54  1.1     elric 	*password = "digestpassword",
     55  1.1     elric 	*target = "DOMAIN";
     56  1.1     elric     struct ntlm_type1 type1;
     57  1.1     elric     struct ntlm_type2 type2;
     58  1.1     elric     struct ntlm_type3 type3;
     59  1.1     elric     struct ntlm_buf data;
     60  1.1     elric     int ret, flags;
     61  1.1     elric 
     62  1.1     elric     memset(&type1, 0, sizeof(type1));
     63  1.1     elric 
     64  1.2  christos     type1.flags = NTLM_NEG_UNICODE|NTLM_NEG_TARGET|NTLM_NEG_NTLM|NTLM_NEG_VERSION;
     65  1.1     elric     type1.domain = rk_UNCONST(domain);
     66  1.2  christos     type1.hostname = rk_UNCONST(hostname);
     67  1.1     elric     type1.os[0] = 0;
     68  1.1     elric     type1.os[1] = 0;
     69  1.1     elric 
     70  1.1     elric     ret = heim_ntlm_encode_type1(&type1, &data);
     71  1.1     elric     if (ret)
     72  1.1     elric 	errx(1, "heim_ntlm_encode_type1");
     73  1.1     elric 
     74  1.1     elric     memset(&type1, 0, sizeof(type1));
     75  1.1     elric 
     76  1.2  christos     if (dumpdata_flag)
     77  1.2  christos 	rk_dumpdata("ntlm-type1", data.data, data.length);
     78  1.2  christos 
     79  1.1     elric     ret = heim_ntlm_decode_type1(&data, &type1);
     80  1.1     elric     free(data.data);
     81  1.1     elric     if (ret)
     82  1.1     elric 	errx(1, "heim_ntlm_encode_type1");
     83  1.1     elric 
     84  1.2  christos     if (strcmp(type1.domain, domain) != 0)
     85  1.2  christos 	errx(1, "parser got domain wrong: %s", type1.domain);
     86  1.2  christos 
     87  1.2  christos     if (strcmp(type1.hostname, hostname) != 0)
     88  1.2  christos 	errx(1, "parser got hostname wrong: %s", type1.hostname);
     89  1.2  christos 
     90  1.1     elric     heim_ntlm_free_type1(&type1);
     91  1.1     elric 
     92  1.1     elric     /*
     93  1.1     elric      *
     94  1.1     elric      */
     95  1.1     elric 
     96  1.1     elric     memset(&type2, 0, sizeof(type2));
     97  1.1     elric 
     98  1.1     elric     flags = NTLM_NEG_UNICODE | NTLM_NEG_NTLM | NTLM_TARGET_DOMAIN;
     99  1.1     elric     type2.flags = flags;
    100  1.1     elric 
    101  1.1     elric     memset(type2.challenge, 0x7f, sizeof(type2.challenge));
    102  1.1     elric     type2.targetname = rk_UNCONST(target);
    103  1.1     elric     type2.targetinfo.data = NULL;
    104  1.1     elric     type2.targetinfo.length = 0;
    105  1.1     elric 
    106  1.1     elric     ret = heim_ntlm_encode_type2(&type2, &data);
    107  1.1     elric     if (ret)
    108  1.1     elric 	errx(1, "heim_ntlm_encode_type2");
    109  1.1     elric 
    110  1.1     elric     memset(&type2, 0, sizeof(type2));
    111  1.1     elric 
    112  1.2  christos     if (dumpdata_flag)
    113  1.2  christos 	rk_dumpdata("ntlm-type2", data.data, data.length);
    114  1.2  christos 
    115  1.1     elric     ret = heim_ntlm_decode_type2(&data, &type2);
    116  1.1     elric     free(data.data);
    117  1.1     elric     if (ret)
    118  1.1     elric 	errx(1, "heim_ntlm_decode_type2");
    119  1.1     elric 
    120  1.1     elric     heim_ntlm_free_type2(&type2);
    121  1.1     elric 
    122  1.1     elric     /*
    123  1.1     elric      *
    124  1.1     elric      */
    125  1.1     elric 
    126  1.1     elric     memset(&type3, 0, sizeof(type3));
    127  1.1     elric 
    128  1.1     elric     type3.flags = flags;
    129  1.1     elric     type3.username = rk_UNCONST(user);
    130  1.1     elric     type3.targetname = rk_UNCONST(target);
    131  1.1     elric     type3.ws = rk_UNCONST("workstation");
    132  1.1     elric 
    133  1.1     elric     {
    134  1.1     elric 	struct ntlm_buf key;
    135  1.1     elric 	heim_ntlm_nt_key(password, &key);
    136  1.1     elric 
    137  1.1     elric 	heim_ntlm_calculate_ntlm1(key.data, key.length,
    138  1.1     elric 				  type2.challenge,
    139  1.1     elric 				  &type3.ntlm);
    140  1.1     elric 	free(key.data);
    141  1.1     elric     }
    142  1.1     elric 
    143  1.2  christos     ret = heim_ntlm_encode_type3(&type3, &data, NULL);
    144  1.1     elric     if (ret)
    145  1.1     elric 	errx(1, "heim_ntlm_encode_type3");
    146  1.1     elric 
    147  1.1     elric     free(type3.ntlm.data);
    148  1.1     elric 
    149  1.1     elric     memset(&type3, 0, sizeof(type3));
    150  1.1     elric 
    151  1.2  christos     if (dumpdata_flag)
    152  1.2  christos 	rk_dumpdata("ntlm-type3", data.data, data.length);
    153  1.2  christos 
    154  1.1     elric     ret = heim_ntlm_decode_type3(&data, 1, &type3);
    155  1.1     elric     free(data.data);
    156  1.1     elric     if (ret)
    157  1.1     elric 	errx(1, "heim_ntlm_decode_type3");
    158  1.1     elric 
    159  1.1     elric     if (strcmp("workstation", type3.ws) != 0)
    160  1.1     elric 	errx(1, "type3 ws wrong");
    161  1.1     elric 
    162  1.1     elric     if (strcmp(target, type3.targetname) != 0)
    163  1.1     elric 	errx(1, "type3 targetname wrong");
    164  1.1     elric 
    165  1.1     elric     if (strcmp(user, type3.username) != 0)
    166  1.1     elric 	errx(1, "type3 username wrong");
    167  1.1     elric 
    168  1.1     elric 
    169  1.1     elric     heim_ntlm_free_type3(&type3);
    170  1.1     elric 
    171  1.1     elric     /*
    172  1.1     elric      * NTLMv2
    173  1.1     elric      */
    174  1.1     elric 
    175  1.1     elric     memset(&type2, 0, sizeof(type2));
    176  1.1     elric 
    177  1.1     elric     flags = NTLM_NEG_UNICODE | NTLM_NEG_NTLM | NTLM_TARGET_DOMAIN;
    178  1.1     elric     type2.flags = flags;
    179  1.1     elric 
    180  1.1     elric     memset(type2.challenge, 0x7f, sizeof(type2.challenge));
    181  1.1     elric     type2.targetname = rk_UNCONST(target);
    182  1.1     elric     type2.targetinfo.data = "\x00\x00";
    183  1.1     elric     type2.targetinfo.length = 2;
    184  1.1     elric 
    185  1.1     elric     ret = heim_ntlm_encode_type2(&type2, &data);
    186  1.1     elric     if (ret)
    187  1.1     elric 	errx(1, "heim_ntlm_encode_type2");
    188  1.1     elric 
    189  1.1     elric     memset(&type2, 0, sizeof(type2));
    190  1.1     elric 
    191  1.1     elric     ret = heim_ntlm_decode_type2(&data, &type2);
    192  1.1     elric     free(data.data);
    193  1.1     elric     if (ret)
    194  1.1     elric 	errx(1, "heim_ntlm_decode_type2");
    195  1.1     elric 
    196  1.1     elric     heim_ntlm_free_type2(&type2);
    197  1.1     elric 
    198  1.1     elric     return 0;
    199  1.1     elric }
    200  1.1     elric 
    201  1.1     elric static int
    202  1.1     elric test_keys(void)
    203  1.1     elric {
    204  1.1     elric     const char
    205  1.1     elric 	*username = "test",
    206  1.1     elric 	*password = "test1234",
    207  1.1     elric 	*target = "TESTNT";
    208  1.1     elric     const unsigned char
    209  1.1     elric 	serverchallenge[8] = "\x67\x7f\x1c\x55\x7a\x5e\xe9\x6c";
    210  1.1     elric     struct ntlm_buf infotarget, infotarget2, answer, key;
    211  1.1     elric     unsigned char ntlmv2[16], ntlmv2_1[16];
    212  1.1     elric     int ret;
    213  1.1     elric 
    214  1.1     elric     infotarget.length = 70;
    215  1.1     elric     infotarget.data =
    216  1.1     elric 	"\x02\x00\x0c\x00\x54\x00\x45\x00\x53\x00\x54\x00\x4e\x00\x54\x00"
    217  1.1     elric 	"\x01\x00\x0c\x00\x4d\x00\x45\x00\x4d\x00\x42\x00\x45\x00\x52\x00"
    218  1.1     elric 	"\x03\x00\x1e\x00\x6d\x00\x65\x00\x6d\x00\x62\x00\x65\x00\x72\x00"
    219  1.1     elric 	    "\x2e\x00\x74\x00\x65\x00\x73\x00\x74\x00\x2e\x00\x63\x00\x6f"
    220  1.1     elric 	    "\x00\x6d\x00"
    221  1.1     elric 	"\x00\x00\x00\x00";
    222  1.1     elric 
    223  1.1     elric     answer.length = 0;
    224  1.1     elric     answer.data = NULL;
    225  1.1     elric 
    226  1.1     elric     heim_ntlm_nt_key(password, &key);
    227  1.1     elric 
    228  1.1     elric     ret = heim_ntlm_calculate_ntlm2(key.data,
    229  1.1     elric 				    key.length,
    230  1.1     elric 				    username,
    231  1.1     elric 				    target,
    232  1.1     elric 				    serverchallenge,
    233  1.1     elric 				    &infotarget,
    234  1.1     elric 				    ntlmv2,
    235  1.1     elric 				    &answer);
    236  1.1     elric     if (ret)
    237  1.1     elric 	errx(1, "heim_ntlm_calculate_ntlm2");
    238  1.1     elric 
    239  1.1     elric     ret = heim_ntlm_verify_ntlm2(key.data,
    240  1.1     elric 				 key.length,
    241  1.1     elric 				 username,
    242  1.1     elric 				 target,
    243  1.1     elric 				 0,
    244  1.1     elric 				 serverchallenge,
    245  1.1     elric 				 &answer,
    246  1.1     elric 				 &infotarget2,
    247  1.1     elric 				 ntlmv2_1);
    248  1.1     elric     if (ret)
    249  1.1     elric 	errx(1, "heim_ntlm_verify_ntlm2");
    250  1.1     elric 
    251  1.1     elric     if (memcmp(ntlmv2, ntlmv2_1, sizeof(ntlmv2)) != 0)
    252  1.1     elric 	errx(1, "ntlm master key not same");
    253  1.1     elric 
    254  1.1     elric     if (infotarget.length > infotarget2.length)
    255  1.1     elric 	errx(1, "infotarget length");
    256  1.1     elric 
    257  1.1     elric     if (memcmp(infotarget.data, infotarget2.data, infotarget.length) != 0)
    258  1.1     elric 	errx(1, "infotarget not the same");
    259  1.1     elric 
    260  1.1     elric     free(key.data);
    261  1.1     elric     free(answer.data);
    262  1.1     elric     free(infotarget2.data);
    263  1.1     elric 
    264  1.1     elric     return 0;
    265  1.1     elric }
    266  1.1     elric 
    267  1.1     elric static int
    268  1.1     elric test_ntlm2_session_resp(void)
    269  1.1     elric {
    270  1.1     elric     int ret;
    271  1.1     elric     struct ntlm_buf lm, ntlm;
    272  1.1     elric 
    273  1.1     elric     const unsigned char lm_resp[24] =
    274  1.1     elric 	"\xff\xff\xff\x00\x11\x22\x33\x44"
    275  1.1     elric 	"\x00\x00\x00\x00\x00\x00\x00\x00"
    276  1.1     elric 	"\x00\x00\x00\x00\x00\x00\x00\x00";
    277  1.1     elric     const unsigned char ntlm2_sess_resp[24] =
    278  1.1     elric 	"\x10\xd5\x50\x83\x2d\x12\xb2\xcc"
    279  1.1     elric 	"\xb7\x9d\x5a\xd1\xf4\xee\xd3\xdf"
    280  1.1     elric 	"\x82\xac\xa4\xc3\x68\x1d\xd4\x55";
    281  1.1     elric 
    282  1.1     elric     const unsigned char client_nonce[8] =
    283  1.1     elric 	"\xff\xff\xff\x00\x11\x22\x33\x44";
    284  1.1     elric     const unsigned char server_challenge[8] =
    285  1.1     elric 	"\x01\x23\x45\x67\x89\xab\xcd\xef";
    286  1.1     elric 
    287  1.1     elric     const unsigned char ntlm_hash[16] =
    288  1.1     elric 	"\xcd\x06\xca\x7c\x7e\x10\xc9\x9b"
    289  1.1     elric 	"\x1d\x33\xb7\x48\x5a\x2e\xd8\x08";
    290  1.1     elric 
    291  1.1     elric     ret = heim_ntlm_calculate_ntlm2_sess(client_nonce,
    292  1.1     elric 					 server_challenge,
    293  1.1     elric 					 ntlm_hash,
    294  1.1     elric 					 &lm,
    295  1.1     elric 					 &ntlm);
    296  1.1     elric     if (ret)
    297  1.1     elric 	errx(1, "heim_ntlm_calculate_ntlm2_sess_resp");
    298  1.1     elric 
    299  1.1     elric     if (lm.length != 24 || memcmp(lm.data, lm_resp, 24) != 0)
    300  1.1     elric 	errx(1, "lm_resp wrong");
    301  1.1     elric     if (ntlm.length != 24 || memcmp(ntlm.data, ntlm2_sess_resp, 24) != 0)
    302  1.1     elric 	errx(1, "ntlm2_sess_resp wrong");
    303  1.1     elric 
    304  1.1     elric     free(lm.data);
    305  1.1     elric     free(ntlm.data);
    306  1.1     elric 
    307  1.1     elric 
    308  1.1     elric     return 0;
    309  1.1     elric }
    310  1.1     elric 
    311  1.1     elric static int
    312  1.2  christos test_ntlmv2(void)
    313  1.2  christos {
    314  1.2  christos     unsigned char type3[413] =
    315  1.2  christos 	"\x4e\x54\x4c\x4d\x53\x53\x50\x00\x03\x00\x00\x00\x18\x00\x18\x00"
    316  1.2  christos 	"\x80\x00\x00\x00\x9e\x00\x9e\x00\x98\x00\x00\x00\x14\x00\x14\x00"
    317  1.2  christos 	"\x48\x00\x00\x00\x10\x00\x10\x00\x5c\x00\x00\x00\x14\x00\x14\x00"
    318  1.2  christos 	"\x6c\x00\x00\x00\x00\x00\x00\x00\x36\x01\x00\x00\x05\x82\x88\xa2"
    319  1.2  christos 	"\x05\x01\x28\x0a\x00\x00\x00\x0f\x43\x00\x4f\x00\x4c\x00\x4c\x00"
    320  1.2  christos 	"\x45\x00\x59\x00\x2d\x00\x58\x00\x50\x00\x34\x00\x54\x00\x45\x00"
    321  1.2  christos 	"\x53\x00\x54\x00\x55\x00\x53\x00\x45\x00\x52\x00\x43\x00\x4f\x00"
    322  1.2  christos 	"\x4c\x00\x4c\x00\x45\x00\x59\x00\x2d\x00\x58\x00\x50\x00\x34\x00"
    323  1.2  christos 	"\x2f\x96\xec\x0a\xf7\x9f\x2e\x24\xba\x09\x48\x10\xa5\x22\xd4\xe1"
    324  1.2  christos 	"\x16\x6a\xca\x58\x74\x9a\xc1\x4f\x54\x6f\xee\x40\x96\xce\x43\x6e"
    325  1.2  christos 	"\xdf\x99\x20\x71\x6c\x9a\xda\x2a\x01\x01\x00\x00\x00\x00\x00\x00"
    326  1.2  christos 	"\x8d\xc0\x57\xc9\x79\x5e\xcb\x01\x16\x6a\xca\x58\x74\x9a\xc1\x4f"
    327  1.2  christos 	"\x00\x00\x00\x00\x02\x00\x14\x00\x4e\x00\x55\x00\x54\x00\x43\x00"
    328  1.2  christos 	"\x52\x00\x41\x00\x43\x00\x4b\x00\x45\x00\x52\x00\x01\x00\x14\x00"
    329  1.2  christos 	"\x4e\x00\x55\x00\x54\x00\x43\x00\x52\x00\x41\x00\x43\x00\x4b\x00"
    330  1.2  christos 	"\x45\x00\x52\x00\x04\x00\x12\x00\x61\x00\x70\x00\x70\x00\x6c\x00"
    331  1.2  christos 	"\x65\x00\x2e\x00\x63\x00\x6f\x00\x6d\x00\x03\x00\x20\x00\x68\x00"
    332  1.2  christos 	"\x75\x00\x6d\x00\x6d\x00\x65\x00\x6c\x00\x2e\x00\x61\x00\x70\x00"
    333  1.2  christos 	"\x70\x00\x6c\x00\x65\x00\x2e\x00\x63\x00\x6f\x00\x6d\x00\x00\x00"
    334  1.2  christos 	"\x00\x00\x00\x00\x00\x00\x00\x57\x00\x69\x00\x6e\x00\x64\x00\x6f"
    335  1.2  christos 	"\x00\x77\x00\x73\x00\x20\x00\x32\x00\x30\x00\x30\x00\x32\x00\x20"
    336  1.2  christos 	"\x00\x53\x00\x65\x00\x72\x00\x76\x00\x69\x00\x63\x00\x65\x00\x20"
    337  1.2  christos 	"\x00\x50\x00\x61\x00\x63\x00\x6b\x00\x20\x00\x33\x00\x20\x00\x32"
    338  1.2  christos 	"\x00\x36\x00\x30\x00\x30\x00\x00\x00\x57\x00\x69\x00\x6e\x00\x64"
    339  1.2  christos 	"\x00\x6f\x00\x77\x00\x73\x00\x20\x00\x32\x00\x30\x00\x30\x00\x32"
    340  1.2  christos 	"\x00\x20\x00\x35\x00\x2e\x00\x31\x00\x00\x00\x00\x00";
    341  1.2  christos     const unsigned char challenge[8] =
    342  1.2  christos 	"\xe4\x9c\x6a\x12\xe1\xbd\xde\x6a";
    343  1.2  christos     unsigned char sessionkey[16];
    344  1.2  christos 
    345  1.2  christos     const char key[16] = "\xD1\x83\x98\x3E\xAE\xA7\xBE\x99\x59\xC8\xF4\xC1\x98\xED\x0E\x68";
    346  1.2  christos 
    347  1.2  christos     struct ntlm_buf data;
    348  1.2  christos     struct ntlm_type3 t3;
    349  1.2  christos     int ret;
    350  1.2  christos 
    351  1.2  christos     struct ntlm_targetinfo ti;
    352  1.2  christos 
    353  1.2  christos     unsigned char timsg[114] =
    354  1.2  christos 	"\002\000\024\000N\000U\000T\000C\000R\000A\000C\000K\000E\000R\000\001\000\024\000N\000U\000T\000C\000R\000A\000C\000K\000E\000R\000\004\000\022\000a\000p\000p\000l\000e\000.\000c\000o\000m\000\003\000 \000h\000u\000m\000m\000e\000l\000.\000a\000p\000p\000l\000e\000.\000c\000o\000m\000\000\000\000\000\000\000\000";
    355  1.2  christos 
    356  1.2  christos 
    357  1.2  christos     data.data = type3;
    358  1.2  christos     data.length = sizeof(type3);
    359  1.2  christos 
    360  1.2  christos     ret = heim_ntlm_decode_type3(&data, 1, &t3);
    361  1.2  christos     if (ret)
    362  1.2  christos 	errx(1, "heim_ntlm_decode_type3");
    363  1.2  christos 
    364  1.2  christos     memset(&ti, 0, sizeof(ti));
    365  1.2  christos 
    366  1.2  christos     data.data = timsg;
    367  1.2  christos     data.length = sizeof(timsg);
    368  1.2  christos 
    369  1.2  christos     ret = heim_ntlm_decode_targetinfo(&data, 1, &ti);
    370  1.2  christos     if (ret)
    371  1.2  christos 	return ret;
    372  1.2  christos 
    373  1.2  christos     ret = heim_ntlm_verify_ntlm2(key, sizeof(key),
    374  1.2  christos 				 t3.username,
    375  1.2  christos 				 t3.targetname,
    376  1.2  christos 				 1285615547,
    377  1.2  christos 				 challenge,
    378  1.2  christos 				 &t3.ntlm,
    379  1.2  christos 				 &data,
    380  1.2  christos 				 sessionkey);
    381  1.2  christos     if (ret)
    382  1.2  christos 	errx(1, "verify_ntlmv2");
    383  1.2  christos 
    384  1.2  christos     if (sizeof(timsg) != data.length || memcmp(timsg, data.data, sizeof(timsg)) != 0)
    385  1.2  christos 	errx(1, "target info wrong: %d != %d",
    386  1.2  christos 	     (int)sizeof(timsg), (int)data.length);
    387  1.2  christos 
    388  1.2  christos     heim_ntlm_free_type3(&t3);
    389  1.2  christos     heim_ntlm_free_targetinfo(&ti);
    390  1.2  christos 
    391  1.2  christos     return 0;
    392  1.2  christos }
    393  1.2  christos 
    394  1.2  christos static int
    395  1.1     elric test_targetinfo(void)
    396  1.1     elric {
    397  1.1     elric     struct ntlm_targetinfo ti;
    398  1.1     elric     struct ntlm_buf buf;
    399  1.1     elric     const char *dnsservername = "dnsservername";
    400  1.2  christos     const char *targetname = "targetname";
    401  1.2  christos     const char z16[16] = { 0 };
    402  1.1     elric     int ret;
    403  1.1     elric 
    404  1.1     elric     memset(&ti, 0, sizeof(ti));
    405  1.1     elric 
    406  1.1     elric     ti.dnsservername = rk_UNCONST(dnsservername);
    407  1.1     elric     ti.avflags = 1;
    408  1.2  christos     ti.targetname = rk_UNCONST(targetname);
    409  1.2  christos     ti.channel_bindings.data = rk_UNCONST(z16);
    410  1.2  christos     ti.channel_bindings.length = sizeof(z16);
    411  1.2  christos 
    412  1.1     elric     ret = heim_ntlm_encode_targetinfo(&ti, 1, &buf);
    413  1.1     elric     if (ret)
    414  1.1     elric 	return ret;
    415  1.1     elric 
    416  1.1     elric     memset(&ti, 0, sizeof(ti));
    417  1.1     elric 
    418  1.1     elric     ret = heim_ntlm_decode_targetinfo(&buf, 1, &ti);
    419  1.1     elric     if (ret)
    420  1.1     elric 	return ret;
    421  1.1     elric 
    422  1.1     elric     if (ti.dnsservername == NULL ||
    423  1.1     elric 	strcmp(ti.dnsservername, dnsservername) != 0)
    424  1.1     elric 	errx(1, "ti.dnshostname != %s", dnsservername);
    425  1.1     elric     if (ti.avflags != 1)
    426  1.1     elric 	errx(1, "ti.avflags != 1");
    427  1.2  christos     if (ti.targetname == NULL ||
    428  1.2  christos 	strcmp(ti.targetname, targetname) != 0)
    429  1.2  christos 	errx(1, "ti.targetname != %s", targetname);
    430  1.2  christos 
    431  1.2  christos     if (ti.channel_bindings.length != sizeof(z16) ||
    432  1.2  christos 	memcmp(ti.channel_bindings.data, z16, sizeof(z16)) != 0)
    433  1.2  christos 	errx(1, "ti.channel_bindings != Z(16)");
    434  1.1     elric 
    435  1.1     elric     heim_ntlm_free_targetinfo(&ti);
    436  1.1     elric 
    437  1.1     elric     return 0;
    438  1.1     elric }
    439  1.1     elric 
    440  1.2  christos static int
    441  1.2  christos test_string2key(void)
    442  1.2  christos {
    443  1.2  christos     const char *pw = "";
    444  1.2  christos     struct ntlm_buf buf;
    445  1.2  christos 
    446  1.2  christos     unsigned char key[16] = {
    447  1.2  christos 	0xc6, 0x5d, 0xc7, 0x61, 0xa1, 0x34, 0x17, 0xa1,
    448  1.2  christos 	0x17, 0x08, 0x9c, 0x1b, 0xb0, 0x0d, 0x0f, 0x19
    449  1.2  christos     };
    450  1.2  christos 
    451  1.2  christos     if (heim_ntlm_nt_key(pw, &buf) != 0)
    452  1.2  christos 	errx(1, "heim_ntlmv_nt_key(jp)");
    453  1.2  christos 
    454  1.2  christos     if (buf.length != 16 || memcmp(buf.data, key, 16) != 0)
    455  1.2  christos 	errx(1, "compare failed");
    456  1.2  christos 
    457  1.2  christos     heim_ntlm_free_buf(&buf);
    458  1.2  christos 
    459  1.2  christos     return 0;
    460  1.2  christos }
    461  1.2  christos 
    462  1.2  christos static int
    463  1.2  christos test_jp(void)
    464  1.2  christos {
    465  1.2  christos     char buf2[220] =
    466  1.2  christos 	"\x4e\x54\x4c\x4d\x53\x53\x50\x00\x02\x00\x00\x00\x06\x00\x06\x00"
    467  1.2  christos 	"\x38\x00\x00\x00\x05\x02\x89\x62\x62\x94\xb1\xf3\x56\x80\xb0\xf9"
    468  1.2  christos 	"\x00\x00\x00\x00\x00\x00\x00\x00\x9e\x00\x9e\x00\x3e\x00\x00\x00"
    469  1.2  christos 	"\x06\x01\xb0\x1d\x00\x00\x00\x0f\x43\x00\x4f\x00\x53\x00\x02\x00"
    470  1.2  christos 	"\x06\x00\x43\x00\x4f\x00\x53\x00\x01\x00\x12\x00\x43\x00\x4f\x00"
    471  1.2  christos 	"\x53\x00\x57\x00\x49\x00\x4e\x00\x37\x00\x4a\x00\x50\x00\x04\x00"
    472  1.2  christos 	"\x1a\x00\x63\x00\x6f\x00\x73\x00\x2e\x00\x61\x00\x70\x00\x70\x00"
    473  1.2  christos 	"\x6c\x00\x65\x00\x2e\x00\x63\x00\x6f\x00\x6d\x00\x03\x00\x2e\x00"
    474  1.2  christos 	"\x63\x00\x6f\x00\x73\x00\x77\x00\x69\x00\x6e\x00\x37\x00\x6a\x00"
    475  1.2  christos 	"\x70\x00\x2e\x00\x63\x00\x6f\x00\x73\x00\x2e\x00\x61\x00\x70\x00"
    476  1.2  christos 	"\x70\x00\x6c\x00\x65\x00\x2e\x00\x63\x00\x6f\x00\x6d\x00\x05\x00"
    477  1.2  christos 	"\x1a\x00\x63\x00\x6f\x00\x73\x00\x2e\x00\x61\x00\x70\x00\x70\x00"
    478  1.2  christos 	"\x6c\x00\x65\x00\x2e\x00\x63\x00\x6f\x00\x6d\x00\x07\x00\x08\x00"
    479  1.2  christos 	"\x94\x51\xf0\xbd\xdc\x61\xcb\x01\x00\x00\x00\x00";
    480  1.2  christos 
    481  1.2  christos     char buf3[362] =
    482  1.2  christos 	"\x4e\x54\x4c\x4d\x53\x53\x50\x00\x03\x00\x00\x00\x18\x00\x18\x00"
    483  1.2  christos 	"\x74\x00\x00\x00\xce\x00\xce\x00\x8c\x00\x00\x00\x1a\x00\x1a\x00"
    484  1.2  christos 	"\x40\x00\x00\x00\x04\x00\x04\x00\x5a\x00\x00\x00\x16\x00\x16\x00"
    485  1.2  christos 	"\x5e\x00\x00\x00\x10\x00\x10\x00\x5a\x01\x00\x00\x05\x02\x89\x62"
    486  1.2  christos 	"\x31\x00\x37\x00\x2e\x00\x32\x00\x30\x00\x31\x00\x2e\x00\x35\x00"
    487  1.2  christos 	"\x37\x00\x2e\x00\x31\x00\x32\x00\x31\x00\x71\x5c\x30\x75\x77\x00"
    488  1.2  christos 	"\x6f\x00\x72\x00\x6b\x00\x73\x00\x74\x00\x61\x00\x74\x00\x69\x00"
    489  1.2  christos 	"\x6f\x00\x6e\x00\xab\xad\xeb\x72\x01\xd4\x5f\xdf\x59\x07\x5f\xa9"
    490  1.2  christos 	"\xfd\x54\x98\x2d\xfa\x17\xbb\xf1\x3c\x8f\xf5\x20\xe6\x8f\xd7\x0a"
    491  1.2  christos 	"\xc9\x19\x3e\x94\x61\x31\xdb\x0f\x55\xe8\xe2\x53\x01\x01\x00\x00"
    492  1.2  christos 	"\x00\x00\x00\x00\x00\x06\x3e\x30\xe4\x61\xcb\x01\x71\x98\x10\x6b"
    493  1.2  christos 	"\x4c\x82\xec\xb3\x00\x00\x00\x00\x02\x00\x06\x00\x43\x00\x4f\x00"
    494  1.2  christos 	"\x53\x00\x01\x00\x12\x00\x43\x00\x4f\x00\x53\x00\x57\x00\x49\x00"
    495  1.2  christos 	"\x4e\x00\x37\x00\x4a\x00\x50\x00\x04\x00\x1a\x00\x63\x00\x6f\x00"
    496  1.2  christos 	"\x73\x00\x2e\x00\x61\x00\x70\x00\x70\x00\x6c\x00\x65\x00\x2e\x00"
    497  1.2  christos 	"\x63\x00\x6f\x00\x6d\x00\x03\x00\x2e\x00\x63\x00\x6f\x00\x73\x00"
    498  1.2  christos 	"\x77\x00\x69\x00\x6e\x00\x37\x00\x6a\x00\x70\x00\x2e\x00\x63\x00"
    499  1.2  christos 	"\x6f\x00\x73\x00\x2e\x00\x61\x00\x70\x00\x70\x00\x6c\x00\x65\x00"
    500  1.2  christos 	"\x2e\x00\x63\x00\x6f\x00\x6d\x00\x05\x00\x1a\x00\x63\x00\x6f\x00"
    501  1.2  christos 	"\x73\x00\x2e\x00\x61\x00\x70\x00\x70\x00\x6c\x00\x65\x00\x2e\x00"
    502  1.2  christos 	"\x63\x00\x6f\x00\x6d\x00\x07\x00\x08\x00\xab\xec\xcc\x30\xe4\x61"
    503  1.2  christos 	"\xcb\x01\x00\x00\x00\x00\x00\x00\x00\x00\xbc\x2e\xba\x3f\xd1\xb1"
    504  1.2  christos 	"\xa7\x70\x00\x9d\x55\xa0\x59\x74\x2b\x78";
    505  1.2  christos 
    506  1.2  christos 
    507  1.2  christos     struct ntlm_type2 type2;
    508  1.2  christos     struct ntlm_type3 type3;
    509  1.2  christos     struct ntlm_buf data;
    510  1.2  christos     int ret;
    511  1.2  christos 
    512  1.2  christos     data.length = sizeof(buf2);
    513  1.2  christos     data.data = buf2;
    514  1.2  christos 
    515  1.2  christos     memset(&type2, 0, sizeof(type2));
    516  1.2  christos 
    517  1.2  christos     ret = heim_ntlm_decode_type2(&data, &type2);
    518  1.2  christos     if (ret)
    519  1.2  christos 	errx(1, "heim_ntlm_decode_type2(jp): %d", ret);
    520  1.2  christos 
    521  1.2  christos     data.data = NULL;
    522  1.2  christos     data.length = 0;
    523  1.2  christos 
    524  1.2  christos     ret = heim_ntlm_encode_type2(&type2, &data);
    525  1.2  christos     if (ret)
    526  1.2  christos 	errx(1, "heim_ntlm_encode_type2(jp): %d", ret);
    527  1.2  christos 
    528  1.2  christos     heim_ntlm_free_type2(&type2);
    529  1.2  christos     heim_ntlm_free_buf(&data);
    530  1.2  christos 
    531  1.2  christos     data.length = sizeof(buf3);
    532  1.2  christos     data.data = buf3;
    533  1.2  christos 
    534  1.2  christos     memset(&type3, 0, sizeof(type3));
    535  1.2  christos 
    536  1.2  christos     ret = heim_ntlm_decode_type3(&data, 1, &type3);
    537  1.2  christos     if (ret)
    538  1.2  christos 	errx(1, "heim_ntlm_decode_type2(jp): %d", ret);
    539  1.2  christos 
    540  1.2  christos     data.data = NULL;
    541  1.2  christos     data.length = 0;
    542  1.2  christos 
    543  1.2  christos     ret = heim_ntlm_encode_type3(&type3, &data, NULL);
    544  1.2  christos     if (ret)
    545  1.2  christos 	errx(1, "heim_ntlm_decode_type2(jp): %d", ret);
    546  1.2  christos 
    547  1.2  christos     heim_ntlm_free_type3(&type3);
    548  1.2  christos     heim_ntlm_free_buf(&data);
    549  1.2  christos 
    550  1.2  christos     return 0;
    551  1.2  christos }
    552  1.2  christos 
    553  1.2  christos 
    554  1.1     elric static int verbose_flag = 0;
    555  1.1     elric static int version_flag = 0;
    556  1.1     elric static int help_flag	= 0;
    557  1.1     elric 
    558  1.1     elric static struct getargs args[] = {
    559  1.1     elric     {"verbose",	0,	arg_flag,	&verbose_flag, "verbose printing", NULL },
    560  1.1     elric     {"version",	0,	arg_flag,	&version_flag, "print version", NULL },
    561  1.1     elric     {"help",	0,	arg_flag,	&help_flag,  NULL, NULL }
    562  1.1     elric };
    563  1.1     elric 
    564  1.1     elric static void
    565  1.1     elric usage (int ret)
    566  1.1     elric {
    567  1.1     elric     arg_printusage (args, sizeof(args)/sizeof(*args),
    568  1.1     elric 		    NULL, "");
    569  1.1     elric     exit (ret);
    570  1.1     elric }
    571  1.1     elric 
    572  1.1     elric int
    573  1.1     elric main(int argc, char **argv)
    574  1.1     elric {
    575  1.2  christos     int ret = 0, optidx = 0;
    576  1.1     elric 
    577  1.1     elric     setprogname(argv[0]);
    578  1.1     elric 
    579  1.2  christos     if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
    580  1.1     elric 	usage(1);
    581  1.1     elric 
    582  1.1     elric     if (help_flag)
    583  1.1     elric 	usage (0);
    584  1.1     elric 
    585  1.1     elric     if(version_flag){
    586  1.1     elric 	print_version(NULL);
    587  1.1     elric 	exit(0);
    588  1.1     elric     }
    589  1.1     elric 
    590  1.1     elric     if (verbose_flag)
    591  1.1     elric 	printf("test_parse\n");
    592  1.2  christos     ret |= test_parse();
    593  1.1     elric 
    594  1.1     elric     if (verbose_flag)
    595  1.1     elric 	printf("test_keys\n");
    596  1.2  christos     ret |= test_keys();
    597  1.1     elric 
    598  1.1     elric     if (verbose_flag)
    599  1.1     elric 	printf("test_ntlm2_session_resp\n");
    600  1.2  christos     ret |= test_ntlm2_session_resp();
    601  1.1     elric 
    602  1.1     elric     if (verbose_flag)
    603  1.1     elric 	printf("test_targetinfo\n");
    604  1.2  christos     ret |= test_targetinfo();
    605  1.2  christos 
    606  1.2  christos     if (verbose_flag)
    607  1.2  christos 	printf("test_ntlmv2\n");
    608  1.2  christos     ret |= test_ntlmv2();
    609  1.2  christos 
    610  1.2  christos     if (verbose_flag)
    611  1.2  christos 	printf("test_string2key\n");
    612  1.2  christos     ret |= test_string2key();
    613  1.2  christos 
    614  1.2  christos     if (verbose_flag)
    615  1.2  christos 	printf("test_jp\n");
    616  1.2  christos     ret |= test_jp();
    617  1.1     elric 
    618  1.1     elric     return ret;
    619  1.1     elric }
    620