| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2009, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \file
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \brief AES encryption/decryption dialplan functions | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \author David Vossel <dvossel@digium.com> | 
					
						
							|  |  |  |  * \ingroup functions | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-21 19:11:32 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							| 
									
										
										
										
											2020-03-23 18:25:30 +01:00
										 |  |  | 	<depend>res_crypto</depend> | 
					
						
							|  |  |  | 	<depend>crypto</depend> | 
					
						
							| 
									
										
										
										
											2011-07-14 20:28:54 +00:00
										 |  |  | 	<support_level>core</support_level> | 
					
						
							| 
									
										
										
										
											2010-07-21 19:11:32 +00:00
										 |  |  |  ***/ | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/app.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-21 19:11:32 +00:00
										 |  |  | #include "asterisk/crypto.h"
 | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define AES_BLOCK_SIZE 16
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*** DOCUMENTATION
 | 
					
						
							|  |  |  | 	<function name="AES_ENCRYPT" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Encrypt a string with AES given a 16 character key. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax> | 
					
						
							|  |  |  | 			<parameter name="key" required="true"> | 
					
						
							|  |  |  | 				<para>AES Key</para> | 
					
						
							|  |  |  | 			</parameter> | 
					
						
							| 
									
										
										
										
											2009-01-30 18:36:56 +00:00
										 |  |  | 			<parameter name="string" required="true"> | 
					
						
							|  |  |  | 				<para>Input string</para> | 
					
						
							|  |  |  | 			</parameter> | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 		</syntax> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para>Returns an AES encrypted string encoded in base64.</para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							| 
									
										
										
										
											2011-01-14 19:39:22 +00:00
										 |  |  | 		<see-also> | 
					
						
							| 
									
										
										
										
											2011-01-14 20:07:02 +00:00
										 |  |  | 			<ref type="function">AES_DECRYPT</ref> | 
					
						
							|  |  |  | 			<ref type="function">BASE64_ENCODE</ref> | 
					
						
							|  |  |  | 			<ref type="function">BASE64_DECODE</ref> | 
					
						
							| 
									
										
										
										
											2011-01-14 19:39:22 +00:00
										 |  |  | 		</see-also> | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 	</function> | 
					
						
							|  |  |  | 	<function name="AES_DECRYPT" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Decrypt a string encoded in base64 with AES given a 16 character key. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax> | 
					
						
							|  |  |  | 			<parameter name="key" required="true"> | 
					
						
							|  |  |  | 				<para>AES Key</para> | 
					
						
							|  |  |  | 			</parameter> | 
					
						
							| 
									
										
										
										
											2009-01-30 18:36:56 +00:00
										 |  |  | 			<parameter name="string" required="true"> | 
					
						
							|  |  |  | 				<para>Input string.</para> | 
					
						
							|  |  |  | 			</parameter> | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 		</syntax> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para>Returns the plain text string.</para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							| 
									
										
										
										
											2011-01-14 19:39:22 +00:00
										 |  |  | 		<see-also> | 
					
						
							| 
									
										
										
										
											2011-01-14 20:07:02 +00:00
										 |  |  | 			<ref type="function">AES_ENCRYPT</ref> | 
					
						
							|  |  |  | 			<ref type="function">BASE64_ENCODE</ref> | 
					
						
							|  |  |  | 			<ref type="function">BASE64_DECODE</ref> | 
					
						
							| 
									
										
										
										
											2011-01-14 19:39:22 +00:00
										 |  |  | 		</see-also> | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 	</function> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int aes_helper(struct ast_channel *chan, const char *cmd, char *data, | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 	       char *buf, size_t len) | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned char curblock[AES_BLOCK_SIZE] = { 0, }; | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 	char *tmp; | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 	char *tmpP; | 
					
						
							|  |  |  | 	int data_len, encrypt; | 
					
						
							|  |  |  | 	ast_aes_encrypt_key ecx;                        /*  AES 128 Encryption context */ | 
					
						
							|  |  |  | 	ast_aes_decrypt_key dcx; | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 	AST_DECLARE_APP_ARGS(args, | 
					
						
							|  |  |  | 		AST_APP_ARG(key); | 
					
						
							|  |  |  | 		AST_APP_ARG(data); | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_STANDARD_APP_ARGS(args, data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(args.data) || ast_strlen_zero(args.key)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - missing argument!\n", cmd); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 	if (strlen(args.key) != AES_BLOCK_SIZE) {        /* key must be of 16 characters in length, 128 bits */ | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Syntax: %s(<key>,<data>) - <key> parameter must be exactly 16 characters!\n", cmd); | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-21 19:11:32 +00:00
										 |  |  | 	ast_aes_set_encrypt_key((unsigned char *) args.key, &ecx);   /* encryption:  plaintext -> encryptedtext -> base64 */ | 
					
						
							|  |  |  | 	ast_aes_set_decrypt_key((unsigned char *) args.key, &dcx);   /* decryption:  base64 -> encryptedtext -> plaintext */ | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 	tmp = ast_calloc(1, len);                     /* requires a tmp buffer for the base64 decode */ | 
					
						
							| 
									
										
										
										
											2012-05-04 22:17:38 +00:00
										 |  |  | 	if (!tmp) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to allocate memory for data\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-04-29 18:53:01 +00:00
										 |  |  | 	tmpP = tmp; | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 	encrypt = strcmp("AES_DECRYPT", cmd);           /* -1 if encrypting, 0 if decrypting */ | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (encrypt) {                                  /* if decrypting first decode src to base64 */ | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 		ast_copy_string(tmp, args.data, len); | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 		data_len = strlen(tmp); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 		data_len = ast_base64decode((unsigned char *) tmp, args.data, len); | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (data_len >= len) {                        /* make sure to not go over buffer len */ | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Syntax: %s(<keys>,<data>) - <data> exceeds buffer length.  Result may be truncated!\n", cmd); | 
					
						
							|  |  |  | 		data_len = len - 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (data_len > 0) { | 
					
						
							|  |  |  | 		memset(curblock, 0, AES_BLOCK_SIZE); | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 		memcpy(curblock, tmpP, (data_len < AES_BLOCK_SIZE) ? data_len : AES_BLOCK_SIZE); | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 		if (encrypt) { | 
					
						
							| 
									
										
										
										
											2009-01-28 00:17:55 +00:00
										 |  |  | 			ast_aes_encrypt(curblock, (unsigned char *) tmpP, &ecx); | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2009-01-28 00:17:55 +00:00
										 |  |  | 			ast_aes_decrypt(curblock, (unsigned char *) tmpP, &dcx); | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		tmpP += AES_BLOCK_SIZE; | 
					
						
							|  |  |  | 		data_len -= AES_BLOCK_SIZE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (encrypt) {                            /* if encrypting encode result to base64 */ | 
					
						
							| 
									
										
										
										
											2016-03-19 02:32:51 +01:00
										 |  |  | 		ast_base64encode(buf, (unsigned char *) tmp, tmpP - tmp, len); | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 		memcpy(buf, tmp, len); | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	ast_free(tmp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_custom_function aes_encrypt_function = { | 
					
						
							|  |  |  | 	.name = "AES_ENCRYPT", | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 	.read = aes_helper, | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_custom_function aes_decrypt_function = { | 
					
						
							|  |  |  | 	.name = "AES_DECRYPT", | 
					
						
							| 
									
										
										
										
											2010-01-26 15:16:59 +00:00
										 |  |  | 	.read = aes_helper, | 
					
						
							| 
									
										
										
										
											2009-01-27 22:43:36 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int unload_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res = ast_custom_function_unregister(&aes_decrypt_function); | 
					
						
							|  |  |  | 	return res | ast_custom_function_unregister(&aes_encrypt_function); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int load_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res = ast_custom_function_register(&aes_decrypt_function); | 
					
						
							|  |  |  | 	res |= ast_custom_function_register(&aes_encrypt_function); | 
					
						
							|  |  |  | 	return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-02 05:27:53 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "AES dialplan functions", | 
					
						
							| 
									
										
										
										
											2015-05-05 20:49:04 -04:00
										 |  |  | 	.support_level = AST_MODULE_SUPPORT_CORE, | 
					
						
							|  |  |  | 	.load = load_module, | 
					
						
							|  |  |  | 	.unload = unload_module, | 
					
						
							| 
									
										
										
										
											2017-11-19 17:30:49 -05:00
										 |  |  | 	.requires = "res_crypto", | 
					
						
							| 
									
										
										
										
											2015-05-05 20:49:04 -04:00
										 |  |  | ); |