2004-04-21 Carlos Guzman Alvarez <carlosga@telefonica.net>
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / ServerContext.cs
1 /* Transport Security Layer (TLS)
2  * Copyright (c) 2003-2004 Carlos Guzman Alvarez
3  * 
4  * Permission is hereby granted, free of charge, to any person 
5  * obtaining a copy of this software and associated documentation 
6  * files (the "Software"), to deal in the Software without restriction, 
7  * including without limitation the rights to use, copy, modify, merge, 
8  * publish, distribute, sublicense, and/or sell copies of the Software, 
9  * and to permit persons to whom the Software is furnished to do so, 
10  * subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be included 
13  * in all copies or substantial portions of the Software.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 using System;
26 using System.Security.Cryptography;
27 using System.Security.Cryptography.X509Certificates;
28
29 using Mono.Security.Protocol.Tls.Handshake;
30 using MonoX509 = Mono.Security.X509;
31
32 namespace Mono.Security.Protocol.Tls
33 {
34         internal class ServerContext : Context
35         {
36                 #region Fields
37
38                 private SslServerStream sslStream;
39                 private bool                    clientCertificateRequired;
40
41                 #endregion
42
43                 #region Properties
44
45                 public SslServerStream SslStream
46                 {
47                         get { return this.sslStream; }
48                 }
49
50                 public bool     ClientCertificateRequired
51                 {
52                         get { return this.clientCertificateRequired; }
53                 }
54
55                 #endregion
56
57                 #region Constructors
58
59                 public ServerContext(
60                         SslServerStream                 stream,
61                         SecurityProtocolType    securityProtocolType,
62                         X509Certificate                 serverCertificate,
63                         bool                                    clientCertificateRequired)
64                         : base(securityProtocolType)
65                 {
66                         this.sslStream                                  = stream;
67                         this.clientCertificateRequired  = clientCertificateRequired;
68
69                         // Convert the System.Security cert to a Mono Cert
70                         MonoX509.X509Certificate cert = new MonoX509.X509Certificate(serverCertificate.GetRawCertData());
71
72                         // Add server certificate to the certificate collection
73                         this.ServerSettings.Certificates = new MonoX509.X509CertificateCollection();
74                         this.ServerSettings.Certificates.Add(cert);
75
76                         this.ServerSettings.UpdateCertificateRSA();
77
78                         // Add requested certificate types
79                         this.ServerSettings.CertificateTypes = new ClientCertificateType[1];
80                         this.ServerSettings.CertificateTypes[0] = ClientCertificateType.RSA;
81
82                         // Add certificate authorities
83                         this.ServerSettings.DistinguisedNames = new string[0];
84                 }
85
86                 #endregion
87         }
88 }