xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/krb5/krb/sendauth.c (revision 92c82aaee9430c329e5095550e89559ba0dd9fee)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  * Copyright (c) 2016 by Delphix. All rights reserved.
5  */
6 
7 
8 /*
9  * lib/krb5/krb/sendauth.c
10  *
11  * Copyright 1991 by the Massachusetts Institute of Technology.
12  * All Rights Reserved.
13  *
14  * Export of this software from the United States of America may
15  *   require a specific license from the United States Government.
16  *   It is the responsibility of any person or organization contemplating
17  *   export to obtain such a license before exporting.
18  *
19  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20  * distribute this software and its documentation for any purpose and
21  * without fee is hereby granted, provided that the above copyright
22  * notice appear in all copies and that both that copyright notice and
23  * this permission notice appear in supporting documentation, and that
24  * the name of M.I.T. not be used in advertising or publicity pertaining
25  * to distribution of the software without specific, written prior
26  * permission.  Furthermore if you modify this software you must label
27  * your software as modified software and not distribute it in such a
28  * fashion that it might be confused with the original M.I.T. software.
29  * M.I.T. makes no representations about the suitability of
30  * this software for any purpose.  It is provided "as is" without express
31  * or implied warranty.
32  *
33  *
34  * convenience sendauth/recvauth functions
35  */
36 
37 
38 #include "k5-int.h"
39 #include "com_err.h"
40 #include "auth_con.h"
41 #include <errno.h>
42 #include <stdio.h>
43 #include <string.h>
44 
45 static const char sendauth_version[] = "KRB5_SENDAUTH_V1.0";
46 
47 krb5_error_code KRB5_CALLCONV
48 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)
49 {
50 	krb5_octet		result;
51 	krb5_creds 		creds;
52 	krb5_creds		 * credsp = NULL;
53 	krb5_creds		 * credspout = NULL;
54 	krb5_error_code		retval = 0;
55 	krb5_data		inbuf, outbuf;
56 	int			len;
57 	krb5_ccache		use_ccache = 0;
58 
59 	if (error)
60 	    *error = 0;
61 
62 	/*
63 	 * First, send over the length of the sendauth version string;
64 	 * then, we send over the sendauth version.  Next, we send
65 	 * over the length of the application version strings followed
66 	 * by the string itself.
67 	 */
68 	outbuf.length = strlen(sendauth_version) + 1;
69 	outbuf.data = (char *) sendauth_version;
70 	if ((retval = krb5_write_message(context, fd, &outbuf)))
71 		return(retval);
72 	outbuf.length = strlen(appl_version) + 1;
73 	outbuf.data = appl_version;
74 	if ((retval = krb5_write_message(context, fd, &outbuf)))
75 		return(retval);
76 	/*
77 	 * Now, read back a byte: 0 means no error, 1 means bad sendauth
78 	 * version, 2 means bad application version
79 	 */
80     if ((len = krb5_net_read(context, *((int *) fd), (char *)&result, 1)) != 1)
81 	return((len < 0) ? errno : ECONNABORTED);
82     if (result == 1)
83 	return(KRB5_SENDAUTH_BADAUTHVERS);
84     else if (result == 2)
85 	return(KRB5_SENDAUTH_BADAPPLVERS);
86     else if (result != 0)
87 	return(KRB5_SENDAUTH_BADRESPONSE);
88 	/*
89 	 * We're finished with the initial negotiations; let's get and
90 	 * send over the authentication header.  (The AP_REQ message)
91 	 */
92 
93 	/*
94 	 * If no credentials were provided, try getting it from the
95 	 * credentials cache.
96 	 */
97 	memset((char *)&creds, 0, sizeof(creds));
98 
99 	/*
100 	 * See if we need to access the credentials cache
101 	 */
102 	if (!in_creds || !in_creds->ticket.length) {
103 		if (ccache)
104 			use_ccache = ccache;
105 		/* Solaris Kerberos */
106 		else if ((retval = krb5int_cc_default(context, &use_ccache)) != 0)
107 			goto error_return;
108 	}
109 	if (!in_creds) {
110 		if ((retval = krb5_copy_principal(context, server,
111 						  &creds.server)))
112 			goto error_return;
113 		if (client)
114 			retval = krb5_copy_principal(context, client,
115 						     &creds.client);
116 		else
117 			retval = krb5_cc_get_principal(context, use_ccache,
118 						       &creds.client);
119 		if (retval) {
120 			krb5_free_principal(context, creds.server);
121 			goto error_return;
122 		}
123 		/* creds.times.endtime = 0; -- memset 0 takes care of this
124 					zero means "as long as possible" */
125 		/* creds.keyblock.enctype = 0; -- as well as this.
126 					zero means no session enctype
127 					preference */
128 		in_creds = &creds;
129 	}
130 	if (!in_creds->ticket.length) {
131 		/* Solaris Kerberos */
132 	    if ((retval = krb5_get_credentials(context, 0,
133 					       use_ccache, in_creds, &credsp)) != 0)
134 		    goto error_return;
135 	    credspout = credsp;
136 	} else {
137 	    credsp = in_creds;
138 	}
139 
140 	if (ap_req_options & AP_OPTS_USE_SUBKEY) {
141 	    /* Provide some more fodder for random number code.
142 	       This isn't strong cryptographically; the point here is
143 	       not to guarantee randomness, but to make it less likely
144 	       that multiple sessions could pick the same subkey.  */
145 	    char rnd_data[1024];
146 	    GETPEERNAME_ARG3_TYPE len2;
147 	    krb5_data d;
148 	    d.length = sizeof (rnd_data);
149 	    d.data = rnd_data;
150 	    len2 = sizeof (rnd_data);
151 	    if (getpeername (*(int*)fd, (GETPEERNAME_ARG2_TYPE *) rnd_data,
152 			     &len2) == 0) {
153 		d.length = len2;
154 		/* Solaris Kerberos */
155 		(void) krb5_c_random_seed (context, &d);
156 	    }
157 	    len2 = sizeof (rnd_data);
158 	    if (getsockname (*(int*)fd, (GETSOCKNAME_ARG2_TYPE *) rnd_data,
159 			     &len2) == 0) {
160 		d.length = len2;
161 		/* Solaris Kerberos */
162 		(void) krb5_c_random_seed (context, &d);
163 	    }
164 	}
165 
166 	/* Solaris Kerberos */
167 	if ((retval = krb5_mk_req_extended(context, auth_context,
168 					   ap_req_options, in_data, credsp,
169 					   &outbuf)) != 0)
170 	    goto error_return;
171 
172 	/*
173 	 * First write the length of the AP_REQ message, then write
174 	 * the message itself.
175 	 */
176 	retval = krb5_write_message(context, fd, &outbuf);
177 	free(outbuf.data);
178 	if (retval)
179 	    goto error_return;
180 
181 	/*
182 	 * Now, read back a message.  If it was a null message (the
183 	 * length was zero) then there was no error.  If not, we the
184 	 * authentication was rejected, and we need to return the
185 	 * error structure.
186 	 */
187 	/* Solaris Kerberos */
188 	if ((retval = krb5_read_message(context, fd, &inbuf)) != 0)
189 	    goto error_return;
190 
191 	if (inbuf.length) {
192 		if (error) {
193 		    /* Solaris Kerberos */
194 		    if ((retval = krb5_rd_error(context, &inbuf, error)) != 0) {
195 			krb5_xfree(inbuf.data);
196 			goto error_return;
197 		    }
198 		}
199 		retval = KRB5_SENDAUTH_REJECTED;
200 		krb5_xfree(inbuf.data);
201 		goto error_return;
202 	}
203 
204 	/*
205 	 * If we asked for mutual authentication, we should now get a
206 	 * length field, followed by a AP_REP message
207 	 */
208 	if ((ap_req_options & AP_OPTS_MUTUAL_REQUIRED)) {
209 	    krb5_ap_rep_enc_part	*repl = 0;
210 	    /* Solaris Kerberos */
211 	    if ((retval = krb5_read_message(context, fd, &inbuf)) != 0)
212 		goto error_return;
213 
214 	    /* Solaris Kerberos */
215 	    if ((retval = krb5_rd_rep(context, *auth_context, &inbuf,
216 				      &repl)) != 0) {
217 		if (repl)
218 		    krb5_free_ap_rep_enc_part(context, repl);
219 	        krb5_xfree(inbuf.data);
220 		goto error_return;
221 	    }
222 
223 	    krb5_xfree(inbuf.data);
224 	    /*
225 	     * If the user wants to look at the AP_REP message,
226 	     * copy it for them.
227 	     */
228 	    if (rep_result)
229 		*rep_result = repl;
230 	    else
231 		krb5_free_ap_rep_enc_part(context, repl);
232 	}
233 	retval = 0;		/* Normal return */
234 	if (out_creds) {
235 	    *out_creds = credsp;
236 	    credspout = NULL;
237 	}
238 
239 error_return:
240     krb5_free_cred_contents(context, &creds);
241     if (credspout != NULL)
242 	krb5_free_creds(context, credspout);
243     /* Solaris Kerberos */
244     if (!ccache && use_ccache)
245 	(void) krb5_cc_close(context, use_ccache);
246     return(retval);
247 }
248 
249 
250