[asterisk-commits] twilson: branch group/srtp r166057 - in /team/group/srtp: channels/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Dec 19 15:25:20 CST 2008


Author: twilson
Date: Fri Dec 19 15:25:19 2008
New Revision: 166057

URL: http://svn.digium.com/view/asterisk?view=rev&rev=166057
Log:
CODING_GUILDELINES fixes, convert to ast memory functions, etc.  Still more to do, but checking in what I have so far.

Modified:
    team/group/srtp/channels/chan_sip.c
    team/group/srtp/channels/sdp_crypto.c
    team/group/srtp/channels/sdp_mikey.c
    team/group/srtp/channels/sip_srtp.c
    team/group/srtp/channels/sip_srtp.h
    team/group/srtp/main/cryptostub.c

Modified: team/group/srtp/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/group/srtp/channels/chan_sip.c?view=diff&rev=166057&r1=166056&r2=166057
==============================================================================
--- team/group/srtp/channels/chan_sip.c (original)
+++ team/group/srtp/channels/chan_sip.c Fri Dec 19 15:25:19 2008
@@ -4901,22 +4901,18 @@
 				ast_debug(1, "Reinviting not possible when using SRTP, ignoring canreinvite setting\n");
 			}
 
-			if (!p->srtp) {
-				if (setup_srtp(p) < 0) {
-					ast_log(LOG_WARNING, "SRTP setup failed\n");
-					return -1;
-				}
+			if (!p->srtp && setup_srtp(p) < 0) {
+				ast_log(LOG_WARNING, "SRTP setup failed\n");
+				return -1;
 			}
 
 			if (!strcasecmp(ast_var_value(current), "optional")) {
 				ast_set_flag(p->srtp, SRTP_ENCR_OPTIONAL);
 			}
 		} else if (!strcasecmp(ast_var_name(current), "SIPSRTP_CRYPTO")) {
-			if (!p->srtp) {
-				if (setup_srtp(p) < 0) {
-					ast_log(LOG_WARNING, "SRTP setup failed\n");
-					return -1;
-				}
+			if (!p->srtp && setup_srtp(p) < 0) {
+				ast_log(LOG_WARNING, "SRTP setup failed\n");
+				return -1;
 			}
 
 			if (!strcasecmp(ast_var_value(current), "enable")) {
@@ -4927,11 +4923,9 @@
 				ast_log(LOG_WARNING,"Invalid SIPSRTP_CRYPTO value (%s), enable or disable expected\n", ast_var_value(current));
 			}
 		} else if (!strcasecmp(ast_var_name(current), "SIPSRTP_MIKEY")) {
-			if (!p->srtp) {
-				if (setup_srtp(p) < 0) {
-					ast_log(LOG_WARNING, "SRTP setup failed\n");
-					return -1;
-				}
+			if (!p->srtp && setup_srtp(p) < 0) {
+				ast_log(LOG_WARNING, "SRTP setup failed\n");
+				return -1;
 			}
 
 			if (!strcasecmp(ast_var_value(current), "enable")) {
@@ -9112,7 +9106,6 @@
 	const char *a_crypto = NULL;
 	const char *a_mikey = NULL;
 
-
 	int x;
 	int capability;
 	const char *protocol = NULL;
@@ -9211,8 +9204,6 @@
 	} else {
 		protocol = "AVP";
 	}
-
-	
 
 	if (debug) 
 		ast_verbose("Audio is at %s port %d\n", ast_inet_ntoa(p->ourip.sin_addr), ntohs(sin.sin_port));	
@@ -23928,9 +23919,9 @@
 		return -1;
 	}
 
-	p->srtp = sip_srtp_alloc();	/* Allocate SRTP data structure */
-	if (!p->srtp)
+	if (!(p->srtp = sip_srtp_alloc())) {	/* Allocate SRTP data structure */
 		return -1;
+	}
 
 	return 0;
 }
@@ -23942,21 +23933,22 @@
 			ast_log(LOG_WARNING, "Ignoring unexpected crypto attribute in SDP answer\n");
 			return -1;
 		}
