]> git.sesse.net Git - vlc/blobdiff - libs/srtp/srtp.c
API cleanup
[vlc] / libs / srtp / srtp.c
index f60147bfa6c0dcdabda9b5ea160787ff83168c82..9978ff44d5e1f08e1cc872d3018723a1b0171f0b 100644 (file)
@@ -38,7 +38,6 @@
 /* TODO:
  * Useful stuff:
  * - ROC profile thingy (multicast really needs this)
- * - replay protection
  *
  * Useless stuff (because nothing depends on it):
  * - non-nul key derivation rate
@@ -62,6 +61,7 @@ struct srtp_session_t
     uint32_t rtcp_index;
     uint32_t rtp_roc;
     uint16_t rtp_seq;
+    uint16_t rtp_rcc;
     uint8_t  tag_len;
 };
 
@@ -75,6 +75,7 @@ enum
     SRTCP_SALT
 };
 
+
 #ifdef WIN32
 # include <winsock2.h>
 #else
@@ -160,33 +161,52 @@ static int proto_create (srtp_proto_t *p, int gcipher, int gmd)
  * internal cryptographic counters; it is however of course feasible to open
  * multiple simultaneous sessions with the same master key.
  *
- * @param name cipher-suite name
- * @param kdr key derivation rate
+ * @param encr encryption algorithm number
+ * @param auth authentication algortihm number
+ * @param tag_len authentication tag byte length (NOT including RCC)
  * @param flags OR'ed optional flags.
  *
  * @return NULL in case of error
  */
 srtp_session_t *
