2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$Id: ntlm.c 20816 2007-06-03 04:36:31Z lha $");
49 #include "krb5-types.h"
50 #include "crypto-headers.h"
61 static const unsigned char ntlmsigature[8] = "NTLMSSP\x00";
68 do { ret = f ; if (ret != (e)) { ret = EINVAL; goto out; } } while(0)
71 _ntlm_free_buf(struct ntlm_buf *p)
81 ascii2ucs2le(const char *string, int up, struct ntlm_buf *buf)
87 if (len / 2 > UINT_MAX)
90 buf->length = len * 2;
91 buf->data = malloc(buf->length);
92 if (buf->data == NULL && len != 0) {
98 for (i = 0; i < len; i++) {
99 unsigned char t = (unsigned char)string[i];
116 static krb5_error_code
117 ret_sec_buffer(krb5_storage *sp, struct sec_buffer *buf)
120 CHECK(krb5_ret_uint16(sp, &buf->length), 0);
121 CHECK(krb5_ret_uint16(sp, &buf->allocated), 0);
122 CHECK(krb5_ret_uint32(sp, &buf->offset), 0);
127 static krb5_error_code
128 store_sec_buffer(krb5_storage *sp, const struct sec_buffer *buf)
131 CHECK(krb5_store_uint16(sp, buf->length), 0);
132 CHECK(krb5_store_uint16(sp, buf->allocated), 0);
133 CHECK(krb5_store_uint32(sp, buf->offset), 0);
139 * Strings are either OEM or UNICODE. The later is encoded as ucs2 on
140 * wire, but using utf8 in memory.
143 static krb5_error_code
144 len_string(int ucs2, const char *s)
146 size_t len = strlen(s);
152 static krb5_error_code
153 ret_string(krb5_storage *sp, int ucs2, struct sec_buffer *desc, char **s)
157 *s = malloc(desc->length + 1);
158 CHECK(krb5_storage_seek(sp, desc->offset, SEEK_SET), desc->offset);
159 CHECK(krb5_storage_read(sp, *s, desc->length), desc->length);
160 (*s)[desc->length] = '\0';
164 for (i = 0; i < desc->length / 2; i++) {
165 (*s)[i] = (*s)[i * 2];
166 if ((*s)[i * 2 + 1]) {
181 static krb5_error_code
182 put_string(krb5_storage *sp, int ucs2, const char *s)
188 ret = ascii2ucs2le(s, 0, &buf);
192 buf.data = rk_UNCONST(s);
193 buf.length = strlen(s);
196 CHECK(krb5_storage_write(sp, buf.data, buf.length), buf.length);
198 _ntlm_free_buf(&buf);
208 static krb5_error_code
209 ret_buf(krb5_storage *sp, struct sec_buffer *desc, struct ntlm_buf *buf)
213 buf->data = malloc(desc->length);
214 buf->length = desc->length;
215 CHECK(krb5_storage_seek(sp, desc->offset, SEEK_SET), desc->offset);
216 CHECK(krb5_storage_read(sp, buf->data, buf->length), buf->length);
222 static krb5_error_code
223 put_buf(krb5_storage *sp, struct ntlm_buf *buf)
226 CHECK(krb5_storage_write(sp, buf->data, buf->length), buf->length);
237 heim_ntlm_free_targetinfo(struct ntlm_targetinfo *ti)
239 free(ti->servername);
240 free(ti->domainname);
241 free(ti->dnsdomainname);
242 free(ti->dnsservername);
243 memset(ti, 0, sizeof(*ti));
247 encode_ti_blob(krb5_storage *out, uint16_t type, int ucs2, char *s)
250 CHECK(krb5_store_uint16(out, type), 0);
251 CHECK(krb5_store_uint16(out, len_string(ucs2, s)), 0);
252 CHECK(put_string(out, ucs2, s), 0);
258 heim_ntlm_encode_targetinfo(struct ntlm_targetinfo *ti,
260 struct ntlm_buf *data)
268 out = krb5_storage_emem();
273 CHECK(encode_ti_blob(out, 1, ucs2, ti->servername), 0);
275 CHECK(encode_ti_blob(out, 2, ucs2, ti->domainname), 0);
276 if (ti->dnsservername)
277 CHECK(encode_ti_blob(out, 3, ucs2, ti->dnsservername), 0);
278 if (ti->dnsdomainname)
279 CHECK(encode_ti_blob(out, 4, ucs2, ti->dnsdomainname), 0);
282 CHECK(krb5_store_int16(out, 0), 0);
283 CHECK(krb5_store_int16(out, 0), 0);
287 ret = krb5_storage_to_data(out, &d);
289 data->length = d.length;
292 krb5_storage_free(out);
297 heim_ntlm_decode_targetinfo(struct ntlm_buf *data, int ucs2,
298 struct ntlm_targetinfo *ti)
300 memset(ti, 0, sizeof(*ti));
305 * encoder/decoder type1 messages
309 heim_ntlm_free_type1(struct ntlm_type1 *data)
314 free(data->hostname);
315 memset(data, 0, sizeof(*data));
319 heim_ntlm_decode_type1(const struct ntlm_buf *buf, struct ntlm_type1 *data)
322 unsigned char sig[8];
324 struct sec_buffer domain, hostname;
327 memset(data, 0, sizeof(*data));
329 in = krb5_storage_from_readonly_mem(buf->data, buf->length);
334 krb5_storage_set_byteorder(in, KRB5_STORAGE_BYTEORDER_LE);
336 CHECK(krb5_storage_read(in, sig, sizeof(sig)), sizeof(sig));
337 CHECK(memcmp(ntlmsigature, sig, sizeof(ntlmsigature)), 0);
338 CHECK(krb5_ret_uint32(in, &type), 0);
340 CHECK(krb5_ret_uint32(in, &data->flags), 0);
341 if (data->flags & NTLM_SUPPLIED_DOMAIN)
342 CHECK(ret_sec_buffer(in, &domain), 0);
343 if (data->flags & NTLM_SUPPLIED_WORKSTAION)
344 CHECK(ret_sec_buffer(in, &hostname), 0);
346 if (domain.offset > 32) {
347 CHECK(krb5_ret_uint32(in, &data->os[0]), 0);
348 CHECK(krb5_ret_uint32(in, &data->os[1]), 0);
351 if (data->flags & NTLM_SUPPLIED_DOMAIN)
352 CHECK(ret_string(in, 0, &domain, &data->domain), 0);
353 if (data->flags & NTLM_SUPPLIED_WORKSTAION)
354 CHECK(ret_string(in, 0, &hostname, &data->hostname), 0);
357 krb5_storage_free(in);
359 heim_ntlm_free_type1(data);
365 heim_ntlm_encode_type1(const struct ntlm_type1 *type1, struct ntlm_buf *data)
368 struct sec_buffer domain, hostname;
370 uint32_t base, flags;
372 flags = type1->flags;
377 flags |= NTLM_SUPPLIED_DOMAIN;
379 if (type1->hostname) {
381 flags |= NTLM_SUPPLIED_WORKSTAION;
387 domain.offset = base;
388 domain.length = len_string(0, type1->domain);
389 domain.allocated = domain.length;
391 if (type1->hostname) {
392 hostname.offset = domain.allocated + domain.offset;
393 hostname.length = len_string(0, type1->hostname);
394 hostname.allocated = hostname.length;
397 out = krb5_storage_emem();
401 krb5_storage_set_byteorder(out, KRB5_STORAGE_BYTEORDER_LE);
402 CHECK(krb5_storage_write(out, ntlmsigature, sizeof(ntlmsigature)),
403 sizeof(ntlmsigature));
404 CHECK(krb5_store_uint32(out, 1), 0);
405 CHECK(krb5_store_uint32(out, flags), 0);
408 CHECK(store_sec_buffer(out, &domain), 0);
410 CHECK(store_sec_buffer(out, &hostname), 0);
412 CHECK(krb5_store_uint32(out, type1->os[0]), 0);
413 CHECK(krb5_store_uint32(out, type1->os[1]), 0);
416 CHECK(put_string(out, 0, type1->domain), 0);
418 CHECK(put_string(out, 0, type1->hostname), 0);
422 ret = krb5_storage_to_data(out, &d);
424 data->length = d.length;
427 krb5_storage_free(out);
433 * encoder/decoder type 2 messages
437 heim_ntlm_free_type2(struct ntlm_type2 *data)
439 if (data->targetname)
440 free(data->targetname);
441 _ntlm_free_buf(&data->targetinfo);
442 memset(data, 0, sizeof(*data));
446 heim_ntlm_decode_type2(const struct ntlm_buf *buf, struct ntlm_type2 *type2)
449 unsigned char sig[8];
450 uint32_t type, ctx[2];
451 struct sec_buffer targetname, targetinfo;
455 memset(type2, 0, sizeof(*type2));
457 in = krb5_storage_from_readonly_mem(buf->data, buf->length);
462 krb5_storage_set_byteorder(in, KRB5_STORAGE_BYTEORDER_LE);
464 CHECK(krb5_storage_read(in, sig, sizeof(sig)), sizeof(sig));
465 CHECK(memcmp(ntlmsigature, sig, sizeof(ntlmsigature)), 0);
466 CHECK(krb5_ret_uint32(in, &type), 0);
469 CHECK(ret_sec_buffer(in, &targetname), 0);
470 CHECK(krb5_ret_uint32(in, &type2->flags), 0);
471 if (type2->flags & NTLM_NEG_UNICODE)
473 CHECK(krb5_storage_read(in, type2->challange, sizeof(type2->challange)),
474 sizeof(type2->challange));
475 CHECK(krb5_ret_uint32(in, &ctx[0]), 0); /* context */
476 CHECK(krb5_ret_uint32(in, &ctx[1]), 0);
477 CHECK(ret_sec_buffer(in, &targetinfo), 0);
480 CHECK(krb5_ret_uint32(in, &type2->os[0]), 0);
481 CHECK(krb5_ret_uint32(in, &type2->os[1]), 0);
484 CHECK(ret_string(in, ucs2, &targetname, &type2->targetname), 0);
485 CHECK(ret_buf(in, &targetinfo, &type2->targetinfo), 0);
489 krb5_storage_free(in);
491 heim_ntlm_free_type2(type2);
497 heim_ntlm_encode_type2(struct ntlm_type2 *type2, struct ntlm_buf *data)
499 struct sec_buffer targetname, targetinfo;
501 krb5_storage *out = NULL;
510 if (type2->flags & NTLM_NEG_UNICODE)
513 targetname.offset = base;
514 targetname.length = len_string(ucs2, type2->targetname);
515 targetname.allocated = targetname.length;
517 targetinfo.offset = targetname.allocated + targetname.offset;
518 targetinfo.length = type2->targetinfo.length;
519 targetinfo.allocated = type2->targetinfo.length;
521 out = krb5_storage_emem();
525 krb5_storage_set_byteorder(out, KRB5_STORAGE_BYTEORDER_LE);
526 CHECK(krb5_storage_write(out, ntlmsigature, sizeof(ntlmsigature)),
527 sizeof(ntlmsigature));
528 CHECK(krb5_store_uint32(out, 2), 0);
529 CHECK(store_sec_buffer(out, &targetname), 0);
530 CHECK(krb5_store_uint32(out, type2->flags), 0);
531 CHECK(krb5_storage_write(out, type2->challange, sizeof(type2->challange)),
532 sizeof(type2->challange));
533 CHECK(krb5_store_uint32(out, 0), 0); /* context */
534 CHECK(krb5_store_uint32(out, 0), 0);
535 CHECK(store_sec_buffer(out, &targetinfo), 0);
538 CHECK(krb5_store_uint32(out, type2->os[0]), 0);
539 CHECK(krb5_store_uint32(out, type2->os[1]), 0);
541 CHECK(put_string(out, ucs2, type2->targetname), 0);
542 CHECK(krb5_storage_write(out, type2->targetinfo.data,
543 type2->targetinfo.length),
544 type2->targetinfo.length);
548 ret = krb5_storage_to_data(out, &d);
550 data->length = d.length;
554 krb5_storage_free(out);
560 * encoder/decoder type 2 messages
564 heim_ntlm_free_type3(struct ntlm_type3 *data)
566 _ntlm_free_buf(&data->lm);
567 _ntlm_free_buf(&data->ntlm);
568 if (data->targetname)
569 free(data->targetname);
571 free(data->username);
574 _ntlm_free_buf(&data->sessionkey);
575 memset(data, 0, sizeof(*data));
583 heim_ntlm_decode_type3(const struct ntlm_buf *buf,
585 struct ntlm_type3 *type3)
588 unsigned char sig[8];
591 struct sec_buffer lm, ntlm, target, username, sessionkey, ws;
593 memset(type3, 0, sizeof(*type3));
594 memset(&sessionkey, 0, sizeof(sessionkey));
596 in = krb5_storage_from_readonly_mem(buf->data, buf->length);
601 krb5_storage_set_byteorder(in, KRB5_STORAGE_BYTEORDER_LE);
603 CHECK(krb5_storage_read(in, sig, sizeof(sig)), sizeof(sig));
604 CHECK(memcmp(ntlmsigature, sig, sizeof(ntlmsigature)), 0);
605 CHECK(krb5_ret_uint32(in, &type), 0);
607 CHECK(ret_sec_buffer(in, &lm), 0);
608 CHECK(ret_sec_buffer(in, &ntlm), 0);
609 CHECK(ret_sec_buffer(in, &target), 0);
610 CHECK(ret_sec_buffer(in, &username), 0);
611 CHECK(ret_sec_buffer(in, &ws), 0);
612 if (lm.offset >= 60) {
613 CHECK(ret_sec_buffer(in, &sessionkey), 0);
615 if (lm.offset >= 64) {
616 CHECK(krb5_ret_uint32(in, &type3->flags), 0);
618 if (lm.offset >= 72) {
619 CHECK(krb5_ret_uint32(in, &type3->os[0]), 0);
620 CHECK(krb5_ret_uint32(in, &type3->os[1]), 0);
622 CHECK(ret_buf(in, &lm, &type3->lm), 0);
623 CHECK(ret_buf(in, &ntlm, &type3->ntlm), 0);
624 CHECK(ret_string(in, ucs2, &target, &type3->targetname), 0);
625 CHECK(ret_string(in, ucs2, &username, &type3->username), 0);
626 CHECK(ret_string(in, ucs2, &username, &type3->ws), 0);
627 if (sessionkey.offset)
628 CHECK(ret_buf(in, &sessionkey, &type3->sessionkey), 0);
631 krb5_storage_free(in);
633 heim_ntlm_free_type3(type3);
639 heim_ntlm_encode_type3(struct ntlm_type3 *type3, struct ntlm_buf *data)
641 struct sec_buffer lm, ntlm, target, username, sessionkey, ws;
643 krb5_storage *out = NULL;
647 memset(&lm, 0, sizeof(lm));
648 memset(&ntlm, 0, sizeof(ntlm));
649 memset(&target, 0, sizeof(target));
650 memset(&username, 0, sizeof(username));
651 memset(&ws, 0, sizeof(ws));
652 memset(&sessionkey, 0, sizeof(sessionkey));
655 if (type3->sessionkey.length) {
656 base += 8; /* sessionkey sec buf */
657 base += 4; /* flags */
663 if (type3->flags & NTLM_NEG_UNICODE)
667 lm.length = type3->lm.length;
668 lm.allocated = type3->lm.length;
670 ntlm.offset = lm.offset + lm.allocated;
671 ntlm.length = type3->ntlm.length;
672 ntlm.allocated = ntlm.length;
674 target.offset = ntlm.offset + ntlm.allocated;
675 target.length = len_string(ucs2, type3->targetname);
676 target.allocated = target.length;
678 username.offset = target.offset + target.allocated;
679 username.length = len_string(ucs2, type3->username);
680 username.allocated = username.length;
682 ws.offset = username.offset + username.allocated;
683 ws.length = len_string(ucs2, type3->ws);
684 ws.allocated = ws.length;
686 sessionkey.offset = ws.offset + ws.allocated;
687 sessionkey.length = type3->sessionkey.length;
688 sessionkey.allocated = type3->sessionkey.length;
690 out = krb5_storage_emem();
694 krb5_storage_set_byteorder(out, KRB5_STORAGE_BYTEORDER_LE);
695 CHECK(krb5_storage_write(out, ntlmsigature, sizeof(ntlmsigature)),
696 sizeof(ntlmsigature));
697 CHECK(krb5_store_uint32(out, 3), 0);
699 CHECK(store_sec_buffer(out, &lm), 0);
700 CHECK(store_sec_buffer(out, &ntlm), 0);
701 CHECK(store_sec_buffer(out, &target), 0);
702 CHECK(store_sec_buffer(out, &username), 0);
703 CHECK(store_sec_buffer(out, &ws), 0);
705 if (type3->sessionkey.length) {
706 CHECK(store_sec_buffer(out, &sessionkey), 0);
707 CHECK(krb5_store_uint32(out, type3->flags), 0);
710 CHECK(krb5_store_uint32(out, 0), 0); /* os0 */
711 CHECK(krb5_store_uint32(out, 0), 0); /* os1 */
714 CHECK(put_buf(out, &type3->lm), 0);
715 CHECK(put_buf(out, &type3->ntlm), 0);
716 CHECK(put_string(out, ucs2, type3->targetname), 0);
717 CHECK(put_string(out, ucs2, type3->username), 0);
718 CHECK(put_string(out, ucs2, type3->ws), 0);
719 CHECK(put_buf(out, &type3->sessionkey), 0);
723 ret = krb5_storage_to_data(out, &d);
725 data->length = d.length;
729 krb5_storage_free(out);
740 splitandenc(unsigned char *hash,
741 unsigned char *challange,
742 unsigned char *answer)
745 DES_key_schedule sched;
747 ((unsigned char*)key)[0] = hash[0];
748 ((unsigned char*)key)[1] = (hash[0] << 7) | (hash[1] >> 1);
749 ((unsigned char*)key)[2] = (hash[1] << 6) | (hash[2] >> 2);
750 ((unsigned char*)key)[3] = (hash[2] << 5) | (hash[3] >> 3);
751 ((unsigned char*)key)[4] = (hash[3] << 4) | (hash[4] >> 4);
752 ((unsigned char*)key)[5] = (hash[4] << 3) | (hash[5] >> 5);
753 ((unsigned char*)key)[6] = (hash[5] << 2) | (hash[6] >> 6);
754 ((unsigned char*)key)[7] = (hash[6] << 1);
756 DES_set_odd_parity(&key);
757 DES_set_key(&key, &sched);
758 DES_ecb_encrypt((DES_cblock *)challange, (DES_cblock *)answer, &sched, 1);
759 memset(&sched, 0, sizeof(sched));
760 memset(key, 0, sizeof(key));
764 heim_ntlm_nt_key(const char *password, struct ntlm_buf *key)
770 key->data = malloc(MD5_DIGEST_LENGTH);
771 if (key->data == NULL)
773 key->length = MD5_DIGEST_LENGTH;
775 ret = ascii2ucs2le(password, 0, &buf);
781 MD4_Update(&ctx, buf.data, buf.length);
782 MD4_Final(key->data, &ctx);
783 _ntlm_free_buf(&buf);
788 heim_ntlm_calculate_ntlm1(void *key, size_t len,
789 unsigned char challange[8],
790 struct ntlm_buf *answer)
792 unsigned char res[21];
794 if (len != MD4_DIGEST_LENGTH)
797 memcpy(res, key, len);
798 memset(&res[MD4_DIGEST_LENGTH], 0, sizeof(res) - MD4_DIGEST_LENGTH);
800 answer->data = malloc(24);
801 if (answer->data == NULL)
805 splitandenc(&res[0], challange, ((unsigned char *)answer->data) + 0);
806 splitandenc(&res[7], challange, ((unsigned char *)answer->data) + 8);
807 splitandenc(&res[14], challange, ((unsigned char *)answer->data) + 16);
813 heim_ntlm_build_ntlm1_master(void *key, size_t len,
814 struct ntlm_buf *session,
815 struct ntlm_buf *master)
819 memset(master, 0, sizeof(*master));
820 memset(session, 0, sizeof(*session));
822 if (len != MD4_DIGEST_LENGTH)
825 session->length = MD4_DIGEST_LENGTH;
826 session->data = malloc(session->length);
827 if (session->data == NULL) {
831 master->length = MD4_DIGEST_LENGTH;
832 master->data = malloc(master->length);
833 if (master->data == NULL) {
834 _ntlm_free_buf(master);
835 _ntlm_free_buf(session);
840 unsigned char sessionkey[MD4_DIGEST_LENGTH];
844 MD4_Update(&ctx, key, len);
845 MD4_Final(sessionkey, &ctx);
847 RC4_set_key(&rc4, sizeof(sessionkey), sessionkey);
850 if (RAND_bytes(session->data, session->length) != 1) {
851 _ntlm_free_buf(master);
852 _ntlm_free_buf(session);
856 RC4(&rc4, master->length, session->data, master->data);
857 memset(&rc4, 0, sizeof(rc4));
867 heim_ntlm_ntlmv2_key(const void *key, size_t len,
868 const char *username,
870 unsigned char ntlmv2[16])
872 unsigned int hmaclen;
876 HMAC_Init_ex(&c, key, len, EVP_md5(), NULL);
879 /* uppercase username and turn it inte ucs2-le */
880 ascii2ucs2le(username, 1, &buf);
881 HMAC_Update(&c, buf.data, buf.length);
883 /* turn target into ucs2-le */
884 ascii2ucs2le(target, 0, &buf);
885 HMAC_Update(&c, buf.data, buf.length);
888 HMAC_Final(&c, ntlmv2, &hmaclen);
889 HMAC_CTX_cleanup(&c);
897 #define NTTIME_EPOCH 0x019DB1DED53E8000LL
900 unix2nttime(time_t unix_time)
903 wt = unix_time * (uint64_t)10000000 + (uint64_t)NTTIME_EPOCH;
908 nt2unixtime(uint64_t t)
910 t = ((t - (uint64_t)NTTIME_EPOCH) / (uint64_t)10000000);
911 if (t > (((time_t)(~(uint64_t)0)) >> 1))
918 heim_ntlm_calculate_ntlm2(const void *key, size_t len,
919 const char *username,
921 const unsigned char serverchallange[8],
922 const struct ntlm_buf *infotarget,
923 unsigned char ntlmv2[16],
924 struct ntlm_buf *answer)
928 unsigned int hmaclen;
929 unsigned char ntlmv2answer[16];
931 unsigned char clientchallange[8];
935 t = unix2nttime(time(NULL));
937 if (RAND_bytes(clientchallange, sizeof(clientchallange)) != 1)
940 /* calculate ntlmv2 key */
942 heim_ntlm_ntlmv2_key(key, len, username, target, ntlmv2);
944 /* calculate and build ntlmv2 answer */
946 sp = krb5_storage_emem();
949 krb5_storage_set_flags(sp, KRB5_STORAGE_BYTEORDER_LE);
951 CHECK(krb5_store_uint32(sp, 0x01010000), 0);
952 CHECK(krb5_store_uint32(sp, 0), 0);
953 /* timestamp le 64 bit ts */
954 CHECK(krb5_store_uint32(sp, t & 0xffffffff), 0);
955 CHECK(krb5_store_uint32(sp, t >> 32), 0);
956 CHECK(krb5_storage_write(sp, clientchallange, 8), 8);
957 CHECK(krb5_storage_write(sp, infotarget->data, infotarget->length),
960 /* CHECK(krb5_store_uint32(sp, 0), 0); */
962 CHECK(krb5_storage_to_data(sp, &data), 0);
963 krb5_storage_free(sp);
967 HMAC_Init_ex(&c, ntlmv2, sizeof(ntlmv2), EVP_md5(), NULL);
968 HMAC_Update(&c, data.data, data.length);
969 HMAC_Update(&c, serverchallange, 8);
970 HMAC_Final(&c, ntlmv2answer, &hmaclen);
971 HMAC_CTX_cleanup(&c);
973 sp = krb5_storage_emem();
975 krb5_data_free(&data);
979 CHECK(krb5_storage_write(sp, ntlmv2answer, 16), 16);
980 CHECK(krb5_storage_write(sp, data.data, data.length), data.length);
981 krb5_data_free(&data);
983 CHECK(krb5_storage_to_data(sp, &data), 0);
984 krb5_storage_free(sp);
987 answer->data = data.data;
988 answer->length = data.length;
993 krb5_storage_free(sp);
997 static const int authtimediff = 3600 * 2; /* 2 hours */
1000 heim_ntlm_verify_ntlm2(const void *key, size_t len,
1001 const char *username,
1004 const unsigned char serverchallange[8],
1005 const struct ntlm_buf *answer,
1006 struct ntlm_buf *infotarget,
1007 unsigned char ntlmv2[16])
1009 krb5_error_code ret;
1010 unsigned int hmaclen;
1011 unsigned char clientanswer[16];
1012 unsigned char serveranswer[16];
1019 infotarget->length = 0;
1020 infotarget->data = NULL;
1022 if (answer->length < 16)
1028 /* calculate ntlmv2 key */
1030 heim_ntlm_ntlmv2_key(key, len, username, target, ntlmv2);
1032 /* calculate and build ntlmv2 answer */
1034 sp = krb5_storage_from_readonly_mem(answer->data, answer->length);
1037 krb5_storage_set_flags(sp, KRB5_STORAGE_BYTEORDER_LE);
1039 CHECK(krb5_storage_read(sp, clientanswer, 16), 16);
1041 CHECK(krb5_ret_uint32(sp, &temp), 0);
1042 CHECK(temp, 0x01010000);
1043 CHECK(krb5_ret_uint32(sp, &temp), 0);
1045 /* timestamp le 64 bit ts */
1046 CHECK(krb5_ret_uint32(sp, &temp), 0);
1048 CHECK(krb5_ret_uint32(sp, &temp), 0);
1049 t |= ((uint64_t)temp)<< 32;
1051 authtime = nt2unixtime(t);
1053 if (abs((int)(authtime - now)) > authtimediff) {
1058 /* client challange */
1059 CHECK(krb5_storage_read(sp, serveranswer, 8), 8);
1061 infotarget->length = answer->length - 40;
1062 infotarget->data = malloc(infotarget->length);
1063 if (infotarget->data == NULL) {
1067 CHECK(krb5_storage_read(sp, infotarget->data, infotarget->length),
1068 infotarget->length);
1069 /* XXX remove the unknown uint32_t */
1070 krb5_storage_free(sp);
1074 HMAC_Init_ex(&c, ntlmv2, sizeof(ntlmv2), EVP_md5(), NULL);
1075 HMAC_Update(&c, ((char *)answer->data) + 16, answer->length - 16);
1076 HMAC_Update(&c, serverchallange, 8);
1077 HMAC_Final(&c, serveranswer, &hmaclen);
1078 HMAC_CTX_cleanup(&c);
1080 if (memcmp(serveranswer, clientanswer, 16) != 0) {
1081 _ntlm_free_buf(infotarget);
1087 _ntlm_free_buf(infotarget);
1089 krb5_storage_free(sp);