[asterisk-commits] kmoore: branch kmoore/pimp_sip_srtp r385986 - in /team/kmoore/pimp_sip_srtp: ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Apr 17 16:05:51 CDT 2013


Author: kmoore
Date: Wed Apr 17 16:05:47 2013
New Revision: 385986

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=385986
Log:
Move the sdp/srtp parsers to a more accessible location

Added:
    team/kmoore/pimp_sip_srtp/include/asterisk/sdp_srtp.h   (with props)
    team/kmoore/pimp_sip_srtp/main/sdp_srtp.c   (with props)
Removed:
    team/kmoore/pimp_sip_srtp/channels/sip/include/sdp_crypto.h
    team/kmoore/pimp_sip_srtp/channels/sip/include/srtp.h
    team/kmoore/pimp_sip_srtp/channels/sip/sdp_crypto.c
    team/kmoore/pimp_sip_srtp/channels/sip/srtp.c
Modified:
    team/kmoore/pimp_sip_srtp/channels/chan_sip.c
    team/kmoore/pimp_sip_srtp/channels/sip/include/sip.h

Modified: team/kmoore/pimp_sip_srtp/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/pimp_sip_srtp/channels/chan_sip.c?view=diff&rev=385986&r1=385985&r2=385986
==============================================================================
--- team/kmoore/pimp_sip_srtp/channels/chan_sip.c (original)
+++ team/kmoore/pimp_sip_srtp/channels/chan_sip.c Wed Apr 17 16:05:47 2013
@@ -286,8 +286,7 @@
 #include "sip/include/config_parser.h"
 #include "sip/include/reqresp_parser.h"
 #include "sip/include/sip_utils.h"
-#include "sip/include/srtp.h"
-#include "sip/include/sdp_crypto.h"
+#include "asterisk/sdp_srtp.h"
 #include "asterisk/ccss.h"
 #include "asterisk/xml.h"
 #include "sip/include/dialog.h"
@@ -1487,8 +1486,8 @@
 static void handle_response(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, uint32_t seqno);
 
 /*------ SRTP Support -------- */
-static int setup_srtp(struct sip_srtp **srtp);
-static int process_crypto(struct sip_pvt *p, struct ast_rtp_instance *rtp, struct sip_srtp **srtp, const char *a);
+static int setup_srtp(struct ast_sdp_srtp **srtp);
+static int process_crypto(struct sip_pvt *p, struct ast_rtp_instance *rtp, struct ast_sdp_srtp **srtp, const char *a);
 
 /*------ T38 Support --------- */
 static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
@@ -5913,7 +5912,7 @@
 }
 
 /*! \brief Initialize DTLS-SRTP support on an RTP instance */
-static int dialog_initialize_dtls_srtp(const struct sip_pvt *dialog, struct ast_rtp_instance *rtp, struct sip_srtp **srtp)
+static int dialog_initialize_dtls_srtp(const struct sip_pvt *dialog, struct ast_rtp_instance *rtp, struct ast_sdp_srtp **srtp)
 {
 	struct ast_rtp_engine_dtls *dtls;
 
@@ -5938,7 +5937,7 @@
 		return -1;
 	}
 