-srtp_create (const char *name, unsigned flags, unsigned kdr)
+srtp_create (int encr, int auth, unsigned tag_len, int prf, unsigned flags)
 {
-    assert (name != NULL);
+    if ((flags & ~SRTP_FLAGS_MASK) || init_libgcrypt ())
+        return NULL;
 
-    if (kdr != 0)
-        return NULL; // FIXME: KDR not implemented yet
+    int cipher, md;
+    switch (encr)
+    {
+        case SRTP_ENCR_NULL:
+            cipher = GCRY_CIPHER_NONE;
+            break;
 
-    uint8_t tag_len;
-    int cipher = GCRY_CIPHER_AES, md = GCRY_MD_SHA1;
+        case SRTP_ENCR_AES_CM:
+            cipher = GCRY_CIPHER_AES;
+            break;
 
-    if (strcmp (name, "AES_CM_128_HMAC_SHA1_80") == 0)
-        tag_len = 10;
-    else
-    if (strcmp (name, "AES_CM_128_HMAC_SHA1_32") == 0)
-        tag_len = 4;
-    else
-    // F8_128_HMAC_SHA1_80 is not implemented
+        default:
+            return NULL;
+    }
+
+    switch (auth)
+    {
+        case SRTP_AUTH_NULL:
+            md = GCRY_MD_NONE;
+            break;
+
+        case SRTP_AUTH_HMAC_SHA1:
+            md = GCRY_MD_SHA1;
+            break;
+
+        default:
+            return NULL;
+    }
+
+    if (tag_len > gcry_md_get_algo_dlen (auth))
         return NULL;
 
-    if ((flags & ~SRTP_FLAGS_MASK) || init_libgcrypt ())
+    if (prf != SRTP_PRF_AES_CM)
         return NULL;
 
     srtp_session_t *s = malloc (sizeof (*s));
@@ -195,7 +215,6 @@ srtp_create (const char *name, unsigned flags, unsigned kdr)
 
     memset (s, 0, sizeof (*s));
     s->flags = flags;
-    s->kdr = kdr;
     s->tag_len = tag_len;
 
     if (proto_create (&s->rtp, cipher, md) == 0)
@@ -337,6 +356,33 @@ srtp_setkey (srtp_session_t *s, const void *key, size_t keylen,
 }
 
 
+/**
+ * Sets Roll-over-Counter Carry (RCC) rate for the SRTP session. If not
+ * specified (through this function), the default rate of ONE is assumed
+ * (i.e. every RTP packets will carry the RoC). RCC rate is ignored if none
+ * of the RCC mode has been selected.
+ *
+ * The RCC mode is selected through one of these flags for srtp_create():
+ *  SRTP_RCC_MODE1: integrity protection only for RoC carrying packets
+ *  SRTP_RCC_MODE2: integrity protection for all packets
+ *  SRTP_RCC_MODE3: no integrity protection
+ *
+ * RCC mode 3 is insecure. Compared to plain RTP, it provides confidentiality
+ * (through encryption) but is much more prone to DoS. It can only be used if
+ * anti-spoofing protection is provided by lower network layers (e.g. IPsec,
+ * or trusted routers and proper source address filtering).
+ *
+ * If RCC rate is 1, RCC mode 1 and 2 are functionally identical.
+ *
+ * @param rate RoC Carry rate (MUST NOT be zero)
+ */
+void srtp_setrcc_rate (srtp_session_t *s, uint16_t rate)
+{
+    assert (rate != 0);
+    s->rtp_rcc = rate;
+}
+
+
 /** AES-CM encryption/decryption (ctr length = 16 bytes) */
 static int
 ctr_crypt (gcry_cipher_hd_t hd, uint32_t *ctr, uint8_t *data, size_t len)
@@ -440,6 +486,7 @@ rtp_digest (srtp_session_t *s, const uint8_t *data, size_t len)
  *
  * @return 0 on success, in case of error:
  *  EINVAL  malformatted RTP packet
+ *  EACCES  replayed packet or out-of-window or sync lost
  */
 static int srtp_crypt (srtp_session_t *s, uint8_t *buf, size_t len)
 {
@@ -473,11 +520,22 @@ static int srtp_crypt (srtp_session_t *s, uint8_t *buf, size_t len)
     memcpy (&ssrc, buf + 8, 4);
 
     /* Updates ROC and sequence (it's safe now) */
-    if (roc > s->rtp_roc)
+    int16_t diff = seq - s->rtp_seq;
+    if (diff > 0)
+    {
+        /* Sequence in the future, good */
+        s->rtp.window = s->rtp.window << diff;
+        s->rtp.window |= 1;
         s->rtp_seq = seq, s->rtp_roc = roc;
+    }
     else
-    if (seq > s->rtp_seq)
-        s->rtp_seq = seq;
+    {
+        /* Sequence in the past/present, bad */
+        diff = -diff;
+        if ((diff >= 64) || ((s->rtp.window >> diff) & 1))
+            return EACCES; /* Replay attack */
+        s->rtp.window |= 1 << diff;
+    }
 
     /* Encrypt/Decrypt */
     if (s->flags & SRTP_UNENCRYPTED)
@@ -504,6 +562,7 @@ static int srtp_crypt (srtp_session_t *s, uint8_t *buf, size_t len)
  * @return 0 on success, in case of error:
  *  EINVAL  malformatted RTP packet or internal error
  *  ENOSPC  bufsize is too small (to add authentication tag)
+ *  EACCES  packet would trigger a replay error on receiver
  */
 int
 srtp_send (srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t bufsize)
@@ -543,7 +602,6 @@ int
 srtp_recv (srtp_session_t *s, uint8_t *buf, size_t *lenp)
 {
     size_t len = *lenp;
-    /* FIXME: anti-replay */
 
     if (!(s->flags & SRTP_UNAUTHENTICATED))
     {
@@ -600,10 +658,33 @@ static int srtcp_crypt (srtp_session_t *s, uint8_t *buf, size_t len)
     if ((len < 12) || ((buf[0] >> 6) != 2))
         return EINVAL;
 
-    uint32_t index = s->rtcp_index++;
-    if (index == 0x7fffffff)
-        s->rtcp_index = 0; /* 31-bit wrap */
+    uint32_t index;
+    memcpy (&index, buf + len, 4);
+    index = ntohl (index);
+    if (((index >> 31) != 0) != ((s->flags & SRTCP_UNENCRYPTED) == 0))
+        return EINVAL; // E-bit mismatch
 
+    index &= ~(1 << 31); // clear E-bit for counter
+
+    /* Updates SRTCP index (safe here) */
+    int32_t diff = index - s->rtcp_index;
+    if (diff > 0)
+    {
+        /* Packet in the future, good */
+        s->rtcp.window = s->rtcp.window << diff;
+        s->rtcp.window |= 1;
+        s->rtcp_index = index;
+    }
+    else
+    {
+        /* Packet in the past/present, bad */
+        diff = -diff;
+        if ((diff >= 64) || ((s->rtcp.window >> diff) & 1))
+            return EACCES; // replay attack!
+        s->rtp.window |= 1 << diff;
+    }
+
+    /* Crypts SRTCP */
     if (s->flags & SRTCP_UNENCRYPTED)
         return 0;
 
@@ -637,7 +718,10 @@ srtcp_send (srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t bufsize)
     if (bufsize < (len + 4 + s->tag_len))
         return ENOSPC;
 
-    uint32_t index = s->rtcp_index;
+    uint32_t index = ++s->rtcp_index;
+    if (index >> 31)
+        s->rtcp_index = index = 0; /* 31-bit wrap */
+
     if ((s->flags & SRTCP_UNENCRYPTED) == 0)
         index |= 0x80000000; /* Set Encrypted bit */
     memcpy (buf + len, &(uint32_t){ htonl (index) }, 4);
@@ -646,7 +730,7 @@ srtcp_send (srtp_session_t *s, uint8_t *buf, size_t *lenp, size_t bufsize)
     if (val)
         return val;
 
-    len += 4; /* Digest SRTCP index too */
+    len += 4; /* Digests SRTCP index too */
 
     const uint8_t *tag = rtcp_digest (s->rtp.mac, buf, len);
     memcpy (buf + len, tag, s->tag_len);
@@ -671,7 +755,6 @@ int
 srtcp_recv (srtp_session_t *s, uint8_t *buf, size_t *lenp)
 {
     size_t len = *lenp;
-    /* FIXME: anti-replay ?? */
 
     if (len < (4u + s->tag_len))
         return EINVAL;
@@ -683,7 +766,6 @@ srtcp_recv (srtp_session_t *s, uint8_t *buf, size_t *lenp)
 
     len -= 4; /* Remove SRTCP index before decryption */
     *lenp = len;
-
     return srtp_crypt (s, buf, len);
 }