Initial release, version 0.0.0.
[gsasl.git] / lib / xstep.c
blobe8267807141fd228766f53691582bc5e258d5149
1 /* Xstep.c perform one SASL authentication step in the X
2 * Copyright (C) 2002 Simon Josefsson
4 * This file is part of libgsasl.
6 * Libgsasl is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libgsasl is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with libgsasl; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 /**
25 * gsasl_client_step:
26 * @xctx: libgsasl client handle.
27 * @input: input byte array.
28 * @input_len: size of input byte array.
29 * @output: output byte array.
30 * @output_len: size of output byte array.
32 * Perform one step of SASL authentication in client. This reads data
33 * from server (specified with input and input_len), processes it
34 * (potentially invoking callbacks to the application), and writes
35 * data to server (into variables output and output_len).
37 * The contents of the output buffer is unspecified if this functions
38 * returns anything other than GSASL_NEEDS_MORE.
40 * Return value: Returns GSASL_OK if authenticated terminated
41 * successfully, GSASL_NEEDS_MORE if more data is needed, or error
42 * code.
43 **/
44 int
45 gsasl_client_step (Gsasl_session_ctx *xctx,
46 const char *input,
47 size_t input_len,
48 char *output,
49 size_t *output_len)
51 return xctx->mech->client.step(xctx, xctx->mech_data,
52 input, input_len,
53 output, output_len);
57 /**
58 * gsasl_server_step:
59 * @xctx: libgsasl server handle.
60 * @input: input byte array.
61 * @input_len: size of input byte array.
62 * @output: output byte array.
63 * @output_len: size of output byte array.
65 * Perform one step of SASL authentication in server. This reads data
66 * from client (specified with input and input_len), processes it
67 * (potentially invoking callbacks to the application), and writes
68 * data to client (into variables output and output_len).
70 * The contents of the output buffer is unspecified if this functions
71 * returns anything other than GSASL_NEEDS_MORE.
73 * Return value: Returns GSASL_OK if authenticated terminated
74 * successfully, GSASL_NEEDS_MORE if more data is needed, or error
75 * code.
76 **/
77 int
78 gsasl_server_step (Gsasl_session_ctx *xctx,
79 const char *input,
80 size_t input_len,
81 char *output,
82 size_t *output_len)
84 return xctx->mech->server.step(xctx, xctx->mech_data,
85 input, input_len,
86 output, output_len);
89 static int
90 _gsasl_session_step_base64 (Gsasl_session_ctx *xctx,
91 const char *b64input,
92 char *b64output,
93 size_t b64output_len,
94 int clientp)
96 size_t input_len, output_len;
97 char *input, *output;
98 int res;
100 if (b64input && strlen(b64input) > 0)
102 input_len = strlen(b64input) + 1;
103 input = (char*) malloc(input_len);
105 input_len = gsasl_base64_decode(b64input, input, input_len);
106 if (input_len == -1)
108 free(input);
109 return GSASL_BASE64_ERROR;
112 else
114 input = NULL;
115 input_len = 0;
118 if (b64output && b64output_len > 0)
120 *b64output = '\0';
121 output_len = b64output_len; /* As good guess as any */
122 output = (char*) malloc(output_len);
124 else
126 output = NULL;
127 output_len = 0;
130 if (clientp)
131 res = gsasl_client_step (xctx, input, input_len, output, &output_len);
132 else
133 res = gsasl_server_step (xctx, input, input_len, output, &output_len);
135 if (res == GSASL_NEEDS_MORE && output && output_len > 0)
137 output_len = gsasl_base64_encode(output, output_len,
138 b64output, b64output_len);
139 if (output_len == -1)
141 free(output);
142 free(input);
143 return GSASL_BASE64_ERROR;
147 free(output);
148 free(input);
150 return res;
154 * gsasl_client_step_base64:
155 * @xctx: libgsasl client handle.
156 * @b64input: input base64 encoded byte array.
157 * @b64output: output base64 encoded byte array.
158 * @b64output_len: size of output base64 encoded byte array.
160 * This is a simple wrapper around gsasl_client_step() that base64
161 * decodes the input and base64 encodes the output.
163 * Return value: See gsasl_client_step().
166 gsasl_client_step_base64 (Gsasl_session_ctx *xctx,
167 const char *b64input,
168 char *b64output,
169 size_t b64output_len)
171 return _gsasl_session_step_base64 (xctx, b64input, b64output,
172 b64output_len, 1);
176 * gsasl_server_step_base64:
177 * @xctx: libgsasl server handle.
178 * @b64input: input base64 encoded byte array.
179 * @b64output: output base64 encoded byte array.
180 * @b64output_len: size of output base64 encoded byte array.
182 * This is a simple wrapper around gsasl_server_step() that base64
183 * decodes the input and base64 encodes the output.
185 * Return value: See gsasl_server_step().
188 gsasl_server_step_base64 (Gsasl_session_ctx *xctx,
189 const char *b64input,
190 char *b64output,
191 size_t b64output_len)
193 return _gsasl_session_step_base64 (xctx, b64input, b64output,
194 b64output_len, 0);