| 1 | /* xcode.c --- Encode and decode application payload in libgsasl session. |
| 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 | static int |
| 26 | _gsasl_code (Gsasl_session * sctx, |
| 27 | Gsasl_code_function code, |
| 28 | const char *input, size_t input_len, |
| 29 | char **output, size_t * output_len) |
| 30 | { |
| 31 | |
| 32 | if (code == NULL) |
| 33 | { |
| 34 | *output_len = input_len; |
| 35 | *output = malloc (*output_len); |
| 36 | if (!*output) |
| 37 | return GSASL_MALLOC_ERROR; |
| 38 | |
| 39 | memcpy (*output, input, input_len); |
| 40 | return GSASL_OK; |
| 41 | } |
| 42 | |
| 43 | return code (sctx, sctx->mech_data, input, input_len, output, output_len); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * gsasl_encode: |
| 48 | * @sctx: libgsasl session handle. |
| 49 | * @input: input byte array. |
| 50 | * @input_len: size of input byte array. |
| 51 | * @output: newly allocated output byte array. |
| 52 | * @output_len: size of output byte array. |
| 53 | * |
| 54 | * Encode data according to negotiated SASL mechanism. This might mean |
| 55 | * that data is integrity or privacy protected. |
| 56 | * |
| 57 | * The @output buffer is allocated by this function, and it is the |
| 58 | * responsibility of caller to deallocate it by calling free(@output). |
| 59 | * |
| 60 | * Return value: Returns %GSASL_OK if encoding was successful, |
| 61 | * otherwise an error code. |
| 62 | **/ |
| 63 | int |
| 64 | gsasl_encode (Gsasl_session * sctx, |
| 65 | const char *input, size_t input_len, |
| 66 | char **output, size_t * output_len) |
| 67 | { |
| 68 | Gsasl_code_function code; |
| 69 | |
| 70 | if (sctx->clientp) |
| 71 | code = sctx->mech->client.encode; |
| 72 | else |
| 73 | code = sctx->mech->server.encode; |
| 74 | |
| 75 | return _gsasl_code (sctx, code, input, input_len, output, output_len); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * gsasl_decode: |
| 80 | * @sctx: libgsasl session handle. |
| 81 | * @input: input byte array. |
| 82 | * @input_len: size of input byte array. |
| 83 | * @output: newly allocated output byte array. |
| 84 | * @output_len: size of output byte array. |
| 85 | * |
| 86 | * Decode data according to negotiated SASL mechanism. This might mean |
| 87 | * that data is integrity or privacy protected. |
| 88 | * |
| 89 | * The @output buffer is allocated by this function, and it is the |
| 90 | * responsibility of caller to deallocate it by calling free(@output). |
| 91 | * |
| 92 | * Return value: Returns %GSASL_OK if encoding was successful, |
| 93 | * otherwise an error code. |
| 94 | **/ |
| 95 | int |
| 96 | gsasl_decode (Gsasl_session * sctx, |
| 97 | const char *input, size_t input_len, |
| 98 | char **output, size_t * output_len) |
| 99 | { |
| 100 | Gsasl_code_function code; |
| 101 | |
| 102 | if (sctx->clientp) |
| 103 | code = sctx->mech->client.decode; |
| 104 | else |
| 105 | code = sctx->mech->server.decode; |
| 106 | |
| 107 | return _gsasl_code (sctx, code, input, input_len, output, output_len); |
| 108 | } |
| 109 | |