ntlm.c

来自「samba最新软件」· C语言 代码 · 共 1,365 行 · 第 1/3 页

C
1,365
字号
/* * Copyright (c) 2006 - 2007 Kungliga Tekniska H鰃skolan * (Royal Institute of Technology, Stockholm, Sweden).  * All rights reserved.  * * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met:  * * 1. Redistributions of source code must retain the above copyright  *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright  *    notice, this list of conditions and the following disclaimer in the  *    documentation and/or other materials provided with the distribution.  * * 3. Neither the name of the Institute nor the names of its contributors  *    may be used to endorse or promote products derived from this software  *    without specific prior written permission.  * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  * SUCH DAMAGE.  */#include <config.h>RCSID("$Id: ntlm.c 22370 2007-12-28 16:12:01Z lha $");#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <string.h>#include <ctype.h>#include <errno.h>#include <limits.h>#include <krb5.h>#include <roken.h>#include "krb5-types.h"#include "crypto-headers.h"#include <heimntlm.h>/*! \mainpage Heimdal NTLM library * * \section intro Introduction * * Heimdal libheimntlm library is a implementation of the NTLM * protocol, both version 1 and 2. The GSS-API mech that uses this * library adds support for transport encryption and integrity * checking. *  * NTLM is a protocol for mutual authentication, its still used in * many protocol where Kerberos is not support, one example is * EAP/X802.1x mechanism LEAP from Microsoft and Cisco. * * This is a support library for the core protocol, its used in * Heimdal to implement and GSS-API mechanism. There is also support * in the KDC to do remote digest authenticiation, this to allow * services to authenticate users w/o direct access to the users ntlm * hashes (same as Kerberos arcfour enctype hashes). * * More information about the NTLM protocol can found here * http://davenport.sourceforge.net/ntlm.html . *  * The Heimdal projects web page: http://www.h5l.org/ *//** @defgroup ntlm_core Heimdal NTLM library  *  * The NTLM core functions implement the string2key generation * function, message encode and decode function, and the hash function * functions. */struct sec_buffer {    uint16_t length;    uint16_t allocated;    uint32_t offset;};static const unsigned char ntlmsigature[8] = "NTLMSSP\x00";/* * */#define CHECK(f, e)							\    do { ret = f ; if (ret != (e)) { ret = EINVAL; goto out; } } while(0)/** * heim_ntlm_free_buf frees the ntlm buffer * * @param p buffer to be freed * * @ingroup ntlm_core */voidheim_ntlm_free_buf(struct ntlm_buf *p){    if (p->data)	free(p->data);    p->data = NULL;    p->length = 0;}    static intascii2ucs2le(const char *string, int up, struct ntlm_buf *buf){    unsigned char *p;    size_t len, i;    len = strlen(string);    if (len / 2 > UINT_MAX)	return ERANGE;    buf->length = len * 2;    buf->data = malloc(buf->length);    if (buf->data == NULL && len != 0) {	heim_ntlm_free_buf(buf);	return ENOMEM;    }    p = buf->data;    for (i = 0; i < len; i++) {	unsigned char t = (unsigned char)string[i];	if (t & 0x80) {	    heim_ntlm_free_buf(buf);	    return EINVAL;	}	if (up)	    t = toupper(t);	p[(i * 2) + 0] = t;	p[(i * 2) + 1] = 0;    }    return 0;}/* * */static krb5_error_coderet_sec_buffer(krb5_storage *sp, struct sec_buffer *buf){    krb5_error_code ret;    CHECK(krb5_ret_uint16(sp, &buf->length), 0);    CHECK(krb5_ret_uint16(sp, &buf->allocated), 0);    CHECK(krb5_ret_uint32(sp, &buf->offset), 0);out:    return ret;}static krb5_error_codestore_sec_buffer(krb5_storage *sp, const struct sec_buffer *buf){    krb5_error_code ret;    CHECK(krb5_store_uint16(sp, buf->length), 0);    CHECK(krb5_store_uint16(sp, buf->allocated), 0);    CHECK(krb5_store_uint32(sp, buf->offset), 0);out:    return ret;}/* * Strings are either OEM or UNICODE. The later is encoded as ucs2 on * wire, but using utf8 in memory. */static krb5_error_codelen_string(int ucs2, const char *s){    size_t len = strlen(s);    if (ucs2)	len *= 2;    return len;}static krb5_error_coderet_string(krb5_storage *sp, int ucs2, struct sec_buffer *desc, char **s){    krb5_error_code ret;    *s = malloc(desc->length + 1);    CHECK(krb5_storage_seek(sp, desc->offset, SEEK_SET), desc->offset);    CHECK(krb5_storage_read(sp, *s, desc->length), desc->length);    (*s)[desc->length] = '\0';    if (ucs2) {	size_t i;	for (i = 0; i < desc->length / 2; i++) {	    (*s)[i] = (*s)[i * 2];	    if ((*s)[i * 2 + 1]) {		free(*s);		*s = NULL;		return EINVAL;	    }	}	(*s)[i] = '\0';    }    ret = 0;out:    return ret;    return 0;}static krb5_error_codeput_string(krb5_storage *sp, int ucs2, const char *s){    krb5_error_code ret;    struct ntlm_buf buf;    if (ucs2) {	ret = ascii2ucs2le(s, 0, &buf);	if (ret)	    return ret;    } else {	buf.data = rk_UNCONST(s);	buf.length = strlen(s);    }    CHECK(krb5_storage_write(sp, buf.data, buf.length), buf.length);    if (ucs2)	heim_ntlm_free_buf(&buf);    ret = 0;out:    return ret;}/* * */static krb5_error_coderet_buf(krb5_storage *sp, struct sec_buffer *desc, struct ntlm_buf *buf){    krb5_error_code ret;    buf->data = malloc(desc->length);    buf->length = desc->length;    CHECK(krb5_storage_seek(sp, desc->offset, SEEK_SET), desc->offset);    CHECK(krb5_storage_read(sp, buf->data, buf->length), buf->length);    ret = 0;out:    return ret;}static krb5_error_codeput_buf(krb5_storage *sp, const struct ntlm_buf *buf){    krb5_error_code ret;    CHECK(krb5_storage_write(sp, buf->data, buf->length), buf->length);    ret = 0;out:    return ret;}/** * Frees the ntlm_targetinfo message * * @param ti targetinfo to be freed * * @ingroup ntlm_core */voidheim_ntlm_free_targetinfo(struct ntlm_targetinfo *ti){    free(ti->servername);    free(ti->domainname);    free(ti->dnsdomainname);    free(ti->dnsservername);    memset(ti, 0, sizeof(*ti));}static intencode_ti_blob(krb5_storage *out, uint16_t type, int ucs2, char *s){    krb5_error_code ret;    CHECK(krb5_store_uint16(out, type), 0);    CHECK(krb5_store_uint16(out, len_string(ucs2, s)), 0);    CHECK(put_string(out, ucs2, s), 0);out:    return ret;}/** * Encodes a ntlm_targetinfo message. * * @param ti the ntlm_targetinfo message to encode. * @param ucs2 if the strings should be encoded with ucs2 (selected by flag in message). * @param data is the return buffer with the encoded message, should be * freed with heim_ntlm_free_buf(). * * @return In case of success 0 is return, an errors, a errno in what * went wrong. * * @ingroup ntlm_core */intheim_ntlm_encode_targetinfo(const struct ntlm_targetinfo *ti,			    int ucs2, 			    struct ntlm_buf *data){    krb5_error_code ret;    krb5_storage *out;    data->data = NULL;    data->length = 0;    out = krb5_storage_emem();    if (out == NULL)	return ENOMEM;    if (ti->servername)	CHECK(encode_ti_blob(out, 1, ucs2, ti->servername), 0);    if (ti->domainname)	CHECK(encode_ti_blob(out, 2, ucs2, ti->domainname), 0);    if (ti->dnsservername)	CHECK(encode_ti_blob(out, 3, ucs2, ti->dnsservername), 0);    if (ti->dnsdomainname)	CHECK(encode_ti_blob(out, 4, ucs2, ti->dnsdomainname), 0);    /* end tag */    CHECK(krb5_store_int16(out, 0), 0);    CHECK(krb5_store_int16(out, 0), 0);    {	krb5_data d;	ret = krb5_storage_to_data(out, &d);	data->data = d.data;	data->length = d.length;    }out:    krb5_storage_free(out);    return ret;}/** * Decodes an NTLM targetinfo message * * @param data input data buffer with the encode NTLM targetinfo message * @param ucs2 if the strings should be encoded with ucs2 (selected by flag in message). * @param ti the decoded target info, should be freed with heim_ntlm_free_targetinfo(). * * @return In case of success 0 is return, an errors, a errno in what * went wrong. * * @ingroup ntlm_core */intheim_ntlm_decode_targetinfo(const struct ntlm_buf *data,			    int ucs2,			    struct ntlm_targetinfo *ti){    memset(ti, 0, sizeof(*ti));    return 0;}/** * Frees the ntlm_type1 message * * @param data message to be freed * * @ingroup ntlm_core */voidheim_ntlm_free_type1(struct ntlm_type1 *data){    if (data->domain)	free(data->domain);    if (data->hostname)	free(data->hostname);    memset(data, 0, sizeof(*data));}intheim_ntlm_decode_type1(const struct ntlm_buf *buf, struct ntlm_type1 *data){    krb5_error_code ret;    unsigned char sig[8];    uint32_t type;    struct sec_buffer domain, hostname;    krb5_storage *in;        memset(data, 0, sizeof(*data));    in = krb5_storage_from_readonly_mem(buf->data, buf->length);    if (in == NULL) {	ret = EINVAL;	goto out;    }    krb5_storage_set_byteorder(in, KRB5_STORAGE_BYTEORDER_LE);    CHECK(krb5_storage_read(in, sig, sizeof(sig)), sizeof(sig));    CHECK(memcmp(ntlmsigature, sig, sizeof(ntlmsigature)), 0);    CHECK(krb5_ret_uint32(in, &type), 0);    CHECK(type, 1);    CHECK(krb5_ret_uint32(in, &data->flags), 0);    if (data->flags & NTLM_SUPPLIED_DOMAIN)	CHECK(ret_sec_buffer(in, &domain), 0);    if (data->flags & NTLM_SUPPLIED_WORKSTAION)	CHECK(ret_sec_buffer(in, &hostname), 0);#if 0    if (domain.offset > 32) {	CHECK(krb5_ret_uint32(in, &data->os[0]), 0);	CHECK(krb5_ret_uint32(in, &data->os[1]), 0);    }#endif    if (data->flags & NTLM_SUPPLIED_DOMAIN)	CHECK(ret_string(in, 0, &domain, &data->domain), 0);    if (data->flags & NTLM_SUPPLIED_WORKSTAION)	CHECK(ret_string(in, 0, &hostname, &data->hostname), 0);out:    krb5_storage_free(in);    if (ret)	heim_ntlm_free_type1(data);    return ret;}/** * Encodes an ntlm_type1 message. * * @param type1 the ntlm_type1 message to encode. * @param data is the return buffer with the encoded message, should be * freed with heim_ntlm_free_buf(). * * @return In case of success 0 is return, an errors, a errno in what * went wrong. * * @ingroup ntlm_core */intheim_ntlm_encode_type1(const struct ntlm_type1 *type1, struct ntlm_buf *data){    krb5_error_code ret;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?