summary refs log tree commit diff
path: root/crypto/src/tls/Certificate.cs
blob: fef35fc1e05a36bada277ac14b4e5854b59d95ff (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections;
using System.IO;

using Org.BouncyCastle.Tls.Crypto;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Tls
{
    /// <summary>Parsing and encoding of a <i>Certificate</i> struct from RFC 4346.</summary>
    /// <remarks>
    /// <pre>
    /// opaque ASN.1Cert&lt;2^24-1&gt;;
    /// struct {
    ///   ASN.1Cert certificate_list&lt;0..2^24-1&gt;;
    /// } Certificate;
    /// </pre>
    /// </remarks>
    public sealed class Certificate
    {
        private static readonly TlsCertificate[] EmptyCerts = new TlsCertificate[0];
        private static readonly CertificateEntry[] EmptyCertEntries = new CertificateEntry[0];

        public static readonly Certificate EmptyChain = new Certificate(EmptyCerts);
        public static readonly Certificate EmptyChainTls13 = new Certificate(TlsUtilities.EmptyBytes, EmptyCertEntries);

        public sealed class ParseOptions
        {
            private int m_maxChainLength = int.MaxValue;

            public int MaxChainLength
            {
                get { return m_maxChainLength; }
            }

            public ParseOptions SetMaxChainLength(int maxChainLength)
            {
                this.m_maxChainLength = maxChainLength;
                return this;
            }
        }

        private static CertificateEntry[] Convert(TlsCertificate[] certificateList)
        {
            if (TlsUtilities.IsNullOrContainsNull(certificateList))
                throw new ArgumentException("cannot be null or contain any nulls", "certificateList");

            int count = certificateList.Length;
            CertificateEntry[] result = new CertificateEntry[count];
            for (int i = 0; i < count; ++i)
            {
                result[i] = new CertificateEntry(certificateList[i], null);
            }
            return result;
        }

        private readonly byte[] m_certificateRequestContext;
        private readonly CertificateEntry[] m_certificateEntryList;

        public Certificate(TlsCertificate[] certificateList)
            : this(null, Convert(certificateList))
        {
        }

        // TODO[tls13] Prefer to manage the certificateRequestContext internally only? 
        public Certificate(byte[] certificateRequestContext, CertificateEntry[] certificateEntryList)
        {
            if (null != certificateRequestContext && !TlsUtilities.IsValidUint8(certificateRequestContext.Length))
                throw new ArgumentException("cannot be longer than 255", "certificateRequestContext");
            if (TlsUtilities.IsNullOrContainsNull(certificateEntryList))
                throw new ArgumentException("cannot be null or contain any nulls", "certificateEntryList");

            this.m_certificateRequestContext = TlsUtilities.Clone(certificateRequestContext);
            this.m_certificateEntryList = certificateEntryList;
        }

        public byte[] GetCertificateRequestContext()
        {
            return TlsUtilities.Clone(m_certificateRequestContext);
        }

        /// <returns>an array of <see cref="TlsCertificate"/> representing a certificate chain.</returns>
        public TlsCertificate[] GetCertificateList()
        {
            return CloneCertificateList();
        }

        public TlsCertificate GetCertificateAt(int index)
        {
            return m_certificateEntryList[index].Certificate;
        }

        public CertificateEntry GetCertificateEntryAt(int index)
        {
            return m_certificateEntryList[index];
        }

        public CertificateEntry[] GetCertificateEntryList()
        {
            return CloneCertificateEntryList();
        }

        public short CertificateType
        {
            get { return Tls.CertificateType.X509; }
        }

        public int Length
        {
            get { return m_certificateEntryList.Length; }
        }

        /// <returns><c>true</c> if this certificate chain contains no certificates, or <c>false</c> otherwise.
        /// </returns>
        public bool IsEmpty
        {
            get { return m_certificateEntryList.Length == 0; }
        }

        /// <summary>Encode this <see cref="Certificate"/> to a <see cref="Stream"/>, and optionally calculate the
        /// "end point hash" (per RFC 5929's tls-server-end-point binding).</summary>
        /// <param name="context">the <see cref="TlsContext"/> of the current connection.</param>
        /// <param name="messageOutput">the <see cref="Stream"/> to encode to.</param>
        /// <param name="endPointHashOutput">the <see cref="Stream"/> to write the "end point hash" to (or null).
        /// </param>
        /// <exception cref="IOException"/>
        public void Encode(TlsContext context, Stream messageOutput, Stream endPointHashOutput)
        {
            bool isTlsV13 = TlsUtilities.IsTlsV13(context);

            if ((null != m_certificateRequestContext) != isTlsV13)
                throw new InvalidOperationException();

            if (isTlsV13)
            {
                TlsUtilities.WriteOpaque8(m_certificateRequestContext, messageOutput);
            }

            int count = m_certificateEntryList.Length;
            IList certEncodings = Platform.CreateArrayList(count);
            IList extEncodings = isTlsV13 ? Platform.CreateArrayList(count) : null;

            long totalLength = 0;
            for (int i = 0; i < count; ++i)
            {
                CertificateEntry entry = m_certificateEntryList[i];
                TlsCertificate cert = entry.Certificate;
                byte[] derEncoding = cert.GetEncoded();

                if (i == 0 && endPointHashOutput != null)
                {
                    CalculateEndPointHash(context, cert, derEncoding, endPointHashOutput);
                }

                certEncodings.Add(derEncoding);
                totalLength += derEncoding.Length;
                totalLength += 3;

                if (isTlsV13)
                {
                    IDictionary extensions = entry.Extensions;
                    byte[] extEncoding = (null == extensions)
                        ?   TlsUtilities.EmptyBytes
                        :   TlsProtocol.WriteExtensionsData(extensions);

                    extEncodings.Add(extEncoding);
                    totalLength += extEncoding.Length;
                    totalLength += 2;
                }
            }

            TlsUtilities.CheckUint24(totalLength);
            TlsUtilities.WriteUint24((int)totalLength, messageOutput);

            for (int i = 0; i < count; ++i)
            {
                byte[] certEncoding = (byte[])certEncodings[i];
                TlsUtilities.WriteOpaque24(certEncoding, messageOutput);

                if (isTlsV13)
                {
                    byte[] extEncoding = (byte[])extEncodings[i];
                    TlsUtilities.WriteOpaque16(extEncoding, messageOutput);
                }
            }
        }

        /// <summary>Parse a <see cref="Certificate"/> from a <see cref="Stream"/>.</summary>
        /// <param name="options">the <see cref="ParseOptions"/> to apply during parsing.</param>
        /// <param name="context">the <see cref="TlsContext"/> of the current connection.</param>
        /// <param name="messageInput">the <see cref="Stream"/> to parse from.</param>
        /// <param name="endPointHashOutput">the <see cref="Stream"/> to write the "end point hash" to (or null).
        /// </param>
        /// <returns>a <see cref="Certificate"/> object.</returns>
        /// <exception cref="IOException"/>
        public static Certificate Parse(ParseOptions options, TlsContext context, Stream messageInput,
            Stream endPointHashOutput)
        {
            SecurityParameters securityParameters = context.SecurityParameters;
            bool isTlsV13 = TlsUtilities.IsTlsV13(securityParameters.NegotiatedVersion);

            byte[] certificateRequestContext = null;
            if (isTlsV13)
            {
                certificateRequestContext = TlsUtilities.ReadOpaque8(messageInput);
            }

            int totalLength = TlsUtilities.ReadUint24(messageInput);
            if (totalLength == 0)
            {
                return !isTlsV13 ? EmptyChain
                    :  certificateRequestContext.Length < 1 ? EmptyChainTls13
                    :  new Certificate(certificateRequestContext, EmptyCertEntries);
            }

            byte[] certListData = TlsUtilities.ReadFully(totalLength, messageInput);
            MemoryStream buf = new MemoryStream(certListData, false);

            TlsCrypto crypto = context.Crypto;
            int maxChainLength = System.Math.Max(1, options.MaxChainLength);

            IList certificate_list = Platform.CreateArrayList();
            while (buf.Position < buf.Length)
            {
                if (certificate_list.Count >= maxChainLength)
                {
                    throw new TlsFatalAlert(AlertDescription.internal_error,
                        "Certificate chain longer than maximum (" + maxChainLength + ")");
                }

                byte[] derEncoding = TlsUtilities.ReadOpaque24(buf, 1);
                TlsCertificate cert = crypto.CreateCertificate(derEncoding);

                if (certificate_list.Count < 1 && endPointHashOutput != null)
                {
                    CalculateEndPointHash(context, cert, derEncoding, endPointHashOutput);
                }

                IDictionary extensions = null;
                if (isTlsV13)
                {
                    byte[] extEncoding = TlsUtilities.ReadOpaque16(buf);

                    extensions = TlsProtocol.ReadExtensionsData13(HandshakeType.certificate, extEncoding);
                }

                certificate_list.Add(new CertificateEntry(cert, extensions));
            }

            CertificateEntry[] certificateList = new CertificateEntry[certificate_list.Count];
            for (int i = 0; i < certificate_list.Count; i++)
            {
                certificateList[i] = (CertificateEntry)certificate_list[i];
            }

            return new Certificate(certificateRequestContext, certificateList);
        }

        private static void CalculateEndPointHash(TlsContext context, TlsCertificate cert, byte[] encoding,
            Stream output)
        {
            byte[] endPointHash = TlsUtilities.CalculateEndPointHash(context, cert, encoding);
            if (endPointHash != null && endPointHash.Length > 0)
            {
                output.Write(endPointHash, 0, endPointHash.Length);
            }
        }

        private TlsCertificate[] CloneCertificateList()
        {
            int count = m_certificateEntryList.Length;
            if (0 == count)
                return EmptyCerts;

            TlsCertificate[] result = new TlsCertificate[count];
            for (int i = 0; i < count; ++i)
            {
                result[i] = m_certificateEntryList[i].Certificate;
            }
            return result;
        }

        private CertificateEntry[] CloneCertificateEntryList()
        {
            int count = m_certificateEntryList.Length;
            if (0 == count)
                return EmptyCertEntries;

            CertificateEntry[] result = new CertificateEntry[count];
            Array.Copy(m_certificateEntryList, 0, result, 0, count);
            return result;
        }
    }
}