summary refs log tree commit diff
path: root/crypto/src/tls/KeyShareEntry.cs
blob: c4be657c067a22711fe51a6f2493922727fb75a3 (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
using System;
using System.IO;

namespace Org.BouncyCastle.Tls
{
    public sealed class KeyShareEntry
    {
        private static bool CheckKeyExchangeLength(int length)
        {
            return 0 < length && length < (1 << 16);
        }

        private readonly int m_namedGroup;
        private readonly byte[] m_keyExchange;

        /// <param name="namedGroup"><see cref="NamedGroup"/></param>
        /// <param name="keyExchange"></param>
        public KeyShareEntry(int namedGroup, byte[] keyExchange)
        {
            if (!TlsUtilities.IsValidUint16(namedGroup))
                throw new ArgumentException("should be a uint16", "namedGroup");
            if (null == keyExchange)
                throw new ArgumentNullException("keyExchange");
            if (!CheckKeyExchangeLength(keyExchange.Length))
                throw new ArgumentException("must have length from 1 to (2^16 - 1)", "keyExchange");

            this.m_namedGroup = namedGroup;
            this.m_keyExchange = keyExchange;
        }

        /// <returns><see cref="NamedGroup"/></returns>
        public int NamedGroup
        {
            get { return m_namedGroup; }
        }

        public byte[] KeyExchange
        {
            get { return m_keyExchange; }
        }

        /// <summary>Encode this <see cref="KeyShareEntry"/> 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.WriteUint16(NamedGroup, output);
            TlsUtilities.WriteOpaque16(KeyExchange, output);
        }

        /// <summary>Parse a <see cref="KeyShareEntry"/> from a <see cref="Stream"/>.</summary>
        /// <param name="input">the <see cref="Stream"/> to parse from.</param>
        /// <returns>a <see cref="KeyShareEntry"/> object.</returns>
        /// <exception cref="IOException"/>
        public static KeyShareEntry Parse(Stream input)
        {
            int namedGroup = TlsUtilities.ReadUint16(input);
            byte[] keyExchange = TlsUtilities.ReadOpaque16(input, 1);
            return new KeyShareEntry(namedGroup, keyExchange);
        }
    }
}