Fri Aug 17 00:17:40 2018

Asterisk developer's documentation


func_aes.c File Reference

AES encryption/decryption dialplan functions. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/crypto.h"

Go to the source code of this file.

Defines

#define AES_BLOCK_SIZE   16

Functions

static int aes_helper (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,"AES dialplan functions",.load=load_module,.unload=unload_module,.nonoptreq="res_crypto",)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_custom_function aes_decrypt_function
static struct ast_custom_function aes_encrypt_function

Detailed Description

AES encryption/decryption dialplan functions.

Author:
David Vossel <dvossel@digium.com>

Definition in file func_aes.c.


Define Documentation

#define AES_BLOCK_SIZE   16

Definition at line 39 of file func_aes.c.

Referenced by aes_helper().


Function Documentation

static int aes_helper ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 87 of file func_aes.c.

References AES_BLOCK_SIZE, args, ast_aes_decrypt(), ast_aes_encrypt(), ast_aes_set_decrypt_key(), ast_aes_set_encrypt_key(), AST_APP_ARG, ast_base64decode(), ast_base64encode(), ast_calloc, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_free, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), LOG_ERROR, and LOG_WARNING.

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;                        /*  AES 128 Encryption context */
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) {        /* key must be of 16 characters in length, 128 bits */
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);   /* encryption:  plaintext -> encryptedtext -> base64 */
00115    ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx);   /* decryption:  base64 -> encryptedtext -> plaintext */
00116    tmp = ast_calloc(1, len);                     /* requires a tmp buffer for the base64 decode */
00117    if (!tmp) {
00118       ast_log(LOG_ERROR, "Unable to allocate memory for data\n");
00119       return -1;
00120    }
00121    tmpP = tmp;
00122    encrypt = strcmp("AES_DECRYPT", cmd);           /* -1 if encrypting, 0 if decrypting */
00123 
00124    if (encrypt) {                                  /* if decrypting first decode src to base64 */
00125       ast_copy_string(tmp, args.data, len);
00126       data_len = strlen(tmp);
00127    } else {
00128       data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
00129    }
00130 
00131    if (data_len >= len) {                        /* make sure to not go over buffer len */
00132       ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n", cmd);
00133       data_len = len - 1;
00134    }
00135 
00136    while (data_len > 0) {
00137       memset(curblock, 0, AES_BLOCK_SIZE);
00138       memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
00139       if (encrypt) {
00140          ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
00141       } else {
00142          ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
00143       }
00144       tmpP += AES_BLOCK_SIZE;
00145       data_len -= AES_BLOCK_SIZE;
00146    }
00147 
00148    if (encrypt) {                            /* if encrypting encode result to base64 */
00149       ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
00150    } else {
00151       memcpy(buf, tmp, len);
00152    }
00153    ast_free(tmp);
00154 
00155    return 0;
00156 }

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_DEFAULT  ,
"AES dialplan functions"  ,
load = load_module,
unload = unload_module,
nonoptreq = "res_crypto" 
)
static int load_module ( void   )  [static]
static int unload_module ( void   )  [static]

Definition at line 168 of file func_aes.c.

References ast_custom_function_unregister().


Variable Documentation

Initial value:
 {
   .name = "AES_DECRYPT",
   .read = aes_helper,
}

Definition at line 163 of file func_aes.c.

Initial value:
 {
   .name = "AES_ENCRYPT",
   .read = aes_helper,
}

Definition at line 158 of file func_aes.c.


Generated on 17 Aug 2018 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1