summary refs log tree commit diff
path: root/crypto/src/tls/TlsSrtpUtilities.cs
blob: 72a9e774b76a6f07247d17da69245c0eded98462 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections;
using System.IO;

namespace Org.BouncyCastle.Tls
{
    /// <summary>RFC 5764 DTLS Extension to Establish Keys for SRTP.</summary>
    public abstract class TlsSrtpUtilities
{
        /// <exception cref="IOException"/>
        public static void AddUseSrtpExtension(IDictionary extensions, UseSrtpData useSrtpData)
        {
            extensions[ExtensionType.use_srtp] = CreateUseSrtpExtension(useSrtpData);
        }

        /// <exception cref="IOException"/>
        public static UseSrtpData GetUseSrtpExtension(IDictionary extensions)
        {
            byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.use_srtp);
            return extensionData == null ? null : ReadUseSrtpExtension(extensionData);
        }

        /// <exception cref="IOException"/>
        public static byte[] CreateUseSrtpExtension(UseSrtpData useSrtpData)
        {
            if (useSrtpData == null)
                throw new ArgumentNullException("useSrtpData");

            MemoryStream buf = new MemoryStream();

            // SRTPProtectionProfiles
            TlsUtilities.WriteUint16ArrayWithUint16Length(useSrtpData.ProtectionProfiles, buf);

            // srtp_mki
            TlsUtilities.WriteOpaque8(useSrtpData.Mki, buf);

            return buf.ToArray();
        }

        /// <exception cref="IOException"/>
        public static UseSrtpData ReadUseSrtpExtension(byte[] extensionData)
        {
            if (extensionData == null)
                throw new ArgumentNullException("extensionData");

            MemoryStream buf = new MemoryStream(extensionData, false);

            // SRTPProtectionProfiles
            int length = TlsUtilities.ReadUint16(buf);
            if (length < 2 || (length & 1) != 0)
                throw new TlsFatalAlert(AlertDescription.decode_error);

            int[] protectionProfiles = TlsUtilities.ReadUint16Array(length / 2, buf);

            // srtp_mki
            byte[] mki = TlsUtilities.ReadOpaque8(buf);

            TlsProtocol.AssertEmpty(buf);

            return new UseSrtpData(protectionProfiles, mki);
        }
    }
}