]> git.sesse.net Git - vlc/blobdiff - libs/srtp/srtp.c
SRTP: try to fix dependent libraries
[vlc] / libs / srtp / srtp.c
index ec303da83ab5470d1f1981fce45b66e36fb8dcc0..87a3c2e4db36ab763a0ff99a63e6467e607b7121 100644 (file)
@@ -106,10 +106,8 @@ static int init_libgcrypt (void)
 {
     int retval;
 #ifndef WIN32
-    static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     static pthread_once_t once = PTHREAD_ONCE_INIT;
 
-    pthread_mutex_lock (&mutex);
     pthread_once (&once, initonce_libgcrypt);
 #else
 # warning FIXME: This is not thread-safe.
@@ -119,10 +117,6 @@ static int init_libgcrypt (void)
 
     retval = libgcrypt_usable ? 0 : -1;
 
-#ifndef WIN32
-    pthread_mutex_unlock (&mutex);
-#endif
-
     return retval;
 
 }
@@ -378,6 +372,53 @@ srtp_setkey (srtp_session_t *s, const void *key, size_t keylen,
     return srtp_derive (s, key, keylen, salt, saltlen) ? EINVAL : 0;
 }
 
+static int hexdigit (char c)
+{
+    if ((c >= '0') && (c <= '9'))
+        return c - '0';
+    if ((c >= 'A') && (c <= 'F'))
+        return c - 'A' + 0xA;
+    if ((c >= 'a') && (c <= 'f'))
+        return c - 'a' + 0xa;
+    return -1;
+}
+
+static ssize_t hexstring (const char *in, uint8_t *out, size_t outlen)
+{
+    size_t inlen = strlen (in);
+
+    if ((inlen > (2 * outlen)) || (inlen & 1))
+        return -1;
+
+    for (size_t i = 0; i < inlen; i += 2)
+    {
+        int a = hexdigit (in[2 * i]), b = hexdigit (in[2 * i + 1]);
+        if ((a == -1) || (b == -1))
+            return EINVAL;
+        out[i] = (a << 4) | b;
+    }
+    return inlen / 2;
+}
+
+/**
+ * Sets (or resets) the master key and master salt for a SRTP session
+ * from hexadecimal strings. See also srtp_setkey().
+ *
+ * @return 0 on success, in case of error:
+ *  EINVAL  invalid or unsupported key/salt sizes combination
+ */
+int
+srtp_setkeystring (srtp_session_t *s, const char *key, const char *salt)
+{
+    uint8_t bkey[32]; /* TODO/NOTE: hard-coded for AES */
+    uint8_t bsalt[14]; /* TODO/NOTE: hard-coded for the PRF-AES-CM */
+    ssize_t bkeylen = hexstring (key, bkey, sizeof (bkey));
+    ssize_t bsaltlen = hexstring (salt, bsalt, sizeof (bsalt));
+
+    if ((bkeylen == -1) || (bsaltlen == -1))
+        return EINVAL;
+    return srtp_derive (s, bkey, bkeylen, bsalt, bsaltlen) ? EINVAL : 0;
+}
 
 /**
  * Sets Roll-over-Counter Carry (RCC) rate for the SRTP session. If not
@@ -651,7 +692,16 @@ srtp_recv (srtp_session_t *s, uint8_t *buf, size_t *lenp)
             rcc = roc;
 
         const uint8_t *tag = rtp_digest (s, buf, len, rcc);
-        if (memcmp (buf + len + roc_len, tag, s->tag_len))
+#if 0
+        printf ("Computed: 0x");
+        for (unsigned i = 0; i < tag_len; i++)
+            printf ("%02x", tag[i]);
+        printf ("\nReceived: 0x");
+        for (unsigned i = 0; i < tag_len; i++)
+            printf ("%02x", buf[len + roc_len + i]);
+        puts ("");
+#endif
+        if (memcmp (buf + len + roc_len, tag, tag_len))
             return EACCES;
 
         if (roc_len)