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

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

namespace Org.BouncyCastle.Tls
{
    public sealed class SessionParameters
    {
        public sealed class Builder
        {
            private int m_cipherSuite = -1;
            private Certificate m_localCertificate = null;
            private TlsSecret m_masterSecret = null;
            private ProtocolVersion m_negotiatedVersion;
            private Certificate m_peerCertificate = null;
            private byte[] m_pskIdentity = null;
            private byte[] m_srpIdentity = null;
            private byte[] m_encodedServerExtensions = null;
            private bool m_extendedMasterSecret = false;

            public Builder()
            {
            }

            public SessionParameters Build()
            {
                Validate(m_cipherSuite >= 0, "cipherSuite");
                Validate(m_masterSecret != null, "masterSecret");
                return new SessionParameters(m_cipherSuite, m_localCertificate, m_masterSecret, m_negotiatedVersion,
                    m_peerCertificate, m_pskIdentity, m_srpIdentity, m_encodedServerExtensions, m_extendedMasterSecret);
            }

            public Builder SetCipherSuite(int cipherSuite)
            {
                this.m_cipherSuite = cipherSuite;
                return this;
            }

            public Builder SetExtendedMasterSecret(bool extendedMasterSecret)
            {
                this.m_extendedMasterSecret = extendedMasterSecret;
                return this;
            }

            public Builder SetLocalCertificate(Certificate localCertificate)
            {
                this.m_localCertificate = localCertificate;
                return this;
            }

            public Builder SetMasterSecret(TlsSecret masterSecret)
            {
                this.m_masterSecret = masterSecret;
                return this;
            }

            public Builder SetNegotiatedVersion(ProtocolVersion negotiatedVersion)
            {
                this.m_negotiatedVersion = negotiatedVersion;
                return this;
            }

            public Builder SetPeerCertificate(Certificate peerCertificate)
            {
                this.m_peerCertificate = peerCertificate;
                return this;
            }

            public Builder SetPskIdentity(byte[] pskIdentity)
            {
                this.m_pskIdentity = pskIdentity;
                return this;
            }

            public Builder SetSrpIdentity(byte[] srpIdentity)
            {
                this.m_srpIdentity = srpIdentity;
                return this;
            }

            /// <exception cref="IOException"/>
            public Builder SetServerExtensions(IDictionary<int, byte[]> serverExtensions)
            {
                if (serverExtensions == null || serverExtensions.Count < 1)
                {
                    this.m_encodedServerExtensions = null;
                }
                else
                {
                    MemoryStream buf = new MemoryStream();
                    TlsProtocol.WriteExtensions(buf, serverExtensions);
                    this.m_encodedServerExtensions = buf.ToArray();
                }
                return this;
            }

            private void Validate(bool condition, string parameter)
            {
                if (!condition)
                    throw new InvalidOperationException("Required session parameter '" + parameter + "' not configured");
            }
        }

        private readonly int m_cipherSuite;
        private readonly Certificate m_localCertificate;
        private readonly TlsSecret m_masterSecret;
        private readonly ProtocolVersion m_negotiatedVersion;
        private readonly Certificate m_peerCertificate;
        private readonly byte[] m_pskIdentity;
        private readonly byte[] m_srpIdentity;
        private readonly byte[] m_encodedServerExtensions;
        private readonly bool m_extendedMasterSecret;

        private SessionParameters(int cipherSuite, Certificate localCertificate, TlsSecret masterSecret,
            ProtocolVersion negotiatedVersion, Certificate peerCertificate, byte[] pskIdentity, byte[] srpIdentity,
            byte[] encodedServerExtensions, bool extendedMasterSecret)
        {
            this.m_cipherSuite = cipherSuite;
            this.m_localCertificate = localCertificate;
            this.m_masterSecret = masterSecret;
            this.m_negotiatedVersion = negotiatedVersion;
            this.m_peerCertificate = peerCertificate;
            this.m_pskIdentity = Arrays.Clone(pskIdentity);
            this.m_srpIdentity = Arrays.Clone(srpIdentity);
            this.m_encodedServerExtensions = encodedServerExtensions;
            this.m_extendedMasterSecret = extendedMasterSecret;
        }

        public int CipherSuite
        {
            get { return m_cipherSuite; }
        }

        public void Clear()
        {
            if (m_masterSecret != null)
            {
                m_masterSecret.Destroy();
            }
        }

        public SessionParameters Copy()
        {
            return new SessionParameters(m_cipherSuite, m_localCertificate, m_masterSecret, m_negotiatedVersion,
                m_peerCertificate, m_pskIdentity, m_srpIdentity, m_encodedServerExtensions, m_extendedMasterSecret);
        }

        public bool IsExtendedMasterSecret
        {
            get { return m_extendedMasterSecret; }
        }

        public Certificate LocalCertificate
        {
            get { return m_localCertificate; }
        }

        public TlsSecret MasterSecret
        {
            get { return m_masterSecret; }
        }

        public ProtocolVersion NegotiatedVersion
        {
            get { return m_negotiatedVersion; }
        }

        public Certificate PeerCertificate
        {
            get { return m_peerCertificate; }
        }

        public byte[] PskIdentity
        {
            get { return m_pskIdentity; }
        }

        /// <exception cref="IOException"/>
        public IDictionary<int, byte[]> ReadServerExtensions()
        {
            if (m_encodedServerExtensions == null)
                return null;

            return TlsProtocol.ReadExtensions(new MemoryStream(m_encodedServerExtensions, false));
        }

        public byte[] SrpIdentity
        {
            get { return m_srpIdentity; }
        }
    }
}