-	if (!(*srtp = sip_srtp_alloc())) {
+	if (!(*srtp = ast_sdp_srtp_alloc())) {
 		ast_log(LOG_ERROR, "Failed to create required SRTP structure on RTP instance '%p'\n",
 			rtp);
 		return -1;
@@ -6688,17 +6687,17 @@
 	destroy_msg_headers(p);
 
 	if (p->srtp) {
-		sip_srtp_destroy(p->srtp);
+		ast_sdp_srtp_destroy(p->srtp);
 		p->srtp = NULL;
 	}
 
 	if (p->vsrtp) {
-		sip_srtp_destroy(p->vsrtp);
+		ast_sdp_srtp_destroy(p->vsrtp);
 		p->vsrtp = NULL;
 	}
 
 	if (p->tsrtp) {
-		sip_srtp_destroy(p->tsrtp);
+		ast_sdp_srtp_destroy(p->tsrtp);
 		p->tsrtp = NULL;
 	}
 
@@ -10151,7 +10150,7 @@
 					secure_audio = 1;
 
 					if (p->srtp) {
-						ast_set_flag(p->srtp, SRTP_CRYPTO_OFFER_OK);
+						ast_set_flag(p->srtp, AST_SRTP_CRYPTO_OFFER_OK);
 					}
 				} else if (!strcmp(protocol, "RTP/SAVP") || !strcmp(protocol, "RTP/SAVPF")) {
 					secure_audio = 1;
@@ -10232,8 +10231,8 @@
 				} else if (!strcmp(protocol, "UDP/TLS/RTP/SAVP") || !strcmp(protocol, "UDP/TLS/RTP/SAVPF")) {
 					secure_video = 1;
 
-					if (p->vsrtp || (p->vsrtp = sip_srtp_alloc())) {
-						ast_set_flag(p->vsrtp, SRTP_CRYPTO_OFFER_OK);
+					if (p->vsrtp || (p->vsrtp = ast_sdp_srtp_alloc())) {
+						ast_set_flag(p->vsrtp, AST_SRTP_CRYPTO_OFFER_OK);
 					}
 				} else if (!strcmp(protocol, "RTP/SAVP") || !strcmp(protocol, "RTP/SAVPF")) {
 					secure_video = 1;
@@ -10513,7 +10512,7 @@
 		goto process_sdp_cleanup;
 	}
 
-	if (secure_audio && !(p->srtp && (ast_test_flag(p->srtp, SRTP_CRYPTO_OFFER_OK)))) {
+	if (secure_audio && !(p->srtp && (ast_test_flag(p->srtp, AST_SRTP_CRYPTO_OFFER_OK)))) {
 		ast_log(LOG_WARNING, "Can't provide secure audio requested in SDP offer\n");
 		res = -1;
 		goto process_sdp_cleanup;
@@ -10525,7 +10524,7 @@
 		goto process_sdp_cleanup;
 	}
 
-	if (secure_video && !(p->vsrtp && (ast_test_flag(p->vsrtp, SRTP_CRYPTO_OFFER_OK)))) {
+	if (secure_video && !(p->vsrtp && (ast_test_flag(p->vsrtp, AST_SRTP_CRYPTO_OFFER_OK)))) {
 		ast_log(LOG_WARNING, "Can't provide secure video requested in SDP offer\n");
 		res = -1;
 		goto process_sdp_cleanup;
@@ -12992,14 +12991,14 @@
 	}
 }
 
-static void get_crypto_attrib(struct sip_pvt *p, struct sip_srtp *srtp, const char **a_crypto)
+static void get_crypto_attrib(struct sip_pvt *p, struct ast_sdp_srtp *srtp, const char **a_crypto)
 {
 	int taglen = 80;
 
 	/* Set encryption properties */
 	if (srtp) {
 		if (!srtp->crypto) {
-			srtp->crypto = sdp_crypto_setup();
+			srtp->crypto = ast_sdp_crypto_setup();
 		}
 
 		if (p->dtls_cfg.enabled) {
@@ -13008,15 +13007,15 @@
 		}
 
 		/* set the key length based on INVITE or settings */
-		if (ast_test_flag(srtp, SRTP_CRYPTO_TAG_80)) {
+		if (ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_80)) {
 			taglen = 80;
 		} else if (ast_test_flag(&p->flags[2], SIP_PAGE3_SRTP_TAG_32) ||
-		    ast_test_flag(srtp, SRTP_CRYPTO_TAG_32)) {
+		    ast_test_flag(srtp, AST_SRTP_CRYPTO_TAG_32)) {
 			taglen = 32;
 		}
 
-		if (srtp->crypto && (sdp_crypto_offer(srtp->crypto, taglen) >= 0)) {
-			*a_crypto = sdp_crypto_attrib(srtp->crypto);
+		if (srtp->crypto && (ast_sdp_crypto_offer(srtp->crypto, taglen) >= 0)) {
+			*a_crypto = ast_sdp_crypto_attrib(srtp->crypto);
 		}
 
 		if (!*a_crypto) {
@@ -26017,7 +26016,7 @@
 				transmit_response_with_t38_sdp(p, "200 OK", req, (reinvite ? XMIT_RELIABLE : (req->ignore ?  XMIT_UNRELIABLE : XMIT_CRITICAL)));
 			} else if ((p->t38.state == T38_DISABLED) || (p->t38.state == T38_REJECTED)) {
 				/* If this is not a re-invite or something to ignore - it's critical */
-				if (p->srtp && !ast_test_flag(p->srtp, SRTP_CRYPTO_OFFER_OK)) {
+				if (p->srtp && !ast_test_flag(p->srtp, AST_SRTP_CRYPTO_OFFER_OK)) {
 					ast_log(LOG_WARNING, "Target does not support required crypto\n");
 					transmit_response_reliable(p, "488 Not Acceptable Here (crypto)", req);
 				} else {
@@ -33301,21 +33300,21 @@
 }
 
 /* SRTP */
-static int setup_srtp(struct sip_srtp **srtp)
+static int setup_srtp(struct ast_sdp_srtp **srtp)
 {
 	if (!ast_rtp_engine_srtp_is_registered()) {
 		ast_debug(1, "No SRTP module loaded, can't setup SRTP session.\n");
 		return -1;
 	}
 
-	if (!(*srtp = sip_srtp_alloc())) { /* Allocate SRTP data structure */
+	if (!(*srtp = ast_sdp_srtp_alloc())) { /* Allocate SRTP data structure */
 		return -1;
 	}
 
 	return 0;
 }
 
-static int process_crypto(struct sip_pvt *p, struct ast_rtp_instance *rtp, struct sip_srtp **srtp, const char *a)
+static int process_crypto(struct sip_pvt *p, struct ast_rtp_instance *rtp, struct ast_sdp_srtp **srtp, const char *a)
 {
 	struct ast_rtp_engine_dtls *dtls;
 
@@ -33339,15 +33338,15 @@
 		}
 	}
 
-	if (!(*srtp)->crypto && !((*srtp)->crypto = sdp_crypto_setup())) {
+	if (!(*srtp)->crypto && !((*srtp)->crypto = ast_sdp_crypto_setup())) {
 		return FALSE;
 	}
 
-	if (sdp_crypto_process((*srtp)->crypto, a, rtp, *srtp) < 0) {
+	if (ast_sdp_crypto_process((*srtp)->crypto, a, rtp, *srtp) < 0) {
 		return FALSE;
 	}
 
-	ast_set_flag(*srtp, SRTP_CRYPTO_OFFER_OK);
+	ast_set_flag(*srtp, AST_SRTP_CRYPTO_OFFER_OK);
 
 	if ((dtls = ast_rtp_instance_get_dtls(rtp))) {
 		dtls->stop(rtp);

Modified: team/kmoore/pimp_sip_srtp/channels/sip/include/sip.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/pimp_sip_srtp/channels/sip/include/sip.h?view=diff&rev=385986&r1=385985&r2=385986
==============================================================================
--- team/kmoore/pimp_sip_srtp/channels/sip/include/sip.h (original)
+++ team/kmoore/pimp_sip_srtp/channels/sip/include/sip.h Wed Apr 17 16:05:47 2013
@@ -1189,9 +1189,9 @@
 	AST_LIST_HEAD_NOLOCK(request_queue, sip_request) request_queue; /*!< Requests that arrived but could not be processed immediately */
 	struct sip_invite_param *options;   /*!< Options for INVITE */
 	struct sip_st_dlg *stimer;          /*!< SIP Session-Timers */
-	struct sip_srtp *srtp;              /*!< Structure to hold Secure RTP session data for audio */
-	struct sip_srtp *vsrtp;             /*!< Structure to hold Secure RTP session data for video */
-	struct sip_srtp *tsrtp;             /*!< Structure to hold Secure RTP session data for text */
+	struct ast_sdp_srtp *srtp;              /*!< Structure to hold Secure RTP session data for audio */
+	struct ast_sdp_srtp *vsrtp;             /*!< Structure to hold Secure RTP session data for video */
+	struct ast_sdp_srtp *tsrtp;             /*!< Structure to hold Secure RTP session data for text */
 
 	int red;                            /*!< T.140 RTP Redundancy */
 	int hangupcause;                    /*!< Storage of hangupcause copied from our owner before we disconnect from the AST channel (only used at hangup) */

Added: team/kmoore/pimp_sip_srtp/include/asterisk/sdp_srtp.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/pimp_sip_srtp/include/asterisk/sdp_srtp.h?view=auto&rev=385986
==============================================================================
--- team/kmoore/pimp_sip_srtp/include/asterisk/sdp_srtp.h (added)
+++ team/kmoore/pimp_sip_srtp/include/asterisk/sdp_srtp.h Wed Apr 17 16:05:47 2013
@@ -1,0 +1,109 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2006 - 2007, Mikael Magnusson
+ *
+ * Mikael Magnusson <mikma at users.sourceforge.net>
+ *
+ * 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 sdp_srtp.h
+ *
+ * \brief SRTP and SDP Security descriptions
+ *
+ * Specified in RFC 4568
+ * Specified in RFC 3711
+ *
+ * \author Mikael Magnusson <mikma at users.sourceforge.net>
+ */
+
+#ifndef _SDP_SRTP_H
+#define _SDP_SRTP_H
+
+#include <asterisk/rtp_engine.h>
+
+struct ast_sdp_crypto;
+
+/*! \brief structure for secure RTP audio */
+struct ast_sdp_srtp {
+	unsigned int flags;
+	struct ast_sdp_crypto *crypto;
+};
+
+/* SRTP flags */
+#define AST_SRTP_CRYPTO_OFFER_OK	(1 << 1)
+#define AST_SRTP_CRYPTO_TAG_32		(1 << 2)
+#define AST_SRTP_CRYPTO_TAG_80		(1 << 3)
+
+/*!
+ * \brief allocate a ast_sdp_srtp structure
+ * \retval a new malloc'd ast_sdp_srtp structure on success
+ * \retval NULL on failure
+*/
+struct ast_sdp_srtp *ast_sdp_srtp_alloc(void);
+
+/*!
+ * \brief free a ast_sdp_srtp structure
+ * \param srtp a ast_sdp_srtp structure
+*/
+void ast_sdp_srtp_destroy(struct ast_sdp_srtp *srtp);
+
+/*! \brief Initialize an return an ast_sdp_crypto struct
+ *
+ * \details
+ * This function allocates a new ast_sdp_crypto struct and initializes its values
+ *
+ * \retval NULL on failure
+ * \retval a pointer to a  new ast_sdp_crypto structure
+ */
+struct ast_sdp_crypto *ast_sdp_crypto_setup(void);
+
+/*! \brief Destroy a previously allocated ast_sdp_crypto struct */
+void ast_sdp_crypto_destroy(struct ast_sdp_crypto *crypto);
+
+/*! \brief Parse the a=crypto line from SDP and set appropriate values on the
+ * ast_sdp_crypto struct.
+ *
+ * \param p A valid ast_sdp_crypto struct
+ * \param attr the a:crypto line from SDP
+ * \param rtp The rtp instance associated with the SDP being parsed
+ * \param srtp SRTP structure
+ *
+ * \retval 0 success
+ * \retval nonzero failure
+ */
+int ast_sdp_crypto_process(struct ast_sdp_crypto *p, const char *attr, struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp);
+
+
+/*! \brief Generate an SRTP a=crypto offer
+ *
+ * \details
+ * The offer is stored on the ast_sdp_crypto struct in a_crypto
+ *
+ * \param p A valid ast_sdp_crypto struct
+ * \param taglen Length
+ *
+ * \retval 0 success
+ * \retval nonzero failure
+ */
+int ast_sdp_crypto_offer(struct ast_sdp_crypto *p, int taglen);
+
+
+/*! \brief Return the a_crypto value of the ast_sdp_crypto struct
+ *
+ * \param p An ast_sdp_crypto struct that has had ast_sdp_crypto_offer called
+ *
+ * \retval The value of the a_crypto for p
+ */
+const char *ast_sdp_crypto_attrib(struct ast_sdp_crypto *p);
+
+#endif	/* _SDP_CRYPTO_H */

Propchange: team/kmoore/pimp_sip_srtp/include/asterisk/sdp_srtp.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/kmoore/pimp_sip_srtp/include/asterisk/sdp_srtp.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/kmoore/pimp_sip_srtp/include/asterisk/sdp_srtp.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/kmoore/pimp_sip_srtp/main/sdp_srtp.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/pimp_sip_srtp/main/sdp_srtp.c?view=auto&rev=385986
==============================================================================
--- team/kmoore/pimp_sip_srtp/main/sdp_srtp.c (added)
+++ team/kmoore/pimp_sip_srtp/main/sdp_srtp.c Wed Apr 17 16:05:47 2013
@@ -1,0 +1,337 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2006 - 2007, Mikael Magnusson
+ *
+ * Mikael Magnusson <mikma at users.sourceforge.net>
+ *
+ * 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 ast_sdp_crypto.c
+ *
+ * \brief SRTP and SDP Security descriptions
+ *
+ * Specified in RFC 3711
+ * Specified in RFC 4568
+ *
+ * \author Mikael Magnusson <mikma at users.sourceforge.net>
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/options.h"
+#include "asterisk/utils.h"
+#include "asterisk/sdp_srtp.h"
+
+#define SRTP_MASTER_LEN 30
+#define SRTP_MASTERKEY_LEN 16
+#define SRTP_MASTERSALT_LEN ((SRTP_MASTER_LEN) - (SRTP_MASTERKEY_LEN))
+#define SRTP_MASTER_LEN64 (((SRTP_MASTER_LEN) * 8 + 5) / 6 + 1)
+
+extern struct ast_srtp_res *res_srtp;
+extern struct ast_srtp_policy_res *res_srtp_policy;
+
+struct ast_sdp_srtp *ast_sdp_srtp_alloc(void)
+{
+	struct ast_sdp_srtp *srtp;
+
+	srtp = ast_calloc(1, sizeof(*srtp));
+
+	return srtp;
+}
+
+void ast_sdp_srtp_destroy(struct ast_sdp_srtp *srtp)
+{
+	if (srtp->crypto) {
+		ast_sdp_crypto_destroy(srtp->crypto);
+	}
+	srtp->crypto = NULL;
+	ast_free(srtp);
+}
+
+struct ast_sdp_crypto {
+	char *a_crypto;
+	unsigned char local_key[SRTP_MASTER_LEN];
+	char *tag;
+	char local_key64[SRTP_MASTER_LEN64];
+	unsigned char remote_key[SRTP_MASTER_LEN];
+};
+
+static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound);
+
+static struct ast_sdp_crypto *ast_sdp_crypto_alloc(void)
+{
+	return ast_calloc(1, sizeof(struct ast_sdp_crypto));
+}
+
+void ast_sdp_crypto_destroy(struct ast_sdp_crypto *crypto)
+{
+	ast_free(crypto->a_crypto);
+	crypto->a_crypto = NULL;
+	ast_free(crypto->tag);
+	crypto->tag = NULL;
+	ast_free(crypto);
+}
+
+struct ast_sdp_crypto *ast_sdp_crypto_setup(void)
+{
+	struct ast_sdp_crypto *p;
+	int key_len;
+	unsigned char remote_key[SRTP_MASTER_LEN];
+
+	if (!ast_rtp_engine_srtp_is_registered()) {
+		return NULL;
+	}
+
+	if (!(p = ast_sdp_crypto_alloc())) {
+		return NULL;
+	}
+
+	if (res_srtp->get_random(p->local_key, sizeof(p->local_key)) < 0) {
+		ast_sdp_crypto_destroy(p);
+		return NULL;
+	}
+
+	ast_base64encode(p->local_key64, p->local_key, SRTP_MASTER_LEN, sizeof(p->local_key64));
+
+	key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key));
+
+	if (key_len != SRTP_MASTER_LEN) {
+		ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", key_len, SRTP_MASTER_LEN);
+		ast_free(p);
+		return NULL;
+	}
+
+	if (memcmp(remote_key, p->local_key, SRTP_MASTER_LEN)) {
+		ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
+		ast_free(p);
+		return NULL;
+	}
+
+	ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64));
+
+	return p;
+}
+
+static int set_crypto_policy(struct ast_srtp_policy *policy, int suite_val, const unsigned char *master_key, unsigned long ssrc, int inbound)
+{
+	const unsigned char *master_salt = NULL;
+
+	if (!ast_rtp_engine_srtp_is_registered()) {
+		return -1;
+	}
+
+	master_salt = master_key + SRTP_MASTERKEY_LEN;
+	if (res_srtp_policy->set_master_key(policy, master_key, SRTP_MASTERKEY_LEN, master_salt, SRTP_MASTERSALT_LEN) < 0) {
+		return -1;
+	}
+
+	if (res_srtp_policy->set_suite(policy, suite_val)) {
+		ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
+		return -1;
+	}
+
+	res_srtp_policy->set_ssrc(policy, ssrc, inbound);
+
+	return 0;
+}
+
+static int ast_sdp_crypto_activate(struct ast_sdp_crypto *p, int suite_val, unsigned char *remote_key, struct ast_rtp_instance *rtp)
+{
+	struct ast_srtp_policy *local_policy = NULL;
+	struct ast_srtp_policy *remote_policy = NULL;
+	struct ast_rtp_instance_stats stats = {0,};
+	int res = -1;
+
+	if (!ast_rtp_engine_srtp_is_registered()) {
+		return -1;
+	}
+
+	if (!p) {
+		return -1;
+	}
+
+	if (!(local_policy = res_srtp_policy->alloc())) {
+		return -1;
+	}
+
+	if (!(remote_policy = res_srtp_policy->alloc())) {
+		goto err;
+	}
+
+	if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_LOCAL_SSRC)) {
+		goto err;
+	}
+
+	if (set_crypto_policy(local_policy, suite_val, p->local_key, stats.local_ssrc, 0) < 0) {
+		goto err;
+	}
+
+	if (set_crypto_policy(remote_policy, suite_val, remote_key, 0, 1) < 0) {
+		goto err;
+	}
+
+	/* Add the SRTP policies */
+	if (ast_rtp_instance_add_srtp_policy(rtp, remote_policy, local_policy)) {
+		ast_log(LOG_WARNING, "Could not set SRTP policies\n");
+		goto err;
+	}
+
+	ast_debug(1 , "SRTP policy activated\n");
+	res = 0;
+
+err:
+	if (local_policy) {
+		res_srtp_policy->destroy(local_policy);
+	}
+
+	if (remote_policy) {
+		res_srtp_policy->destroy(remote_policy);
+	}
+
+	return res;
+}
+
+int ast_sdp_crypto_process(struct ast_sdp_crypto *p, const char *attr, struct ast_rtp_instance *rtp, struct ast_sdp_srtp *srtp)
+{
+	char *str = NULL;
+	char *tag = NULL;
+	char *suite = NULL;
+	char *key_params = NULL;
+	char *key_param = NULL;
+	char *session_params = NULL;
+	char *key_salt = NULL;
+	char *lifetime = NULL;
+	int found = 0;
+	int key_len = 0;
+	int suite_val = 0;
+	unsigned char remote_key[SRTP_MASTER_LEN];
+	int taglen = 0;
+
+	if (!ast_rtp_engine_srtp_is_registered()) {
+		return -1;
+	}
+
+	str = ast_strdupa(attr);
+
+	strsep(&str, ":");
+	tag = strsep(&str, " ");
+	suite = strsep(&str, " ");
+	key_params = strsep(&str, " ");
+	session_params = strsep(&str, " ");
+
+	if (!tag || !suite) {
+		ast_log(LOG_WARNING, "Unrecognized a=%s", attr);
+		return -1;
+	}
+
+	if (!ast_strlen_zero(session_params)) {
+		ast_log(LOG_WARNING, "Unsupported crypto parameters: %s", session_params);
+		return -1;
+	}
+
+	if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_80")) {
+		suite_val = AST_AES_CM_128_HMAC_SHA1_80;
+		ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_80);
+		taglen = 80;
+	} else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
+		suite_val = AST_AES_CM_128_HMAC_SHA1_32;
+		ast_set_flag(srtp, AST_SRTP_CRYPTO_TAG_32);
+		taglen = 32;
+	} else {
+		ast_log(LOG_WARNING, "Unsupported crypto suite: %s\n", suite);
+		return -1;
+	}
+
+	while ((key_param = strsep(&key_params, ";"))) {
+		char *method = NULL;
+		char *info = NULL;
+
+		method = strsep(&key_param, ":");
+		info = strsep(&key_param, ";");
+
+		if (!strcmp(method, "inline")) {
+			key_salt = strsep(&info, "|");
+			lifetime = strsep(&info, "|");
+
+			if (lifetime) {
+				ast_log(LOG_NOTICE, "Crypto life time unsupported: %s\n", attr);
+				continue;
+			}
+
+			found = 1;
+			break;
+		}
+	}
+
+	if (!found) {
+		ast_log(LOG_NOTICE, "SRTP crypto offer not acceptable\n");
+		return -1;
+	}
+
+	if ((key_len = ast_base64decode(remote_key, key_salt, sizeof(remote_key))) != SRTP_MASTER_LEN) {
+		ast_log(LOG_WARNING, "SRTP descriptions key %d != %d\n", key_len, SRTP_MASTER_LEN);
+		return -1;
+	}
+
+	if (!memcmp(p->remote_key, remote_key, sizeof(p->remote_key))) {
+		ast_debug(1, "SRTP remote key unchanged; maintaining current policy\n");
+		return 0;
+	}
+	memcpy(p->remote_key, remote_key, sizeof(p->remote_key));
+
+	if (ast_sdp_crypto_activate(p, suite_val, remote_key, rtp) < 0) {
+		return -1;
+	}
+
+	if (!p->tag) {
+		ast_log(LOG_DEBUG, "Accepting crypto tag %s\n", tag);
+		p->tag = ast_strdup(tag);
+		if (!p->tag) {
+			ast_log(LOG_ERROR, "Could not allocate memory for tag\n");
+			return -1;
+		}
+	}
+
+	/* Finally, rebuild the crypto line */
+	return ast_sdp_crypto_offer(p, taglen);
+}
+
+int ast_sdp_crypto_offer(struct ast_sdp_crypto *p, int taglen)
+{
+	/* Rebuild the crypto line */
+	if (p->a_crypto) {
+		ast_free(p->a_crypto);
+	}
+
+	if (ast_asprintf(&p->a_crypto, "a=crypto:%s AES_CM_128_HMAC_SHA1_%i inline:%s\r\n",
+			 p->tag ? p->tag : "1", taglen, p->local_key64) == -1) {
+			ast_log(LOG_ERROR, "Could not allocate memory for crypto line\n");
+		return -1;
+	}
+
+	ast_log(LOG_DEBUG, "Crypto line: %s", p->a_crypto);
+
+	return 0;
+}
+
+const char *ast_sdp_crypto_attrib(struct ast_sdp_crypto *p)
+{
+	return p->a_crypto;
+}
+

Propchange: team/kmoore/pimp_sip_srtp/main/sdp_srtp.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/kmoore/pimp_sip_srtp/main/sdp_srtp.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/kmoore/pimp_sip_srtp/main/sdp_srtp.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list