5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2015 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 MONO_FEATURE_NEW_TLS && SECURITY_DEP
27 #if MONO_SECURITY_ALIAS
28 extern alias MonoSecurity;
29 using IMonoTlsContext = MonoSecurity::Mono.Security.Interface.IMonoTlsContext;
31 using IMonoTlsContext = Mono.Security.Interface.IMonoTlsContext;
34 using System.Runtime.InteropServices;
35 using System.Security.Cryptography.X509Certificates;
37 namespace System.Net.Security
39 class DummySafeHandle : SafeHandle
41 protected DummySafeHandle ()
42 : base ((IntPtr)(-1), true)
46 protected override bool ReleaseHandle ()
51 public override bool IsInvalid {
52 get { return handle == (IntPtr)(-1); }
56 class SafeFreeCertContext : DummySafeHandle
60 class SafeFreeCredentials : DummySafeHandle
62 SecureCredential credential;
64 public X509Certificate2 Certificate {
67 throw new ObjectDisposedException ("Certificate");
68 return credential.certificate;
72 public SafeFreeCredentials (SecureCredential credential)
74 this.credential = credential;
76 DangerousAddRef (ref success);
79 public override bool IsInvalid {
81 return credential.certificate == null;
85 protected override bool ReleaseHandle ()
88 return base.ReleaseHandle ();
92 class SafeDeleteContext : DummySafeHandle
94 IMonoTlsContext context;
96 public IMonoTlsContext Context {
99 throw new ObjectDisposedException ("TlsContext");
104 public SafeDeleteContext (IMonoTlsContext context)
106 this.context = context;
109 public override bool IsInvalid {
111 return context == null || !context.IsValid;
115 protected override bool ReleaseHandle ()
119 return base.ReleaseHandle ();
123 struct SecureCredential
125 public const int CurrentVersion = 0x4;
131 NoSystemMapper = 0x02,
133 ValidateManual = 0x08,
134 NoDefaultCred = 0x10,
136 UseStrongCrypto = 0x00400000
140 internal X509Certificate2 certificate;
141 SchProtocols protocols;
142 EncryptionPolicy policy;
144 public SecureCredential (int version, X509Certificate2 certificate, SecureCredential.Flags flags, SchProtocols protocols, EncryptionPolicy policy)
146 this.version = version;
147 this.certificate = certificate;
148 this.protocols = protocols;
149 this.policy = policy;
158 internal class SafeCredentialReference : DummySafeHandle
161 // Static cache will return the target handle if found the reference in the table.
163 internal SafeFreeCredentials _Target;
167 internal static SafeCredentialReference CreateReference (SafeFreeCredentials target)
169 SafeCredentialReference result = new SafeCredentialReference (target);
170 if (result.IsInvalid)
176 private SafeCredentialReference (SafeFreeCredentials target)
179 // Bumps up the refcount on Target to signify that target handle is statically cached so
180 // its dispose should be postponed
183 target.DangerousAddRef (ref b);
186 target.DangerousRelease ();
192 SetHandle (new IntPtr (0)); // make this handle valid
197 override protected bool ReleaseHandle ()
199 SafeFreeCredentials target = _Target;
201 target.DangerousRelease ();