summary refs log tree commit diff
path: root/crypto/src/tls/ProtocolName.cs
blob: 529e81b1999c1d0911f3150230ac98d9e77ff65f (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.IO;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Tls
{
    /// <summary>RFC 7301 Represents a protocol name for use with ALPN.</summary>
    public sealed class ProtocolName
    {
        public static ProtocolName AsRawBytes(byte[] bytes)
        {
            return new ProtocolName(Arrays.Clone(bytes));
        }

        public static ProtocolName AsUtf8Encoding(String name)
        {
            return new ProtocolName(Strings.ToUtf8ByteArray(name));
        }

        public static readonly ProtocolName Http_1_1 = AsUtf8Encoding("http/1.1");
        public static readonly ProtocolName Spdy_1 = AsUtf8Encoding("spdy/1");
        public static readonly ProtocolName Spdy_2 = AsUtf8Encoding("spdy/2");
        public static readonly ProtocolName Spdy_3 = AsUtf8Encoding("spdy/3");
        public static readonly ProtocolName Stun_Turn = AsUtf8Encoding("stun.turn");
        public static readonly ProtocolName Stun_Nat_Discovery = AsUtf8Encoding("stun.nat-discovery");
        public static readonly ProtocolName Http_2_Tls = AsUtf8Encoding("h2");
        public static readonly ProtocolName Http_2_Tcp = AsUtf8Encoding("h2c");
        public static readonly ProtocolName WebRtc = AsUtf8Encoding("webrtc");
        public static readonly ProtocolName WebRtc_Confidential = AsUtf8Encoding("c-webrtc");
        public static readonly ProtocolName Ftp = AsUtf8Encoding("ftp");
        public static readonly ProtocolName Imap = AsUtf8Encoding("imap");
        public static readonly ProtocolName Pop3 = AsUtf8Encoding("pop3");
        public static readonly ProtocolName ManageSieve = AsUtf8Encoding("managesieve");
        public static readonly ProtocolName Coap = AsUtf8Encoding("coap");
        public static readonly ProtocolName Xmpp_Client = AsUtf8Encoding("xmpp-client");
        public static readonly ProtocolName Xmpp_Server = AsUtf8Encoding("xmpp-server");

        private readonly byte[] m_bytes;

        private ProtocolName(byte[] bytes)
        {
            if (bytes == null)
                throw new ArgumentNullException("bytes");
            if (bytes.Length < 1 || bytes.Length > 255)
                throw new ArgumentException("must have length from 1 to 255", "bytes");

            this.m_bytes = bytes;
        }

        public byte[] GetBytes()
        {
            return Arrays.Clone(m_bytes);
        }

        public string GetUtf8Decoding()
        {
            return Strings.FromUtf8ByteArray(m_bytes);
        }

        /// <summary>Encode this <see cref="ProtocolName"/> to a <see cref="Stream"/>.</summary>
        /// <param name="output">the <see cref="Stream"/> to encode to.</param>
        /// <exception cref="IOException"/>
        public void Encode(Stream output)
        {
            TlsUtilities.WriteOpaque8(m_bytes, output);
        }

        /// <summary>Parse a <see cref="ProtocolName"/> from a <see cref="Stream"/>.</summary>
        /// <param name="input">the <see cref="Stream"/> to parse from.</param>
        /// <returns>a <see cref="ProtocolName"/> object.</returns>
        /// <exception cref="IOException"/>
        public static ProtocolName Parse(Stream input)
        {
            return new ProtocolName(TlsUtilities.ReadOpaque8(input, 1));
        }

        public override bool Equals(object obj)
        {
            return obj is ProtocolName && Arrays.AreEqual(m_bytes, ((ProtocolName)obj).m_bytes);
        }

        public override int GetHashCode()
        {
            return Arrays.GetHashCode(m_bytes);
        }
    }
}