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
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 ();
58 using (var test = (IDisposable)null) {
62 untrustedChain = storeCtx.GetUntrusted ();
64 if (untrustedChain != null) {
65 untrusted = new X509Certificate2Collection ();
66 policy.ExtraStore = untrusted;
67 for (int i = 0; i < untrustedChain.Count; i++) {
68 using (var cert = untrustedChain.GetCertificate (i))
69 using (var impl = new X509CertificateImplBtls (cert))
70 untrusted.Add (new X509Certificate2 (impl));
76 internal X509ChainImplBtls ()
78 chain = new MonoBtlsX509Chain ();
79 elements = new X509ChainElementCollection ();
80 policy = new X509ChainPolicy ();
83 public override bool IsValid {
84 get { return chain != null && chain.IsValid; }
87 public override IntPtr Handle {
88 get { return chain.Handle.DangerousGetHandle (); }
91 internal MonoBtlsX509Chain Chain {
93 ThrowIfContextInvalid ();
98 internal MonoBtlsX509StoreCtx StoreCtx {
100 ThrowIfContextInvalid ();
105 public override X509ChainElementCollection ChainElements {
107 ThrowIfContextInvalid ();
108 if (elements != null)
111 elements = new X509ChainElementCollection ();
112 certificates = new X509Certificate2 [chain.Count];
114 for (int i = 0; i < certificates.Length; i++) {
115 var cert = chain.GetCertificate (i);
116 var impl = new X509CertificateImplBtls (cert);
117 certificates [i] = new X509Certificate2 (impl);
118 elements.Add (certificates [i]);
125 public override X509ChainPolicy ChainPolicy {
126 get { return policy; }
127 set { policy = value; }
130 public override X509ChainStatus[] ChainStatus {
131 get { throw new NotImplementedException (); }
134 public override bool Build (X509Certificate2 certificate)
139 public override void Reset ()
141 if (certificates != null) {
142 foreach (var certificate in certificates)
143 certificate.Dispose ();
146 if (elements != null) {
152 protected override void Dispose (bool disposing)
159 if (storeCtx != null) {
163 if (untrustedChain != null) {
164 untrustedChain.Dispose ();
165 untrustedChain = null;
167 if (untrusted != null) {
168 foreach (var cert in untrusted)
172 base.Dispose (disposing);