[System]: Enable AndroidPlatform.CertStoreLookup().
[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 using Mono.Btls;
37 #endif
38
39 namespace System {
40
41         internal static class AndroidPlatform {
42                 delegate int GetInterfaceAddressesDelegate (out IntPtr ifap);
43                 delegate void FreeInterfaceAddressesDelegate (IntPtr ifap);
44                 
45 #if SECURITY_DEP
46                 static readonly Converter<List <byte[]>, bool> trustEvaluateSsl;
47                 static readonly Func<long, bool, byte[]> certStoreLookup;
48 #endif  // SECURITY_DEP
49                 static readonly Func<IWebProxy> getDefaultProxy;
50                 static readonly GetInterfaceAddressesDelegate getInterfaceAddresses;
51                 static readonly FreeInterfaceAddressesDelegate freeInterfaceAddresses;
52
53                 static AndroidPlatform ()
54                 {
55                         var t = Type.GetType ("Android.Runtime.AndroidEnvironment, Mono.Android", throwOnError:true);
56 #if SECURITY_DEP
57                         trustEvaluateSsl = (Converter<List<byte[]>, bool>)
58                                 Delegate.CreateDelegate (typeof (Converter<List<byte[]>, bool>),
59                                                         t,
60                                                         "TrustEvaluateSsl",
61                                                         ignoreCase:false,
62                                                         throwOnBindFailure:true);
63                         certStoreLookup = (Func<long, bool, byte[]>)
64                                 Delegate.CreateDelegate (typeof (Func<long, bool, byte[]>),
65                                                         t,
66                                                         "CertStoreLookup",
67                                                         ignoreCase:false,
68                                                         throwOnBindFailure:true);
69 #endif  // SECURITY_DEP
70                         getDefaultProxy = (Func<IWebProxy>)Delegate.CreateDelegate (
71                                 typeof (Func<IWebProxy>), t, "GetDefaultProxy",
72                                 ignoreCase:false,
73                                 throwOnBindFailure:true);
74
75                         getInterfaceAddresses = (GetInterfaceAddressesDelegate)Delegate.CreateDelegate (
76                                 typeof (GetInterfaceAddressesDelegate), t, "GetInterfaceAddresses",
77                                 ignoreCase: false,
78                                 throwOnBindFailure: false);
79                         
80                         freeInterfaceAddresses = (FreeInterfaceAddressesDelegate)Delegate.CreateDelegate (
81                                 typeof (FreeInterfaceAddressesDelegate), t, "FreeInterfaceAddresses",
82                                 ignoreCase: false,
83                                 throwOnBindFailure: false);
84                 }
85
86 #if SECURITY_DEP
87                 internal static bool TrustEvaluateSsl (X509CertificateCollection collection)
88                 {
89                         var certsRawData = new List <byte[]> (collection.Count);
90                         foreach (var cert in collection)
91                                 certsRawData.Add (cert.GetRawCertData ());
92                         return trustEvaluateSsl (certsRawData);
93                 }
94
95                 internal static MonoBtlsX509 CertStoreLookup (MonoBtlsX509Name name)
96                 {
97                         var hash = name.GetHash ();
98                         var hashOld = name.GetHashOld ();
99                         var result = certStoreLookup (hash, false);
100                         if (result == null)
101                                 result = certStoreLookup (hashOld, false);
102                         if (result == null)
103                                 result = certStoreLookup (hash, true);
104                         if (result == null)
105                                 result = certStoreLookup (hashOld, true);
106
107                         if (result == null)
108                                 return null;
109
110                         return MonoBtlsX509.LoadFromData (result, MonoBtlsX509Format.DER);
111                 }
112 #endif  // SECURITY_DEP
113
114                 internal static IWebProxy GetDefaultProxy ()
115                 {
116                         return getDefaultProxy ();
117                 }
118
119                 internal static int GetInterfaceAddresses (out IntPtr ifap)
120                 {
121                         ifap = IntPtr.Zero;
122                         if (getInterfaceAddresses == null)
123                                 return -1;
124
125                         return getInterfaceAddresses (out ifap);
126                 }
127
128                 internal static void FreeInterfaceAddresses (IntPtr ifap)
129                 {
130                         if (freeInterfaceAddresses == null)
131                                 return;
132
133                         freeInterfaceAddresses (ifap);
134                 }
135         }
136 }
137 #endif  // MONODROID