ModernHttpClient icon indicating copy to clipboard operation
ModernHttpClient copied to clipboard

System.NotSupportedException: cannot decode public key from unknown oid '1.2.840.10045.2.1'

Open Alex-Dobrynin opened this issue 5 years ago • 27 comments
trafficstars

when I use modernhttpclient.nativemessagehandler and trying to get access trough this link: https://restcountries.eu/data/ala.svg I get an exception mentioned in title of this issue. If I use httpclient without nativemessagehandler it is ok

Alex-Dobrynin avatar Jun 30 '20 12:06 Alex-Dobrynin

same issue here. plugin does not support ECDSA, which is a big issue

YuliaLoyko avatar Oct 07 '20 11:10 YuliaLoyko

Let me se what i can do.

alexrainman avatar Oct 07 '20 12:10 alexrainman

Having the same problem. But I worked around it by setting TLSConfig and SSL Pinning. Not sure why this works.

danieljgmaclean avatar Oct 07 '20 14:10 danieljgmaclean

@alexrainman thank you! do you have any ETA for this? or do you need some help?

YuliaLoyko avatar Oct 07 '20 16:10 YuliaLoyko

Is this iOS only?

alexrainman avatar Oct 22 '20 19:10 alexrainman

Is this iOS only?

no, it`s both

Alex-Dobrynin avatar Oct 22 '20 20:10 Alex-Dobrynin

Well, i am facing this issue too so, i have to fix it :)

alexrainman avatar Oct 23 '20 02:10 alexrainman

I cannot reproduce this. I can get https://restcountries.eu/data/ala.svg without any issues as soon as i provide the public key in the TLSConfig.

alexrainman avatar Oct 23 '20 16:10 alexrainman

You can get that server public key running this code in Android:

var hostname = "restcountries.eu";

var certificatePinner = new Square.OkHttp3.CertificatePinner.Builder()
    .Add(hostname, "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
    .Build();

var client = new OkHttpClient.Builder()
    .CertificatePinner(certificatePinner)
    .Build();

var request = new Request.Builder()
    .Url("https://" + hostname)
    .Build();

var call = client.NewCall(request);

var response = await call.ExecuteAsync();

Then provide it in the TLSConfig:

readonly HttpClient client = new HttpClient(new NativeMessageHandler(false, new TLSConfig()
{
    Pins = new List<Pin>()
    {
        new Pin()
        {
            Hostname = "restcountries.eu",
            PublicKeys = new string []
            {
                "sha256/wxgZ6Jx5WaNt5zAgUSDnLsK8E5uy+DUumAogHk4P7R8="
            }
        }
    },
    DangerousAcceptAnyServerCertificateValidator = false
})
{
    DisableCaching = true,
    Timeout = new TimeSpan(0, 0, 9)
});

alexrainman avatar Oct 23 '20 16:10 alexrainman

what about ios? and i provide this handler to FF Image Loading once at app startup, so i need to have universal approach. because the user may have different images or svgs from different hosts and from our backend during runtime.

so what i did, i just left default http client

image

Alex-Dobrynin avatar Oct 23 '20 17:10 Alex-Dobrynin

It is universal.

You get the Public Key using that trick in Android, then configure TLS for both platforms in your shared code.

Also, i found where the library fails and this is not something i can fix unless i replace this piece of code:

if (!chain.Build(root))
{
    errors = SslPolicyErrors.RemoteCertificateChainErrors;
    PinningFailureMessage = FailureMessages.ChainError;
    goto sslErrorVerify;
}

With something like this:

var valid = chain.ChainElements.Cast<X509ChainElement>().All(x => x.Certificate.Thumbprint == root.Thumbprint);

if (!valid)
{
    errors = SslPolicyErrors.RemoteCertificateChainErrors;
    PinningFailureMessage = FailureMessages.ChainError;
    goto sslErrorVerify;
}

chain.Build(root) fails with ECDSA because it is not supported by Mono:

https://github.com/mono/mono/blob/1547af6a278321d5dbc56a63f18b2380c757608e/mcs/class/System/System.Security.Cryptography.X509Certificates/PublicKey.cs#L141

alexrainman avatar Oct 23 '20 17:10 alexrainman

By the way, i have exactly the same use case where i need to share my single HttpClient instance with FFImageLoading but i want to use native handlers to make it faster so, i may apply this fix and release a new version.

alexrainman avatar Oct 23 '20 17:10 alexrainman

same issue here. plugin does not support ECDSA, which is a big issue

chain.Build(root) fails with ECDSA because it is not supported by Mono:

https://github.com/mono/mono/blob/1547af6a278321d5dbc56a63f18b2380c757608e/mcs/class/System/System.Security.Cryptography.X509Certificates/PublicKey.cs#L141

alexrainman avatar Oct 23 '20 17:10 alexrainman

but why the default http client works well?

Alex-Dobrynin avatar Oct 23 '20 19:10 Alex-Dobrynin

Because it doesn’t use PublicKey at all.

alexrainman avatar Oct 23 '20 19:10 alexrainman

As i said, use Android to get server certificate public key, and once you have it, configure modernhttpclient with it for both platforms.

alexrainman avatar Oct 23 '20 19:10 alexrainman

hi,

i have same problem. Is there any news on the update that fixes this crash?

thank you so much

michelinaFolino avatar Jan 14 '21 14:01 michelinaFolino

Also ran into this issue. @alexrainman thanks for the code to discover the public keys! Interestingly enough - for me - this was only crashing on iOS.

InquisitorJax avatar Nov 30 '21 08:11 InquisitorJax

Hi @alexrainman Can you estimate when there will be a fix for this in iOS?

AlonRom avatar Sep 18 '22 05:09 AlonRom

Hi @alexrainman, we get this exception all the time, is there an update regarding this issue?

NoamMani avatar Nov 08 '22 07:11 NoamMani

@alexrainman ??

AlonRom avatar Nov 21 '22 22:11 AlonRom

I have been out for most of the year. Still recovering from surgery. But a new revamped version of the plugin will be out soon.

alexrainman avatar Nov 21 '22 22:11 alexrainman

Glad you’re on the mend!! When you do revamp the library, can you make it a . NET MAUI class library project as opposed to the classic xam plugin model

gcadmes-extron avatar Nov 21 '22 22:11 gcadmes-extron

That's the plan. All my plugins will be out for .NET MAUI

alexrainman avatar Nov 21 '22 22:11 alexrainman

@alexrainman Hope you are feeling well! Happy to hear that a version with fixes will be released soon, we have been waiting for it for quite a long time. a Xamarin.Forms version with this fix will be released as well, right?

AlonRom avatar Nov 21 '22 22:11 AlonRom

Yes, XF will be released in a minor version while .NET MAUI will be a major release.

alexrainman avatar Nov 21 '22 23:11 alexrainman

@alexrainman any update?

NoamMani avatar Jun 21 '23 10:06 NoamMani