Bug 41490 - Java.Security.KeyStore.Load fails for Null Stream (not duplicate of Bug 6766)
Summary: Java.Security.KeyStore.Load fails for Null Stream (not duplicate of Bug 6766)
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 6.0.1 (C6SR1)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2016-06-03 16:31 UTC by Evgeniy Zverev
Modified: 2016-06-06 11:50 UTC (History)
2 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 Evgeniy Zverev 2016-06-03 16:31:51 UTC
I am using Xamarin.Android for Visual Studio. Xamarin.Android   6.0.4.0 (ee215fc).
Recently I started receiving the following exception reports from my clients:

OS: Android 4.4.4 (Kitkat) KTU84P.I9300IXWUBOF1
ID: 0ed202e3
Timestamp: 03.06.2016 13:33:54

Java.IO.IOException: Exception of type 'Java.IO.IOException' was thrown.
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x7b5aed70 + 0x00024> in <filename unknown>:0 
  at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue* parms) <0x7ae5e2a8 + 0x000bf> in <filename unknown>:0 
  at Java.Security.KeyStore.Load (System.IO.Stream stream, System.Char[] password) <0x7b5ad6d8 + 0x001b3> in <filename unknown>:0 
  at Xamarin.Auth.AndroidAccountStore..ctor (Android.Content.Context context) <0x7b5aaf10 + 0x00117> in <filename unknown>:0 
  at Xamarin.Auth.AccountStore.Create (Android.Content.Context context) <0x7b5aad50 + 0x00023> in <filename unknown>:0 
  at AIAndroidApplicationService1.CGooglePlayStore.AccountStoreGet (AIPortableService2.SApplicationLicenseInfo iApplicationLicenseInfo) <0x7b5aa998 + 0x001ff> in <filename unknown>:0 
  --- End of managed exception stack trace ---
java.io.EOFException
	at libcore.io.Streams.readFully(Streams.java:83)
	at java.io.DataInputStream.readInt(DataInputStream.java:103)
	at com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi.engineLoad(BcKeyStoreSpi.java:799)
	at java.security.KeyStore.load(KeyStore.java:589)
	at md58617ae6ebcf2aaf0d17f07d05d4ecc1e.MainActivity.n_onStart(Native Method)
	at md58617ae6ebcf2aaf0d17f07d05d4ecc1e.MainActivity.onStart(MainActivity.java:67)
	at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
	at android.app.Activity.performStart(Activity.java:5592)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2414)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
	at android.app.ActivityThread.access$900(ActivityThread.java:171)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:146)
	at android.app.ActivityThread.main(ActivityThread.java:5679)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:515)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
	at dalvik.system.NativeStart.main(Native Method)

This is my Xamarin.Auth.AndroidAccountStore..ctor implementation where the exception happens:

        public AndroidAccountStore(Context context)
        {
            keyStore = KeyStore.GetInstance(KeyStore.DefaultType);
            bool loaded = false;

            try
            {
                lock (fileLock)
                {
                    string[] files = context.FileList();

                    if (files.Contains(FileName))
                    {
                        using (var s = context.OpenFileInput(FileName))
                        {
                            keyStore.Load(s, Password);
                        }

                        loaded = true;
                    }
                }
            }
            catch
            {
            }

            if (!loaded)
            {
                keyStore.Load(null, Password);
            }
        }

As you can see, the exception is rising from the second call to keyStore.Load i.e. keyStore.Load(null, Password);

I cannot reproduce this exception in my development environment. Nevertheless many of my clients send this exception via my internal exception tracking.

I have read the Bug 6766 - Java.Security.KeyStore.Load wraps null streams. It is said that this was fixed long time ago. It appears this was not.
Comment 1 Jonathan Pryor 2016-06-03 19:26:00 UTC
> Bug 6766 - Java.Security.KeyStore.Load wraps null streams.

