Tue Aug 24 20:35:23 2010

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/aes.h"
Include dependency graph for func_aes.c:

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_STANDARD (ASTERISK_GPL_KEY,"AES dialplan functions")
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 35 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 73 of file func_aes.c.

References AES_BLOCK_SIZE, ast_aes_decrypt, ast_aes_decrypt_key, ast_aes_encrypt, ast_aes_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(), and LOG_WARNING.

00075 {
00076    unsigned char curblock[AES_BLOCK_SIZE] = { 0, };
00077    char *tmp;
00078    char *tmpP;
00079    int data_len, encrypt;
00080    ast_aes_encrypt_key ecx;                        /*  AES 128 Encryption context */
00081    ast_aes_decrypt_key dcx;
00082 
00083    AST_DECLARE_APP_ARGS(args,
00084       AST_APP_ARG(key);
00085       AST_APP_ARG(data);
00086    );
00087 
00088    AST_STANDARD_APP_ARGS(args, data);
00089 
00090    if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
00091       ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd);
00092       return -1;
00093    }
00094 
00095    if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */
00096       ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
00097       return -1;
00098    }
00099 
00100    ast_aes_encrypt_key((unsigned char *) args.key, &ecx);   /* encryption:  plaintext -> encryptedtext -> base64 */
00101    ast_aes_decrypt_key((unsigned char *) args.key, &dcx);   /* decryption:  base64 -> encryptedtext -> plaintext */
00102    tmp = ast_calloc(1, len);                     /* requires a tmp buffer for the base64 decode */
00103    tmpP = tmp;
00104    encrypt = strcmp("AES_DECRYPT", cmd);           /* -1 if encrypting, 0 if decrypting */
00105 
00106    if (encrypt) {                                  /* if decrypting first decode src to base64 */
00107       ast_copy_string(tmp, args.data, len);
00108       data_len = strlen(tmp);
00109    } else {
00110       data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
00111    }
00112 
00113    if (data_len >= len) {                        /* make sure to not go over buffer len */
00114       ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n", cmd);
00115       data_len = len - 1;
00116    }
00117 
00118    while (data_len > 0) {
00119       memset(curblock, 0, AES_BLOCK_SIZE);
00120       memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
00121       if (encrypt) {
00122          ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
00123       } else {
00124          ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
00125       }
00126       tmpP += AES_BLOCK_SIZE;
00127       data_len -= AES_BLOCK_SIZE;
00128    }
00129 
00130    if (encrypt) {                            /* if encrypting encode result to base64 */
00131       ast_base64encode(buf, (unsigned char *) tmp, strlen(tmp), len);
00132    } else {
00133       memcpy(buf, tmp, len);
00134    }
00135    ast_free(tmp);
00136 
00137    return 0;
00138 }

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"AES dialplan functions"   
)
static int load_module ( void   )  [static]
static int unload_module ( void   )  [static]

Definition at line 150 of file func_aes.c.

References ast_custom_function_unregister().


Variable Documentation

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

Definition at line 145 of file func_aes.c.

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

Definition at line 140 of file func_aes.c.


Generated on 24 Aug 2010 for Asterisk - the Open Source PBX by  doxygen 1.6.1