2 // MonoBtlsX509StoreCtx.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
.Runtime
.InteropServices
;
30 using System
.Runtime
.CompilerServices
;
34 class MonoBtlsX509StoreCtx
: MonoBtlsObject
36 internal class BoringX509StoreCtxHandle
: MonoBtlsHandle
40 internal BoringX509StoreCtxHandle (IntPtr handle
, bool ownsHandle
= true)
41 : base (handle
, ownsHandle
)
43 dontFree
= !ownsHandle
;
47 internal BoringX509StoreCtxHandle (IntPtr handle
)
55 protected override bool ReleaseHandle ()
58 mono_btls_x509_store_ctx_free (handle
);
65 new internal BoringX509StoreCtxHandle Handle
{
66 get { return (BoringX509StoreCtxHandle)base.Handle; }
69 [DllImport (BTLS_DYLIB
)]
70 extern static IntPtr
mono_btls_x509_store_ctx_new ();
72 [DllImport (BTLS_DYLIB
)]
73 extern static IntPtr
mono_btls_x509_store_ctx_from_ptr (IntPtr ctx
);
75 [DllImport (BTLS_DYLIB
)]
76 extern static MonoBtlsX509Error
mono_btls_x509_store_ctx_get_error (IntPtr handle
, out IntPtr error_string
);
78 [DllImport (BTLS_DYLIB
)]
79 extern static int mono_btls_x509_store_ctx_get_error_depth (IntPtr handle
);
81 [DllImport (BTLS_DYLIB
)]
82 extern static IntPtr
mono_btls_x509_store_ctx_get_chain (IntPtr handle
);
84 [DllImport (BTLS_DYLIB
)]
85 extern static int mono_btls_x509_store_ctx_init (IntPtr handle
, IntPtr store
, IntPtr chain
);
87 [DllImport (BTLS_DYLIB
)]
88 extern static int mono_btls_x509_store_ctx_set_param (IntPtr handle
, IntPtr param
);
90 [DllImport (BTLS_DYLIB
)]
91 extern static int mono_btls_x509_store_ctx_verify_cert (IntPtr handle
);
93 [DllImport (BTLS_DYLIB
)]
94 extern static IntPtr
mono_btls_x509_store_ctx_get_by_subject (IntPtr handle
, IntPtr name
);
96 [DllImport (BTLS_DYLIB
)]
97 extern static IntPtr
mono_btls_x509_store_ctx_get_current_cert (IntPtr handle
);
99 [DllImport (BTLS_DYLIB
)]
100 extern static IntPtr
mono_btls_x509_store_ctx_get_current_issuer (IntPtr handle
);
102 [DllImport (BTLS_DYLIB
)]
103 extern static IntPtr
mono_btls_x509_store_ctx_get_verify_param (IntPtr handle
);
105 [DllImport (BTLS_DYLIB
)]
106 extern static IntPtr
mono_btls_x509_store_ctx_get_untrusted (IntPtr handle
);
108 [DllImport (BTLS_DYLIB
)]
109 extern static IntPtr
mono_btls_x509_store_ctx_up_ref (IntPtr handle
);
111 [DllImport (BTLS_DYLIB
)]
112 extern static void mono_btls_x509_store_ctx_free (IntPtr handle
);
114 internal MonoBtlsX509StoreCtx ()
115 : base (new BoringX509StoreCtxHandle (mono_btls_x509_store_ctx_new ()))
119 static BoringX509StoreCtxHandle
Create_internal (IntPtr store_ctx
)
121 var handle
= mono_btls_x509_store_ctx_from_ptr (store_ctx
);
122 if (handle
== IntPtr
.Zero
)
123 throw new MonoBtlsException ();
124 return new BoringX509StoreCtxHandle (handle
);
127 internal MonoBtlsX509StoreCtx (int preverify_ok
, IntPtr store_ctx
)
128 : base (Create_internal (store_ctx
))
130 verifyResult
= preverify_ok
;
133 internal MonoBtlsX509StoreCtx (BoringX509StoreCtxHandle ptr
, int? verifyResult
)
136 this.verifyResult
= verifyResult
;
139 public MonoBtlsX509Error
GetError ()
141 IntPtr error_string_ptr
;
142 return mono_btls_x509_store_ctx_get_error (Handle
.DangerousGetHandle (), out error_string_ptr
);
145 public int GetErrorDepth ()
147 return mono_btls_x509_store_ctx_get_error_depth (Handle
.DangerousGetHandle ());
150 public MonoBtlsX509Exception
GetException ()
152 IntPtr error_string_ptr
;
153 var error
= mono_btls_x509_store_ctx_get_error (Handle
.DangerousGetHandle (), out error_string_ptr
);
156 if (error_string_ptr
!= IntPtr
.Zero
) {
157 var error_string
= Marshal
.PtrToStringAnsi (error_string_ptr
);
158 return new MonoBtlsX509Exception (error
, error_string
);
160 return new MonoBtlsX509Exception (error
, "Unknown verify error.");
163 public MonoBtlsX509Chain
GetChain ()
165 var chain
= mono_btls_x509_store_ctx_get_chain (Handle
.DangerousGetHandle ());
166 CheckError (chain
!= IntPtr
.Zero
);
167 return new MonoBtlsX509Chain (new MonoBtlsX509Chain
.BoringX509ChainHandle (chain
));
170 public MonoBtlsX509Chain
GetUntrusted ()
172 var chain
= mono_btls_x509_store_ctx_get_untrusted (Handle
.DangerousGetHandle ());
173 CheckError (chain
!= IntPtr
.Zero
);
174 return new MonoBtlsX509Chain (new MonoBtlsX509Chain
.BoringX509ChainHandle (chain
));
177 public void Initialize (MonoBtlsX509Store store
, MonoBtlsX509Chain chain
)
179 var ret
= mono_btls_x509_store_ctx_init (
180 Handle
.DangerousGetHandle (),
181 store
.Handle
.DangerousGetHandle (),
182 chain
.Handle
.DangerousGetHandle ());
186 public void SetVerifyParam (MonoBtlsX509VerifyParam param
)
188 var ret
= mono_btls_x509_store_ctx_set_param (
189 Handle
.DangerousGetHandle (),
190 param
.Handle
.DangerousGetHandle ());
194 public int VerifyResult
{
196 if (verifyResult
== null)
197 throw new InvalidOperationException ();
198 return verifyResult
.Value
;
204 verifyResult
= mono_btls_x509_store_ctx_verify_cert (Handle
.DangerousGetHandle ());
205 return verifyResult
.Value
;
208 public MonoBtlsX509
LookupBySubject (MonoBtlsX509Name name
)
210 var handle
= mono_btls_x509_store_ctx_get_by_subject (
211 Handle
.DangerousGetHandle (), name
.Handle
.DangerousGetHandle ());
212 if (handle
== IntPtr
.Zero
)
214 return new MonoBtlsX509 (new MonoBtlsX509
.BoringX509Handle (handle
));
217 public MonoBtlsX509
GetCurrentCertificate ()
219 var x509
= mono_btls_x509_store_ctx_get_current_cert (Handle
.DangerousGetHandle ());
220 if (x509
== IntPtr
.Zero
)
222 return new MonoBtlsX509 (new MonoBtlsX509
.BoringX509Handle (x509
));
225 public MonoBtlsX509
GetCurrentIssuer ()
227 var x509
= mono_btls_x509_store_ctx_get_current_issuer (Handle
.DangerousGetHandle ());
228 if (x509
== IntPtr
.Zero
)
230 return new MonoBtlsX509 (new MonoBtlsX509
.BoringX509Handle (x509
));
233 public MonoBtlsX509VerifyParam
GetVerifyParam ()
235 var param
= mono_btls_x509_store_ctx_get_verify_param (Handle
.DangerousGetHandle ());
236 if (param
== IntPtr
.Zero
)
238 return new MonoBtlsX509VerifyParam (new MonoBtlsX509VerifyParam
.BoringX509VerifyParamHandle (param
));
241 public MonoBtlsX509StoreCtx
Copy ()
243 var copy
= mono_btls_x509_store_ctx_up_ref (Handle
.DangerousGetHandle ());
244 CheckError (copy
!= IntPtr
.Zero
);
245 return new MonoBtlsX509StoreCtx (new BoringX509StoreCtxHandle (copy
), verifyResult
);