Thu Apr 28 2011 17:13:33

Asterisk developer's documentation


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 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 301848 $")
00029 
00030 #include "asterisk/module.h"
00031 #include "asterisk/pbx.h"
00032 #include "asterisk/app.h"
00033 #include "asterisk/aes.h"
00034 
00035 #define AES_BLOCK_SIZE 16
00036 
00037 /*** DOCUMENTATION
00038    <function name="AES_ENCRYPT" language="en_US">
00039       <synopsis>
00040          Encrypt a string with AES given a 16 character key.
00041       </synopsis>
00042       <syntax>
00043          <parameter name="key" required="true">
00044             <para>AES Key</para>
00045          </parameter>
00046          <parameter name="string" required="true">
00047             <para>Input string</para>
00048          </parameter>
00049       </syntax>
00050       <description>
00051          <para>Returns an AES encrypted string encoded in base64.</para>
00052       </description>
00053       <see-also>
00054          <ref type="function">AES_DECRYPT</ref>
00055          <ref type="function">BASE64_ENCODE</ref>
00056          <ref type="function">BASE64_DECODE</ref>
00057       </see-also>
00058    </function>
00059    <function name="AES_DECRYPT" language="en_US">
00060       <synopsis>
00061          Decrypt a string encoded in base64 with AES given a 16 character key.
00062       </synopsis>
00063       <syntax>
00064          <parameter name="key" required="true">
00065             <para>AES Key</para>
00066          </parameter>
00067          <parameter name="string" required="true">
00068             <para>Input string.</para>
00069          </parameter>
00070       </syntax>
00071       <description>
00072          <para>Returns the plain text string.</para>
00073       </description>
00074       <see-also>
00075          <ref type="function">AES_ENCRYPT</ref>
00076          <ref type="function">BASE64_ENCODE</ref>
00077          <ref type="function">BASE64_DECODE</ref>
00078       </see-also>
00079    </function>
00080  ***/
00081 
00082 
00083 static int aes_helper(struct ast_channel *chan, const char *cmd, char *data,
00084           char *buf, size_t len)
00085 {
00086    unsigned char curblock[AES_BLOCK_SIZE] = { 0, };
00087    char *tmp;
00088    char *tmpP;
00089    int data_len, encrypt;
00090    ast_aes_encrypt_key ecx;                        /*  AES 128 Encryption context */
00091    ast_aes_decrypt_key dcx;
00092 
00093    AST_DECLARE_APP_ARGS(args,
00094       AST_APP_ARG(key);
00095       AST_APP_ARG(data);
00096    );
00097 
00098    AST_STANDARD_APP_ARGS(args, data);
00099 
00100    if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
00101       ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd);
00102       return -1;
00103    }
00104 
00105    if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */
00106       ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
00107       return -1;
00108    }
00109 
00110    ast_aes_encrypt_key((unsigned char *) args.key, &ecx);   /* encryption:  plaintext -> encryptedtext -> base64 */
00111    ast_aes_decrypt_key((unsigned char *) args.key, &dcx);   /* decryption:  base64 -> encryptedtext -> plaintext */
00112    tmp = ast_calloc(1, len);                     /* requires a tmp buffer for the base64 decode */
00113    tmpP = tmp;
00114    encrypt = strcmp("AES_DECRYPT", cmd);           /* -1 if encrypting, 0 if decrypting */
00115 
00116    if (encrypt) {                                  /* if decrypting first decode src to base64 */
00117       ast_copy_string(tmp, args.data, len);
00118       data_len = strlen(tmp);
00119    } else {
00120       data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
00121    }
00122 
00123    if (data_len >= len) {                        /* make sure to not go over buffer len */
00124       ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n", cmd);
00125       data_len = len - 1;
00126    }
00127 
00128    while (data_len > 0) {
00129       memset(curblock, 0, AES_BLOCK_SIZE);
00130       memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
00131       if (encrypt) {
00132          ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
00133       } else {
00134          ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
00135       }
00136       tmpP += AES_BLOCK_SIZE;
00137       data_len -= AES_BLOCK_SIZE;
00138    }
00139 
00140    if (encrypt) {                            /* if encrypting encode result to base64 */
00141       ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
00142    } else {
00143       memcpy(buf, tmp, len);
00144    }
00145    ast_free(tmp);
00146 
00147    return 0;
00148 }
00149 
00150 static struct ast_custom_function aes_encrypt_function = {
00151    .name = "AES_ENCRYPT",
00152    .read = aes_helper,
00153 };
00154 
00155 static struct ast_custom_function aes_decrypt_function = {
00156    .name = "AES_DECRYPT",
00157    .read = aes_helper,
00158 };
00159 
00160 static int unload_module(void)
00161 {
00162    int res = ast_custom_function_unregister(&aes_decrypt_function);
00163    return res | ast_custom_function_unregister(&aes_encrypt_function);
00164 }
00165 
00166 static int load_module(void)
00167 {
00168    int res = ast_custom_function_register(&aes_decrypt_function);
00169    res |= ast_custom_function_register(&aes_encrypt_function);
00170    return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00171 }
00172 
00173 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AES dialplan functions");