Changeset 7bdd4ba5b1c58233b656c75e4abd9017f7ef02ee

Show
Ignore:
Timestamp:
03/11/07 19:53:53 (1 year ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1173639233 +0000
git-parent:

[a65a62289c22ad0c988d833eb25e3a616b524aba]

git-author:
Rémi Denis-Courmont <rem@videolan.org> 1173639233 +0000
Message:

Untested support for RFC4771:
Integrity Transform Carrying Roll-Over Counter for SRTP

Once debugged, it should provide a simple way to synchronize live
(multicast...) secure streams.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libs/srtp/srtp.c

    r9c7f833 r7bdd4ba  
    1818 */ 
    1919 
     20/* TODO: 
     21 * Useless stuff (because nothing depends on it): 
     22 * - non-nul key derivation rate 
     23 * - MKI payload 
     24 */ 
     25 
    2026#ifdef HAVE_CONFIG_H 
    2127# include <config.h> 
     
    3440#include <gcrypt.h> 
    3541 
     42#ifdef WIN32 
     43# include <winsock2.h> 
     44#else 
     45# include <netinet/in.h> 
     46# include <pthread.h> 
     47GCRY_THREAD_OPTION_PTHREAD_IMPL; 
     48#endif 
     49 
    3650#define debug( ... ) (void)0 
    37  
    38 /* TODO: 
    39  * Useful stuff: 
    40  * - ROC profile thingy (multicast really needs this) 
    41  * 
    42  * Useless stuff (because nothing depends on it): 
    43  * - non-nul key derivation rate 
    44  * - MKI payload 
    45  */ 
    4651 
    4752typedef struct srtp_proto_t 
     
    7782 
    7883 
    79 #ifdef WIN32 
    80 # include <winsock2.h> 
    81 #else 
    82 # include <netinet/in.h> 
    83 # include <pthread.h> 
    84 GCRY_THREAD_OPTION_PTHREAD_IMPL; 
    85 #endif 
     84static inline unsigned rcc_mode (const srtp_session_t *s) 
     85
     86    return (s->flags >> 4) & 3; 
     87
    8688 
    8789static bool libgcrypt_usable = false; 
     
    217219    s->flags = flags; 
    218220    s->tag_len = tag_len; 
     221    s->rtp_rcc = 1; /* Default RCC rate */ 
     222    if (rcc_mode (s)) 
     223    { 
     224        if (tag_len < 4) 
     225            goto error; 
     226    } 
    219227 
    220228    if (proto_create (&s->rtp, cipher, md) == 0) 
     
    225233    } 
    226234 
     235error: 
    227236    free (s); 
    228237    return NULL; 
     
    441450/** Message Authentication and Integrity for RTP */ 
    442451static const uint8_t * 
    443 rtp_digest (srtp_session_t *s, const uint8_t *data, size_t len) 
     452rtp_digest (srtp_session_t *s, const uint8_t *data, size_t len, 
     453            uint32_t roc) 
    444454{ 
    445455    const gcry_md_hd_t md = s->rtp.mac; 
    446     uint32_t roc = htonl (srtp_compute_roc (s, rtp_seq (data))); 
    447456 
    448457    gcry_md_reset (md); 
    449458    gcry_md_write (md, data, len); 
    450     gcry_md_write (md, &roc, 4); 
     459    gcry_md_write (md, &(uint32_t){ htonl (roc) }, 4); 
    451460    return gcry_md_read (md, 0); 
    452461} 
     
    533542 * @param buf RTP packet to be encrypted/digested 
    534543 * @param lenp pointer to the RTP packet length on entry, 
    535  *             set to the SRTP length on exit (undefined in case of error) 
     544 *             set to the SRTP length on exit (undefined on non-ENOSPC error) 
    536545 * @param bufsize size (bytes) of the packet buffer 
    537546 * 
    538547 * @return 0 on success, in case of error: 
    539548 *  EINVAL  malformatted RTP packet or internal error 
    540  *  ENOSPC  bufsize is too small (to add authentication tag) 
     549 *  ENOSPC  bufsize is too small to add authentication tag 
     550 *          (<lenp> will hold the required byte size) 
    541551 *  EACCES  packet would trigger a replay error on receiver 
    542552 */ 
     
    551561    if (!(s->flags & SRTP_UNAUTHENTICATED)) 
    552562    { 
    553         if (bufsize < (len + s->tag_len)) 
     563        size_t tag_len = s->tag_len; 
     564        *lenp = len + tag_len; 
     565        if (bufsize < (len + tag_len)) 
    554566            return ENOSPC; 
    555567 
    556         const uint8_t *tag = rtp_digest (s, buf, len); 
    557         memcpy (buf + len, tag, s->tag_len); 
    558         *lenp = len + s->tag_len; 
     568        uint32_t roc = srtp_compute_roc (s, rtp_seq (buf)); 
     569        const uint8_t *tag = rtp_digest (s, buf, len, roc); 
     570        if (rcc_mode (s)) 
     571        { 
     572            assert (s->rtp_rcc); 
     573            if ((rtp_seq (buf) % s->rtp_rcc) == 0) 
     574            { 
     575                memcpy (buf + len, &(uint32_t){ htonl (s->rtp_roc) }, 4); 
     576                len += 4; 
     577                if (rcc_mode (s) == 3) 
     578                    tag_len = 0; 
     579                else 
     580                    tag_len -= 4; 
     581            } 
     582            else 
     583            { 
     584                if (rcc_mode (s) & 1) 
     585                    tag_len = 0; 
     586            } 
     587        } 
     588        memcpy (buf + len, tag, tag_len); 
    559589    } 
    560590 
     
    579609{ 
    580610    size_t len = *lenp; 
     611    if (len < 12u) 
     612        return EINVAL; 
    581613 
    582614    if (!(s->flags & SRTP_UNAUTHENTICATED)) 
    583615    { 
    584         if (len < (12u + s->tag_len)) 
     616        size_t tag_len = s->tag_len, roc_len = 0; 
     617        if (rcc_mode (s)) 
     618        { 
     619            if ((rtp_seq (buf) % s->rtp_rcc) == 0) 
     620            { 
     621                roc_len = 4; 
     622                if (rcc_mode (s) == 3) 
     623                    tag_len = 0; 
     624                else 
     625                    tag_len -= 4; 
     626            } 
     627            else 
     628            { 
     629                if (rcc_mode (s) & 1) 
     630                    tag_len = 0; // RCC mode 1 or 3: no auth 
     631            } 
     632        } 
     633 
     634        if (len < (12u + roc_len + tag_len)) 
    585635            return EINVAL; 
    586         len -= s->tag_len; 
    587  
    588         const uint8_t *tag = rtp_digest (s, buf, len); 
    589         if (memcmp (buf + len, tag, s->tag_len)) 
     636        len -= roc_len + tag_len; 
     637 
     638        uint32_t roc = srtp_compute_roc (s, rtp_seq (buf)), rcc; 
     639        if (roc_len) 
     640        { 
     641            assert (roc_len == 4); 
     642            memcpy (&rcc, buf + len, 4); 
     643            rcc = ntohl (rcc); 
     644        } 
     645        else 
     646            rcc = roc; 
     647 
     648        const uint8_t *tag = rtp_digest (s, buf, len, rcc); 
     649        if (memcmp (buf + len + roc_len, tag, s->tag_len)) 
    590650            return EACCES; 
    591651 
     652        if (roc_len) 
     653        { 
     654            /* Authenticated packet carried a Roll-Over-Counter */ 
     655            s->rtp_roc += rcc - roc; 
     656            assert (srtp_compute_roc (s, rtp_seq (buf)) == rcc); 
     657        } 
    592658        *lenp = len; 
    593659    }