Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / System / System / AndroidPlatform.cs
1 //
2 // System.AndroidPlatform.cs
3 //
4 // Author:
5 //   Jonathan Pryor (jonp@xamarin.com)
6 //
7 // Copyright (C) 2012 Xamarin Inc (http://xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if MONODROID
29 using System;
30 using System.Collections.Generic;
31 using System.Net;
32 using System.Net.Security;
33 using System.Security.Cryptography.X509Certificates;
34 #if SECURITY_DEP
35 using MSX = Mono.Security.X509;
36 #if MONO_FEATURE_BTLS
37 using Mono.Btls;
38 #endif
39 #endif
40
41 namespace System {
42
43         internal static class AndroidPlatform {
44                 delegate int GetInterfaceAddressesDelegate (out IntPtr ifap);
45                 delegate void FreeInterfaceAddressesDelegate (IntPtr ifap);
46                 
47 #if SECURITY_DEP
48                 static readonly Converter<List <byte[]>, bool> trustEvaluateSsl;
49                 static readonly Func<long, bool, byte[]> certStoreLookup;
50 #endif  // SECURITY_DEP
51                 static readonly Func<IWebProxy> getDefaultProxy;
52                 static readonly GetInterfaceAddressesDelegate getInterfaceAddresses;
53                 static readonly FreeInterfaceAddressesDelegate freeInterfaceAddresses;
54
55                 static AndroidPlatform ()
56                 {
57                         var t = Type.GetType ("Android.Runtime.AndroidEnvironment, Mono.Android", throwOnError:true);
58 #if SECURITY_DEP
59                         trustEvaluateSsl = (Converter<List<byte[]>, bool>)
60                                 Delegate.CreateDelegate (typeof (Converter<List<byte[]>, bool>),
61                                                         t,
62                                                         "TrustEvaluateSsl",
63                                                         ignoreCase:false,
64                                                         throwOnBindFailure:true);
65 #if MONO_FEATURE_BTLS
66                         certStoreLookup = (Func<long, bool, byte[]>)
67                                 Delegate.CreateDelegate (typeof (Func<long, bool, byte[]>),
68                                                         t,
69                                                         "CertStoreLookup",
70                                                         ignoreCase:false,
71                                                         throwOnBindFailure:true);
72 #endif  // MONO_FEATURE_BTLS
73 #endif  // SECURITY_DEP
74                         getDefaultProxy = (Func<IWebProxy>)Delegate.CreateDelegate (
75                                 typeof (Func<IWebProxy>), t, "GetDefaultProxy",
76                                 ignoreCase:false,
77                                 throwOnBindFailure:true);
78
79                         getInterfaceAddresses = (GetInterfaceAddressesDelegate)Delegate.CreateDelegate (
80                                 typeof (GetInterfaceAddressesDelegate), t, "GetInterfaceAddresses",
81                                 ignoreCase: false,
82                                 throwOnBindFailure: false);
83                         
84                         freeInterfaceAddresses = (FreeInterfaceAddressesDelegate)Delegate.CreateDelegate (
85                                 typeof (FreeInterfaceAddressesDelegate), t, "FreeInterfaceAddresses",
86                                 ignoreCase: false,
87                                 throwOnBindFailure: false);
88                 }
89
90 #if SECURITY_DEP
91                 internal static bool TrustEvaluateSsl (X509CertificateCollection collection)
92                 {
93                         var certsRawData = new List <byte[]> (collection.Count);
94                         foreach (var cert in collection)
95                                 certsRawData.Add (cert.GetRawCertData ());
96                         return trustEvaluateSsl (certsRawData);
97                 }
98
99 #if MONO_FEATURE_BTLS
100                 internal static MonoBtlsX509 CertStoreLookup (MonoBtlsX509Name name)
101                 {
102                         var hash = name.GetHash ();
103                         var hashOld = name.GetHashOld ();
104                         var result = certStoreLookup (hash, false);
105                         if (result == null)
106                                 result = certStoreLookup (hashOld, false);
107                         if (result == null)
108                                 result = certStoreLookup (hash, true);
109                         if (result == null)
110                                 result = certStoreLookup (hashOld, true);
111
112                         if (result == null)
113                                 return null;
114
115                         return MonoBtlsX509.LoadFromData (result, MonoBtlsX509Format.DER);
116                 }
117 #endif  // MONO_FEATURE_BTLS
118 #endif  // SECURITY_DEP
119
120                 internal static IWebProxy GetDefaultProxy ()
121                 {
122                         return getDefaultProxy ();
123                 }
124
125                 internal static int GetInterfaceAddresses (out IntPtr ifap)
126                 {
127                         ifap = IntPtr.Zero;
128                         if (getInterfaceAddresses == null)
129                                 return -1;
130
131                         return getInterfaceAddresses (out ifap);
132                 }
133
134                 internal static void FreeInterfaceAddresses (IntPtr ifap)
135                 {
136                         if (freeInterfaceAddresses == null)
137                                 return;
138
139                         freeInterfaceAddresses (ifap);
140                 }
141         }
142 }
143 #endif  // MONODROID