SessionParameters.cs
5.11 KB
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
using System;
using System.Collections;
using System.IO;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Crypto.Tls
{
public sealed class SessionParameters
{
public sealed class Builder
{
private int mCipherSuite = -1;
private short mCompressionAlgorithm = -1;
private byte[] mMasterSecret = null;
private Certificate mPeerCertificate = null;
private byte[] mPskIdentity = null;
private byte[] mSrpIdentity = null;
private byte[] mEncodedServerExtensions = null;
public Builder()
{
}
public SessionParameters Build()
{
Validate(this.mCipherSuite >= 0, "cipherSuite");
Validate(this.mCompressionAlgorithm >= 0, "compressionAlgorithm");
Validate(this.mMasterSecret != null, "masterSecret");
return new SessionParameters(mCipherSuite, (byte)mCompressionAlgorithm, mMasterSecret, mPeerCertificate,
mPskIdentity, mSrpIdentity, mEncodedServerExtensions);
}
public Builder SetCipherSuite(int cipherSuite)
{
this.mCipherSuite = cipherSuite;
return this;
}
public Builder SetCompressionAlgorithm(byte compressionAlgorithm)
{
this.mCompressionAlgorithm = compressionAlgorithm;
return this;
}
public Builder SetMasterSecret(byte[] masterSecret)
{
this.mMasterSecret = masterSecret;
return this;
}
public Builder SetPeerCertificate(Certificate peerCertificate)
{
this.mPeerCertificate = peerCertificate;
return this;
}
public Builder SetPskIdentity(byte[] pskIdentity)
{
this.mPskIdentity = pskIdentity;
return this;
}
public Builder SetSrpIdentity(byte[] srpIdentity)
{
this.mSrpIdentity = srpIdentity;
return this;
}
public Builder SetServerExtensions(IDictionary serverExtensions)
{
if (serverExtensions == null)
{
mEncodedServerExtensions = null;
}
else
{
MemoryStream buf = new MemoryStream();
TlsProtocol.WriteExtensions(buf, serverExtensions);
mEncodedServerExtensions = buf.ToArray();
}
return this;
}
private void Validate(bool condition, string parameter)
{
if (!condition)
throw new InvalidOperationException("Required session parameter '" + parameter + "' not configured");
}
}
private int mCipherSuite;
private byte mCompressionAlgorithm;
private byte[] mMasterSecret;
private Certificate mPeerCertificate;
private byte[] mPskIdentity;
private byte[] mSrpIdentity;
private byte[] mEncodedServerExtensions;
private SessionParameters(int cipherSuite, byte compressionAlgorithm, byte[] masterSecret,
Certificate peerCertificate, byte[] pskIdentity, byte[] srpIdentity, byte[] encodedServerExtensions)
{
this.mCipherSuite = cipherSuite;
this.mCompressionAlgorithm = compressionAlgorithm;
this.mMasterSecret = Arrays.Clone(masterSecret);
this.mPeerCertificate = peerCertificate;
this.mPskIdentity = Arrays.Clone(pskIdentity);
this.mSrpIdentity = Arrays.Clone(srpIdentity);
this.mEncodedServerExtensions = encodedServerExtensions;
}
public void Clear()
{
if (this.mMasterSecret != null)
{
Arrays.Fill(this.mMasterSecret, (byte)0);
}
}
public SessionParameters Copy()
{
return new SessionParameters(mCipherSuite, mCompressionAlgorithm, mMasterSecret, mPeerCertificate,
mPskIdentity, mSrpIdentity, mEncodedServerExtensions);
}
public int CipherSuite
{
get { return mCipherSuite; }
}
public byte CompressionAlgorithm
{
get { return mCompressionAlgorithm; }
}
public byte[] MasterSecret
{
get { return mMasterSecret; }
}
public Certificate PeerCertificate
{
get { return mPeerCertificate; }
}
public byte[] PskIdentity
{
get { return mPskIdentity; }
}
public byte[] SrpIdentity
{
get { return mSrpIdentity; }
}
public IDictionary ReadServerExtensions()
{
if (mEncodedServerExtensions == null)
return null;
MemoryStream buf = new MemoryStream(mEncodedServerExtensions, false);
return TlsProtocol.ReadExtensions(buf);
}
}
}