Skip to content

Commit 12c7d3f

Browse files
update to compilable version
enable self signed certs (where chain.Count==1) only in iOS yet merge parts of https://github.com/Youscribe/ModernHttpClient/blob/hotfix/cycle8-issue/src/ModernHttpClient/Android/OkHttpNetworkHandler.cs
1 parent 64b36bb commit 12c7d3f

File tree

10 files changed

+170
-168
lines changed

10 files changed

+170
-168
lines changed

ModernHttpClient.sln

Lines changed: 108 additions & 75 deletions
Large diffs are not rendered by default.

src/ModernHttpClient/Android/OkHttpNetworkHandler.cs

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -116,26 +116,18 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
116116
// NB: Even closing a socket must be done off the UI thread. Cray!
117117
cancellationToken.Register(() => Task.Run(() => call.Cancel()));
118118

119-
var resp = default(Response);
120-
try {
121-
resp = await call.EnqueueAsync().ConfigureAwait(false);
122-
var newReq = resp.Request();
123-
var newUri = newReq == null ? null : newReq.Uri();
124-
request.RequestUri = new Uri(newUri.ToString());
125-
if (throwOnCaptiveNetwork && newUri != null) {
126-
if (url.Host != newUri.Host) {
127-
throw new CaptiveNetworkException(new Uri(java_uri), new Uri(newUri.ToString()));
128-
}
129-
}
130-
} catch (IOException ex) {
131-
if (ex.Message.ToLowerInvariant().Contains("canceled")) {
132-
throw new OperationCanceledException();
133-
}
134-
135-
throw;
136-
}
137-
138-
var respBody = resp.Body();
119+
var resp = await call.EnqueueAsync().ConfigureAwait(false);
120+
var newReq = resp.Request();
121+
var newUri = newReq == null ? null : newReq.Uri();
122+
request.RequestUri = new Uri(newUri.ToString());
123+
124+
if (throwOnCaptiveNetwork && newUri != null)
125+
{
126+
if (url.Host != newUri.Host)
127+
throw new CaptiveNetworkException(new Uri(java_uri), new Uri(newUri.ToString()));
128+
}
129+
130+
var respBody = resp.Body();
139131

140132
cancellationToken.ThrowIfCancellationRequested();
141133

@@ -180,8 +172,12 @@ public void OnFailure(Request p0, Java.IO.IOException p1)
180172
// Kind of a hack, but the simplest way to find out that server cert. validation failed
181173
if (p1.Message == String.Format("Hostname '{0}' was not verified", p0.Url().Host)) {
182174
tcs.TrySetException(new WebException(p1.LocalizedMessage, WebExceptionStatus.TrustFailure));
183-
} else {
184-
tcs.TrySetException(p1);
175+
}
176+
else if (p1.Message.ToLowerInvariant().Contains("canceled"))
177+
{
178+
tcs.TrySetException(new System.OperationCanceledException());
179+
} else {
180+
tcs.TrySetException(new WebException(p1.Message));
185181
}
186182
}
187183

@@ -198,7 +194,7 @@ class HostnameVerifier : Java.Lang.Object, IHostnameVerifier
198194

199195
public bool Verify(string hostname, ISSLSession session)
200196
{
201-
return verifyServerCertificate(hostname, session) & verifyClientCiphers(hostname, session);
197+
return verifyServerCertificate(hostname, session);
202198
}
203199

204200
/// <summary>
@@ -224,12 +220,13 @@ static bool verifyServerCertificate(string hostname, ISSLSession session)
224220
if (certificates == null || certificates.Length == 0) {//no cert at all
225221
errors = System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable;
226222
goto bail;
227-
}
223+
}
228224

229-
if (certificates.Length == 1) {//no root?
230-
errors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors;
231-
goto bail;
232-
}
225+
// this disables self signed certificates
226+
//if (certificates.Length == 1) {//no root?
227+
// errors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors;
228+
// goto bail;
229+
// }
233230

234231
var netCerts = certificates.Select(x => new X509Certificate2(x.GetEncoded())).ToArray();
235232

@@ -261,22 +258,5 @@ static bool verifyServerCertificate(string hostname, ISSLSession session)
261258
// Call the delegate to validate
262259
return ServicePointManager.ServerCertificateValidationCallback(hostname, root, chain, errors);
263260
}
264-
265-
/// <summary>
266-
/// Verifies client ciphers and is only available in Mono and Xamarin products.
267-
/// </summary>
268-
/// <returns><c>true</c>, if client ciphers was verifyed, <c>false</c> otherwise.</returns>
269-
/// <param name="hostname"></param>
270-
/// <param name="session"></param>
271-
static bool verifyClientCiphers(string hostname, ISSLSession session)
272-
{
273-
var callback = ServicePointManager.ClientCipherSuitesCallback;
274-
if (callback == null) return true;
275-
276-
var protocol = session.Protocol.StartsWith("SSL", StringComparison.InvariantCulture) ? SecurityProtocolType.Ssl3 : SecurityProtocolType.Tls;
277-
var acceptedCiphers = callback(protocol, new[] { session.CipherSuite });
278-
279-
return acceptedCiphers.Contains(session.CipherSuite);
280-
}
281261
}
282262
}

