Merge pull request #17279 from imhameed/bug-gh-16935-netcore-equalsall-codegen
[mono-project.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake.Client / TlsClientCertificate.cs
blob49e2d85f4c3a9917b3efe1882f27f9272e202952
1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 using System;
26 using System.Security.Cryptography.X509Certificates;
28 using Mono.Security.Protocol.Tls;
30 namespace Mono.Security.Protocol.Tls.Handshake.Client
32 internal class TlsClientCertificate : HandshakeMessage
34 private bool clientCertSelected;
35 private X509Certificate clientCert;
37 #region Constructors
39 public TlsClientCertificate(Context context)
40 : base(context, HandshakeType.Certificate)
44 #endregion
46 #region Properties
48 public X509Certificate ClientCertificate {
49 get {
50 if (!clientCertSelected)
52 GetClientCertificate ();
53 clientCertSelected = true;
55 return clientCert;
59 #endregion
61 #region Methods
63 public override void Update()
65 base.Update();
66 this.Reset();
69 #endregion
71 #region Protected Methods
73 private void GetClientCertificate ()
75 // TODO: Client certificate selection is unfinished
76 ClientContext context = (ClientContext)this.Context;
78 // note: the server may ask for mutual authentication
79 // but may not require it (i.e. it can be optional).
80 if (context.ClientSettings.Certificates != null &&
81 context.ClientSettings.Certificates.Count > 0)
83 clientCert = context.SslStream.RaiseClientCertificateSelection(
84 this.Context.ClientSettings.Certificates,
85 new X509Certificate(this.Context.ServerSettings.Certificates[0].RawData),
86 this.Context.ClientSettings.TargetHost,
87 null);
88 // Note: the application code can raise it's
89 // own exception to stop the connection too.
92 // Update the selected client certificate
93 context.ClientSettings.ClientCertificate = clientCert;
96 private void SendCertificates ()
98 TlsStream chain = new TlsStream ();
100 X509Certificate currentCert = this.ClientCertificate;
101 while (currentCert != null) {
102 byte[] rawCert = currentCert.GetRawCertData ();
103 chain.WriteInt24 (rawCert.Length);
104 chain.Write(rawCert);
105 currentCert = FindParentCertificate (currentCert);
107 this.WriteInt24 ((int)chain.Length);
108 this.Write (chain.ToArray ());
111 protected override void ProcessAsSsl3()
113 if (this.ClientCertificate != null) {
114 SendCertificates ();
115 } else {
116 // an Alert warning for NoCertificate (41)
117 // should be sent from here - but that would
118 // break the current message handling
122 protected override void ProcessAsTls1()
124 if (this.ClientCertificate != null) {
125 SendCertificates ();
126 } else {
127 // return message with empty certificate (see 7.4.6 in RFC2246)
128 this.WriteInt24 (0);
132 private X509Certificate FindParentCertificate (X509Certificate cert)
134 #pragma warning disable 618
135 // This certificate is the root certificate
136 if (cert.GetName () == cert.GetIssuerName ())
137 return null;
139 foreach (X509Certificate certificate in this.Context.ClientSettings.Certificates) {
140 if (certificate.GetName () == cert.GetIssuerName ())
141 return certificate;
143 return null;
144 #pragma warning restore 618
147 #endregion