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
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
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
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
00077
00078
00079
00080
00081
00082
00083
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;
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) {
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);
00115 ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx);
00116 tmp = ast_calloc(1, len);
00117 tmpP = tmp;
00118 encrypt = strcmp("AES_DECRYPT", cmd);
00119
00120 if (encrypt) {
00121 ast_copy_string(tmp, args.data, len);
00122 data_len = strlen(tmp);
00123 } else {
00124 data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
00125 }
00126
00127 if (data_len >= len) {
00128 ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length. Result may be truncated!\n", cmd);
00129 data_len = len - 1;
00130 }
00131
00132 while (data_len > 0) {
00133 memset(curblock, 0, AES_BLOCK_SIZE);
00134 memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
00135 if (encrypt) {
00136 ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
00137 } else {
00138 ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
00139 }
00140 tmpP += AES_BLOCK_SIZE;
00141 data_len -= AES_BLOCK_SIZE;
00142 }
00143
00144 if (encrypt) {
00145 ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
00146 } else {
00147 memcpy(buf, tmp, len);
00148 }
00149 ast_free(tmp);
00150
00151 return 0;
00152 }
00153
00154 static struct ast_custom_function aes_encrypt_function = {
00155 .name = "AES_ENCRYPT",
00156 .read = aes_helper,
00157 };
00158
00159 static struct ast_custom_function aes_decrypt_function = {
00160 .name = "AES_DECRYPT",
00161 .read = aes_helper,
00162 };
00163
00164 static int unload_module(void)
00165 {
00166 int res = ast_custom_function_unregister(&aes_decrypt_function);
00167 return res | ast_custom_function_unregister(&aes_encrypt_function);
00168 }
00169
00170 static int load_module(void)
00171 {
00172 int res = ast_custom_function_register(&aes_decrypt_function);
00173 res |= ast_custom_function_register(&aes_encrypt_function);
00174 return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00175 }
00176
00177 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AES dialplan functions",
00178 .load = load_module,
00179 .unload = unload_module,
00180 .nonoptreq = "res_crypto",
00181 );