Bug 23020 - TLS when using Twitter API
Summary: TLS when using Twitter API
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.18.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2014-09-15 15:16 UTC by fwaris
Modified: 2014-12-03 16:06 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description fwaris 2014-09-15 15:16:03 UTC
I am running into an issue with TLS while trying to use the twitter API on Android emulator. The same code runs fine on the IOS emulator and a real Android device.

I have tried two different emulators targeting API 4.0.3 and 4.4.2.


I am using latest stable bits as of Sept 15, 2014:

Xamarin Studio
Version 5.3 (build 441)
Installation UUID: 30ccc163-2256-45f4-adf4-9d61fc18d3f4
Runtime:
	Mono 3.8.0 ((no/45d0ba1)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 308000009

Xamarin.Android
Version: 4.16.0 (Business Edition)
Android SDK: /Users/Fai/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0.3  (API level 15)
		4.4    (API level 19)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)




Here is the stack trace:


System.Net.WebException: Error: SendFailure (Error writing headers) ---> System.Exception: Error writing headers ---> System.Exception: The authentication or decryption has failed. ---> System.Exception: The server stopped the handshake.
  at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (IAsyncResult ar, Boolean ignoreEmpty) [0x00000] in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslClientStream.EndNegotiateHandshake (IAsyncResult result) [0x00000] in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  --- End of inner exception stack trace ---
  at at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult) <IL 0x00064, 0x0026c>
  at at System.Net.HttpWebRequest.GetResponse () <IL 0x00010, 0x000c3>
  at TwitterStream.getStream (string,string) [0x00022] in /Users/Fai/Projects/GLTSDemo/GlitsDemo_Common/TwitterStream.fs:15
  at TwitterStream.track (string,string,string) [0x0007d] in /Users/Fai/Projects/GLTSDemo/GlitsDemo_Common/TwitterStream.fs:32
  at TwitterStream/startTracking@50.Invoke (string) [0x00000] in /Users/Fai/Projects/GLTSDemo/GlitsDemo_Common/TwitterStream.fs:50
  at GlitsDemo_Android.MainActivity.OnCreate (Android.OS.Bundle) [0x00013] in /Users/Fai/Projects/GLTSDemo/GlitsDemo_Android/MainActivity.fs:55
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1944
  at at (wrapper dynamic-method) object.fa804d48-b434-44b7-9cc2-5e05cee17aa4 (intptr,intptr,intptr) <IL 0x00017, 0x00043>

  at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (IAsyncResult ar, Boolean ignoreEmpty) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0
Comment 1 fwaris 2014-09-15 19:44:41 UTC
Well I also got the same error while running on an actual device (not just the emulator):
System.Net.WebException: Error: SendFailure (Error writing headers) ---> System.Exception: Error writing headers ---> System.Exception: The authentication or decryption has failed. ---> System.Exception: The server stopped the handshake.
  at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (IAsyncResult ar, Boolean ignoreEmpty) [0x00000] in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslClientStream.EndNegotiateHandshake (IAsyncResult result) [0x00000] in <filename unknown>:0
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0
  --- End of inner exception stack trace ---
  --- End of inner exception stack trace ---
  at at System.Net.HttpWebRequest.EndGetResponse (System.IAsyncResult) <IL 0x00064, 0x0026c>
  at at System.Net.HttpWebRequest.GetResponse () <IL 0x00010, 0x000c3>
  at TwitterStream.getStream (string,string) [0x00022] in /Users/Fai/Projects/GLTSDemo/GlitsDemo_Common/TwitterStream.fs:15
  at TwitterStream.track (string,string,string) [0x0007d] in /Users/Fai/Projects/GLTSDemo/GlitsDemo_Common/TwitterStream.fs:32
  at TwitterStream/startTracking@50.Invoke (string) [0x00000] in /Users/Fai/Projects/GLTSDemo/GlitsDemo_Common/TwitterStream.fs:50
  at GlitsDemo_Android.MainActivity.OnCreate (Android.OS.Bundle) [0x00013] in /Users/Fai/Projects/GLTSDemo/GlitsDemo_Android/MainActivity.fs:55
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1944
  at at (wrapper dynamic-method) object.bebf90f9-3f72-4a5a-a526-e3226aa313ae (intptr,intptr,intptr) <IL 0x00017, 0x00043>
