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
29 using System.Security.Cryptography.X509Certificates;
30 using System.Runtime.CompilerServices;
31 using System.Runtime.InteropServices;
35 class MonoBtlsPkcs12 : MonoBtlsObject
37 internal class BoringPkcs12Handle : MonoBtlsHandle
39 public BoringPkcs12Handle (IntPtr handle)
44 protected override bool ReleaseHandle ()
46 mono_btls_pkcs12_free (handle);
51 new internal BoringPkcs12Handle Handle {
52 get { return (BoringPkcs12Handle)base.Handle; }
55 [MethodImpl (MethodImplOptions.InternalCall)]
56 extern static void mono_btls_pkcs12_free (IntPtr handle);
58 [MethodImpl (MethodImplOptions.InternalCall)]
59 extern static IntPtr mono_btls_pkcs12_new ();
61 [MethodImpl (MethodImplOptions.InternalCall)]
62 extern static int mono_btls_pkcs12_get_count (IntPtr handle);
64 [MethodImpl (MethodImplOptions.InternalCall)]
65 extern static IntPtr mono_btls_pkcs12_get_cert (IntPtr Handle, int index);
67 [MethodImpl (MethodImplOptions.InternalCall)]
68 extern static int mono_btls_pkcs12_add_cert (IntPtr chain, IntPtr x509);
70 [MethodImpl (MethodImplOptions.InternalCall)]
71 extern unsafe static int mono_btls_pkcs12_import (IntPtr chain, void* data, int len, IntPtr password);
73 [MethodImpl (MethodImplOptions.InternalCall)]
74 extern static int mono_btls_pkcs12_has_private_key (IntPtr pkcs12);
76 [MethodImpl (MethodImplOptions.InternalCall)]
77 extern static IntPtr mono_btls_pkcs12_get_private_key (IntPtr pkcs12);
79 internal MonoBtlsPkcs12 ()
80 : base (new BoringPkcs12Handle (mono_btls_pkcs12_new ()))
84 internal MonoBtlsPkcs12 (BoringPkcs12Handle handle)
89 MonoBtlsKey privateKey;
92 get { return mono_btls_pkcs12_get_count (Handle.DangerousGetHandle ()); }
95 public MonoBtlsX509 GetCertificate (int index)
98 throw new IndexOutOfRangeException ();
99 var handle = mono_btls_pkcs12_get_cert (Handle.DangerousGetHandle (), index);
100 CheckError (handle != IntPtr.Zero);
101 return new MonoBtlsX509 (new MonoBtlsX509.BoringX509Handle (handle));
104 public void AddCertificate (MonoBtlsX509 x509)
106 mono_btls_pkcs12_add_cert (
107 Handle.DangerousGetHandle (),
108 x509.Handle.DangerousGetHandle ());
111 public unsafe void Import (byte[] buffer, string password)
113 var passptr = IntPtr.Zero;
114 fixed (void* ptr = buffer)
116 if (password != null)
117 passptr = Marshal.StringToHGlobalAnsi (password);
118 var ret = mono_btls_pkcs12_import (
119 Handle.DangerousGetHandle (), ptr,
120 buffer.Length, passptr);
123 if (passptr != IntPtr.Zero)
124 Marshal.FreeHGlobal (passptr);
128 public bool HasPrivateKey {
129 get { return mono_btls_pkcs12_has_private_key (Handle.DangerousGetHandle ()) != 0; }
132 public MonoBtlsKey GetPrivateKey ()
135 throw new InvalidOperationException ();
136 if (privateKey == null) {
137 var handle = mono_btls_pkcs12_get_private_key (Handle.DangerousGetHandle ());
138 CheckError (handle != IntPtr.Zero);
139 privateKey = new MonoBtlsKey (new MonoBtlsKey.BoringKeyHandle (handle));