Windows global stuff:
[krb5.git] / src / lib / gssapi / krb5 / util_cksum.c
1 /*
2  * Copyright 1993 by OpenVision Technologies, Inc.
3  * 
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without fee,
6  * provided that the above copyright notice appears in all copies and
7  * that both that copyright notice and this permission notice appear in
8  * supporting documentation, and that the name of OpenVision not be used
9  * in advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission. OpenVision makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  * 
14  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20  * PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include "gssapiP_krb5.h"
24 #include <memory.h>
25
26 krb5_error_code
27 kg_checksum_channel_bindings(cb, cksum, bigend)
28      gss_channel_bindings_t cb;
29      krb5_checksum *cksum;
30      int bigend;
31 {
32    int len;
33    char *buf, *ptr;
34    krb5_error_code code;
35
36    if (!kg_context && (code=kg_get_context()))
37            return code;
38    
39    /* generate a buffer full of zeros if no cb specified */
40
41    if (cb == GSS_C_NO_CHANNEL_BINDINGS) {
42       /* allocate the cksum contents buffer */
43       if ((cksum->contents = (krb5_octet *)
44            xmalloc(krb5_checksum_size(context, CKSUMTYPE_RSA_MD5))) == NULL)
45          return(ENOMEM);
46
47       cksum->checksum_type = CKSUMTYPE_RSA_MD5;
48       memset(cksum->contents, '\0',
49              (cksum->length = krb5_checksum_size(kg_context, CKSUMTYPE_RSA_MD5)));
50       return(0);
51    }
52
53    /* create the buffer to checksum into */
54
55    len = (sizeof(long)*5+
56           cb->initiator_address.length+
57           cb->acceptor_address.length+
58           cb->application_data.length);
59
60    if ((buf = (char *) xmalloc(len)) == NULL)
61       return(ENOMEM);
62
63    /* allocate the cksum contents buffer */
64    if ((cksum->contents = (krb5_octet *)
65         xmalloc(krb5_checksum_size(context, CKSUMTYPE_RSA_MD5))) == NULL) {
66       free(buf);
67       return(ENOMEM);
68    }
69
70    /* helper macros.  This code currently depends on a long being 32
71       bits, and htonl dtrt. */
72
73    ptr = buf;
74
75    TWRITE_INT(ptr, cb->initiator_addrtype, bigend);
76    TWRITE_BUF(ptr, cb->initiator_address, bigend);
77    TWRITE_INT(ptr, cb->acceptor_addrtype, bigend);
78    TWRITE_BUF(ptr, cb->acceptor_address, bigend);
79    TWRITE_BUF(ptr, cb->application_data, bigend);
80
81    /* checksum the data */
82
83    if (code = krb5_calculate_checksum(kg_context, CKSUMTYPE_RSA_MD5, 
84                                       buf, len, NULL, 0, cksum)) {
85       xfree(cksum->contents);
86       xfree(buf);
87       return(code);
88    }
89
90    /* success */
91
92    xfree(buf);
93    return(0);
94 }