1/* xstep.c --- Perform one SASL authentication step.
2 * Copyright (C) 2002-2012 Simon Josefsson
3 *
4 * This file is part of GNU SASL Library.
5 *
6 * GNU SASL Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * GNU SASL Library 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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License License along with GNU SASL Library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "internal.h"
24
25/**
26 * gsasl_step:
27 * @sctx: libgsasl session handle.
28 * @input: input byte array.
29 * @input_len: size of input byte array.
30 * @output: newly allocated output byte array.
31 * @output_len: pointer to output variable with size of output byte array.
32 *
33 * Perform one step of SASL authentication. This reads data from the
34 * other end (from @input and @input_len), processes it (potentially
35 * invoking callbacks to the application), and writes data to server
36 * (into newly allocated variable @output and @output_len that
37 * indicate the length of @output).
38 *
39 * The contents of the @output buffer is unspecified if this functions
40 * returns anything other than %GSASL_OK or %GSASL_NEEDS_MORE. If
41 * this function return %GSASL_OK or %GSASL_NEEDS_MORE, however, the
42 * @output buffer is allocated by this function, and it is the
43 * responsibility of caller to deallocate it by calling free
44 * (@output).
45 *
46 * Return value: Returns %GSASL_OK if authenticated terminated
47 * successfully, %GSASL_NEEDS_MORE if more data is needed, or error
48 * code.
49 **/
50int
51gsasl_step (Gsasl_session * sctx,
52 const char *input, size_t input_len,
53 char **output, size_t * output_len)
54{
55 Gsasl_step_function step;
56
57 if (sctx->clientp)
58 step = sctx->mech->client.step;
59 else
60 step = sctx->mech->server.step;
61
62 return step (sctx, sctx->mech_data, input, input_len, output, output_len);
63}
64
65/**
66 * gsasl_step64:
67 * @sctx: libgsasl client handle.
68 * @b64input: input base64 encoded byte array.
69 * @b64output: newly allocated output base64 encoded byte array.
70 *
71 * This is a simple wrapper around gsasl_step() that base64 decodes
72 * the input and base64 encodes the output.
73 *
74 * The contents of the @b64output buffer is unspecified if this
75 * functions returns anything other than %GSASL_OK or
76 * %GSASL_NEEDS_MORE. If this function return %GSASL_OK or
77 * %GSASL_NEEDS_MORE, however, the @b64output buffer is allocated by
78 * this function, and it is the responsibility of caller to deallocate
79 * it by calling free (@b64output).
80 *
81 * Return value: Returns %GSASL_OK if authenticated terminated
82 * successfully, %GSASL_NEEDS_MORE if more data is needed, or error
83 * code.
84 **/
85int
86gsasl_step64 (Gsasl_session * sctx, const char *b64input, char **b64output)
87{
88 size_t input_len = 0, output_len = 0;
89 char *input = NULL, *output = NULL;
90 int res;
91
92 if (b64input)
93 {
94 res = gsasl_base64_from (b64input, strlen (b64input),
95 &input, &input_len);
96 if (res != GSASL_OK)
97 return GSASL_BASE64_ERROR;
98 }
99
100 res = gsasl_step (sctx, input, input_len, &output, &output_len);
101
102 free (input);
103
104 if (res == GSASL_OK || res == GSASL_NEEDS_MORE)
105 {
106 int tmpres = gsasl_base64_to (output, output_len, b64output, NULL);
107
108 free (output);
109
110 if (tmpres != GSASL_OK)
111 return tmpres;
112 }
113
114 return res;
115}
116