Wed Apr 6 11:29:45 2011

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

Generated on Wed Apr 6 11:29:45 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7