-		
-		if (setup_srtp(p) < 0)
+
+		if (setup_srtp(p) < 0) {
 			return -1;
-	}
-	
-	if (!p->srtp->crypto)
-		p->srtp->crypto = sdp_crypto_setup();
-	
-	if (!p->srtp->crypto) 
+		}
+	}
+
+	if (!p->srtp->crypto && !(p->srtp->crypto = sdp_crypto_setup())) {
 		return -1;
-
-	if (sdp_crypto_process(p->srtp->crypto, a, p->rtp) < 0)
+	}
+
+	if (sdp_crypto_process(p->srtp->crypto, a, p->rtp) < 0) {
 		return -1;
+	}
 
 	ast_set_flag(p->srtp, SRTP_CRYPTO_OFFER_OK);
+
 	return 0;
 }
 
@@ -23967,24 +23959,24 @@
 			ast_log(LOG_WARNING, "Ignoring unexpected mikey attribute in SDP answer\n");
 			return -1;
 		}
-		
+
 		if (setup_srtp(p) < 0) {
 			ast_log(LOG_WARNING, "Can't setup crypto\n");
 			return -1;
 		}
 	}
-	if (!p->srtp->mikey) {
-		p->srtp->mikey = sdp_mikey_setup(p->peersecret, p->rtp);
-		if (!p->srtp->mikey) {
-			ast_log(LOG_WARNING, "Can't setup MIKEY\n");
-			return -1;
-		}
-	}
-
-	if (sdp_mikey_process(p->srtp->mikey, a + 15, p->rtp) < 0)
+
+	if (!(p->srtp->mikey = sdp_mikey_setup(p->peersecret, p->rtp))) {
+		ast_log(LOG_WARNING, "Can't setup MIKEY\n");
 		return -1;
+	}
+
+	if (sdp_mikey_process(p->srtp->mikey, a + 15, p->rtp) < 0) {
+		return -1;
+	}
 
 	ast_set_flag(p->srtp, SRTP_MIKEY_OFFER_OK);
+
 	return -1;
 }
 

Modified: team/group/srtp/channels/sdp_crypto.c
URL: http://svn.digium.com/view/asterisk/team/group/srtp/channels/sdp_crypto.c?view=diff&rev=166057&r1=166056&r2=166057
==============================================================================
--- team/group/srtp/channels/sdp_crypto.c (original)
+++ team/group/srtp/channels/sdp_crypto.c Fri Dec 19 15:25:19 2008
@@ -19,9 +19,9 @@
 /*! \file sdp_crypto.c
  *
  * \brief SDP Security descriptions
- * 
+ *
  * Specified in RFC 4568
- * 
+ *
  * \author Mikael Magnusson <mikma at users.sourceforge.net>
  */
 
@@ -43,72 +43,68 @@
 	char local_key64[SRTP_MASTER_LEN64];
 };
 
-static int set_crypto_policy(struct ast_srtp_policy *policy,
-			     int suite_val, const unsigned char *master_key,
-			     unsigned long ssrc, int inbound);
-
+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 sdp_crypto *sdp_crypto_alloc(void)
 {
-	struct sdp_crypto *crypto = malloc(sizeof(*crypto));
-
-	if (crypto)
-		memset(crypto, 0, sizeof(*crypto));
-	else
+	struct sdp_crypto *crypto;
+
+	if (!(crypto = ast_calloc(1, sizeof(*crypto)))) {
 		ast_log(LOG_ERROR, "Out of memory, can't allocate crypto structure\n");
+	}
 
 	return crypto;
 }
 
 void sdp_crypto_destroy(struct sdp_crypto *crypto)
 {
-	if (crypto->a_crypto)
-		free(crypto->a_crypto);
+	if (crypto->a_crypto) {
+		ast_free(crypto->a_crypto);
+	}
 	crypto->a_crypto = NULL;
-	free(crypto);
+	ast_free(crypto);
 }
 
 struct sdp_crypto *sdp_crypto_setup(void)
 {
-	struct sdp_crypto *p = sdp_crypto_alloc();
+	struct sdp_crypto *p;
 	int key_len;
 	unsigned char remote_key[SRTP_MASTER_LEN];
 
-	if (!p)
+	if (!(p = sdp_crypto_alloc())) {
 		return NULL;
+	}
 
 	if (ast_srtp_get_random(p->local_key, sizeof(p->local_key)) < 0) {
 		sdp_crypto_destroy(p);
 		return NULL;
 	}
 
-	ast_base64encode(p->local_key64, p->local_key,
-			 SRTP_MASTER_LEN, sizeof(p->local_key64));
+	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)
+	if (key_len != SRTP_MASTER_LEN) {
 		ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", key_len, SRTP_MASTER_LEN);
-
-	if (memcmp(remote_key, p->local_key, SRTP_MASTER_LEN))
+	}
+
+	if (memcmp(remote_key, p->local_key, SRTP_MASTER_LEN)) {
 		ast_log(LOG_ERROR, "base64 encode/decode bad key\n");
+	}
 
 	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)