src/ModernHttpClient/ModernHttpClient.Android.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -13,9 +13,9 @@
1313
<AndroidResgenClass>Resource</AndroidResgenClass>
1414
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
1515
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
16-
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
16+
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
1717
<AssemblyName>ModernHttpClient</AssemblyName>
18-
<TargetFrameworkVersion>v2.3</TargetFrameworkVersion>
18+
<TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
1919
</PropertyGroup>
2020
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2121
<DebugSymbols>true</DebugSymbols>
@@ -49,9 +49,7 @@
4949
<Compile Include="Android\OkHttpNetworkHandler.cs" />
5050
<None Include="Resources\AboutResources.txt" />
5151
<AndroidResource Include="Resources\values\Strings.xml" />
52-
<Folder Include="Android\Properties\" />
5352
<Compile Include="Android\ConcatenatingStream.cs" />
54-
<Folder Include="Properties\" />
5553
<Compile Include="Properties\AssemblyInfo.cs" />
5654
<Compile Include="ProgressStreamContent.cs" />
5755
<Compile Include="Utility.cs" />
@@ -65,5 +63,8 @@
6563
<HintPath>..\..\packages\Square.OkHttp.2.4.0.3\lib\MonoAndroid\Square.OkHttp.dll</HintPath>
6664
</Reference>
6765
</ItemGroup>
66+
<ItemGroup>
67+
<Folder Include="Android\Properties\" />
68+
</ItemGroup>
6869
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
69-
</Project>
70+
</Project>

src/ModernHttpClient/Resources/Resource.designer.cs

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ModernHttpClient/iOS/NSUrlSessionHandler.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,11 @@ public override void DidReceiveChallenge(NSUrlSession session, NSUrlSessionTask
303303
goto sslErrorVerify;
304304
}
305305

306-
if (serverCertChain.Count == 1) {
307-
errors = SslPolicyErrors.RemoteCertificateChainErrors;
308-
goto sslErrorVerify;
309-
}
306+
// this disables self signed certificates
307+
//if (serverCertChain.Count == 1) {
308+
// errors = SslPolicyErrors.RemoteCertificateChainErrors;
309+
// goto sslErrorVerify;
310+
//}
310311

311312
var netCerts = Enumerable.Range(0, serverCertChain.Count)
312313
.Select(x => serverCertChain[x].ToX509Certificate2())

src/Playground.Android/MainActivity.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ protected override void OnCreate (Bundle bundle)
4949
// Set our view from the "main" layout resource
5050
SetContentView (Resource.Layout.Main);
5151

52-
//This API is only available in Mono and Xamarin products.
53-
//You can filter and/or re-order the ciphers suites that the SSL/TLS server will accept from a client.
54-
//The following example removes weak (export) ciphers from the list that will be offered to the server.
55-
ServicePointManager.ClientCipherSuitesCallback += (protocol, allCiphers) =>
56-
allCiphers.Where(x => !x.Contains("EXPORT")).ToList();
57-
5852
//Here we accept any certificate and just print the cert's data.
5953
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => {
6054
System.Diagnostics.Debug.WriteLine("Callback Server Certificate: " + sslPolicyErrors);

src/Playground.Android/Playground.Android.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -14,9 +14,9 @@
1414
<AndroidResgenClass>Resource</AndroidResgenClass>
1515
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
1616
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
17-
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
17+
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
1818
<AssemblyName>Playground.Android</AssemblyName>
19-
<TargetFrameworkVersion>v4.0.3</TargetFrameworkVersion>
19+
<TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
2020
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
2121
</PropertyGroup>
2222
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
@@ -81,4 +81,4 @@
8181
<Name>ModernHttpClient.Android</Name>
8282
</ProjectReference>
8383
</ItemGroup>
84-
</Project>
84+
</Project>
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="Playground.Android">
33
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />
4-
<application android:label="Playground.Android">
5-
</application>
4+
<application android:label="Playground.Android"></application>
65
<uses-permission android:name="android.permission.INTERNET" />
76
</manifest>

src/Playground.Android/Resources/Resource.designer.cs

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Playground.iOS/Playground_iOSViewController.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ public Playground_iOSViewController () : base ("Playground_iOSViewController", n
3131
});
3232
*/
3333

34-
//This API is only available in Mono and Xamarin products.
35-
//You can filter and/or re-order the ciphers suites that the SSL/TLS server will accept from a client.
36-
//The following example removes weak (export) ciphers from the list that will be offered to the server.
37-
ServicePointManager.ClientCipherSuitesCallback += (protocol, allCiphers) =>
38-
allCiphers.Where(x => !x.Contains("EXPORT")).ToList();
39-
4034
//Here we accept any certificate and just print the cert's data.
4135
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => {
4236
System.Diagnostics.Debug.WriteLine("Callback Server Certificate: " + sslPolicyErrors);

0 commit comments

Comments
 (0)