Asterisk - The Open Source Telephony Project  18.5.0
func_aes.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 /*! \file
18  *
19  * \brief AES encryption/decryption dialplan functions
20  *
21  * \author David Vossel <[email protected]>
22  * \ingroup functions
23  */
24 
25 /*** MODULEINFO
26  <depend>res_crypto</depend>
27  <depend>crypto</depend>
28  <support_level>core</support_level>
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 /*** DOCUMENTATION
41  <function name="AES_ENCRYPT" language="en_US">
42  <synopsis>
43  Encrypt a string with AES given a 16 character key.
44  </synopsis>
45  <syntax>
46  <parameter name="key" required="true">
47  <para>AES Key</para>
48  </parameter>
49  <parameter name="string" required="true">
50  <para>Input string</para>
51  </parameter>
52  </syntax>
53  <description>
54  <para>Returns an AES encrypted string encoded in base64.</para>
55  </description>
56  <see-also>
57  <ref type="function">AES_DECRYPT</ref>
58  <ref type="function">BASE64_ENCODE</ref>
59  <ref type="function">BASE64_DECODE</ref>
60  </see-also>
61  </function>
62  <function name="AES_DECRYPT" language="en_US">
63  <synopsis>
64  Decrypt a string encoded in base64 with AES given a 16 character key.
65  </synopsis>
66  <syntax>
67  <parameter name="key" required="true">
68  <para>AES Key</para>
69  </parameter>
70  <parameter name="string" required="true">
71  <para>Input string.</para>
72  </parameter>
73  </syntax>
74  <description>
75  <para>Returns the plain text string.</para>
76  </description>
77  <see-also>
78  <ref type="function">AES_ENCRYPT</ref>
79  <ref type="function">BASE64_ENCODE</ref>
80  <ref type="function">BASE64_DECODE</ref>
81  </see-also>
82  </function>
83  ***/
84 
85 
86 static int aes_helper(struct ast_channel *chan, const char *cmd, char *data,
87  char *buf, size_t len)
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 }
156 
158  .name = "AES_ENCRYPT",
159  .read = aes_helper,
160 };
161 
163  .name = "AES_DECRYPT",
164  .read = aes_helper,
165 };
166 
167 static int unload_module(void)
168 {
169  int res = ast_custom_function_unregister(&aes_decrypt_function);
170  return res | ast_custom_function_unregister(&aes_encrypt_function);
171 }
172 
173 static int load_module(void)
174 {
175  int res = ast_custom_function_register(&aes_decrypt_function);
176  res |= ast_custom_function_register(&aes_encrypt_function);
178 }
179 
180 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AES dialplan functions",
181  .support_level = AST_MODULE_SUPPORT_CORE,
182  .load = load_module,
183  .unload = unload_module,
184  .requires = "res_crypto",
185 );
const char * name
Definition: pbx.h:119
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
static int load_module(void)
Definition: func_aes.c:173
static int unload_module(void)
Definition: func_aes.c:167
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
Provide cryptographic signature routines.
static int tmp()
Definition: bt_open.c:389
#define AES_BLOCK_SIZE
Definition: func_aes.c:38
const char * args
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
#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
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
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
static struct ast_custom_function aes_decrypt_function
Definition: func_aes.c:162
static int aes_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_aes.c:86
Core PBX routines and definitions.
#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
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static struct ast_custom_function aes_encrypt_function
Definition: func_aes.c:157
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
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
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.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508
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.