Bug #6766 was a very different issue: instead of "forwarding" `null` to the underlying Java method, `null` would be "wrapped", passing a *non-null* value to the Java method:

    // Old-and-busted, Bug #6766; pseudo-code:
    var adapter = new InputStreamAdapter (stream); // stream is null!
    JNIEnv.CallVoidMethod (KeyStore_load, adapter.Handle, ...);

    // Bug #6766 Fix; pseudo-code:
    var adapter = stream != null ? new InputStreamAdapter (stream) : null;
    JNIEnv.CallVoidMethod (KeyStore_load, adapter?.Handle, ...);

Later on, a System.NullReferenceException would be thrown from InputStreamAdapter because the underlying Stream instance was null.

This is a *very* different situation than what you are seeing. You're seeing a java.io.EOFException:


> java.io.EOFException
> 	at libcore.io.Streams.readFully(Streams.java:83)
> 	at java.io.DataInputStream.readInt(DataInputStream.java:103)
> 	at com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi.engineLoad(BcKeyStoreSpi.java:799)
> 	at java.security.KeyStore.load(KeyStore.java:589)
> 	at md58617ae6ebcf2aaf0d17f07d05d4ecc1e.MainActivity.n_onStart(Native Method)

I don't know why you're getting an EOFException, so let's hit Android source.

https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/libcore/io/Streams.java#83


Streams.readFully() wants to read `byteCount` bytes from `in`, and throws EOFException when it can't read that many bytes. Therefore, whatever stream it's reading from is too small.

Going up, java.io.DataInputStream.readInt():

https://android.googlesource.com/platform/libcore/+/cff1616/luni/src/main/java/java/io/DataInputStream.java#123

It wants to read 4 bytes, and the stream doesn't have 4 bytes to give. (Note: the line number differs here -- line 123 vs. 103 -- and DataInputStream.java doesn't exist in the `master` branch in the libcore repo. I don't know where the "right" version is, or what branch you're running on, which could be important.)

Moving up, we have BcKeyStoreSpi.engineLoad():

https://android.googlesource.com/platform/external/bouncycastle/+/a198e1ecc615e26a167d0f2dca9fa7e5fc62de10/bcprov/src/main/java/org/bouncycastle/jcajce/provider/keystore/bc/BcKeyStoreSpi.java#799

Here's the important bit: look at line 793:

        if (stream == null)     // just initialising
        {
            return;
        }

If this were *actually* the `keyStore.Load(null, Password)` codepath, stream would be null, and *you wouldn't even reach* line 799.

Therefore, Load()ing a null value has nothing to do with it. It's actually the earlier code which is failing:

    using (var s = context.OpenFileInput(FileName))
    {
        keyStore.Load(s, Password);
    }

If FileName refers to a file that is less than 4 bytes in size, you'll get an EOFException exactly and you originally described.

Why are you trying to load *every* file returned from context.FileList()?
Comment 2 Evgeniy Zverev 2016-06-04 22:39:29 UTC
Thanks for explanations.

Nevertheless, where do you see me load every file returned from context.FileList()? I do not do that. I get FileList just to check that the file I am after exists.

You are saying that the exception I get has something to do with the first code path:

    using (var s = context.OpenFileInput(FileName))
    {
        keyStore.Load(s, Password);
    }

How can that be true? It is inside the try/catch with empty catch i.e. ignoring any exceptions. Any exception leads to the 

    if (!loaded)
    {
        keyStore.Load(null, Password);
    }

code path. What am I missing?
Comment 3 Evgeniy Zverev 2016-06-06 11:50:14 UTC
I must correct myself. I am sorry I took so much of your time. Shame on me. 

I missed that the exceptions I am receiving are coming from the previous version of my code which came with the original version of Xamarin.Auth.

It was:

    try 
    {
        lock(fileLock) 
        {
            using (var s = context.OpenFileInput(FileName)) 
            {
                ks.Load(s, Password);
            }
        }
    }
    catch(FileNotFoundException) 
    {
        //ks.Load(null, Password);
        LoadEmptyKeyStore(Password);
    }

The problem was in the catch section filter (FileNotFoundException). So bad files i.e. files that were created but were not filled cause the exceptions I get.

Again, I am very sorry for the mess.