Comment 2 Arpit Jha 2014-09-16 04:22:33 UTC
I have checked this issue and unable to reproduce it.

I tried to reproduce it with the help of MWC Sample you can download it from
  https://github.com/xamarin/mobile-samples/tree/master/MWC and application deploy successfully as well as emulator.

I observed that Exception is like authority of api access and you can check it at
https://twittercommunity.com/t/restricting-api-twitter-com-to-ssl-tls-traffic/12331.I think it would be help.

Could you please provide us sample project,build info and environment info also, So that I can able to reproduce this issue at our end.

Environment Info:
=== Xamarin Studio ===

Version 5.3 (build 441)
Installation UUID: 2591d519-875d-4afe-a3d9-5fcf391bbd2d
Runtime:
 Mono 3.8.0 ((no/45d0ba1)
 GTK+ 2.24.23 (Raleigh theme)

 Package version: 308000009

=== Apple Developer Tools ===

Xcode 4.6.3 (2068)
Build 4H1503

=== Xamarin.Mac ===

Version: 1.10.0.10 (Enterprise Edition)

=== Xamarin.iOS ===

Version: 7.4.0.108 (Enterprise Edition)
Hash: 77efa3f
Branch: 
Build date: 2014-08-26 07:59:55-0400

=== Xamarin.Android ===

Version: 4.17.0 (Enterprise Edition)
Android SDK: /Users/nischal/Desktop/android-sdk-macosx
 Supported Android versions:
  2.1    (API level 7)
  2.2    (API level 8)
  2.3    (API level 10)
  3.1    (API level 12)
  3.2    (API level 13)
  4.0    (API level 14)
  4.0.3  (API level 15)
  4.1    (API level 16)
  4.2    (API level 17)
  4.3    (API level 18)
  4.4    (API level 19)
  4.4.87 (API level 20)
  4.5    (API level 21)
Java SDK: /usr
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

=== Build Information ===

Release ID: 503000441
Git revision: befb6aa1176d37a5f678f4274f340a0159091b7a
Build date: 2014-09-08 17:57:02-04
Xamarin addins: 6dc7c388e31fdfc8014689839d37de0d4622435c

=== Operating System ===

Mac OS X 10.7.4
Darwin nischals-Mac-mini.local 11.4.0 Darwin Kernel Version 11.4.0
    Mon Apr  9 19:32:15 PDT 2012
    root:xnu-1699.26.8~1/RELEASE_X86_64 x86_64
Comment 3 fwaris 2014-09-16 12:25:21 UTC
I have attached a solution that reproduces this issue.

I also tested it on Windows with the same results.

What I have found is that I almost always get an exception when running on the Android emulator with the debugger connected. However if I disconnect the debugger and then force stop the app and run it from the emulator it seems to work fine most of the the time.

The app also works most of the time on a real device, even with the debugger attached. Sometimes there are failures but then I get a different error message.

I don't seen any such issues on IOS. The solution contains both Android and IOS versions so you can compare the difference.

Note the attached app can run into the Twitter rate limit for the streaming api. This usually happens when you run the app too many times (connect/disconnect). You will get a 402 in this case. Just wait 15 minutes before retrying.

Here is the info from the windows environment:

Xamarin Studio
Version 5.3 (build 441)
Installation UUID: edd8b27e-6594-4af4-b050-48e5ca77dfe5
Runtime:
	Microsoft .NET 4.0.30319.18444
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.26

Xamarin.Android
Version: 4.16.0 (Enterprise Edition)
Android SDK: C:\Users\fwaris\AppData\Local\Android\android-sdk
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.0    (API level 11)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.4    (API level 19)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_39
java version "1.6.0_39"
Java(TM) SE Runtime Environment (build 1.6.0_39-b04)
Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode, sharing)

Build Information
Release ID: 503000441
Git revision: befb6aa1176d37a5f678f4274f340a0159091b7a
Build date: 2014-09-08 20:54:12-04
Xamarin addins: 6dc7c388e31fdfc8014689839d37de0d4622435c

Operating System
Windows 6.1.7601.65536 (64-bit)
Comment 5 Arpit Jha 2014-09-17 02:12:58 UTC
I have checked this issue and now able to reproduce it with the help of bug 
 description.