+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;
 
 	master_salt = master_key + SRTP_MASTERKEY_LEN;
-	if (ast_srtp_policy_set_master_key(policy,
-					   master_key, SRTP_MASTERKEY_LEN,
-					   master_salt, SRTP_MASTERSALT_LEN) < 0)
-		return -1;
-
+	if (ast_srtp_policy_set_master_key(policy, master_key, SRTP_MASTERKEY_LEN, master_salt, SRTP_MASTERSALT_LEN) < 0) {
+		return -1;
+	}
 
 	if (ast_srtp_policy_set_suite(policy, suite_val)) {
 		ast_log(LOG_WARNING, "Could not set remote SRTP suite\n");
@@ -120,34 +116,34 @@
 	return 0;
 }
 
-static int sdp_crypto_activate(struct sdp_crypto *p, int suite_val,
-			       unsigned char *remote_key,
-			       struct ast_rtp *rtp)
+static int sdp_crypto_activate(struct sdp_crypto *p, int suite_val, unsigned char *remote_key, struct ast_rtp *rtp)
 {
 	struct ast_srtp_policy *local_policy = NULL;
 	struct ast_srtp_policy *remote_policy = NULL;
 	int res = -1;
 
-	if (!p)
-		return -1;
-
-	local_policy = ast_srtp_policy_alloc();
-	if (!local_policy)
-		goto err;
-
-	remote_policy = ast_srtp_policy_alloc();
-	if (!remote_policy) {
-		goto err;
-	}
-
-	if (set_crypto_policy(local_policy, suite_val, p->local_key,
-			      ast_rtp_get_ssrc(rtp), 0) < 0)
-		goto err;
-	
-	if (set_crypto_policy(remote_policy, suite_val, remote_key, 0, 1) < 0)
-		goto err;
-
-/* FIXME MIKMA */
+	if (!p) {
+		return -1;
+	}
+
+	if (!(local_policy = ast_srtp_policy_alloc())) {
+		return -1;
+	}
+
+	if (!(remote_policy = ast_srtp_policy_alloc())) {
+		goto err;
+	}
+
+	if (set_crypto_policy(local_policy, suite_val, p->local_key, ast_rtp_get_ssrc(rtp), 0) < 0) {
+		goto err;
+	}
+
+	if (set_crypto_policy(remote_policy, suite_val, remote_key, 0, 1) < 0) {
+		goto err;
+	}
+
+	/* FIXME MIKMA */
+	/* ^^^ I wish I knew what needed fixing... */
 	if (ast_rtp_add_srtp_policy(rtp, local_policy)) {
 		ast_log(LOG_WARNING, "Could not set local SRTP policy\n");
 		goto err;
@@ -158,21 +154,22 @@
 		goto err;
 	}
 
-
 	ast_debug(1 , "SRTP policy activated\n");
 	res = 0;
 
 err:
-	if (local_policy)
+	if (local_policy) {
 		ast_srtp_policy_destroy(local_policy);
-
-	if (remote_policy)
+	}
+
+	if (remote_policy) {
 		ast_srtp_policy_destroy(remote_policy);
+	}
+
 	return res;
 }
 
-int sdp_crypto_process(struct sdp_crypto *p, const char *attr,
-		       struct ast_rtp *rtp)
+int sdp_crypto_process(struct sdp_crypto *p, const char *attr, struct ast_rtp *rtp)
 {
 	char *str = NULL;
 	char *name = NULL;
@@ -186,15 +183,12 @@
 	int found = 0;
 	int attr_len = strlen(attr);
 	int key_len = 0;
+	int suite_val = 0;
 	unsigned char remote_key[SRTP_MASTER_LEN];
-	int suite_val = 0;
-
-	if (!ast_srtp_is_registered())
-		return -1;
-
-	/* Crypto already accepted */
-/* 	if (p && p->a_crypto) */
-/* 		return -1; */
+
+	if (!ast_srtp_is_registered()) {
+		return -1;
+	}
 
 	str = ast_strdupa(attr);
 
@@ -210,8 +204,7 @@
 	}
 
 	if (session_params) {
-		ast_log(LOG_WARNING, "Unsupported crypto parameters: %s",
-			session_params);
+		ast_log(LOG_WARNING, "Unsupported crypto parameters: %s", session_params);
 		return -1;
 	}
 
