1*0a6a1f1dSLionel Sambuc /* $NetBSD: sendauth.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "krb5_locl.h"
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc /*
39ebfedea0SLionel Sambuc * The format seems to be:
40ebfedea0SLionel Sambuc * client -> server
41ebfedea0SLionel Sambuc *
42ebfedea0SLionel Sambuc * 4 bytes - length
43ebfedea0SLionel Sambuc * KRB5_SENDAUTH_V1.0 (including zero)
44ebfedea0SLionel Sambuc * 4 bytes - length
45ebfedea0SLionel Sambuc * protocol string (with terminating zero)
46ebfedea0SLionel Sambuc *
47ebfedea0SLionel Sambuc * server -> client
48ebfedea0SLionel Sambuc * 1 byte - (0 = OK, else some kind of error)
49ebfedea0SLionel Sambuc *
50ebfedea0SLionel Sambuc * client -> server
51ebfedea0SLionel Sambuc * 4 bytes - length
52ebfedea0SLionel Sambuc * AP-REQ
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * server -> client
55ebfedea0SLionel Sambuc * 4 bytes - length (0 = OK, else length of error)
56ebfedea0SLionel Sambuc * (error)
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc * if(mutual) {
59ebfedea0SLionel Sambuc * server -> client
60ebfedea0SLionel Sambuc * 4 bytes - length
61ebfedea0SLionel Sambuc * AP-REP
62ebfedea0SLionel Sambuc * }
63ebfedea0SLionel Sambuc */
64ebfedea0SLionel Sambuc
65ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_sendauth(krb5_context context,krb5_auth_context * auth_context,krb5_pointer p_fd,const 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 ** ret_error,krb5_ap_rep_enc_part ** rep_result,krb5_creds ** out_creds)66ebfedea0SLionel Sambuc krb5_sendauth(krb5_context context,
67ebfedea0SLionel Sambuc krb5_auth_context *auth_context,
68ebfedea0SLionel Sambuc krb5_pointer p_fd,
69ebfedea0SLionel Sambuc const char *appl_version,
70ebfedea0SLionel Sambuc krb5_principal client,
71ebfedea0SLionel Sambuc krb5_principal server,
72ebfedea0SLionel Sambuc krb5_flags ap_req_options,
73ebfedea0SLionel Sambuc krb5_data *in_data,
74ebfedea0SLionel Sambuc krb5_creds *in_creds,
75ebfedea0SLionel Sambuc krb5_ccache ccache,
76ebfedea0SLionel Sambuc krb5_error **ret_error,
77ebfedea0SLionel Sambuc krb5_ap_rep_enc_part **rep_result,
78ebfedea0SLionel Sambuc krb5_creds **out_creds)
79ebfedea0SLionel Sambuc {
80ebfedea0SLionel Sambuc krb5_error_code ret;
81ebfedea0SLionel Sambuc uint32_t len, net_len;
82ebfedea0SLionel Sambuc const char *version = KRB5_SENDAUTH_VERSION;
83ebfedea0SLionel Sambuc u_char repl;
84ebfedea0SLionel Sambuc krb5_data ap_req, error_data;
85ebfedea0SLionel Sambuc krb5_creds this_cred;
86ebfedea0SLionel Sambuc krb5_principal this_client = NULL;
87ebfedea0SLionel Sambuc krb5_creds *creds;
88ebfedea0SLionel Sambuc ssize_t sret;
89ebfedea0SLionel Sambuc krb5_boolean my_ccache = FALSE;
90ebfedea0SLionel Sambuc
91ebfedea0SLionel Sambuc len = strlen(version) + 1;
92ebfedea0SLionel Sambuc net_len = htonl(len);
93ebfedea0SLionel Sambuc if (krb5_net_write (context, p_fd, &net_len, 4) != 4
94ebfedea0SLionel Sambuc || krb5_net_write (context, p_fd, version, len) != len) {
95ebfedea0SLionel Sambuc ret = errno;
96ebfedea0SLionel Sambuc krb5_set_error_message (context, ret, "write: %s", strerror(ret));
97ebfedea0SLionel Sambuc return ret;
98ebfedea0SLionel Sambuc }
99ebfedea0SLionel Sambuc
100ebfedea0SLionel Sambuc len = strlen(appl_version) + 1;
101ebfedea0SLionel Sambuc net_len = htonl(len);
102ebfedea0SLionel Sambuc if (krb5_net_write (context, p_fd, &net_len, 4) != 4
103ebfedea0SLionel Sambuc || krb5_net_write (context, p_fd, appl_version, len) != len) {
104ebfedea0SLionel Sambuc ret = errno;
105ebfedea0SLionel Sambuc krb5_set_error_message (context, ret, "write: %s", strerror(ret));
106ebfedea0SLionel Sambuc return ret;
107ebfedea0SLionel Sambuc }
108ebfedea0SLionel Sambuc
109ebfedea0SLionel Sambuc sret = krb5_net_read (context, p_fd, &repl, sizeof(repl));
110ebfedea0SLionel Sambuc if (sret < 0) {
111ebfedea0SLionel Sambuc ret = errno;
112ebfedea0SLionel Sambuc krb5_set_error_message (context, ret, "read: %s", strerror(ret));
113ebfedea0SLionel Sambuc return ret;
114ebfedea0SLionel Sambuc } else if (sret != sizeof(repl)) {
115ebfedea0SLionel Sambuc krb5_clear_error_message (context);
116ebfedea0SLionel Sambuc return KRB5_SENDAUTH_BADRESPONSE;
117ebfedea0SLionel Sambuc }
118ebfedea0SLionel Sambuc
119ebfedea0SLionel Sambuc if (repl != 0) {
120ebfedea0SLionel Sambuc krb5_clear_error_message (context);
121ebfedea0SLionel Sambuc return KRB5_SENDAUTH_REJECTED;
122ebfedea0SLionel Sambuc }
123ebfedea0SLionel Sambuc
124ebfedea0SLionel Sambuc if (in_creds == NULL) {
125ebfedea0SLionel Sambuc if (ccache == NULL) {
126ebfedea0SLionel Sambuc ret = krb5_cc_default (context, &ccache);
127ebfedea0SLionel Sambuc if (ret)
128ebfedea0SLionel Sambuc return ret;
129ebfedea0SLionel Sambuc my_ccache = TRUE;
130ebfedea0SLionel Sambuc }
131ebfedea0SLionel Sambuc
132ebfedea0SLionel Sambuc if (client == NULL) {
133ebfedea0SLionel Sambuc ret = krb5_cc_get_principal (context, ccache, &this_client);
134ebfedea0SLionel Sambuc if (ret) {
135ebfedea0SLionel Sambuc if(my_ccache)
136ebfedea0SLionel Sambuc krb5_cc_close(context, ccache);
137ebfedea0SLionel Sambuc return ret;
138ebfedea0SLionel Sambuc }
139ebfedea0SLionel Sambuc client = this_client;
140ebfedea0SLionel Sambuc }
141ebfedea0SLionel Sambuc memset(&this_cred, 0, sizeof(this_cred));
142ebfedea0SLionel Sambuc this_cred.client = client;
143ebfedea0SLionel Sambuc this_cred.server = server;
144ebfedea0SLionel Sambuc this_cred.times.endtime = 0;
145ebfedea0SLionel Sambuc this_cred.ticket.length = 0;
146ebfedea0SLionel Sambuc in_creds = &this_cred;
147ebfedea0SLionel Sambuc }
148ebfedea0SLionel Sambuc if (in_creds->ticket.length == 0) {
149ebfedea0SLionel Sambuc ret = krb5_get_credentials (context, 0, ccache, in_creds, &creds);
150ebfedea0SLionel Sambuc if (ret) {
151ebfedea0SLionel Sambuc if(my_ccache)
152ebfedea0SLionel Sambuc krb5_cc_close(context, ccache);
153ebfedea0SLionel Sambuc return ret;
154ebfedea0SLionel Sambuc }
155ebfedea0SLionel Sambuc } else {
156ebfedea0SLionel Sambuc creds = in_creds;
157ebfedea0SLionel Sambuc }
158ebfedea0SLionel Sambuc if(my_ccache)
159ebfedea0SLionel Sambuc krb5_cc_close(context, ccache);
160ebfedea0SLionel Sambuc ret = krb5_mk_req_extended (context,
161ebfedea0SLionel Sambuc auth_context,
162ebfedea0SLionel Sambuc ap_req_options,
163ebfedea0SLionel Sambuc in_data,
164ebfedea0SLionel Sambuc creds,
165ebfedea0SLionel Sambuc &ap_req);
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc if (out_creds)
168ebfedea0SLionel Sambuc *out_creds = creds;
169ebfedea0SLionel Sambuc else
170ebfedea0SLionel Sambuc krb5_free_creds(context, creds);
171ebfedea0SLionel Sambuc if(this_client)
172ebfedea0SLionel Sambuc krb5_free_principal(context, this_client);
173ebfedea0SLionel Sambuc
174ebfedea0SLionel Sambuc if (ret)
175ebfedea0SLionel Sambuc return ret;
176ebfedea0SLionel Sambuc
177ebfedea0SLionel Sambuc ret = krb5_write_message (context,
178ebfedea0SLionel Sambuc p_fd,
179ebfedea0SLionel Sambuc &ap_req);
180ebfedea0SLionel Sambuc if (ret)
181ebfedea0SLionel Sambuc return ret;
182ebfedea0SLionel Sambuc
183ebfedea0SLionel Sambuc krb5_data_free (&ap_req);
184ebfedea0SLionel Sambuc
185ebfedea0SLionel Sambuc ret = krb5_read_message (context, p_fd, &error_data);
186ebfedea0SLionel Sambuc if (ret)
187ebfedea0SLionel Sambuc return ret;
188ebfedea0SLionel Sambuc
189ebfedea0SLionel Sambuc if (error_data.length != 0) {
190ebfedea0SLionel Sambuc KRB_ERROR error;
191ebfedea0SLionel Sambuc
192ebfedea0SLionel Sambuc ret = krb5_rd_error (context, &error_data, &error);
193ebfedea0SLionel Sambuc krb5_data_free (&error_data);
194ebfedea0SLionel Sambuc if (ret == 0) {
195ebfedea0SLionel Sambuc ret = krb5_error_from_rd_error(context, &error, NULL);
196ebfedea0SLionel Sambuc if (ret_error != NULL) {
197ebfedea0SLionel Sambuc *ret_error = malloc (sizeof(krb5_error));
198ebfedea0SLionel Sambuc if (*ret_error == NULL) {
199ebfedea0SLionel Sambuc krb5_free_error_contents (context, &error);
200ebfedea0SLionel Sambuc } else {
201ebfedea0SLionel Sambuc **ret_error = error;
202ebfedea0SLionel Sambuc }
203ebfedea0SLionel Sambuc } else {
204ebfedea0SLionel Sambuc krb5_free_error_contents (context, &error);
205ebfedea0SLionel Sambuc }
206ebfedea0SLionel Sambuc return ret;
207ebfedea0SLionel Sambuc } else {
208ebfedea0SLionel Sambuc krb5_clear_error_message(context);
209ebfedea0SLionel Sambuc return ret;
210ebfedea0SLionel Sambuc }
211ebfedea0SLionel Sambuc } else
212ebfedea0SLionel Sambuc krb5_data_free (&error_data);
213ebfedea0SLionel Sambuc
214ebfedea0SLionel Sambuc if (ap_req_options & AP_OPTS_MUTUAL_REQUIRED) {
215ebfedea0SLionel Sambuc krb5_data ap_rep;
216*0a6a1f1dSLionel Sambuc krb5_ap_rep_enc_part *ignore = NULL;
217ebfedea0SLionel Sambuc
218ebfedea0SLionel Sambuc krb5_data_zero (&ap_rep);
219ebfedea0SLionel Sambuc ret = krb5_read_message (context,
220ebfedea0SLionel Sambuc p_fd,
221ebfedea0SLionel Sambuc &ap_rep);
222ebfedea0SLionel Sambuc if (ret)
223ebfedea0SLionel Sambuc return ret;
224ebfedea0SLionel Sambuc
225ebfedea0SLionel Sambuc ret = krb5_rd_rep (context, *auth_context, &ap_rep,
226ebfedea0SLionel Sambuc rep_result ? rep_result : &ignore);
227ebfedea0SLionel Sambuc krb5_data_free (&ap_rep);
228ebfedea0SLionel Sambuc if (ret)
229ebfedea0SLionel Sambuc return ret;
230ebfedea0SLionel Sambuc if (rep_result == NULL)
231ebfedea0SLionel Sambuc krb5_free_ap_rep_enc_part (context, ignore);
232ebfedea0SLionel Sambuc }
233ebfedea0SLionel Sambuc return 0;
234ebfedea0SLionel Sambuc }
235