1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
3 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
26 using System.Collections;
27 using SSCX = System.Security.Cryptography.X509Certificates;
28 using Mono.Security.X509;
29 using Mono.Security.X509.Extensions;
31 namespace Mono.Security.Protocol.Tls.Handshake.Server
33 internal class TlsClientCertificate : HandshakeMessage
37 private X509CertificateCollection clientCertificates;
43 public TlsClientCertificate(Context context, byte[] buffer)
44 : base(context, HandshakeType.Certificate, buffer)
52 public override void Update()
54 foreach (X509Certificate certificate in clientCertificates) {
55 this.Context.ClientSettings.Certificates.Add (new SSCX.X509Certificate (certificate.RawData));
59 public bool HasCertificate {
60 get { return clientCertificates.Count > 0; }
65 #region Protected Methods
67 protected override void ProcessAsSsl3()
72 protected override void ProcessAsTls1()
75 int length = this.ReadInt24 ();
76 this.clientCertificates = new X509CertificateCollection ();
77 while (length > bytesRead) {
78 int certLength = this.ReadInt24 ();
79 bytesRead += certLength + 3;
80 byte[] cert = this.ReadBytes (certLength);
81 this.clientCertificates.Add (new X509Certificate (cert));
84 if (this.clientCertificates.Count > 0)
86 this.validateCertificates (this.clientCertificates);
88 else if ((this.Context as ServerContext).ClientCertificateRequired)
90 throw new TlsException (AlertDescription.NoCertificate);
96 #region Private Methods
98 private bool checkCertificateUsage (X509Certificate cert)
100 ServerContext context = (ServerContext)this.Context;
102 // certificate extensions are required for this
103 // we "must" accept older certificates without proofs
104 if (cert.Version < 3)
107 KeyUsages ku = KeyUsages.none;
108 switch (context.Negotiating.Cipher.ExchangeAlgorithmType)
110 case ExchangeAlgorithmType.RsaSign:
111 case ExchangeAlgorithmType.RsaKeyX:
112 ku = KeyUsages.digitalSignature;
114 case ExchangeAlgorithmType.DiffieHellman:
115 ku = KeyUsages.keyAgreement;
117 case ExchangeAlgorithmType.Fortezza:
118 return false; // unsupported certificate type
121 KeyUsageExtension kux = null;
122 ExtendedKeyUsageExtension eku = null;
124 X509Extension xtn = cert.Extensions["2.5.29.15"];
126 kux = new KeyUsageExtension (xtn);
128 xtn = cert.Extensions["2.5.29.37"];
130 eku = new ExtendedKeyUsageExtension (xtn);
132 if ((kux != null) && (eku != null))
134 // RFC3280 states that when both KeyUsageExtension and
135 // ExtendedKeyUsageExtension are present then BOTH should
137 return (kux.Support (ku) &&
138 eku.KeyPurpose.Contains ("1.3.6.1.5.5.7.3.2"));
140 else if (kux != null)
142 return kux.Support (ku);
144 else if (eku != null)
146 // Client Authentication (1.3.6.1.5.5.7.3.2)
147 return eku.KeyPurpose.Contains ("1.3.6.1.5.5.7.3.2");
150 // last chance - try with older (deprecated) Netscape extensions
151 xtn = cert.Extensions["2.16.840.1.113730.1.1"];
154 NetscapeCertTypeExtension ct = new NetscapeCertTypeExtension (xtn);
155 return ct.Support (NetscapeCertTypeExtension.CertTypes.SslClient);
158 // certificate isn't valid for SSL server usage
162 private void validateCertificates (X509CertificateCollection certificates)
164 ServerContext context = (ServerContext)this.Context;
165 AlertDescription description = AlertDescription.BadCertificate;
166 SSCX.X509Certificate client = null;
167 int[] certificateErrors = null;
169 // note: certificate may be null is no certificate is sent
170 // (e.g. optional mutual authentication)
171 if (certificates.Count > 0) {
172 X509Certificate leaf = certificates[0];
174 ArrayList errors = new ArrayList ();
176 // SSL specific check - not all certificates can be
177 // used to server-side SSL some rules applies after
179 if (!checkCertificateUsage (leaf))
181 // WinError.h CERT_E_PURPOSE 0x800B0106
182 errors.Add ((int)-2146762490);
186 // was a chain supplied ? if so use it, if not
187 if (certificates.Count > 1) {
188 // if so use it (and don't build our own)
189 X509CertificateCollection chain = new X509CertificateCollection (certificates);
191 verify = new X509Chain (chain);
193 // if not, then let's build our own (based on what's available in the stores)
194 verify = new X509Chain ();
201 result = verify.Build (leaf);
210 switch (verify.Status)
212 case X509ChainStatusFlags.InvalidBasicConstraints:
213 // WinError.h TRUST_E_BASIC_CONSTRAINTS 0x80096019
214 errors.Add ((int)-2146869223);
217 case X509ChainStatusFlags.NotSignatureValid:
218 // WinError.h TRUST_E_BAD_DIGEST 0x80096010
219 errors.Add ((int)-2146869232);
222 case X509ChainStatusFlags.NotTimeNested:
223 // WinError.h CERT_E_VALIDITYPERIODNESTING 0x800B0102
224 errors.Add ((int)-2146762494);
227 case X509ChainStatusFlags.NotTimeValid:
228 // WinError.h CERT_E_EXPIRED 0x800B0101
229 description = AlertDescription.CertificateExpired;
230 errors.Add ((int)-2146762495);
233 case X509ChainStatusFlags.PartialChain:
234 // WinError.h CERT_E_CHAINING 0x800B010A
235 description = AlertDescription.UnknownCA;
236 errors.Add ((int)-2146762486);
239 case X509ChainStatusFlags.UntrustedRoot:
240 // WinError.h CERT_E_UNTRUSTEDROOT 0x800B0109
241 description = AlertDescription.UnknownCA;
242 errors.Add ((int)-2146762487);
247 description = AlertDescription.CertificateUnknown;
248 errors.Add ((int)verify.Status);
252 client = new SSCX.X509Certificate (leaf.RawData);
253 certificateErrors = (int[])errors.ToArray (typeof (int));
257 certificateErrors = new int[0];
260 SSCX.X509CertificateCollection certCollection = new SSCX.X509CertificateCollection ();
261 foreach (X509Certificate certificate in certificates) {
262 certCollection.Add (new SSCX.X509Certificate (certificate.RawData));
264 if (!context.SslStream.RaiseClientCertificateValidation(client, certificateErrors))
266 throw new TlsException (
268 "Invalid certificate received from client.");
271 this.Context.ClientSettings.ClientCertificate = client;