@@ -220,8 +213,7 @@
 	} else if (!strcmp(suite, "AES_CM_128_HMAC_SHA1_32")) {
 		suite_val = AST_AES_CM_128_HMAC_SHA1_32;
 	} else {
-		ast_log(LOG_WARNING, "Unsupported crypto suite: %s",
-			suite);
+		ast_log(LOG_WARNING, "Unsupported crypto suite: %s", suite);
 		return -1;
 	}
 
@@ -237,16 +229,9 @@
 			lifetime = strsep(&info, "|");
 
 			if (lifetime) {
-				ast_log(LOG_NOTICE, "Crypto life time unsupported: %s\n",
-					attr);
+				ast_log(LOG_NOTICE, "Crypto life time unsupported: %s\n", attr);
 				continue;
 			}
-
-/* 			if (info || strncmp(lifetime, "2^", 2)) { */
-/* 				ast_log(LOG_NOTICE, "MKI unsupported: %s\n", */
-/* 					attr); */
-/* 				continue; */
-/* 			} */
 
 			found = 1;
 			break;
@@ -258,23 +243,23 @@
 		return -1;
 	}
 
-	key_len = ast_base64decode(remote_key, key_salt, sizeof(remote_key));
-	if (key_len != SRTP_MASTER_LEN) {
-		ast_log(LOG_WARNING, "SRTP sdescriptions key %d != %d\n",
-			key_len, SRTP_MASTER_LEN);
-		return -1;
-	}
-
-	if (sdp_crypto_activate(p, suite_val, remote_key, rtp) < 0)
-		return -1;
+
+	if ((key_len = ast_base64decode(remote_key, key_salt, sizeof(remote_key))) != SRTP_MASTER_LEN) {
+		ast_log(LOG_WARNING, "SRTP sdescriptions key %d != %d\n", key_len, SRTP_MASTER_LEN);
+		return -1;
+	}
+
+	if (sdp_crypto_activate(p, suite_val, remote_key, rtp) < 0) {
+		return -1;
+	}
 
 	if (!p->a_crypto) {
-		free(p->a_crypto);
-
-		p->a_crypto = malloc(attr_len+11);
-		snprintf(p->a_crypto, attr_len+10,
-			 "a=crypto:%s %s inline:%s\r\n",
-			 tag, suite, p->local_key64);
+		if (!(p->a_crypto = ast_calloc(1, attr_len + 11))) {
+			ast_log(LOG_ERROR, "Could not allocate memory for a_crypto\n");
+			return -1;
+		}
+
+		snprintf(p->a_crypto, attr_len + 10, "a=crypto:%s %s inline:%s\r\n", tag, suite, p->local_key64);
 	}
 
 	return 0;
@@ -283,17 +268,14 @@
 int sdp_crypto_offer(struct sdp_crypto *p)
 {
 	char crypto_buf[128];
-
-	/* Crypto offer */
-	const char *crypto_suite = "AES_CM_128_HMAC_SHA1_80";
-
-	if (p->a_crypto)
-		free(p->a_crypto);
-
-	snprintf(crypto_buf, sizeof(crypto_buf),
-		 "a=crypto:1 %s inline:%s\r\n",
-		 crypto_suite, p->local_key64);
-	p->a_crypto = strdup(crypto_buf);
+	const char *crypto_suite = "AES_CM_128_HMAC_SHA1_80"; /* Crypto offer */
+
+	if (p->a_crypto) {
+		ast_free(p->a_crypto);
+	}
+
+	snprintf(crypto_buf, sizeof(crypto_buf), "a=crypto:1 %s inline:%s\r\n",  crypto_suite, p->local_key64);
+	p->a_crypto = ast_strdup(crypto_buf);
 
 	return 0;
 }

