channel: Clear channel flag in error branch.
[asterisk/asterisk.git] / funcs / func_aes.c
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 <dvossel@digium.com>
22  * \ingroup functions
23  */
24
25 /*** MODULEINFO
26         <use type="external">crypto</use>
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/module.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/app.h"
35 #include "asterisk/crypto.h"
36
37 #define AES_BLOCK_SIZE 16
38
39 /*** DOCUMENTATION
40         <function name="AES_ENCRYPT" language="en_US">
41                 <synopsis>
42                         Encrypt a string with AES given a 16 character key.
43                 </synopsis>
44                 <syntax>
45                         <parameter name="key" required="true">
46                                 <para>AES Key</para>
47                         </parameter>
48                         <parameter name="string" required="true">
49                                 <para>Input string</para>
50                         </parameter>
51                 </syntax>
52                 <description>
53                         <para>Returns an AES encrypted string encoded in base64.</para>
54                 </description>
55                 <see-also>
56                         <ref type="function">AES_DECRYPT</ref>
57                         <ref type="function">BASE64_ENCODE</ref>
58                         <ref type="function">BASE64_DECODE</ref>
59                 </see-also>
60         </function>
61         <function name="AES_DECRYPT" language="en_US">
62                 <synopsis>
63                         Decrypt a string encoded in base64 with AES given a 16 character key.
64                 </synopsis>
65                 <syntax>
66                         <parameter name="key" required="true">
67                                 <para>AES Key</para>
68                         </parameter>
69                         <parameter name="string" required="true">
70                                 <para>Input string.</para>
71                         </parameter>
72                 </syntax>
73                 <description>
74                         <para>Returns the plain text string.</para>
75                 </description>
76                 <see-also>
77                         <ref type="function">AES_ENCRYPT</ref>
78                         <ref type="function">BASE64_ENCODE</ref>
79                         <ref type="function">BASE64_DECODE</ref>
80                 </see-also>
81         </function>
82  ***/
83
84
85 static int aes_helper(struct ast_channel *chan, const char *cmd, char *data,
86                char *buf, size_t len)
87 {
88         unsigned char curblock[AES_BLOCK_SIZE] = { 0, };
89         char *tmp;
90         char *tmpP;
91         int data_len, encrypt;
92         ast_aes_encrypt_key ecx;                        /*  AES 128 Encryption context */
93         ast_aes_decrypt_key dcx;
94
95         AST_DECLARE_APP_ARGS(args,
96                 AST_APP_ARG(key);
97                 AST_APP_ARG(data);
98         );
99
100         AST_STANDARD_APP_ARGS(args, data);
101
102         if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) {
103                 ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd);
104                 return -1;
105         }
106
107         if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */
108                 ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd);
109                 return -1;
110         }
111
112         ast_aes_set_encrypt_key((unsigned char *) args.key, &ecx);   /* encryption:  plaintext -> encryptedtext -> base64 */
113         ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx);   /* decryption:  base64 -> encryptedtext -> plaintext */
114         tmp = ast_calloc(1, len);                     /* requires a tmp buffer for the base64 decode */
115         if (!tmp) {
116                 ast_log(LOG_ERROR, "Unable to allocate memory for data\n");
117                 return -1;
118         }
119         tmpP = tmp;
120         encrypt = strcmp("AES_DECRYPT", cmd);           /* -1 if encrypting, 0 if decrypting */
121
122         if (encrypt) {                                  /* if decrypting first decode src to base64 */
123                 ast_copy_string(tmp, args.data, len);
124                 data_len = strlen(tmp);
125         } else {
126                 data_len = ast_base64decode((unsigned char *) tmp, args.data, len);
127         }
128
129         if (data_len >= len) {                        /* make sure to not go over buffer len */
130                 ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n", cmd);
131                 data_len = len - 1;
132         }
133
134         while (data_len > 0) {
135                 memset(curblock, 0, AES_BLOCK_SIZE);
136                 memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE);
137                 if (encrypt) {
138                         ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx);
139                 } else {
140                         ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx);
141                 }
142                 tmpP += AES_BLOCK_SIZE;
143                 data_len -= AES_BLOCK_SIZE;
144         }
145
146         if (encrypt) {                            /* if encrypting encode result to base64 */
147                 ast_base64encode(buf, (unsigned char *) tmp, tmpP - tmp, len);
148         } else {
149                 memcpy(buf, tmp, len);
150         }
151         ast_free(tmp);
152
153         return 0;
154 }
155
156 static struct ast_custom_function aes_encrypt_function = {
157         .name = "AES_ENCRYPT",
158         .read = aes_helper,
159 };
160
161 static struct ast_custom_function aes_decrypt_function = {
162         .name = "AES_DECRYPT",
163         .read = aes_helper,
164 };
165
166 static int unload_module(void)
167 {
168         int res = ast_custom_function_unregister(&aes_decrypt_function);
169         return res | ast_custom_function_unregister(&aes_encrypt_function);
170 }
171
172 static int load_module(void)
173 {
174         int res = ast_custom_function_register(&aes_decrypt_function);
175         res |= ast_custom_function_register(&aes_encrypt_function);
176         return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
177 }
178
179 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AES dialplan functions",
180         .support_level = AST_MODULE_SUPPORT_CORE,
181         .load = load_module,
182         .unload = unload_module,
183         .nonoptreq = "res_crypto",
184 );