1ab9b2e15Sgtb /*
2ab9b2e15Sgtb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3ab9b2e15Sgtb  * Use is subject to license terms.
4ab9b2e15Sgtb  */
5ab9b2e15Sgtb 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
8*1da57d55SToomas Soome  *
97c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
107c478bd9Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
127c478bd9Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
137c478bd9Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
147c478bd9Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
157c478bd9Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
167c478bd9Sstevel@tonic-gate  * representations about the suitability of this software for any
177c478bd9Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
18*1da57d55SToomas Soome  *
197c478bd9Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
207c478bd9Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
217c478bd9Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
227c478bd9Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
237c478bd9Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
247c478bd9Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
257c478bd9Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28ab9b2e15Sgtb #include "gssapiP_krb5.h"
29ab9b2e15Sgtb #ifdef HAVE_MEMORY_H
307c478bd9Sstevel@tonic-gate #include <memory.h>
31ab9b2e15Sgtb #endif
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /* Checksumming the channel bindings always uses plain MD5.  */
347c478bd9Sstevel@tonic-gate krb5_error_code
kg_checksum_channel_bindings(context,cb,cksum,bigend)357c478bd9Sstevel@tonic-gate kg_checksum_channel_bindings(context, cb, cksum, bigend)
367c478bd9Sstevel@tonic-gate      krb5_context context;
377c478bd9Sstevel@tonic-gate      gss_channel_bindings_t cb;
387c478bd9Sstevel@tonic-gate      krb5_checksum *cksum;
397c478bd9Sstevel@tonic-gate      int bigend;
407c478bd9Sstevel@tonic-gate {
41ab9b2e15Sgtb    size_t len;
42ab9b2e15Sgtb    char *buf = 0;
43ab9b2e15Sgtb    char *ptr;
447c478bd9Sstevel@tonic-gate    size_t sumlen;
457c478bd9Sstevel@tonic-gate    krb5_data plaind;
467c478bd9Sstevel@tonic-gate    krb5_error_code code;
47ab9b2e15Sgtb    void *temp;
487c478bd9Sstevel@tonic-gate 
49ab9b2e15Sgtb    /* initialize the the cksum */
50ab9b2e15Sgtb    code = krb5_c_checksum_length(context, CKSUMTYPE_RSA_MD5, &sumlen);
51ab9b2e15Sgtb    if (code)
527c478bd9Sstevel@tonic-gate        return(code);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate    cksum->checksum_type = CKSUMTYPE_RSA_MD5;
557c478bd9Sstevel@tonic-gate    cksum->length = sumlen;
56*1da57d55SToomas Soome 
577c478bd9Sstevel@tonic-gate    /* generate a buffer full of zeros if no cb specified */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate    if (cb == GSS_C_NO_CHANNEL_BINDINGS) {
607c478bd9Sstevel@tonic-gate        if ((cksum->contents = (krb5_octet *) xmalloc(cksum->length)) == NULL) {
617c478bd9Sstevel@tonic-gate 	   return(ENOMEM);
627c478bd9Sstevel@tonic-gate        }
637c478bd9Sstevel@tonic-gate        memset(cksum->contents, '\0', cksum->length);
647c478bd9Sstevel@tonic-gate        return(0);
657c478bd9Sstevel@tonic-gate    }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate    /* create the buffer to checksum into */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate    len = (sizeof(krb5_int32)*5+
707c478bd9Sstevel@tonic-gate 	  cb->initiator_address.length+
717c478bd9Sstevel@tonic-gate 	  cb->acceptor_address.length+
727c478bd9Sstevel@tonic-gate 	  cb->application_data.length);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate    if ((buf = (char *) xmalloc(len)) == NULL)
757c478bd9Sstevel@tonic-gate       return(ENOMEM);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate    /* helper macros.  This code currently depends on a long being 32
787c478bd9Sstevel@tonic-gate       bits, and htonl dtrt. */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate    ptr = buf;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate    TWRITE_INT(ptr, cb->initiator_addrtype, bigend);
837c478bd9Sstevel@tonic-gate    TWRITE_BUF(ptr, cb->initiator_address, bigend);
847c478bd9Sstevel@tonic-gate    TWRITE_INT(ptr, cb->acceptor_addrtype, bigend);
857c478bd9Sstevel@tonic-gate    TWRITE_BUF(ptr, cb->acceptor_address, bigend);
867c478bd9Sstevel@tonic-gate    TWRITE_BUF(ptr, cb->application_data, bigend);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate    /* checksum the data */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate    plaind.length = len;
917c478bd9Sstevel@tonic-gate    plaind.data = buf;
927c478bd9Sstevel@tonic-gate 
93ab9b2e15Sgtb #if 0
94ab9b2e15Sgtb    /*
95ab9b2e15Sgtb     * SUNW15resync
96ab9b2e15Sgtb     * MIT 1.5-6 seems/is wrong here in 2 ways
97ab9b2e15Sgtb     *   - why free then alloc contents again?
98ab9b2e15Sgtb     *   - calling krb5_free_checksum_contents results in cksum->length
99ab9b2e15Sgtb     *     getting set to 0 which causes ftp to fail
100ab9b2e15Sgtb     * so lets stick w/oldey-but-goodey code.
101ab9b2e15Sgtb     */
102ab9b2e15Sgtb    code = krb5_c_make_checksum(context, CKSUMTYPE_RSA_MD5, 0, 0,
103ab9b2e15Sgtb 			       &plaind, cksum);
104ab9b2e15Sgtb    if (code)
105ab9b2e15Sgtb        goto cleanup;
106ab9b2e15Sgtb 
107ab9b2e15Sgtb    if ((temp = xmalloc(cksum->length)) == NULL) {
108ab9b2e15Sgtb        krb5_free_checksum_contents(context, cksum);
109ab9b2e15Sgtb        code = ENOMEM;
110ab9b2e15Sgtb        goto cleanup;
1117c478bd9Sstevel@tonic-gate    }
1127c478bd9Sstevel@tonic-gate 
113ab9b2e15Sgtb    memcpy(temp, cksum->contents, cksum->length);
114ab9b2e15Sgtb    krb5_free_checksum_contents(context, cksum);
115ab9b2e15Sgtb    cksum->contents = (krb5_octet *)temp;
116ab9b2e15Sgtb    /* SUNW15resync - need to reset cksum->length here */
1177c478bd9Sstevel@tonic-gate 
118ab9b2e15Sgtb    /* success */
119ab9b2e15Sgtb  cleanup:
120ab9b2e15Sgtb    if (buf)
121ab9b2e15Sgtb        xfree(buf);
122ab9b2e15Sgtb #endif /* 0 */
123ab9b2e15Sgtb 
124*1da57d55SToomas Soome    if (code = krb5_c_make_checksum(context, CKSUMTYPE_RSA_MD5, 0, 0,
125*1da57d55SToomas Soome                                    &plaind, cksum)) {
126ab9b2e15Sgtb       xfree(cksum->contents); /* SUNW15resync -just in case not already free */
127*1da57d55SToomas Soome       xfree(buf);
128*1da57d55SToomas Soome       return(code);
129*1da57d55SToomas Soome    }
130*1da57d55SToomas Soome 
131*1da57d55SToomas Soome    /* success */
132*1da57d55SToomas Soome 
133*1da57d55SToomas Soome    xfree(buf);
134ab9b2e15Sgtb    return code;
1357c478bd9Sstevel@tonic-gate }
136