arcfour.c

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

C
761
字号
        p0 = _gssapi_make_mech_header(output_message_buffer->value,				  len,				  GSS_KRB5_MECHANISM);    p = p0;    *p++ = 0x02; /* TOK_ID */    *p++ = 0x01;    *p++ = 0x11; /* SGN_ALG */    *p++ = 0x00;    if (conf_req_flag) {	*p++ = 0x10; /* SEAL_ALG */	*p++ = 0x00;    } else {	*p++ = 0xff; /* SEAL_ALG */	*p++ = 0xff;    }    *p++ = 0xff; /* Filler */    *p++ = 0xff;    p = NULL;    HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);    krb5_auth_con_getlocalseqnumber (context,				     context_handle->auth_context,				     &seq_number);    _gsskrb5_encode_be_om_uint32(seq_number, p0 + 8);    krb5_auth_con_setlocalseqnumber (context,				     context_handle->auth_context,				     ++seq_number);    HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);    memset (p0 + 8 + 4,	    (context_handle->more_flags & LOCAL) ? 0 : 0xff,	    4);    krb5_generate_random_block(p0 + 24, 8); /* fill in Confounder */        /* p points to data */    p = p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE;    memcpy(p, input_message_buffer->value, input_message_buffer->length);    if (!IS_DCE_STYLE(context_handle))	p[input_message_buffer->length] = 1; /* padding */    ret = arcfour_mic_cksum(context,			    key, KRB5_KU_USAGE_SEAL,			    p0 + 16, 8, /* SGN_CKSUM */ 			    p0, 8, /* TOK_ID, SGN_ALG, SEAL_ALG, Filler */			    p0 + 24, 8, /* Confounder */			    p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE, 			    datalen);    if (ret) {	*minor_status = ret;	_gsskrb5_release_buffer(minor_status, output_message_buffer);	return GSS_S_FAILURE;    }    {	int i;	Klocal.keytype = key->keytype;	Klocal.keyvalue.data = Klocaldata;	Klocal.keyvalue.length = sizeof(Klocaldata);	for (i = 0; i < 16; i++)	    Klocaldata[i] = ((u_char *)key->keyvalue.data)[i] ^ 0xF0;    }    ret = arcfour_mic_key(context, &Klocal,			  p0 + 8, 4, /* SND_SEQ */			  k6_data, sizeof(k6_data));    memset(Klocaldata, 0, sizeof(Klocaldata));    if (ret) {	_gsskrb5_release_buffer(minor_status, output_message_buffer);	*minor_status = ret;	return GSS_S_FAILURE;    }    if(conf_req_flag) {	RC4_KEY rc4_key;	RC4_set_key (&rc4_key, sizeof(k6_data), (void *)k6_data);	/* XXX ? */	RC4 (&rc4_key, 8 + datalen, p0 + 24, p0 + 24); /* Confounder + data */	memset(&rc4_key, 0, sizeof(rc4_key));    }    memset(k6_data, 0, sizeof(k6_data));    ret = arcfour_mic_key(context, key,			  p0 + 16, 8, /* SGN_CKSUM */			  k6_data, sizeof(k6_data));    if (ret) {	_gsskrb5_release_buffer(minor_status, output_message_buffer);	*minor_status = ret;	return GSS_S_FAILURE;    }    {	RC4_KEY rc4_key;		RC4_set_key (&rc4_key, sizeof(k6_data), k6_data);	RC4 (&rc4_key, 8, p0 + 8, p0 + 8); /* SND_SEQ */	memset(&rc4_key, 0, sizeof(rc4_key));	memset(k6_data, 0, sizeof(k6_data));    }    if (conf_state)	*conf_state = conf_req_flag;    *minor_status = 0;    return GSS_S_COMPLETE;}OM_uint32 _gssapi_unwrap_arcfour(OM_uint32 *minor_status,				 const gsskrb5_ctx context_handle,				 krb5_context context,				 const gss_buffer_t input_message_buffer,				 gss_buffer_t output_message_buffer,				 int *conf_state,				 gss_qop_t *qop_state,				 krb5_keyblock *key){    u_char Klocaldata[16];    krb5_keyblock Klocal;    krb5_error_code ret;    uint32_t seq_number;    size_t datalen;    OM_uint32 omret;    u_char k6_data[16], SND_SEQ[8], Confounder[8];    u_char cksum_data[8];    u_char *p, *p0;    int cmp;    int conf_flag;    size_t padlen = 0, len;        if (conf_state)	*conf_state = 0;    if (qop_state)	*qop_state = 0;    p0 = input_message_buffer->value;    if (IS_DCE_STYLE(context_handle)) {	len = GSS_ARCFOUR_WRAP_TOKEN_SIZE + 	    GSS_ARCFOUR_WRAP_TOKEN_DCE_DER_HEADER_SIZE;	if (input_message_buffer->length < len)	    return GSS_S_BAD_MECH;    } else {	len = input_message_buffer->length;    }    omret = _gssapi_verify_mech_header(&p0,				       len,				       GSS_KRB5_MECHANISM);    if (omret)	return omret;    /* length of mech header */    len = (p0 - (u_char *)input_message_buffer->value) + 	GSS_ARCFOUR_WRAP_TOKEN_SIZE;    if (len > input_message_buffer->length)	return GSS_S_BAD_MECH;    /* length of data */    datalen = input_message_buffer->length - len;    p = p0;    if (memcmp(p, "\x02\x01", 2) != 0)	return GSS_S_BAD_SIG;    p += 2;    if (memcmp(p, "\x11\x00", 2) != 0) /* SGN_ALG = HMAC MD5 ARCFOUR */	return GSS_S_BAD_SIG;    p += 2;    if (memcmp (p, "\x10\x00", 2) == 0)	conf_flag = 1;    else if (memcmp (p, "\xff\xff", 2) == 0)	conf_flag = 0;    else	return GSS_S_BAD_SIG;    p += 2;    if (memcmp (p, "\xff\xff", 2) != 0)	return GSS_S_BAD_MIC;    p = NULL;    ret = arcfour_mic_key(context, key,			  p0 + 16, 8, /* SGN_CKSUM */			  k6_data, sizeof(k6_data));    if (ret) {	*minor_status = ret;	return GSS_S_FAILURE;    }    {	RC4_KEY rc4_key;		RC4_set_key (&rc4_key, sizeof(k6_data), k6_data);	RC4 (&rc4_key, 8, p0 + 8, SND_SEQ); /* SND_SEQ */	memset(&rc4_key, 0, sizeof(rc4_key));	memset(k6_data, 0, sizeof(k6_data));    }    _gsskrb5_decode_be_om_uint32(SND_SEQ, &seq_number);    if (context_handle->more_flags & LOCAL)	cmp = memcmp(&SND_SEQ[4], "\xff\xff\xff\xff", 4);    else	cmp = memcmp(&SND_SEQ[4], "\x00\x00\x00\x00", 4);    if (cmp != 0) {	*minor_status = 0;	return GSS_S_BAD_MIC;    }    {	int i;	Klocal.keytype = key->keytype;	Klocal.keyvalue.data = Klocaldata;	Klocal.keyvalue.length = sizeof(Klocaldata);	for (i = 0; i < 16; i++)	    Klocaldata[i] = ((u_char *)key->keyvalue.data)[i] ^ 0xF0;    }    ret = arcfour_mic_key(context, &Klocal,			  SND_SEQ, 4,			  k6_data, sizeof(k6_data));    memset(Klocaldata, 0, sizeof(Klocaldata));    if (ret) {	*minor_status = ret;	return GSS_S_FAILURE;    }    output_message_buffer->value = malloc(datalen);    if (output_message_buffer->value == NULL) {	*minor_status = ENOMEM;	return GSS_S_FAILURE;    }    output_message_buffer->length = datalen;    if(conf_flag) {	RC4_KEY rc4_key;	RC4_set_key (&rc4_key, sizeof(k6_data), k6_data);	RC4 (&rc4_key, 8, p0 + 24, Confounder); /* Confounder */	RC4 (&rc4_key, datalen, p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE,	     output_message_buffer->value);	memset(&rc4_key, 0, sizeof(rc4_key));    } else {	memcpy(Confounder, p0 + 24, 8); /* Confounder */	memcpy(output_message_buffer->value, 	       p0 + GSS_ARCFOUR_WRAP_TOKEN_SIZE,	       datalen);    }    memset(k6_data, 0, sizeof(k6_data));    if (!IS_DCE_STYLE(context_handle)) {	ret = _gssapi_verify_pad(output_message_buffer, datalen, &padlen);	if (ret) {	    _gsskrb5_release_buffer(minor_status, output_message_buffer);	    *minor_status = 0;	    return ret;	}	output_message_buffer->length -= padlen;    }    ret = arcfour_mic_cksum(context,			    key, KRB5_KU_USAGE_SEAL,			    cksum_data, sizeof(cksum_data),			    p0, 8, 			    Confounder, sizeof(Confounder),			    output_message_buffer->value, 			    output_message_buffer->length + padlen);    if (ret) {	_gsskrb5_release_buffer(minor_status, output_message_buffer);	*minor_status = ret;	return GSS_S_FAILURE;    }    cmp = memcmp(cksum_data, p0 + 16, 8); /* SGN_CKSUM */    if (cmp) {	_gsskrb5_release_buffer(minor_status, output_message_buffer);	*minor_status = 0;	return GSS_S_BAD_MIC;    }    HEIMDAL_MUTEX_lock(&context_handle->ctx_id_mutex);    omret = _gssapi_msg_order_check(context_handle->order, seq_number);    HEIMDAL_MUTEX_unlock(&context_handle->ctx_id_mutex);    if (omret)	return omret;    if (conf_state)	*conf_state = conf_flag;    *minor_status = 0;    return GSS_S_COMPLETE;}static OM_uint32max_wrap_length_arcfour(const gsskrb5_ctx ctx,			krb5_crypto crypto,			size_t input_length,			OM_uint32 *max_input_size){    /*      * if GSS_C_DCE_STYLE is in use:     *  - we only need to encapsulate the WRAP token     * However, since this is a fixed since, we just      */    if (IS_DCE_STYLE(ctx)) {	size_t len, total_len;	len = GSS_ARCFOUR_WRAP_TOKEN_SIZE;	_gssapi_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);	if (input_length < len)	    *max_input_size = 0;	else	    *max_input_size = input_length - len;    } else {	size_t extrasize = GSS_ARCFOUR_WRAP_TOKEN_SIZE;	size_t blocksize = 8;	size_t len, total_len;	len = 8 + input_length + blocksize + extrasize;	_gsskrb5_encap_length(len, &len, &total_len, GSS_KRB5_MECHANISM);	total_len -= input_length; /* token length */	if (total_len < input_length) {	    *max_input_size = (input_length - total_len);	    (*max_input_size) &= (~(OM_uint32)(blocksize - 1));	} else {	    *max_input_size = 0;	}    }    return GSS_S_COMPLETE;}OM_uint32_gssapi_wrap_size_arcfour(OM_uint32 *minor_status,			  const gsskrb5_ctx ctx,			  krb5_context context,			  int conf_req_flag,			  gss_qop_t qop_req,			  OM_uint32 req_output_size,			  OM_uint32 *max_input_size,			  krb5_keyblock *key){    krb5_error_code ret;    krb5_crypto crypto;    ret = krb5_crypto_init(context, key, 0, &crypto);    if (ret != 0) {	*minor_status = ret;	return GSS_S_FAILURE;    }    ret = max_wrap_length_arcfour(ctx, crypto,				  req_output_size, max_input_size);    if (ret != 0) {	*minor_status = ret;	krb5_crypto_destroy(context, crypto);	return GSS_S_FAILURE;    }    krb5_crypto_destroy(context, crypto);    return GSS_S_COMPLETE;}

⌨️ 快捷键说明

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