Skip to content

Commit

Permalink
Properly decrypt/encrypt RTCPs when they are muxed in.
Browse files Browse the repository at this point in the history
  • Loading branch information
sobomax committed Jul 8, 2024
1 parent bd900ee commit 95d37b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
28 changes: 20 additions & 8 deletions modules/dtls_gw/rtpp_dtls_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "rtp.h"
#include "rtpp_time.h"
#include "rtp_packet.h"
#include "rtpp_packetops.h"
#include "rtpp_proc_async.h"
#include "rtpp_pthread.h"
#include "rtpp_ssrc.h"
Expand Down Expand Up @@ -449,6 +450,14 @@ rtpp_dtls_conn_dtls_recv(struct rtpp_dtls_conn *self,
pthread_mutex_unlock(&pvt->state_lock);
}

#if SRTP_PROTECT_NARGS == 4
#define SRTP_PROTECT(a, b, c) srtp_protect(a, b, c, 0);
#define SRTCP_PROTECT(a, b, c) srtp_protect_rtcp(a, b, c, 0);
#else
#define SRTP_PROTECT(a, b, c) srtp_protect(a, b, c);
#define SRTCP_PROTECT(a, b, c) srtp_protect_rtcp(a, b, c);
#endif

static int
rtpp_dtls_conn_rtp_send(struct rtpp_dtls_conn *self, struct pkt_proc_ctx *pktxp)
{
Expand All @@ -467,11 +476,11 @@ rtpp_dtls_conn_rtp_send(struct rtpp_dtls_conn *self, struct pkt_proc_ctx *pktxp)
}

len = pktxp->pktp->size;
#if SRTP_PROTECT_NARGS == 4
status = srtp_protect(pvt->srtp_ctx_out, pktxp->pktp->data.buf, &len, 0);
#else
status = srtp_protect(pvt->srtp_ctx_out, pktxp->pktp->data.buf, &len);
#endif
if (rtpp_is_rtcp_tst(pktxp)) {
status = SRTCP_PROTECT(pvt->srtp_ctx_out, pktxp->pktp->data.buf, &len);
} else {
status = SRTP_PROTECT(pvt->srtp_ctx_out, pktxp->pktp->data.buf, &len);
}
if (status){
return (-1);
}
Expand Down Expand Up @@ -499,8 +508,12 @@ rtpp_dtls_conn_srtp_recv(struct rtpp_dtls_conn *self, struct pkt_proc_ctx *pktxp
}

len = pktxp->pktp->size;
status = srtp_unprotect(pvt->srtp_ctx_in, pktxp->pktp->data.buf, &len);
if (status){
if (rtpp_is_rtcp_tst(pktxp)) {
status = srtp_unprotect_rtcp(pvt->srtp_ctx_in, pktxp->pktp->data.buf, &len);
} else {
status = srtp_unprotect(pvt->srtp_ctx_in, pktxp->pktp->data.buf, &len);
}
if (status) {
return (-1);
}
pktxp->pktp->size = len;
Expand All @@ -512,7 +525,6 @@ rtpp_dtls_conn_srtp_recv(struct rtpp_dtls_conn *self, struct pkt_proc_ctx *pktxp
static int
bio_write(BIO *b, const char *buf, int len)
{
struct sthread_args *sender;
struct rtpp_dtls_conn_priv *pvt = BIO_get_data(b);
struct rtp_packet *packet;

Expand Down
17 changes: 2 additions & 15 deletions modules/dtls_gw/rtpp_dtls_gw.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "rtp.h"
#include "rtpp_time.h"
#include "rtp_packet.h"
#include "rtpp_packetops.h"
#include "rtpp_command.h"
#include "rtpp_command_args.h"
#include "rtpp_command_sub.h"
Expand Down Expand Up @@ -107,7 +108,6 @@ static void rtpp_dtls_gw_dtor(struct rtpp_module_priv *);
static void rtpp_dtls_gw_worker(const struct rtpp_wthrdata *);
static int rtpp_dtls_gw_handle_command(struct rtpp_module_priv *,
const struct rtpp_subc_ctx *);
static bool is_dtls_packet(const struct rtp_packet *);
static int rtpp_dtls_gw_taste_encrypted(struct pkt_proc_ctx *);
static int rtpp_dtls_gw_taste_plain(struct pkt_proc_ctx *);
static enum pproc_action rtpp_dtls_gw_enqueue(const struct pkt_proc_ctx *);
Expand Down Expand Up @@ -468,7 +468,7 @@ rtpp_dtls_gw_taste_encrypted(struct pkt_proc_ctx *pktxp)
static __thread struct rtpp_dtls_gw_aux strp_in = {.direction = SRTP_IN};
struct rtpp_dtls_gw_aux *rdgap;

if (!is_dtls_packet(pktxp->pktp))
if (!rtpp_is_dtls_tst(pktxp))
rdgap = &strp_in;
else
rdgap = &dtls_in;
Expand Down Expand Up @@ -550,16 +550,3 @@ rtpp_dtls_gw_dtor(struct rtpp_module_priv *pvt)
mod_free(pvt);
return;
}

static bool
is_dtls_packet(const struct rtp_packet *pktp)
{
uint8_t b;

if (pktp->size < 13)
return false;

b = pktp->data.buf[0];

return (19 < b && b < 64);
}

0 comments on commit 95d37b9

Please sign in to comment.