17c478bd9Sstevel@tonic-gate /*
2159d09a2SMark Phalan  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
448bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * lib/krb5/krb/sendauth.c
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Copyright 1991 by the Massachusetts Institute of Technology.
127c478bd9Sstevel@tonic-gate  * All Rights Reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
157c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
167c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
177c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
1848bbca81SDaniel Hoffman  *
197c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
207c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
217c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
227c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
237c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
247c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
257c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
267c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
277c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
287c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
297c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
307c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
317c478bd9Sstevel@tonic-gate  * or implied warranty.
3248bbca81SDaniel Hoffman  *
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * convenience sendauth/recvauth functions
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 
38159d09a2SMark Phalan #include "k5-int.h"
39159d09a2SMark Phalan #include "com_err.h"
40159d09a2SMark Phalan #include "auth_con.h"
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate 
45505d05c7Sgtb static const char sendauth_version[] = "KRB5_SENDAUTH_V1.0";
467c478bd9Sstevel@tonic-gate 
47505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_sendauth(krb5_context context,krb5_auth_context * auth_context,krb5_pointer fd,char * appl_version,krb5_principal client,krb5_principal server,krb5_flags ap_req_options,krb5_data * in_data,krb5_creds * in_creds,krb5_ccache ccache,krb5_error ** error,krb5_ap_rep_enc_part ** rep_result,krb5_creds ** out_creds)48505d05c7Sgtb krb5_sendauth(krb5_context context, krb5_auth_context *auth_context, krb5_pointer fd, char *appl_version, krb5_principal client, krb5_principal server, krb5_flags ap_req_options, krb5_data *in_data, krb5_creds *in_creds, krb5_ccache ccache, krb5_error **error, krb5_ap_rep_enc_part **rep_result, krb5_creds **out_creds)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	krb5_octet		result;
517c478bd9Sstevel@tonic-gate 	krb5_creds 		creds;
52159d09a2SMark Phalan 	krb5_creds		 * credsp = NULL;
53159d09a2SMark Phalan 	krb5_creds		 * credspout = NULL;
547c478bd9Sstevel@tonic-gate 	krb5_error_code		retval = 0;
557c478bd9Sstevel@tonic-gate 	krb5_data		inbuf, outbuf;
567c478bd9Sstevel@tonic-gate 	int			len;
577c478bd9Sstevel@tonic-gate 	krb5_ccache		use_ccache = 0;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	if (error)
607c478bd9Sstevel@tonic-gate 	    *error = 0;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/*
637c478bd9Sstevel@tonic-gate 	 * First, send over the length of the sendauth version string;
647c478bd9Sstevel@tonic-gate 	 * then, we send over the sendauth version.  Next, we send
657c478bd9Sstevel@tonic-gate 	 * over the length of the application version strings followed
6648bbca81SDaniel Hoffman 	 * by the string itself.
677c478bd9Sstevel@tonic-gate 	 */
687c478bd9Sstevel@tonic-gate 	outbuf.length = strlen(sendauth_version) + 1;
69505d05c7Sgtb 	outbuf.data = (char *) sendauth_version;
707c478bd9Sstevel@tonic-gate 	if ((retval = krb5_write_message(context, fd, &outbuf)))
717c478bd9Sstevel@tonic-gate 		return(retval);
727c478bd9Sstevel@tonic-gate 	outbuf.length = strlen(appl_version) + 1;
737c478bd9Sstevel@tonic-gate 	outbuf.data = appl_version;
747c478bd9Sstevel@tonic-gate 	if ((retval = krb5_write_message(context, fd, &outbuf)))
757c478bd9Sstevel@tonic-gate 		return(retval);
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * Now, read back a byte: 0 means no error, 1 means bad sendauth
787c478bd9Sstevel@tonic-gate 	 * version, 2 means bad application version
797c478bd9Sstevel@tonic-gate 	 */
80*49bf4c2bSToomas Soome 	len = krb5_net_read(context, *((int *) fd), (char *)&result, 1);
81*49bf4c2bSToomas Soome 	if (len != 1)
82*49bf4c2bSToomas Soome 		return((len < 0) ? errno : ECONNABORTED);
83*49bf4c2bSToomas Soome 	if (result == 1)
84*49bf4c2bSToomas Soome 		return(KRB5_SENDAUTH_BADAUTHVERS);
85*49bf4c2bSToomas Soome 	else if (result == 2)
86*49bf4c2bSToomas Soome 		return(KRB5_SENDAUTH_BADAPPLVERS);
87*49bf4c2bSToomas Soome 	else if (result != 0)
88*49bf4c2bSToomas Soome 		return(KRB5_SENDAUTH_BADRESPONSE);
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * We're finished with the initial negotiations; let's get and
917c478bd9Sstevel@tonic-gate 	 * send over the authentication header.  (The AP_REQ message)
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/*
957c478bd9Sstevel@tonic-gate 	 * If no credentials were provided, try getting it from the
967c478bd9Sstevel@tonic-gate 	 * credentials cache.
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 	memset((char *)&creds, 0, sizeof(creds));
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * See if we need to access the credentials cache
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 	if (!in_creds || !in_creds->ticket.length) {
1047c478bd9Sstevel@tonic-gate 		if (ccache)
1057c478bd9Sstevel@tonic-gate 			use_ccache = ccache;
106159d09a2SMark Phalan 		/* Solaris Kerberos */
1077c478bd9Sstevel@tonic-gate 		else if ((retval = krb5int_cc_default(context, &use_ccache)) != 0)
1087c478bd9Sstevel@tonic-gate 			goto error_return;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	if (!in_creds) {
1117c478bd9Sstevel@tonic-gate 		if ((retval = krb5_copy_principal(context, server,
1127c478bd9Sstevel@tonic-gate 						  &creds.server)))
1137c478bd9Sstevel@tonic-gate 			goto error_return;
1147c478bd9Sstevel@tonic-gate 		if (client)
11548bbca81SDaniel Hoffman 			retval = krb5_copy_principal(context, client,
1167c478bd9Sstevel@tonic-gate 						     &creds.client);
1177c478bd9Sstevel@tonic-gate 		else
1187c478bd9Sstevel@tonic-gate 			retval = krb5_cc_get_principal(context, use_ccache,
1197c478bd9Sstevel@tonic-gate 						       &creds.client);
1207c478bd9Sstevel@tonic-gate 		if (retval) {
1217c478bd9Sstevel@tonic-gate 			krb5_free_principal(context, creds.server);
1227c478bd9Sstevel@tonic-gate 			goto error_return;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 		/* creds.times.endtime = 0; -- memset 0 takes care of this
1257c478bd9Sstevel@tonic-gate 					zero means "as long as possible" */
1267c478bd9Sstevel@tonic-gate 		/* creds.keyblock.enctype = 0; -- as well as this.
1277c478bd9Sstevel@tonic-gate 					zero means no session enctype
1287c478bd9Sstevel@tonic-gate 					preference */
1297c478bd9Sstevel@tonic-gate 		in_creds = &creds;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	if (!in_creds->ticket.length) {
132159d09a2SMark Phalan 		/* Solaris Kerberos */
1337c478bd9Sstevel@tonic-gate 	    if ((retval = krb5_get_credentials(context, 0,
1347c478bd9Sstevel@tonic-gate 					       use_ccache, in_creds, &credsp)) != 0)
1357c478bd9Sstevel@tonic-gate 		    goto error_return;
1367c478bd9Sstevel@tonic-gate 	    credspout = credsp;
1377c478bd9Sstevel@tonic-gate 	} else {
1387c478bd9Sstevel@tonic-gate 	    credsp = in_creds;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (ap_req_options & AP_OPTS_USE_SUBKEY) {
1427c478bd9Sstevel@tonic-gate 	    /* Provide some more fodder for random number code.
1437c478bd9Sstevel@tonic-gate 	       This isn't strong cryptographically; the point here is
1447c478bd9Sstevel@tonic-gate 	       not to guarantee randomness, but to make it less likely
1457c478bd9Sstevel@tonic-gate 	       that multiple sessions could pick the same subkey.  */
1467c478bd9Sstevel@tonic-gate 	    char rnd_data[1024];
147159d09a2SMark Phalan 	    GETPEERNAME_ARG3_TYPE len2;
1487c478bd9Sstevel@tonic-gate 	    krb5_data d;
1497c478bd9Sstevel@tonic-gate 	    d.length = sizeof (rnd_data);
1507c478bd9Sstevel@tonic-gate 	    d.data = rnd_data;
151159d09a2SMark Phalan 	    len2 = sizeof (rnd_data);
15248bbca81SDaniel Hoffman 	    if (getpeername (*(int*)fd, (GETPEERNAME_ARG2_TYPE *) rnd_data,
153159d09a2SMark Phalan 			     &len2) == 0) {
154159d09a2SMark Phalan 		d.length = len2;
155159d09a2SMark Phalan 		/* Solaris Kerberos */
1567c478bd9Sstevel@tonic-gate 		(void) krb5_c_random_seed (context, &d);
1577c478bd9Sstevel@tonic-gate 	    }
158159d09a2SMark Phalan 	    len2 = sizeof (rnd_data);
15948bbca81SDaniel Hoffman 	    if (getsockname (*(int*)fd, (GETSOCKNAME_ARG2_TYPE *) rnd_data,
160159d09a2SMark Phalan 			     &len2) == 0) {
161159d09a2SMark Phalan 		d.length = len2;
162159d09a2SMark Phalan 		/* Solaris Kerberos */
1637c478bd9Sstevel@tonic-gate 		(void) krb5_c_random_seed (context, &d);
1647c478bd9Sstevel@tonic-gate 	    }
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
167159d09a2SMark Phalan 	/* Solaris Kerberos */
1687c478bd9Sstevel@tonic-gate 	if ((retval = krb5_mk_req_extended(context, auth_context,
1697c478bd9Sstevel@tonic-gate 					   ap_req_options, in_data, credsp,
1707c478bd9Sstevel@tonic-gate 					   &outbuf)) != 0)
1717c478bd9Sstevel@tonic-gate 	    goto error_return;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * First write the length of the AP_REQ message, then write
1757c478bd9Sstevel@tonic-gate 	 * the message itself.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	retval = krb5_write_message(context, fd, &outbuf);
1787c478bd9Sstevel@tonic-gate 	free(outbuf.data);
1797c478bd9Sstevel@tonic-gate 	if (retval)
1807c478bd9Sstevel@tonic-gate 	    goto error_return;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * Now, read back a message.  If it was a null message (the
1847c478bd9Sstevel@tonic-gate 	 * length was zero) then there was no error.  If not, we the
1857c478bd9Sstevel@tonic-gate 	 * authentication was rejected, and we need to return the
1867c478bd9Sstevel@tonic-gate 	 * error structure.
1877c478bd9Sstevel@tonic-gate 	 */
188159d09a2SMark Phalan 	/* Solaris Kerberos */
1897c478bd9Sstevel@tonic-gate 	if ((retval = krb5_read_message(context, fd, &inbuf)) != 0)
1907c478bd9Sstevel@tonic-gate 	    goto error_return;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (inbuf.length) {
1937c478bd9Sstevel@tonic-gate 		if (error) {
194159d09a2SMark Phalan 		    /* Solaris Kerberos */
1957c478bd9Sstevel@tonic-gate 		    if ((retval = krb5_rd_error(context, &inbuf, error)) != 0) {
1967c478bd9Sstevel@tonic-gate 			krb5_xfree(inbuf.data);
1977c478bd9Sstevel@tonic-gate 			goto error_return;
1987c478bd9Sstevel@tonic-gate 		    }
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		retval = KRB5_SENDAUTH_REJECTED;
2017c478bd9Sstevel@tonic-gate 		krb5_xfree(inbuf.data);
2027c478bd9Sstevel@tonic-gate 		goto error_return;
2037c478bd9Sstevel@tonic-gate 	}
20448bbca81SDaniel Hoffman 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * If we asked for mutual authentication, we should now get a
2077c478bd9Sstevel@tonic-gate 	 * length field, followed by a AP_REP message
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	if ((ap_req_options & AP_OPTS_MUTUAL_REQUIRED)) {
2107c478bd9Sstevel@tonic-gate 	    krb5_ap_rep_enc_part	*repl = 0;
211159d09a2SMark Phalan 	    /* Solaris Kerberos */
2127c478bd9Sstevel@tonic-gate 	    if ((retval = krb5_read_message(context, fd, &inbuf)) != 0)
2137c478bd9Sstevel@tonic-gate 		goto error_return;
2147c478bd9Sstevel@tonic-gate 
215159d09a2SMark Phalan 	    /* Solaris Kerberos */
2167c478bd9Sstevel@tonic-gate 	    if ((retval = krb5_rd_rep(context, *auth_context, &inbuf,
2177c478bd9Sstevel@tonic-gate 				      &repl)) != 0) {
2187c478bd9Sstevel@tonic-gate 		if (repl)
2197c478bd9Sstevel@tonic-gate 		    krb5_free_ap_rep_enc_part(context, repl);
2207c478bd9Sstevel@tonic-gate 	        krb5_xfree(inbuf.data);
2217c478bd9Sstevel@tonic-gate 		goto error_return;
2227c478bd9Sstevel@tonic-gate 	    }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	    krb5_xfree(inbuf.data);
2257c478bd9Sstevel@tonic-gate 	    /*
2267c478bd9Sstevel@tonic-gate 	     * If the user wants to look at the AP_REP message,
22748bbca81SDaniel Hoffman 	     * copy it for them.
2287c478bd9Sstevel@tonic-gate 	     */
22948bbca81SDaniel Hoffman 	    if (rep_result)
2307c478bd9Sstevel@tonic-gate 		*rep_result = repl;
2317c478bd9Sstevel@tonic-gate 	    else
2327c478bd9Sstevel@tonic-gate 		krb5_free_ap_rep_enc_part(context, repl);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	retval = 0;		/* Normal return */
2357c478bd9Sstevel@tonic-gate 	if (out_creds) {
2367c478bd9Sstevel@tonic-gate 	    *out_creds = credsp;
2377c478bd9Sstevel@tonic-gate 	    credspout = NULL;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate error_return:
2417c478bd9Sstevel@tonic-gate     krb5_free_cred_contents(context, &creds);
2427c478bd9Sstevel@tonic-gate     if (credspout != NULL)
24348bbca81SDaniel Hoffman 	krb5_free_creds(context, credspout);
244159d09a2SMark Phalan     /* Solaris Kerberos */
2457c478bd9Sstevel@tonic-gate     if (!ccache && use_ccache)
2467c478bd9Sstevel@tonic-gate 	(void) krb5_cc_close(context, use_ccache);
2477c478bd9Sstevel@tonic-gate     return(retval);
2487c478bd9Sstevel@tonic-gate }
249159d09a2SMark Phalan 
250159d09a2SMark Phalan 
251