1 /* Transport Security Layer (TLS)
\r
2 * Copyright (c) 2003 Carlos Guzmán Álvarez
\r
4 * Permission is hereby granted, free of charge, to any person
\r
5 * obtaining a copy of this software and associated documentation
\r
6 * files (the "Software"), to deal in the Software without restriction,
\r
7 * including without limitation the rights to use, copy, modify, merge,
\r
8 * publish, distribute, sublicense, and/or sell copies of the Software,
\r
9 * and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included
\r
13 * in all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
\r
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
\r
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
\r
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
22 * DEALINGS IN THE SOFTWARE.
\r
26 using System.Security.Cryptography;
\r
29 namespace Mono.Security.Protocol.Tls.Handshake.Client
\r
31 internal class TlsServerKeyExchange : TlsHandshakeMessage
\r
35 private RSAParameters rsaParams;
\r
36 private byte[] signedParams;
\r
40 #region CONSTRUCTORS
\r
42 public TlsServerKeyExchange(TlsSession session, byte[] buffer)
\r
43 : base(session, TlsHandshakeType.ServerKeyExchange, buffer)
\r
52 public override void UpdateSession()
\r
54 base.UpdateSession();
\r
56 this.Session.Context.ServerSettings.ServerKeyExchange = true;
\r
57 this.Session.Context.ServerSettings.RsaParameters = this.rsaParams;
\r
58 this.Session.Context.ServerSettings.SignedParams = this.signedParams;
\r
63 #region PROTECTED_METHODS
\r
65 protected override void Parse()
\r
67 rsaParams = new RSAParameters();
\r
70 short length = ReadInt16();
\r
71 rsaParams.Modulus = ReadBytes(length);
\r
74 length = ReadInt16();
\r
75 rsaParams.Exponent = ReadBytes(length);
\r
77 // Read signed params
\r
78 length = ReadInt16();
\r
79 signedParams = ReadBytes(length);
\r
84 #region PRIVATE_METHODS
\r
86 private void verify()
\r
88 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
\r
89 SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
\r
91 // Create server params array
\r
92 TlsStream stream = new TlsStream();
\r
94 stream.Write(Session.Context.ClientRandom);
\r
95 stream.Write(Session.Context.ServerRandom);
\r
96 stream.Write(rsaParams.Modulus.Length);
\r
97 stream.Write(rsaParams.Modulus);
\r
98 stream.Write(rsaParams.Exponent.Length);
\r
99 stream.Write(rsaParams.Exponent);
\r
100 byte[] serverParams = stream.ToArray();
\r
103 // Compute md5 and sha hashes
\r
104 byte[] md5Hash = md5.ComputeHash(serverParams, 0, serverParams.Length);
\r
105 byte[] shaHash = sha.ComputeHash(serverParams, 0, serverParams.Length);
\r
107 // Calculate signature
\r
108 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(rsaParams.Modulus.Length << 3);
\r
109 rsa.ImportParameters(rsaParams);
\r
111 #warning "Verify Signature here"
\r
113 // RSAPKCS1SignatureDeformatter rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
\r