Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
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"
Include dependency graph for func_aes.c:

Go to the source code of this file.

Macros

#define AES_BLOCK_SIZE   16
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int aes_helper (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "AES dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_crypto", }
 
static struct ast_custom_function aes_decrypt_function
 
static struct ast_custom_function aes_encrypt_function
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

AES encryption/decryption dialplan functions.

Author
David Vossel dvoss.nosp@m.el@d.nosp@m.igium.nosp@m..com

Definition in file func_aes.c.

Macro Definition Documentation

◆ AES_BLOCK_SIZE

#define AES_BLOCK_SIZE   16

Definition at line 38 of file func_aes.c.

Referenced by aes_helper().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 185 of file func_aes.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 185 of file func_aes.c.

◆ aes_helper()

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

Definition at line 86 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, LOG_WARNING, and tmp().

88 {
89  unsigned char curblock[AES_BLOCK_SIZE] = { 0, };
90  char *tmp;
91  char *tmpP;
92  int data_len, encrypt;
93  ast_aes_encrypt_key ecx; /* AES 128 Encryption context */
95 
97  AST_APP_ARG(key);
98  AST_APP_ARG(data);
99  );
100 
102 
103  if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
104  ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd);
105  return -1;
106  }
107 
108  if (strlen(args.key) != AES_BLOCK_SIZE) { /* key must be of 16 characters in length, 128 bits */
109  ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
110  return -1;
111  }
112 
113  ast_aes_set_encrypt_key((unsigned char *) args.key, &ecx); /* encryption: plaintext -> encryptedtext -> base64 */
114  ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx); /* decryption: base64 -> encryptedtext -> plaintext */
115  tmp = ast_calloc(1, len); /* requires a tmp buffer for the base64 decode */
116  if (!tmp) {
117  ast_log(LOG_ERROR, "Unable to allocate memory for data\n");
118  return -1;
119  }
120  tmpP = tmp;
121  encrypt = strcmp("AES_DECRYPT", cmd); /* -1 if encrypting, 0 if decrypting */
122 
123  if (encrypt) { /* if decrypting first decode src to base64 */
124  ast_copy_string(tmp, args.data, len);
125  data_len = strlen(tmp);
126  } else {
127  data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
128  }
129 
130  if (data_len >= len) { /* make sure to not go over buffer len */
131  ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length. Result may be truncated!\n", cmd);
132  data_len = len - 1;
133  }
134 
135  while (data_len > 0) {
136  memset(curblock, 0, AES_BLOCK_SIZE);
137  memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
138  if (encrypt) {
139  ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
140  } else {
141  ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
142  }
143  tmpP += AES_BLOCK_SIZE;
144  data_len -= AES_BLOCK_SIZE;
145  }
146 
147  if (encrypt) { /* if encrypting encode result to base64 */
148  ast_base64encode(buf, (unsigned char *) tmp, tmpP - tmp, len);
149  } else {
150  memcpy(buf, tmp, len);
151  }
152  ast_free(tmp);
153 
154  return 0;
155 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
#define AES_BLOCK_SIZE
Definition: func_aes.c:38
const char * args
#define ast_strlen_zero(foo)
Definition: strings.h:52
AES_KEY ast_aes_encrypt_key
Definition: crypto.h:35
int ast_base64decode(unsigned char *dst, const char *src, int max)
Decode data from base64.
Definition: main/utils.c:294
#define ast_log
Definition: astobj2.c:42
void AST_OPTIONAL_API_NAME() ast_aes_encrypt(const unsigned char *in, unsigned char *out, const ast_aes_encrypt_key *ctx)
Definition: res_crypto.c:476
#define LOG_ERROR
Definition: logger.h:285
int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max)
Encode data in base64.
Definition: main/utils.c:404
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
AES_KEY ast_aes_decrypt_key
Definition: crypto.h:36
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
int AST_OPTIONAL_API_NAME() ast_aes_set_encrypt_key(const unsigned char *key, ast_aes_encrypt_key *ctx)
Definition: res_crypto.c:466
int AST_OPTIONAL_API_NAME() ast_aes_set_decrypt_key(const unsigned char *key, ast_aes_decrypt_key *ctx)
Definition: res_crypto.c:471
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
void AST_OPTIONAL_API_NAME() ast_aes_decrypt(const unsigned char *in, unsigned char *out, const ast_aes_decrypt_key *ctx)
Definition: res_crypto.c:481
#define AST_APP_ARG(name)
Define an application argument.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 185 of file func_aes.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 173 of file func_aes.c.

References ast_custom_function_register, AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, and unload_module().

174 {
178 }
static struct ast_custom_function aes_decrypt_function
Definition: func_aes.c:162
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct ast_custom_function aes_encrypt_function
Definition: func_aes.c:157
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 167 of file func_aes.c.

References ast_custom_function_unregister().

Referenced by load_module().

168 {
171 }
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function aes_decrypt_function
Definition: func_aes.c:162
static struct ast_custom_function aes_encrypt_function
Definition: func_aes.c:157

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "AES dialplan functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_crypto", }
static

Definition at line 185 of file func_aes.c.

◆ aes_decrypt_function

struct ast_custom_function aes_decrypt_function
static
Initial value:
= {
.name = "AES_DECRYPT",
.read = aes_helper,
}
static int aes_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_aes.c:86

Definition at line 162 of file func_aes.c.

◆ aes_encrypt_function

struct ast_custom_function aes_encrypt_function
static
Initial value:
= {
.name = "AES_ENCRYPT",
.read = aes_helper,
}
static int aes_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_aes.c:86

Definition at line 157 of file func_aes.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 185 of file func_aes.c.