Modified: team/group/srtp/channels/sdp_mikey.c
URL: http://svn.digium.com/view/asterisk/team/group/srtp/channels/sdp_mikey.c?view=diff&rev=166057&r1=166056&r2=166057
==============================================================================
--- team/group/srtp/channels/sdp_mikey.c (original)
+++ team/group/srtp/channels/sdp_mikey.c Fri Dec 19 15:25:19 2008
@@ -16,14 +16,17 @@
  * at the top of the source tree.
  */
 
-/*! \file sdp_mikey.c 
- *
- * \brief SDP MIKEY key management 
- * 
- * SDP MIKEY key management 
+/*! \file sdp_mikey.c
+ *
+ * \brief SDP MIKEY key management
+ *
+ * SDP MIKEY key management
  * Specified in RFC 3830 and 4567
- * 
+ *
  * \author Mikael Magnusson <mikma at users.sourceforge.net>
+ * \todo disable rtp until keys are available.
+ * \todo sometimes first RTP packet is received before rtp callback
+ * is installed, leads to that ssrc not being activated.
  */
 
 #include "asterisk.h"
@@ -35,17 +38,6 @@
 #include "asterisk/mikey.h"
 #include "sdp_mikey.h"
 
-/*
-  MIKEY
-  Specified in: RFC 3830, RFC 4567
-*/
-
-/* 
-   TODO disable rtp until keys are available.
-   TODO sometimes first RTP packet is received before rtp callback
-   is installed, leads to that ssrc not being activated.
- */
-
 struct sdp_mikey {
 	struct mikey *mikey;
 	char *a_mikey;
@@ -53,7 +45,6 @@
 
 static int sdp_mikey_activate(struct sdp_mikey *p, struct ast_rtp *rtp);
 
-
 int sdp_mikey_init(void)
 {
 	return 0;
@@ -61,59 +52,70 @@
 
 void sdp_mikey_uninit(void)
 {
+	return;
 }
 
 static struct sdp_mikey *sdp_mikey_alloc(void)
 {
-	struct ast_mikey_res *res = ast_get_mikey();
 	struct sdp_mikey *mikey;
 
-	if (!res) {
-		ast_log(LOG_ERROR, "res_mikey not loaded\n");
-		return NULL;
-	}
-
-	mikey = malloc(sizeof(*mikey));
-
-	if (mikey)
-		memset(mikey, 0, sizeof(*mikey));
-	else
+	if (!ast_get_mikey()) {
+		ast_log(LOG_ERROR, "res_mikey not loaded\n");
+		return NULL;
+	}
+
+	if (!(mikey = ast_calloc(1, sizeof(*mikey)))) {
 		ast_log(LOG_ERROR, "Out of memory, can't allocate mikey structure\n");
+	}
 
 	return mikey;
 }
 
 void sdp_mikey_destroy(struct sdp_mikey *p)
 {
-	struct ast_mikey_res *res = ast_get_mikey();
-
-	if (p->mikey)
+	struct ast_mikey_res *res;
+
+	if (!(res = ast_get_mikey())) {
+		ast_log(LOG_ERROR, "res_mikey not loaded\n");
+		return;
+	}
+
+	if (p->mikey) {
 		res->mikey_destroy(p->mikey);
+	}
 	p->mikey = NULL;
 
-	if (p->a_mikey)
-		free(p->a_mikey);
+	if (p->a_mikey) {
+		ast_free(p->a_mikey);
+	}
 	p->a_mikey = NULL;
 
-	free(p);
-}
-
-struct sdp_mikey *sdp_mikey_setup(const char *peersecret,
-				  struct ast_rtp *rtp)
-{
-	struct ast_mikey_res *res = ast_get_mikey();
-	struct sdp_mikey *p = sdp_mikey_alloc();
-
-	if (!p)
-		return NULL;
-
-	p->mikey = res->mikey_alloc();
+	ast_free(p);
+}
+
+struct sdp_mikey *sdp_mikey_setup(const char *peersecret, struct ast_rtp *rtp)
+{
+	struct ast_mikey_res *res;
+	struct sdp_mikey *p;
+
+	if (!(res = ast_get_mikey())) {
+		ast_log(LOG_ERROR, "res_mikey not loaded\n");
+		return NULL;
+	}
+
+	if (!(p = sdp_mikey_alloc())) {
+		return NULL;
+	}
+
+	if (!(p->mikey = res->mikey_alloc())) {
+		ast_free(p);
+		return NULL;
+	}
+
 	if (peersecret) {
 		ast_log(LOG_NOTICE, "Using MIKEY PSK %s\n", peersecret);
-		res->mikey_set_psk_secret(p->mikey, (unsigned char*)peersecret,
-					  strlen(peersecret));
-	}
-	else {
+		res->mikey_set_psk_secret(p->mikey, (unsigned char*)peersecret, strlen(peersecret));
+	} else {
 		ast_log(LOG_NOTICE, "Now MIKEY PSK available\n");
 	}
 
@@ -122,154 +124,162 @@
 	return p;
 }
 
-int sdp_mikey_process(struct sdp_mikey *p, const char *attr,
-		      struct ast_rtp *rtp)
-{
-	struct ast_mikey_res *mod = ast_get_mikey();
+int sdp_mikey_process(struct sdp_mikey *p, const char *attr, struct ast_rtp *rtp)
+{
+	struct ast_mikey_res *mod;
 	char buf[8192] = "a=key-mgmt:mikey ";
 	size_t prefixlen = strlen(buf);
 	size_t buflen = sizeof(buf) - prefixlen - 2;
 	int res;
- 
+
+	if (!(mod = ast_get_mikey())) {
+		ast_log(LOG_ERROR, "res_mikey not loaded\n");
+		return -1;
+	}
+
 	if (!p->mikey) {
 		ast_log(LOG_ERROR, "No MIKEY object\n");
 		return -1;
 	}
 
-	ast_log(LOG_DEBUG, "%s\n", attr);
-
-	res = mod->mikey_process(p->mikey, attr, buf + prefixlen, buflen);
-
-	if (res < 0) {
-		ast_log(LOG_NOTICE, "Couldn't parse MIKEY offer\n");
-		return -1;
-	}
-
-	if (p->a_mikey)
-		free(p->a_mikey);
+	ast_debug(1, "%s\n", attr);
+
+	if ((res = mod->mikey_process(p->mikey, attr, buf + prefixlen, buflen)) < 0) {
+		ast_log(LOG_WARNING, "Couldn't parse MIKEY offer\n");
+		return -1;
+	}
+
+	if (p->a_mikey) {
+		ast_free(p->a_mikey);
+	}
 	p->a_mikey = NULL;
 
-	if (sdp_mikey_activate(p, rtp) < 0)
-		return -1;
+	if (sdp_mikey_activate(p, rtp) < 0) {
+		return -1;
+	}
 
 	if (res > 0) {
 		/* Parsed offer, built response */
 		strcat(buf, "\r\n");
 
-		p->a_mikey = strdup(buf);
-	}
+		p->a_mikey = ast_strdup(buf);
+	}
+
 	return 0;
 }
 
 int sdp_mikey_offer(struct sdp_mikey *p, struct ast_rtp *rtp)
 {
-	struct ast_mikey_res *mod = ast_get_mikey();
+	struct ast_mikey_res *mod;
 	char buf[8192] = "a=key-mgmt:mikey ";
 	size_t prefixlen = strlen(buf);
 	size_t buflen = sizeof(buf) - prefixlen - 2;
 	int res;
 
+	if (!(mod = ast_get_mikey())) {
+		ast_log(LOG_ERROR, "res_mikey not loaded\n");
+		return -1;
+	}
+
 	/* Crypto already accepted */
-	if (p && p->a_mikey)
-		return -1;
-
-	res = mod->mikey_build_offer(p->mikey, buf + prefixlen, buflen, AST_MIKEY_TYPE_DH_HMAC);
-
-	if (res < 0) {
+	if (p && p->a_mikey) {
+		return -1;
+	}
+
+	if ((res = mod->mikey_build_offer(p->mikey, buf + prefixlen, buflen, AST_MIKEY_TYPE_DH_HMAC)) < 0) {
 		ast_log(LOG_NOTICE, "Couldn't build MIKEY offer\n");
 		return -1;
 	}
 
-	if (sdp_mikey_activate(p, rtp) < 0)
-		return -1;
+	if (sdp_mikey_activate(p, rtp) < 0) {
+		return -1;
+	}
 
 	strcat(buf, "\r\n");
 
-	if (p->a_mikey)
-		free(p->a_mikey);
-
-	p->a_mikey = strdup(buf);
+	if (p->a_mikey) {
+		ast_free(p->a_mikey);
+	}
+
+	p->a_mikey = ast_strdup(buf);
+
 	return 0;
 }
 
 static int cb_no_ctx(struct ast_rtp *rtp, unsigned long ssrc, void *data)
 {
-	struct ast_mikey_res *mod = ast_get_mikey();
+	struct ast_mikey_res *mod;
 	struct sdp_mikey *p = data;
-	struct ast_srtp_policy *policy = NULL;
-	int res = -1;
-
-	ast_log(LOG_DEBUG, "SRTP cb\n");
+	struct ast_srtp_policy *policy;
+
+	if (!(mod = ast_get_mikey())) {
+		ast_log(LOG_ERROR, "res_mikey not loaded\n");
+		return -1;
+	}
 
 	if (!p) {
-		ast_log(LOG_WARNING, "No pvt\n");
-		goto err;
+		ast_log(LOG_ERROR, "No pvt\n");
+		return -1;
 	}
 
 	if (!p->mikey) {
 		ast_log(LOG_WARNING, "No mikey\n");
-		goto err;
-	}
-
-	policy = mod->mikey_create_policy(p->mikey, ssrc);
-	if (!policy) {
+		return -1;
+	}
+
+	if (!(policy = mod->mikey_create_policy(p->mikey, ssrc))) {
 		ast_log(LOG_ERROR, "Could not create MIKEY policy\n");
-		goto err;
+		return -1;
 	}
 
 	/* was p->rtp */
 	if (ast_rtp_add_srtp_policy(rtp, policy)) {
 		ast_log(LOG_ERROR, "Could not set SRTP policy\n");
-		goto err;
-	}
-
-	res = 0;
-
-err:
-	if (policy)
 		ast_srtp_policy_destroy(policy);
-	return res;
+		return -1;
+	}
+
+	return 0;
 }
 
 struct ast_srtp_cb srtp_cb = {
-	no_ctx: cb_no_ctx
+	.no_ctx = cb_no_ctx,
 };
 
 static int sdp_mikey_activate(struct sdp_mikey *p, struct ast_rtp *rtp)
 {
-	struct ast_mikey_res *mod = ast_get_mikey();
-	struct ast_srtp_policy *policy = NULL;
-	int res = -1;
+	struct ast_mikey_res *mod;
+	struct ast_srtp_policy *policy;
+
+	if (!(mod = ast_get_mikey())) {
+		ast_log(LOG_ERROR, "res_mikey not loaded\n");
+		return -1;
+	}
 
 	if (!p || !p->mikey)
 		return -1;
 
-	policy = mod->mikey_create_policy(p->mikey, ast_rtp_get_ssrc(rtp));
-	if (!policy) {
+	if (!(policy = mod->mikey_create_policy(p->mikey, ast_rtp_get_ssrc(rtp)))) {
 		ast_log(LOG_ERROR, "Could not create MIKEY policy\n");
-		goto err;
+		return -1;
 	}
 
 	if (ast_rtp_add_srtp_policy(rtp, policy)) {
 		ast_log(LOG_ERROR, "Could not set local SRTP policy\n");
-		goto err;
+		ast_srtp_policy_destroy(policy);
+		return -1;
 	}
 
 	ast_rtp_set_srtp_cb(rtp, &srtp_cb, p);
 
-	if (option_debug > 1)
-		ast_log(LOG_NOTICE, "SRTP policy activated\n");
-	res = 0;
-
-err:
-	if (policy)
-		ast_srtp_policy_destroy(policy);
-	return res;
+	ast_debug(1, "SRTP policy activated\n");
+
+	return 0;
 }
 
 const char *sdp_mikey_attrib(struct sdp_mikey *p)
 {
-	ast_log(LOG_DEBUG, "Return mikey attrib %s\n", p->a_mikey);
-	
+	ast_debug(1, "Return mikey attrib %s\n", p->a_mikey);
+
 	return p->a_mikey;
 }

Modified: team/group/srtp/channels/sip_srtp.c
URL: http://svn.digium.com/view/asterisk/team/group/srtp/channels/sip_srtp.c?view=diff&rev=166057&r1=166056&r2=166057
==============================================================================
--- team/group/srtp/channels/sip_srtp.c (original)
+++ team/group/srtp/channels/sip_srtp.c Fri Dec 19 15:25:19 2008
@@ -33,22 +33,24 @@
 
 struct sip_srtp *sip_srtp_alloc(void)
 {
-	struct sip_srtp *srtp = malloc(sizeof(*srtp));
+	struct sip_srtp *srtp;
+   
+	if (!(srtp = ast_calloc(1, sizeof(*srtp)))) {
+		ast_log(LOG_ERROR, "Out of memory, can't allocate srtp structure\n");
+	}
 
-	if (srtp)
-		memset(srtp, 0, sizeof(*srtp));
-	else
-		ast_log(LOG_ERROR, "Out of memory, can't allocate srtp structure\n");
 	return srtp;
 }
 
 void sip_srtp_destroy(struct sip_srtp *srtp)
 {
-	if (srtp->crypto)
+	if (srtp->crypto) {
 		sdp_crypto_destroy(srtp->crypto);
+	}
 	srtp->crypto = NULL;
 
-	if (srtp->mikey)
+	if (srtp->mikey) {
 		sdp_mikey_destroy(srtp->mikey);
+	}
 	srtp->mikey = NULL;
 }

Modified: team/group/srtp/channels/sip_srtp.h
URL: http://svn.digium.com/view/asterisk/team/group/srtp/channels/sip_srtp.h?view=diff&rev=166057&r1=166056&r2=166057
==============================================================================
--- team/group/srtp/channels/sip_srtp.h (original)
+++ team/group/srtp/channels/sip_srtp.h Fri Dec 19 15:25:19 2008
@@ -19,7 +19,7 @@
 /*! \file sip_srtp.h
  *
  * \brief SIP Secure RTP (SRTP)
- * 
+ *
  * Specified in RFC 3711
  *
  * \author Mikael Magnusson <mikma at users.sourceforge.net>
@@ -38,7 +38,6 @@
 #define SRTP_CRYPTO_OFFER_OK	(1<<5)
 #define SRTP_MIKEY_OFFER_OK	(1<<6)
 
-
 /*! \brief structure for secure RTP audio */
 struct sip_srtp {
 	unsigned int flags;
@@ -46,24 +45,30 @@
 	struct sdp_mikey *mikey;
 };
 
-/*----- SRTP interface functions */
+/*!
+ * \brief allocate a sip_srtp structure
+ * \retval a new malloc'd sip_srtp structure on success
+ * \retval NULL on failure
+*/
 struct sip_srtp *sip_srtp_alloc(void);
+
+/*!
+ * \brief free a sip_srtp structure
+ * \param srtp a sip_srtp structure
+*/
 void sip_srtp_destroy(struct sip_srtp *srtp);
 
 #if 0
 struct sip_sdp_keymgmt {
 	int (*init)(void);
 	void (*uninit)(void);
-	struct sdp_keymgmt *(*setup)(const char *peersecret,
-				     struct ast_rtp *rtp);
+	struct sdp_keymgmt *(*setup)(const char *peersecret, struct ast_rtp *rtp);
 	void (*destroy)(struct sdp_keymgmt *p);
 	int (*offer)(struct sdp_keymgmt *p, struct ast_rtp *rtp);
 	const char *(*attrib)(struct sdp_keymgmt *p);
 	/* FIX attr offset */
-	int (*process)(struct sdp_keymgmt *p, const char *attr,
-		       struct ast_rtp *rtp);
+	int (*process)(struct sdp_keymgmt *p, const char *attr, struct ast_rtp *rtp);
 };
-
 #endif
 
 #endif	/* _SIP_SRTP_H */

Modified: team/group/srtp/main/cryptostub.c
URL: http://svn.digium.com/view/asterisk/team/group/srtp/main/cryptostub.c?view=diff&rev=166057&r1=166056&r2=166057
==============================================================================
--- team/group/srtp/main/cryptostub.c (original)
+++ team/group/srtp/main/cryptostub.c Fri Dec 19 15:25:19 2008
@@ -68,7 +68,7 @@
 build_stub(ast_encrypt_bin, unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key);
 build_stub(ast_decrypt_bin, unsigned char *dst, const unsigned char *src, int srclen, struct ast_key *key);
 
-static struct ast_mikey_res *g_ast_mikey_res;
+static struct ast_mikey_res *g_ast_mikey_res = NULL;
 
 int ast_register_mikey(struct ast_mikey_res *mikey_res)
 {




More information about the asterisk-commits mailing list