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 SECURITY_DEP && MONO_FEATURE_BTLS
29 using System
.Security
.Cryptography
.X509Certificates
;
30 using System
.Runtime
.CompilerServices
;
31 using System
.Runtime
.InteropServices
;
32 using Microsoft
.Win32
.SafeHandles
;
36 class MonoBtlsPkcs12
: MonoBtlsObject
38 internal class BoringPkcs12Handle
: MonoBtlsHandle
40 public BoringPkcs12Handle (IntPtr handle
)
45 protected override bool ReleaseHandle ()
47 mono_btls_pkcs12_free (handle
);
52 new internal BoringPkcs12Handle Handle
{
53 get { return (BoringPkcs12Handle)base.Handle; }
56 [DllImport (BTLS_DYLIB
)]
57 extern static void mono_btls_pkcs12_free (IntPtr handle
);
59 [DllImport (BTLS_DYLIB
)]
60 extern static IntPtr
mono_btls_pkcs12_new ();
62 [DllImport (BTLS_DYLIB
)]
63 extern static int mono_btls_pkcs12_get_count (IntPtr handle
);
65 [DllImport (BTLS_DYLIB
)]
66 extern static IntPtr
mono_btls_pkcs12_get_cert (IntPtr Handle
, int index
);
68 [DllImport (BTLS_DYLIB
)]
69 extern static int mono_btls_pkcs12_add_cert (IntPtr chain
, IntPtr x509
);
71 [DllImport (BTLS_DYLIB
)]
72 extern unsafe static int mono_btls_pkcs12_import (IntPtr chain
, void* data
, int len
, SafePasswordHandle password
);
74 [DllImport (BTLS_DYLIB
)]
75 extern static int mono_btls_pkcs12_has_private_key (IntPtr pkcs12
);
77 [DllImport (BTLS_DYLIB
)]
78 extern static IntPtr
mono_btls_pkcs12_get_private_key (IntPtr pkcs12
);
80 internal MonoBtlsPkcs12 ()
81 : base (new BoringPkcs12Handle (mono_btls_pkcs12_new ()))
85 internal MonoBtlsPkcs12 (BoringPkcs12Handle handle
)
90 MonoBtlsKey privateKey
;
93 get { return mono_btls_pkcs12_get_count (Handle.DangerousGetHandle ()); }
96 public MonoBtlsX509
GetCertificate (int index
)
99 throw new IndexOutOfRangeException ();
100 var handle
= mono_btls_pkcs12_get_cert (Handle
.DangerousGetHandle (), index
);
101 CheckError (handle
!= IntPtr
.Zero
);
102 return new MonoBtlsX509 (new MonoBtlsX509
.BoringX509Handle (handle
));
105 public void AddCertificate (MonoBtlsX509 x509
)
107 mono_btls_pkcs12_add_cert (
108 Handle
.DangerousGetHandle (),
109 x509
.Handle
.DangerousGetHandle ());
112 public unsafe void Import (byte[] buffer
, SafePasswordHandle password
)
114 fixed (void* ptr
= buffer
) {
115 var ret
= mono_btls_pkcs12_import (
116 Handle
.DangerousGetHandle (), ptr
, buffer
.Length
, password
);
121 public bool HasPrivateKey
{
122 get { return mono_btls_pkcs12_has_private_key (Handle.DangerousGetHandle ()) != 0; }
125 public MonoBtlsKey
GetPrivateKey ()
128 throw new InvalidOperationException ();
129 if (privateKey
== null) {
130 var handle
= mono_btls_pkcs12_get_private_key (Handle
.DangerousGetHandle ());
131 CheckError (handle
!= IntPtr
.Zero
);
132 privateKey
= new MonoBtlsKey (new MonoBtlsKey
.BoringKeyHandle (handle
));