2 // X509ChainImplBtls.cs
5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #if SECURITY_DEP && MONO_FEATURE_BTLS
29 using System.Security;
30 using System.Security.Cryptography;
31 using System.Security.Cryptography.X509Certificates;
32 using MX = Mono.Security.X509;
36 class X509ChainImplBtls : X509ChainImpl
38 MonoBtlsX509StoreCtx storeCtx;
39 MonoBtlsX509Chain chain;
40 MonoBtlsX509Chain untrustedChain;
41 X509ChainElementCollection elements;
42 X509Certificate2Collection untrusted;
43 X509Certificate2[] certificates;
44 X509ChainPolicy policy;
46 internal X509ChainImplBtls (MonoBtlsX509Chain chain)
48 this.chain = chain.Copy ();
49 policy = new X509ChainPolicy ();
52 internal X509ChainImplBtls (MonoBtlsX509StoreCtx storeCtx)
54 this.storeCtx = storeCtx.Copy ();
55 this.chain = storeCtx.GetChain ();
57 policy = new X509ChainPolicy ();
59 untrustedChain = storeCtx.GetUntrusted ();
61 if (untrustedChain != null) {
62 untrusted = new X509Certificate2Collection ();
63 policy.ExtraStore = untrusted;
64 for (int i = 0; i < untrustedChain.Count; i++) {
65 var cert = untrustedChain.GetCertificate (i);
66 using (var impl = new X509CertificateImplBtls (cert))
67 untrusted.Add (new X509Certificate2 (impl));
72 internal X509ChainImplBtls ()
74 chain = new MonoBtlsX509Chain ();
75 elements = new X509ChainElementCollection ();
76 policy = new X509ChainPolicy ();
79 public override bool IsValid {
80 get { return chain != null && chain.IsValid; }
83 public override IntPtr Handle {
84 get { return chain.Handle.DangerousGetHandle (); }
87 internal MonoBtlsX509Chain Chain {
89 ThrowIfContextInvalid ();
94 internal MonoBtlsX509StoreCtx StoreCtx {
96 ThrowIfContextInvalid ();
101 public override X509ChainElementCollection ChainElements {
103 ThrowIfContextInvalid ();
104 if (elements != null)
107 elements = new X509ChainElementCollection ();
108 certificates = new X509Certificate2 [chain.Count];
110 for (int i = 0; i < certificates.Length; i++) {
111 var cert = chain.GetCertificate (i);
112 using (var impl = new X509CertificateImplBtls (cert))
113 certificates [i] = new X509Certificate2 (impl);
114 elements.Add (certificates [i]);
121 public override X509ChainPolicy ChainPolicy {
122 get { return policy; }
123 set { policy = value; }
126 public override X509ChainStatus[] ChainStatus {
127 get { throw new NotImplementedException (); }
130 public override bool Build (X509Certificate2 certificate)
135 public override void Reset ()
137 if (certificates != null) {
138 foreach (var certificate in certificates)
139 certificate.Dispose ();
142 if (elements != null) {
148 protected override void Dispose (bool disposing)
155 if (storeCtx != null) {
159 if (untrustedChain != null) {
160 untrustedChain.Dispose ();
161 untrustedChain = null;
163 if (untrusted != null) {
164 foreach (var cert in untrusted)
168 if (certificates != null) {
169 foreach (var cert in certificates)
174 base.Dispose (disposing);