I observed that  application working fine on android device but when I deployed application on emulator it thrown an exception of "System.Net.WebException" and also observed that error seem like twitter authentication because module TwitterAuth is missing.

Screencast regarding same:

http://screencast.com/t/SYG0iScnOMB
http://www.screencast.com/t/G6ZtM1jy( Android device Galaxy S5  android version 4.4.2)

Environment Info:

=== Xamarin Studio ===

Version 5.3 (build 441)
Installation UUID: f75f53ae-7c21-493c-8339-c2031a7f0448
Runtime:
	Microsoft .NET 4.0.30319.34014
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.25

=== Xamarin.Android ===

Version: 4.18.0 (Trial Edition)
Android SDK: D:\android-sdk
	Supported Android versions:
		1.6    (API level 4)
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.0    (API level 11)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_31
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) Client VM (build 20.6-b01, mixed mode)

=== Build Information ===

Release ID: 503000441
Git revision: befb6aa1176d37a5f678f4274f340a0159091b7a
Build date: 2014-09-08 20:54:12-04
Xamarin addins: 6dc7c388e31fdfc8014689839d37de0d4622435c

=== Operating System ===

Windows 6.2.9200.0 (64-bit)
Comment 6 fwaris 2014-09-18 10:55:51 UTC
The red squiggles are just IDE issues they are not pertinent to the issue at hand. The fact that the code is compiled and deployed to the emulator means that there are no compilation issues.

(I think later versions of F# Add-in fixes the intellisense issues).


The actual error is at runtime somewhere in the TLS negotiation. The module missing squiggle is not a runtime error.

I also see the "System.Net.WebException: Error: SendFailure (Error writing headers)" on my side. This is the one that we need to resolve.
Comment 7 Marek Habersack 2014-11-27 17:05:38 UTC
I've just ran the sample on a device and I get the WebException as well, but it's thrown because of a failure to resolve IP of the stream.twitter.com host. Oddly enough, it is possible to resolve the IP from adb shell.
Comment 8 Marek Habersack 2014-11-27 17:53:55 UTC
OK, this is odd... Compiled the sample on a different machine, with fsharp/master, Xamarin.Android/master and everything works fine - no exceptions at all, tweet stream is refreshing
Comment 9 fwaris 2014-11-28 12:20:20 UTC
Marek,

This problem is intermittent. 

What I found was that running on an emulator the problem was more apparent (occurred more frequently) than when running on an actual device. 

However, even on the emulator it run successfully some of the time.


Faisal
Comment 10 Marek Habersack 2014-12-01 08:23:05 UTC
@Faisal, are you getting the same exact SendFailure error every time, or does the reason change?
Comment 11 Marek Habersack 2014-12-01 09:12:25 UTC
@Faisal, would you be able to test your app with Xamarin.Android 4.20 and see if the issue still happens there? I've tried several dozen times in 2 different emulators, on two different devices and, try as I might, I can't repro the issue.
Comment 12 fwaris 2014-12-02 09:06:35 UTC
I ran it with the latest Xamarin bits and the app works in most places various emulators and devices.

However the only issue is with an emulator running v4.0.3 Android behind a proxy or firewall. 

Can you please try it on a v4.0.3 emulator image and if there are no issues on your end, we can consider this as fixed.

Thanks,

Faisal
Comment 13 Marek Habersack 2014-12-02 10:27:06 UTC
@Faisal I'll try it, but can you tell me more about the kind of proxy the emulator is behind? Is it a transparent one, filtering, authenticated, anonymous? What OS does the proxy run on, or perhaps it's a proxy hardware? Proxies, especially the transparent ones, are a tricky business to work with, especially with mobile devices which are more fragile as far as connectivity is concerned.
Comment 14 Marek Habersack 2014-12-03 08:52:07 UTC
@Faisal, tested as requested - no failures in the emulator.
Comment 15 fwaris 2014-12-03 10:53:56 UTC
Ok thanks. It is probably our proxy setup then. Not sure exactly what it is but there is no authentication required. The only configuration in the emulator is the 'access point' proxy ip and port.
Comment 16 Marek Habersack 2014-12-03 16:06:08 UTC
@Faisal, I saw a few proxies which break SSL/TLS from time to time. OK then, I'm going to close this bug but feel free to reopen if it resurfaces itself (hopefully not though!)