| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/pbx.h" |
| 35 |
#include "asterisk/app.h" |
| 36 |
#include "asterisk/crypto.h" |
| 37 |
|
| 38 |
#define AES_BLOCK_SIZE 16 |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static int aes_helper(struct ast_channel *chan, const char *cmd, char *data, |
| 93 |
char *buf, size_t len) |
| 94 |
{ |
| 95 |
unsigned char curblock[AES_BLOCK_SIZE] = { 0, }; |
| 96 |
char *tmp; |
| 97 |
char *tmpP; |
| 98 |
int data_len, encrypt; |
| 99 |
ast_aes_encrypt_key ecx; |
| 100 |
ast_aes_decrypt_key dcx; |
| 101 |
|
| 102 |
AST_DECLARE_APP_ARGS(args, |
| 103 |
AST_APP_ARG(key); |
| 104 |
AST_APP_ARG(data); |
| 105 |
); |
| 106 |
|
| 107 |
AST_STANDARD_APP_ARGS(args, data); |
| 108 |
|
| 109 |
if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) { |
| 110 |
ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd); |
| 111 |
return -1; |
| 112 |
} |
| 113 |
|
| 114 |
if (strlen(args.key) != AES_BLOCK_SIZE) { |
| 115 |
ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
|
| 119 |
ast_aes_set_encrypt_key((unsigned char *) args.key, &ecx); |
| 120 |
ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx); |
| 121 |
tmp = ast_calloc(1, len); |
| 122 |
if (!tmp) { |
| 123 |
ast_log(LOG_ERROR, "Unable to allocate memory for data\n"); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
tmpP = tmp; |
| 127 |
encrypt = strcmp("AES_DECRYPT", cmd); |
| 128 |
|
| 129 |
if (encrypt) { |
| 130 |
ast_copy_string(tmp, args.data, len); |
| 131 |
data_len = strlen(tmp); |
| 132 |
} else { |
| 133 |
data_len = ast_base64decode((unsigned char *) tmp, args.data, len); |
| 134 |
} |
| 135 |
|
| 136 |
if (data_len >= len) { |
| 137 |
ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length. Result may be truncated!\n", cmd); |
| 138 |
data_len = len - 1; |
| 139 |
} |
| 140 |
|
| 141 |
while (data_len > 0) { |
| 142 |
memset(curblock, 0, AES_BLOCK_SIZE); |
| 143 |
memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE); |
| 144 |
if (encrypt) { |
| 145 |
ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx); |
| 146 |
} else { |
| 147 |
ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx); |
| 148 |
} |
| 149 |
tmpP += AES_BLOCK_SIZE; |
| 150 |
data_len -= AES_BLOCK_SIZE; |
| 151 |
} |
| 152 |
|
| 153 |
if (encrypt) { |
| 154 |
ast_base64encode(buf, (unsigned char *) tmp, tmpP - tmp, len); |
| 155 |
} else { |
| 156 |
memcpy(buf, tmp, len); |
| 157 |
} |
| 158 |
ast_free(tmp); |
| 159 |
|
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static struct ast_custom_function aes_encrypt_function = { |
| 164 |
.name = "AES_ENCRYPT", |
| 165 |
.read = aes_helper, |
| 166 |
}; |
| 167 |
|
| 168 |
static struct ast_custom_function aes_decrypt_function = { |
| 169 |
.name = "AES_DECRYPT", |
| 170 |
.read = aes_helper, |
| 171 |
}; |
| 172 |
|
| 173 |
static int unload_module(void) |
| 174 |
{ |
| 175 |
int res = ast_custom_function_unregister(&aes_decrypt_function); |
| 176 |
return res | ast_custom_function_unregister(&aes_encrypt_function); |
| 177 |
} |
| 178 |
|
| 179 |
static int load_module(void) |
| 180 |
{ |
| 181 |
int res = ast_custom_function_register(&aes_decrypt_function); |
| 182 |
res |= ast_custom_function_register(&aes_encrypt_function); |
| 183 |
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 184 |
} |
| 185 |
|
| 186 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AES dialplan functions", |
| 187 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 188 |
.load = load_module, |
| 189 |
.unload = unload_module, |
| 190 |
.requires = "res_crypto", |
| 191 |
); |