1 //
2 // MonoBtlsPkcs12.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
8 //
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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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
25 // THE SOFTWARE.
26 #if SECURITY_DEP && MONO_FEATURE_BTLS
27 using System;
28 using System.IO;
29 using System.Security.Cryptography.X509Certificates;
30 using System.Runtime.CompilerServices;
31 using System.Runtime.InteropServices;
32 
33 namespace Mono.Btls
34 {
35 	class MonoBtlsPkcs12 : MonoBtlsObject
36 	{
37 		internal class BoringPkcs12Handle : MonoBtlsHandle
38 		{
BoringPkcs12Handle(IntPtr handle)39 			public BoringPkcs12Handle (IntPtr handle)
40 				: base (handle, true)
41 			{
42 			}
43 
ReleaseHandle()44 			protected override bool ReleaseHandle ()
45 			{
46 				mono_btls_pkcs12_free (handle);
47 				return true;
48 			}
49 		}
50 
51 		new internal BoringPkcs12Handle Handle {
52 			get { return (BoringPkcs12Handle)base.Handle; }
53 		}
54 
55 		[DllImport (BTLS_DYLIB)]
mono_btls_pkcs12_free(IntPtr handle)56 		extern static void mono_btls_pkcs12_free (IntPtr handle);
57 
58 		[DllImport (BTLS_DYLIB)]
mono_btls_pkcs12_new()59 		extern static IntPtr mono_btls_pkcs12_new ();
60 
61 		[DllImport (BTLS_DYLIB)]
mono_btls_pkcs12_get_count(IntPtr handle)62 		extern static int mono_btls_pkcs12_get_count (IntPtr handle);
63 
64 		[DllImport (BTLS_DYLIB)]
mono_btls_pkcs12_get_cert(IntPtr Handle, int index)65 		extern static IntPtr mono_btls_pkcs12_get_cert (IntPtr Handle, int index);
66 
67 		[DllImport (BTLS_DYLIB)]
mono_btls_pkcs12_add_cert(IntPtr chain, IntPtr x509)68 		extern static int mono_btls_pkcs12_add_cert (IntPtr chain, IntPtr x509);
69 
70 		[DllImport (BTLS_DYLIB)]
mono_btls_pkcs12_import(IntPtr chain, void* data, int len, IntPtr password)71 		extern unsafe static int mono_btls_pkcs12_import (IntPtr chain, void* data, int len, IntPtr password);
72 
73 		[DllImport (BTLS_DYLIB)]
mono_btls_pkcs12_has_private_key(IntPtr pkcs12)74 		extern static int mono_btls_pkcs12_has_private_key (IntPtr pkcs12);
75 
76 		[DllImport (BTLS_DYLIB)]
mono_btls_pkcs12_get_private_key(IntPtr pkcs12)77 		extern static IntPtr mono_btls_pkcs12_get_private_key (IntPtr pkcs12);
78 
MonoBtlsPkcs12()79 		internal MonoBtlsPkcs12 ()
80 			: base (new BoringPkcs12Handle (mono_btls_pkcs12_new ()))
81 		{
82 		}
83 
MonoBtlsPkcs12(BoringPkcs12Handle handle)84 		internal MonoBtlsPkcs12 (BoringPkcs12Handle handle)
85 			: base (handle)
86 		{
87 		}
88 
89 		MonoBtlsKey privateKey;
90 
91 		public int Count {
92 			get { return mono_btls_pkcs12_get_count (Handle.DangerousGetHandle ()); }
93 		}
94 
GetCertificate(int index)95 		public MonoBtlsX509 GetCertificate (int index)
96 		{
97 			if (index >= Count)
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));
102 		}
103 
AddCertificate(MonoBtlsX509 x509)104 		public void AddCertificate (MonoBtlsX509 x509)
105 		{
106 			mono_btls_pkcs12_add_cert (
107 				Handle.DangerousGetHandle (),
108 				x509.Handle.DangerousGetHandle ());
109 		}
110 
Import(byte[] buffer, string password)111 		public unsafe void Import (byte[] buffer, string password)
112 		{
113 			var passptr = IntPtr.Zero;
114 			fixed (void* ptr = buffer)
115 			try {
116 				if (password != null)
117 					passptr = Marshal.StringToHGlobalAnsi (password);
118 				var ret = mono_btls_pkcs12_import (
119 					Handle.DangerousGetHandle (), ptr,
120 					buffer.Length, passptr);
121 				CheckError (ret);
122 			} finally {
123 				if (passptr != IntPtr.Zero)
124 					Marshal.FreeHGlobal (passptr);
125 			}
126 		}
127 
128 		public bool HasPrivateKey {
129 			get { return mono_btls_pkcs12_has_private_key (Handle.DangerousGetHandle ()) != 0; }
130 		}
131 
GetPrivateKey()132 		public MonoBtlsKey GetPrivateKey ()
133 		{
134 			if (!HasPrivateKey)
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));
140 			}
141 			return privateKey;
142 		}
143 	}
144 }
145 #endif
146