summary refs log tree commit diff
path: root/crypto/src/tls/ClientHello.cs
blob: 700d424cdc39dadc2be362a3309df4798c64f0a6 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections;
using System.IO;

using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Tls
{
    public sealed class ClientHello
    {
        private readonly ProtocolVersion m_version;
        private readonly byte[] m_random;
        private readonly byte[] m_sessionID;
        private readonly byte[] m_cookie;
        private readonly int[] m_cipherSuites;
        private readonly IDictionary m_extensions;
        private readonly int m_bindersSize;

        public ClientHello(ProtocolVersion version, byte[] random, byte[] sessionID, byte[] cookie,
            int[] cipherSuites, IDictionary extensions, int bindersSize)
        {
            this.m_version = version;
            this.m_random = random;
            this.m_sessionID = sessionID;
            this.m_cookie = cookie;
            this.m_cipherSuites = cipherSuites;
            this.m_extensions = extensions;
            this.m_bindersSize = bindersSize;
        }

        public int BindersSize
        {
            get { return m_bindersSize; }
        }

        public int[] CipherSuites
        {
            get { return m_cipherSuites; }
        }

        public byte[] Cookie
        {
            get { return m_cookie; }
        }

        public IDictionary Extensions
        {
            get { return m_extensions; }
        }

        public byte[] Random
        {
            get { return m_random; }
        }

        public byte[] SessionID
        {
            get { return m_sessionID; }
        }

        public ProtocolVersion Version
        {
            get { return m_version; }
        }

        /// <summary>Encode this <see cref="ClientHello"/> to a <see cref="Stream"/>.</summary>
        /// <param name="context">the <see cref="TlsContext"/> of the current connection.</param>
        /// <param name="output">the <see cref="Stream"/> to encode to.</param>
        /// <exception cref="IOException"/>
        public void Encode(TlsContext context, Stream output)
        {
            TlsUtilities.WriteVersion(m_version, output);

            output.Write(m_random, 0, m_random.Length);

            TlsUtilities.WriteOpaque8(m_sessionID, output);

            if (null != m_cookie)
            {
                TlsUtilities.WriteOpaque8(m_cookie, output);
            }

            TlsUtilities.WriteUint16ArrayWithUint16Length(m_cipherSuites, output);

            TlsUtilities.WriteUint8ArrayWithUint8Length(new short[]{ CompressionMethod.cls_null }, output);

            TlsProtocol.WriteExtensions(output, m_extensions, m_bindersSize);
        }

        /// <summary>Parse a <see cref="ClientHello"/> from a <see cref="MemoryStream"/>.</summary>
        /// <param name="messageInput">the <see cref="MemoryStream"/> to parse from.</param>
        /// <param name="dtlsOutput">for DTLS this should be non-null; the input is copied to this
        /// <see cref="Stream"/>, minus the cookie field.</param>
        /// <returns>a <see cref="ClientHello"/> object.</returns>
        /// <exception cref="TlsFatalAlert"/>
        public static ClientHello Parse(MemoryStream messageInput, Stream dtlsOutput)
        {
            try
            {
                return ImplParse(messageInput, dtlsOutput);
            }
            catch (TlsFatalAlert e)
            {
                throw e;
            }
            catch (IOException e)
            {
                throw new TlsFatalAlert(AlertDescription.decode_error, e);
            }
        }

        /// <exception cref="IOException"/>
        private static ClientHello ImplParse(MemoryStream messageInput, Stream dtlsOutput)
        {
            Stream input = messageInput;
            if (null != dtlsOutput)
            {
                input = new TeeInputStream(input, dtlsOutput);
            }

            ProtocolVersion clientVersion = TlsUtilities.ReadVersion(input);

            byte[] random = TlsUtilities.ReadFully(32, input);

            byte[] sessionID = TlsUtilities.ReadOpaque8(input, 0, 32);

            byte[] cookie = null;
            if (null != dtlsOutput)
            {
                /*
                 * RFC 6347 This specification increases the cookie size limit to 255 bytes for greater
                 * future flexibility. The limit remains 32 for previous versions of DTLS.
                 */
                int maxCookieLength = ProtocolVersion.DTLSv12.IsEqualOrEarlierVersionOf(clientVersion) ? 255 : 32;

                cookie = TlsUtilities.ReadOpaque8(messageInput, 0, maxCookieLength);
            }

            int cipher_suites_length = TlsUtilities.ReadUint16(input);
            if (cipher_suites_length < 2 || (cipher_suites_length & 1) != 0
                || (int)(messageInput.Length - messageInput.Position) < cipher_suites_length)
            {
                throw new TlsFatalAlert(AlertDescription.decode_error);
            }

            /*
             * NOTE: "If the session_id field is not empty (implying a session resumption request) this
             * vector must include at least the cipher_suite from that session."
             */
            int[] cipherSuites = TlsUtilities.ReadUint16Array(cipher_suites_length / 2, input);

            short[] compressionMethods = TlsUtilities.ReadUint8ArrayWithUint8Length(input, 1);
            if (!Arrays.Contains(compressionMethods, CompressionMethod.cls_null))
                throw new TlsFatalAlert(AlertDescription.handshake_failure);

            /*
             * NOTE: Can't use TlsProtocol.ReadExtensions directly because TeeInputStream a) won't have
             * 'Length' or 'Position' properties in the FIPS provider, b) isn't a MemoryStream.
             */
            IDictionary extensions = null;
            if (messageInput.Position < messageInput.Length)
            {
                byte[] extBytes = TlsUtilities.ReadOpaque16(input);

                TlsProtocol.AssertEmpty(messageInput);

                extensions = TlsProtocol.ReadExtensionsDataClientHello(extBytes);
            }

            return new ClientHello(clientVersion, random, sessionID, cookie, cipherSuites, extensions, 0);
        }
    }
}