00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "asterisk.h"
00030
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 284610 $")
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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 static int aes_helper(struct ast_channel *chan, const char *cmd, char *data,
00077 char *buf, size_t len)
00078 {
00079 unsigned char curblock[AES_BLOCK_SIZE] = { 0, };
00080 char *tmp;
00081 char *tmpP;
00082 int data_len, encrypt;
00083 ast_aes_encrypt_key ecx;
00084 ast_aes_decrypt_key dcx;
00085
00086 AST_DECLARE_APP_ARGS(args,
00087 AST_APP_ARG(key);
00088 AST_APP_ARG(data);
00089 );
00090
00091 AST_STANDARD_APP_ARGS(args, data);
00092
00093 if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
00094 ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd);
00095 return -1;
00096 }
00097
00098 if (strlen(args.key) != AES_BLOCK_SIZE) {
00099 ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
00100 return -1;
00101 }
00102
00103 ast_aes_set_encrypt_key((unsigned char *) args.key, &ecx);
00104 ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx);
00105 tmp = ast_calloc(1, len);
00106 tmpP = tmp;
00107 encrypt = strcmp("AES_DECRYPT", cmd);
00108
00109 if (encrypt) {
00110 ast_copy_string(tmp, args.data, len);
00111 data_len = strlen(tmp);
00112 } else {
00113 data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
00114 }
00115
00116 if (data_len >= len) {
00117 ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length. Result may be truncated!\n", cmd);
00118 data_len = len - 1;
00119 }
00120
00121 while (data_len > 0) {
00122 memset(curblock, 0, AES_BLOCK_SIZE);
00123 memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
00124 if (encrypt) {
00125 ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
00126 } else {
00127 ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
00128 }
00129 tmpP += AES_BLOCK_SIZE;
00130 data_len -= AES_BLOCK_SIZE;
00131 }
00132
00133 if (encrypt) {
00134 ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
00135 } else {
00136 memcpy(buf, tmp, len);
00137 }
00138 ast_free(tmp);
00139
00140 return 0;
00141 }
00142
00143 static struct ast_custom_function aes_encrypt_function = {
00144 .name = "AES_ENCRYPT",
00145 .read = aes_helper,
00146 };
00147
00148 static struct ast_custom_function aes_decrypt_function = {
00149 .name = "AES_DECRYPT",
00150 .read = aes_helper,
00151 };
00152
00153 static int unload_module(void)
00154 {
00155 int res = ast_custom_function_unregister(&aes_decrypt_function);
00156 return res | ast_custom_function_unregister(&aes_encrypt_function);
00157 }
00158
00159 static int load_module(void)
00160 {
00161 int res = ast_custom_function_register(&aes_decrypt_function);
00162 res |= ast_custom_function_register(&aes_encrypt_function);
00163 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00164 }
00165
00166 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AES dialplan functions",
00167 .load = load_module,
00168 .unload = unload_module,
00169 .nonoptreq = "res_crypto",
00170 );