10Sstevel@tonic-gate /*
2*7934SMark.Phalan@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate * lib/krb5/krb/sendauth.c
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * Copyright 1991 by the Massachusetts Institute of Technology.
110Sstevel@tonic-gate * All Rights Reserved.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * Export of this software from the United States of America may
140Sstevel@tonic-gate * require a specific license from the United States Government.
150Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
160Sstevel@tonic-gate * export to obtain such a license before exporting.
17*7934SMark.Phalan@Sun.COM *
180Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
190Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
200Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
210Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
220Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
230Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
240Sstevel@tonic-gate * to distribution of the software without specific, written prior
250Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
260Sstevel@tonic-gate * your software as modified software and not distribute it in such a
270Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
280Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
290Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
300Sstevel@tonic-gate * or implied warranty.
31*7934SMark.Phalan@Sun.COM *
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * convenience sendauth/recvauth functions
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate
37*7934SMark.Phalan@Sun.COM #include "k5-int.h"
38*7934SMark.Phalan@Sun.COM #include "com_err.h"
39*7934SMark.Phalan@Sun.COM #include "auth_con.h"
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate
44781Sgtb static const char sendauth_version[] = "KRB5_SENDAUTH_V1.0";
450Sstevel@tonic-gate
46781Sgtb 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)47781Sgtb 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)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate krb5_octet result;
500Sstevel@tonic-gate krb5_creds creds;
51*7934SMark.Phalan@Sun.COM krb5_creds * credsp = NULL;
52*7934SMark.Phalan@Sun.COM krb5_creds * credspout = NULL;
530Sstevel@tonic-gate krb5_error_code retval = 0;
540Sstevel@tonic-gate krb5_data inbuf, outbuf;
550Sstevel@tonic-gate int len;
560Sstevel@tonic-gate krb5_ccache use_ccache = 0;
570Sstevel@tonic-gate
580Sstevel@tonic-gate if (error)
590Sstevel@tonic-gate *error = 0;
600Sstevel@tonic-gate
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * First, send over the length of the sendauth version string;
630Sstevel@tonic-gate * then, we send over the sendauth version. Next, we send
640Sstevel@tonic-gate * over the length of the application version strings followed
65*7934SMark.Phalan@Sun.COM * by the string itself.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate outbuf.length = strlen(sendauth_version) + 1;
68781Sgtb outbuf.data = (char *) sendauth_version;
690Sstevel@tonic-gate if ((retval = krb5_write_message(context, fd, &outbuf)))
700Sstevel@tonic-gate return(retval);
710Sstevel@tonic-gate outbuf.length = strlen(appl_version) + 1;
720Sstevel@tonic-gate outbuf.data = appl_version;
730Sstevel@tonic-gate if ((retval = krb5_write_message(context, fd, &outbuf)))
740Sstevel@tonic-gate return(retval);
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Now, read back a byte: 0 means no error, 1 means bad sendauth
770Sstevel@tonic-gate * version, 2 means bad application version
780Sstevel@tonic-gate */
790Sstevel@tonic-gate if ((len = krb5_net_read(context, *((int *) fd), (char *)&result, 1)) != 1)
800Sstevel@tonic-gate return((len < 0) ? errno : ECONNABORTED);
810Sstevel@tonic-gate if (result == 1)
820Sstevel@tonic-gate return(KRB5_SENDAUTH_BADAUTHVERS);
830Sstevel@tonic-gate else if (result == 2)
840Sstevel@tonic-gate return(KRB5_SENDAUTH_BADAPPLVERS);
850Sstevel@tonic-gate else if (result != 0)
860Sstevel@tonic-gate return(KRB5_SENDAUTH_BADRESPONSE);
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate * We're finished with the initial negotiations; let's get and
890Sstevel@tonic-gate * send over the authentication header. (The AP_REQ message)
900Sstevel@tonic-gate */
910Sstevel@tonic-gate
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate * If no credentials were provided, try getting it from the
940Sstevel@tonic-gate * credentials cache.
950Sstevel@tonic-gate */
960Sstevel@tonic-gate memset((char *)&creds, 0, sizeof(creds));
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * See if we need to access the credentials cache
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate if (!in_creds || !in_creds->ticket.length) {
1020Sstevel@tonic-gate if (ccache)
1030Sstevel@tonic-gate use_ccache = ccache;
104*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
1050Sstevel@tonic-gate else if ((retval = krb5int_cc_default(context, &use_ccache)) != 0)
1060Sstevel@tonic-gate goto error_return;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate if (!in_creds) {
1090Sstevel@tonic-gate if ((retval = krb5_copy_principal(context, server,
1100Sstevel@tonic-gate &creds.server)))
1110Sstevel@tonic-gate goto error_return;
1120Sstevel@tonic-gate if (client)
113*7934SMark.Phalan@Sun.COM retval = krb5_copy_principal(context, client,
1140Sstevel@tonic-gate &creds.client);
1150Sstevel@tonic-gate else
1160Sstevel@tonic-gate retval = krb5_cc_get_principal(context, use_ccache,
1170Sstevel@tonic-gate &creds.client);
1180Sstevel@tonic-gate if (retval) {
1190Sstevel@tonic-gate krb5_free_principal(context, creds.server);
1200Sstevel@tonic-gate goto error_return;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate /* creds.times.endtime = 0; -- memset 0 takes care of this
1230Sstevel@tonic-gate zero means "as long as possible" */
1240Sstevel@tonic-gate /* creds.keyblock.enctype = 0; -- as well as this.
1250Sstevel@tonic-gate zero means no session enctype
1260Sstevel@tonic-gate preference */
1270Sstevel@tonic-gate in_creds = &creds;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate if (!in_creds->ticket.length) {
130*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
1310Sstevel@tonic-gate if ((retval = krb5_get_credentials(context, 0,
1320Sstevel@tonic-gate use_ccache, in_creds, &credsp)) != 0)
1330Sstevel@tonic-gate goto error_return;
1340Sstevel@tonic-gate credspout = credsp;
1350Sstevel@tonic-gate } else {
1360Sstevel@tonic-gate credsp = in_creds;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if (ap_req_options & AP_OPTS_USE_SUBKEY) {
1400Sstevel@tonic-gate /* Provide some more fodder for random number code.
1410Sstevel@tonic-gate This isn't strong cryptographically; the point here is
1420Sstevel@tonic-gate not to guarantee randomness, but to make it less likely
1430Sstevel@tonic-gate that multiple sessions could pick the same subkey. */
1440Sstevel@tonic-gate char rnd_data[1024];
145*7934SMark.Phalan@Sun.COM GETPEERNAME_ARG3_TYPE len2;
1460Sstevel@tonic-gate krb5_data d;
1470Sstevel@tonic-gate d.length = sizeof (rnd_data);
1480Sstevel@tonic-gate d.data = rnd_data;
149*7934SMark.Phalan@Sun.COM len2 = sizeof (rnd_data);
150*7934SMark.Phalan@Sun.COM if (getpeername (*(int*)fd, (GETPEERNAME_ARG2_TYPE *) rnd_data,
151*7934SMark.Phalan@Sun.COM &len2) == 0) {
152*7934SMark.Phalan@Sun.COM d.length = len2;
153*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
1540Sstevel@tonic-gate (void) krb5_c_random_seed (context, &d);
1550Sstevel@tonic-gate }
156*7934SMark.Phalan@Sun.COM len2 = sizeof (rnd_data);
157*7934SMark.Phalan@Sun.COM if (getsockname (*(int*)fd, (GETSOCKNAME_ARG2_TYPE *) rnd_data,
158*7934SMark.Phalan@Sun.COM &len2) == 0) {
159*7934SMark.Phalan@Sun.COM d.length = len2;
160*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
1610Sstevel@tonic-gate (void) krb5_c_random_seed (context, &d);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
165*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
1660Sstevel@tonic-gate if ((retval = krb5_mk_req_extended(context, auth_context,
1670Sstevel@tonic-gate ap_req_options, in_data, credsp,
1680Sstevel@tonic-gate &outbuf)) != 0)
1690Sstevel@tonic-gate goto error_return;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate * First write the length of the AP_REQ message, then write
1730Sstevel@tonic-gate * the message itself.
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate retval = krb5_write_message(context, fd, &outbuf);
1760Sstevel@tonic-gate free(outbuf.data);
1770Sstevel@tonic-gate if (retval)
1780Sstevel@tonic-gate goto error_return;
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate * Now, read back a message. If it was a null message (the
1820Sstevel@tonic-gate * length was zero) then there was no error. If not, we the
1830Sstevel@tonic-gate * authentication was rejected, and we need to return the
1840Sstevel@tonic-gate * error structure.
1850Sstevel@tonic-gate */
186*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
1870Sstevel@tonic-gate if ((retval = krb5_read_message(context, fd, &inbuf)) != 0)
1880Sstevel@tonic-gate goto error_return;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate if (inbuf.length) {
1910Sstevel@tonic-gate if (error) {
192*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
1930Sstevel@tonic-gate if ((retval = krb5_rd_error(context, &inbuf, error)) != 0) {
1940Sstevel@tonic-gate krb5_xfree(inbuf.data);
1950Sstevel@tonic-gate goto error_return;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate retval = KRB5_SENDAUTH_REJECTED;
1990Sstevel@tonic-gate krb5_xfree(inbuf.data);
2000Sstevel@tonic-gate goto error_return;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * If we asked for mutual authentication, we should now get a
2050Sstevel@tonic-gate * length field, followed by a AP_REP message
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate if ((ap_req_options & AP_OPTS_MUTUAL_REQUIRED)) {
2080Sstevel@tonic-gate krb5_ap_rep_enc_part *repl = 0;
209*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
2100Sstevel@tonic-gate if ((retval = krb5_read_message(context, fd, &inbuf)) != 0)
2110Sstevel@tonic-gate goto error_return;
2120Sstevel@tonic-gate
213*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
2140Sstevel@tonic-gate if ((retval = krb5_rd_rep(context, *auth_context, &inbuf,
2150Sstevel@tonic-gate &repl)) != 0) {
2160Sstevel@tonic-gate if (repl)
2170Sstevel@tonic-gate krb5_free_ap_rep_enc_part(context, repl);
2180Sstevel@tonic-gate krb5_xfree(inbuf.data);
2190Sstevel@tonic-gate goto error_return;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate krb5_xfree(inbuf.data);
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate * If the user wants to look at the AP_REP message,
2250Sstevel@tonic-gate * copy it for him
2260Sstevel@tonic-gate */
227*7934SMark.Phalan@Sun.COM if (rep_result)
2280Sstevel@tonic-gate *rep_result = repl;
2290Sstevel@tonic-gate else
2300Sstevel@tonic-gate krb5_free_ap_rep_enc_part(context, repl);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate retval = 0; /* Normal return */
2330Sstevel@tonic-gate if (out_creds) {
2340Sstevel@tonic-gate *out_creds = credsp;
2350Sstevel@tonic-gate credspout = NULL;
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate error_return:
2390Sstevel@tonic-gate krb5_free_cred_contents(context, &creds);
2400Sstevel@tonic-gate if (credspout != NULL)
241*7934SMark.Phalan@Sun.COM krb5_free_creds(context, credspout);
242*7934SMark.Phalan@Sun.COM /* Solaris Kerberos */
2430Sstevel@tonic-gate if (!ccache && use_ccache)
2440Sstevel@tonic-gate (void) krb5_cc_close(context, use_ccache);
2450Sstevel@tonic-gate return(retval);
2460Sstevel@tonic-gate }
247*7934SMark.Phalan@Sun.COM
248*7934SMark.Phalan@Sun.COM
249