func_aes.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2009, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief AES encryption/decryption dialplan functions
00020  *
00021  * \author David Vossel <dvossel@digium.com>
00022  * \ingroup functions
00023  */
00024 
00025 /*** MODULEINFO
00026    <use type="external">crypto</use>
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00033 
00034 #include "asterisk/module.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/app.h"
00037 #include "asterisk/crypto.h"
00038 
00039 #define AES_BLOCK_SIZE 16
00040 
00041 /*** DOCUMENTATION
00042    <function name="AES_ENCRYPT" language="en_US">
00043       <synopsis>
00044          Encrypt a string with AES given a 16 character key.
00045       </synopsis>
00046       <syntax>
00047          <parameter name="key" required="true">
00048             <para>AES Key</para>
00049          </parameter>
00050          <parameter name="string" required="true">
00051             <para>Input string</para>
00052          </parameter>
00053       </syntax>
00054       <description>
00055          <para>Returns an AES encrypted string encoded in base64.</para>
00056       </description>
00057       <see-also>
00058          <ref type="function">AES_DECRYPT</ref>
00059          <ref type="function">BASE64_ENCODE</ref>
00060          <ref type="function">BASE64_DECODE</ref>
00061       </see-also>
00062    </function>
00063    <function name="AES_DECRYPT" language="en_US">
00064       <synopsis>
00065          Decrypt a string encoded in base64 with AES given a 16 character key.
00066       </synopsis>
00067       <syntax>
00068          <parameter name="key" required="true">
00069             <para>AES Key</para>
00070          </parameter>
00071          <parameter name="string" required="true">
00072             <para>Input string.</para>
00073          </parameter>
00074       </syntax>
00075       <description>
00076          <para>Returns the plain text string.</para>
00077       </description>
00078       <see-also>
00079          <ref type="function">AES_ENCRYPT</ref>
00080          <ref type="function">BASE64_ENCODE</ref>
00081          <ref type="function">BASE64_DECODE</ref>
00082       </see-also>
00083    </function>
00084  ***/
00085 
00086 
00087 static int aes_helper(struct ast_channel *chan, const char *cmd, char *data,
00088           char *buf, size_t len)
00089 {
00090    unsigned char curblock[AES_BLOCK_SIZE] = { 0, };
00091    char *tmp;
00092    char *tmpP;
00093    int data_len, encrypt;
00094    ast_aes_encrypt_key ecx;                        /*  AES 128 Encryption context */
00095    ast_aes_decrypt_key dcx;
00096 
00097    AST_DECLARE_APP_ARGS(args,
00098       AST_APP_ARG(key);
00099       AST_APP_ARG(data);
00100    );
00101 
00102    AST_STANDARD_APP_ARGS(args, data);
00103 
00104    if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
00105       ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd);
00106       return -1;
00107    }
00108 
00109    if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */
00110       ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
00111       return -1;
00112    }
00113 
00114    ast_aes_set_encrypt_key((unsigned char *) args.key, &ecx);   /* encryption:  plaintext -> encryptedtext -> base64 */
00115    ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx);   /* decryption:  base64 -> encryptedtext -> plaintext */
00116    tmp = ast_calloc(1, len);                     /* requires a tmp buffer for the base64 decode */
00117    if (!tmp) {
00118       ast_log(LOG_ERROR, "Unable to allocate memory for data\n");
00119       return -1;
00120    }
00121    tmpP = tmp;
00122    encrypt = strcmp("AES_DECRYPT", cmd);           /* -1 if encrypting, 0 if decrypting */
00123 
00124    if (encrypt) {                                  /* if decrypting first decode src to base64 */
00125       ast_copy_string(tmp, args.data, len);
00126       data_len = strlen(tmp);
00127    } else {
00128       data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
00129    }
00130 
00131    if (data_len >= len) {                        /* make sure to not go over buffer len */
00132       ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n", cmd);
00133       data_len = len - 1;
00134    }
00135 
00136    while (data_len > 0) {
00137       memset(curblock, 0, AES_BLOCK_SIZE);
00138       memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
00139       if (encrypt) {
00140          ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
00141       } else {
00142          ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
00143       }
00144       tmpP += AES_BLOCK_SIZE;
00145       data_len -= AES_BLOCK_SIZE;
00146    }
00147 
00148    if (encrypt) {                            /* if encrypting encode result to base64 */
00149       ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
00150    } else {
00151       memcpy(buf, tmp, len);
00152    }
00153    ast_free(tmp);
00154 
00155    return 0;
00156 }
00157 
00158 static struct ast_custom_function aes_encrypt_function = {
00159    .name = "AES_ENCRYPT",
00160    .read = aes_helper,
00161 };
00162 
00163 static struct ast_custom_function aes_decrypt_function = {
00164    .name = "AES_DECRYPT",
00165    .read = aes_helper,
00166 };
00167 
00168 static int unload_module(void)
00169 {
00170    int res = ast_custom_function_unregister(&aes_decrypt_function);
00171    return res | ast_custom_function_unregister(&aes_encrypt_function);
00172 }
00173 
00174 static int load_module(void)
00175 {
00176    int res = ast_custom_function_register(&aes_decrypt_function);
00177    res |= ast_custom_function_register(&aes_encrypt_function);
00178    return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00179 }
00180 
00181 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AES dialplan functions",
00182       .support_level = AST_MODULE_SUPPORT_CORE,
00183       .load = load_module,
00184       .unload = unload_module,
00185       .nonoptreq = "res_crypto",
00186    );

Generated on Thu Apr 16 06:27:35 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6