From 71f31347fcddaa5f0e9e329a7320efe60bc7c281 Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Mon, 18 Mar 2013 15:58:05 +0100 Subject: [PATCH] Send ZRTP hello hash in SIP SDP. --- coreapi/linphonecall.c | 41 ++++++++++++++++++++++++++++++--------- coreapi/linphonecore.c | 8 +++++++- coreapi/offeranswer.c | 1 + coreapi/sal.h | 3 ++- coreapi/sal_eXosip2_sdp.c | 28 +++++++++++++++++++++++--- mediastreamer2 | 2 +- oRTP | 2 +- 7 files changed, 69 insertions(+), 16 deletions(-) diff --git a/coreapi/linphonecall.c b/coreapi/linphonecall.c index 6c01c65b..e9640701 100644 --- a/coreapi/linphonecall.c +++ b/coreapi/linphonecall.c @@ -254,6 +254,14 @@ void linphone_call_make_local_media_description(LinphoneCore *lc, LinphoneCall * l=ms_list_append(l,pt); md->streams[0].payloads=l; + // if ZRTP is enabled, put the hello hash into the audiostream's desc + if (call->audiostream && call->audiostream->ms.zrtp_context!=NULL){ + ortp_zrtp_get_hello_hash(call->audiostream->ms.zrtp_context, + md->streams[0].zrtp_hello_hash, + sizeof(md->streams[0].zrtp_hello_hash)); + ms_message("Audio stream zrtp hash: %s", md->streams[0].zrtp_hello_hash); + } + if (call->params.has_video){ md->n_active_streams++; md->streams[1].rtp_port=call->video_port; @@ -262,6 +270,13 @@ void linphone_call_make_local_media_description(LinphoneCore *lc, LinphoneCall * md->streams[1].type=SalVideo; l=make_codec_list(lc,lc->codecs_conf.video_codecs,0,NULL,-1); md->streams[1].payloads=l; + // if ZRTP is enabled, put the hello hash into the audiostream's desc + if (call->videostream->ms.zrtp_context!=NULL){ + ortp_zrtp_get_hello_hash(call->videostream->ms.zrtp_context, + md->streams[1].zrtp_hello_hash, + sizeof(md->streams[1].zrtp_hello_hash)); + ms_message("Video stream zrtp hash: %s", md->streams[1].zrtp_hello_hash); + } } if (md->n_total_streams < md->n_active_streams) md->n_total_streams = md->n_active_streams; @@ -1294,6 +1309,20 @@ void linphone_call_init_video_stream(LinphoneCall *call){ void linphone_call_init_media_streams(LinphoneCall *call){ linphone_call_init_audio_stream(call); linphone_call_init_video_stream(call); + + // moved from linphone_call_start_media_streams, because ZRTP needs to be + // at least partially initialized so that the SDP can contain 'zrtp-hash' + if (call->params.media_encryption==LinphoneMediaEncryptionZRTP) { + OrtpZrtpParams params; + /*will be set later when zrtp is activated*/ + call->current_params.media_encryption=LinphoneMediaEncryptionNone; + + params.zid_file=call->core->zrtp_secrets_cache; + audio_stream_enable_zrtp(call->audiostream,¶ms); + } else if (call->params.media_encryption==LinphoneMediaEncryptionSRTP){ + call->current_params.media_encryption=linphone_call_are_all_streams_encrypted(call) ? + LinphoneMediaEncryptionSRTP : LinphoneMediaEncryptionNone; + } } @@ -1736,16 +1765,10 @@ void linphone_call_start_media_streams(LinphoneCall *call, bool_t all_inputs_mut call->playing_ringbacktone=send_ringbacktone; call->up_bw=linphone_core_get_upload_bandwidth(lc); + // ZRTP was initialized in linphone_call_init_media_streams with a + // partially iniitalized RtpSession, and now needs to get an update if (call->params.media_encryption==LinphoneMediaEncryptionZRTP) { - OrtpZrtpParams params; - /*will be set later when zrtp is activated*/ - call->current_params.media_encryption=LinphoneMediaEncryptionNone; - - params.zid_file=lc->zrtp_secrets_cache; - audio_stream_enable_zrtp(call->audiostream,¶ms); - }else if (call->params.media_encryption==LinphoneMediaEncryptionSRTP){ - call->current_params.media_encryption=linphone_call_are_all_streams_encrypted(call) ? - LinphoneMediaEncryptionSRTP : LinphoneMediaEncryptionNone; + ortp_zrtp_start_engine(call->audiostream->ms.zrtp_context,call->audiostream->ms.session); } /*also reflect the change if the "wished" params, in order to avoid to propose SAVP or video again diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index a2c91006..431d3e37 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -3125,8 +3125,14 @@ int linphone_core_accept_call_with_params(LinphoneCore *lc, LinphoneCall *call, sal_call_set_local_media_description(call->op,call->localdesc); } - if (call->audiostream==NULL) + if (call->audiostream==NULL){ linphone_call_init_media_streams(call); + // the local media description must be regenerated after the audiostream + // is initialized, otherwise the ZRTP hello hash will not be available + linphone_call_make_local_media_description(lc,call); + sal_call_set_local_media_description(call->op,call->localdesc); + } + if (!was_ringing && call->audiostream->ms.ticker==NULL){ audio_stream_prepare_sound(call->audiostream,lc->sound_conf.play_sndcard,lc->sound_conf.capt_sndcard); } diff --git a/coreapi/offeranswer.c b/coreapi/offeranswer.c index 9823c24a..eefe34d4 100644 --- a/coreapi/offeranswer.c +++ b/coreapi/offeranswer.c @@ -261,6 +261,7 @@ static void initiate_incoming(const SalStreamDescription *local_cap, result->ice_completed = local_cap->ice_completed; memcpy(result->ice_candidates, local_cap->ice_candidates, sizeof(result->ice_candidates)); memcpy(result->ice_remote_candidates, local_cap->ice_remote_candidates, sizeof(result->ice_remote_candidates)); + memcpy(result->zrtp_hello_hash,local_cap->zrtp_hello_hash, sizeof(result->zrtp_hello_hash)); } /** diff --git a/coreapi/sal.h b/coreapi/sal.h index 25d8d20b..5f38c15a 100644 --- a/coreapi/sal.h +++ b/coreapi/sal.h @@ -140,7 +140,7 @@ typedef struct SalIceRemoteCandidate { } SalIceRemoteCandidate; #define SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES 2 - +#define SAL_MEDIA_DESCRIPTION_MAX_ZRTP_HELLO_HASH 128 #define SAL_MEDIA_DESCRIPTION_MAX_ICE_UFRAG_LEN 256 #define SAL_MEDIA_DESCRIPTION_MAX_ICE_PWD_LEN 256 @@ -172,6 +172,7 @@ typedef struct SalStreamDescription{ SalIceRemoteCandidate ice_remote_candidates[SAL_MEDIA_DESCRIPTION_MAX_ICE_REMOTE_CANDIDATES]; char ice_ufrag[SAL_MEDIA_DESCRIPTION_MAX_ICE_UFRAG_LEN]; char ice_pwd[SAL_MEDIA_DESCRIPTION_MAX_ICE_PWD_LEN]; + char zrtp_hello_hash[SAL_MEDIA_DESCRIPTION_MAX_ZRTP_HELLO_HASH]; bool_t ice_mismatch; bool_t ice_completed; } SalStreamDescription; diff --git a/coreapi/sal_eXosip2_sdp.c b/coreapi/sal_eXosip2_sdp.c index debd8550..13944644 100644 --- a/coreapi/sal_eXosip2_sdp.c +++ b/coreapi/sal_eXosip2_sdp.c @@ -108,6 +108,17 @@ static int _sdp_message_get_a_ptime(sdp_message_t *sdp, int mline){ return 0; } +static char * _sdp_message_get_a_zrtp_hash(sdp_message_t *sdp, int mline){ + int i; + sdp_attribute_t *attr; + for (i=0;(attr=sdp_message_attribute_get(sdp,mline,i))!=NULL;i++){ + if (keywordcmp("zrtp-hash",attr->a_att_field)==0){ + return attr->a_att_value; + } + } + return NULL; +} + static int _sdp_message_get_mline_dir(sdp_message_t *sdp, int mline){ int i; sdp_attribute_t *attr; @@ -337,6 +348,11 @@ static void add_line(sdp_message_t *msg, int lineno, const SalStreamDescription int_2char(desc->bandwidth)); if (desc->ptime>0) sdp_message_a_attribute_add(msg,lineno,osip_strdup("ptime"), int_2char(desc->ptime)); + + // if the ZRTP hello hash is available, create an a attribute for it + if (desc->zrtp_hello_hash[0]) + sdp_message_a_attribute_add(msg,lineno,osip_strdup("zrtp-hash"), osip_strdup(desc->zrtp_hello_hash)); + strip_well_known_rtpmaps=ms_list_size(desc->payloads)>5; if (desc->payloads){ for(elem=desc->payloads;elem!=NULL;elem=elem->next){ @@ -433,7 +449,7 @@ static int payload_type_fill_from_rtpmap(PayloadType *pt, const char *rtpmap){ int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){ int i,j; - const char *mtype,*proto,*rtp_port,*rtp_addr,*number; + const char *mtype,*proto,*rtp_port,*rtp_addr,*number,*zrtp_info; const char *sess; sdp_bandwidth_t *sbw=NULL; sdp_attribute_t *attr; @@ -490,7 +506,12 @@ int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){ stream->rtp_port=atoi(rtp_port); if (stream->rtp_port > 0) desc->n_active_streams++; - + + // if the SDP contains a zrtp-hash, add it to the StreamDesc + zrtp_info = _sdp_message_get_a_zrtp_hash(msg, i); + if (zrtp_info != NULL) + strncpy(stream->zrtp_hello_hash, zrtp_info, sizeof(stream->zrtp_hello_hash)); + stream->ptime=_sdp_message_get_a_ptime(msg,i); if (strcasecmp("audio", mtype) == 0){ stream->type=SalAudio; @@ -528,7 +549,8 @@ int sdp_to_media_description(sdp_message_t *msg, SalMediaDescription *desc){ for (j = 0; ((attr = sdp_message_attribute_get(msg, i, j)) != NULL); j++) { if ((keywordcmp("rtcp", attr->a_att_field) == 0) && (attr->a_att_value != NULL)) { char tmp[256]; - int nb = sscanf(attr->a_att_value, "%d IN IP4 %s", &stream->rtcp_port, tmp); + // added bounds check + int nb = sscanf(attr->a_att_value, "%d IN IP4 %256s", &stream->rtcp_port, tmp); if (nb == 1) { /* SDP rtcp attribute only contains the port */ } else if (nb == 2) { diff --git a/mediastreamer2 b/mediastreamer2 index 07824fcf..45c9c651 160000 --- a/mediastreamer2 +++ b/mediastreamer2 @@ -1 +1 @@ -Subproject commit 07824fcf3879d265c59beaf970d833b5859f3691 +Subproject commit 45c9c65168a9912bd2aa97344b396771ff1cdaf0 diff --git a/oRTP b/oRTP index 20b52714..c702c0ea 160000 --- a/oRTP +++ b/oRTP @@ -1 +1 @@ -Subproject commit 20b527144f9850dd9065d96db7a20244e8a8b227 +Subproject commit c702c0ea0e66bbe1f27c79690003d9748b01560f -- 2.39.2