Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AndroidMessageHandler seems to be ignoring ClientCertificates #9576

Closed
nk-alex opened this issue Dec 3, 2024 · 6 comments
Closed

AndroidMessageHandler seems to be ignoring ClientCertificates #9576

nk-alex opened this issue Dec 3, 2024 · 6 comments
Labels
need-attention A xamarin-android contributor needs to review

Comments

@nk-alex
Copy link

nk-alex commented Dec 3, 2024

Description

I have an old .net framework 4.5 project using HttpClient with HttpClientHandler in order to load client certificate for query. This is the code I'm using. Works as expected:

var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.SslProtocols = SslProtocols.Tls12;
handler.ClientCertificates.Add(new X509Certificate2(certificate, password));

using (var httpClient = new HttpClient(handler))
{
    var httpRequest = new HttpRequestMessage(HttpMethod.Post, uri)
    {
        Content = new StringContent(body, Encoding.UTF8, "text/xml")
    };
    httpRequest.Headers.Add("SOAPAction", action);
    var httpResponse = await httpClient.SendAsync(httpRequest);
}

Now I'm trying to recreate that code on my .net 8 MAUI project. I started with the Android specific code:

#if ANDROID
                var handler = new AndroidMessageHandler();
                handler.ClientCertificateOptions = ClientCertificateOption.Manual;
                handler.SslProtocols = SslProtocols.Tls12;
                handler.ClientCertificates = new X509CertificateCollection { new X509Certificate2(certificate,  password) };
#else

#endif

                using (var httpClient = new HttpClient(handler))
                {
                    var httpRequest = new HttpRequestMessage(HttpMethod.Post, uri)
                    {
                        Content = new StringContent(body, Encoding.UTF8, "text/xml")
                    };
                    httpRequest.Headers.Add("SOAPAction", action);
                    var httpResponse = await httpClient.SendAsync(httpRequest);
                }

But the web service I'm sending my request keeps on returning me 403 - No certificate detected.

  • The web service mentioned above is from a third-party. I don't have control over it.
  • I'm using the same message and certificate on both my .net framework application and MAUI application. Since in .net framework this is working, my best guess is I'm either not using AndroidMessageHandler as intended, or this issue is still around.

Steps to Reproduce

Link to public reproduction project repository

No response

Version with bug

8.0.100 SR10

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14

Did you find any workaround?

Relevant log output

Copy link

We've found some similar issues:

If any of the above are duplicates, please consider closing this issue out and adding additional context in the original issue.

Note: You can give me feedback by 👍 or 👎 this comment.

@jfversluis jfversluis transferred this issue from dotnet/maui Dec 3, 2024
@dotnet-policy-service dotnet-policy-service bot added the needs-triage Issues that need to be assigned. label Dec 3, 2024
@jpobst
Copy link
Contributor

jpobst commented Dec 3, 2024

It looks like the fixed issue you reference was fixed in July 2024, so it would likely only be in .NET 9 and not .NET 8.

#8961

Can you try on .NET 9 and see if it works there?

@jpobst jpobst added need-info Issues that need more information from the author. and removed needs-triage Issues that need to be assigned. labels Dec 3, 2024
@nk-alex
Copy link
Author

nk-alex commented Dec 4, 2024

Hi @jpobst, thanks for the answer.

I updated my project from .NET8 to .NET9 and now the certificate seems to be working as expected.

Since in .NET9 new X509Certificate2(certificate, password) is now obsolete: "Loading certificate data through the constructor or Import is obsolete. Use X509CertificateLoader instead to load certificates."

To load the certificate I had to use X509CertificateLoader.LoadPkcs12FromFile(certificate, password)

Just to know, are there any plans on implementing this solution also for .NET8?

@dotnet-policy-service dotnet-policy-service bot added need-attention A xamarin-android contributor needs to review and removed need-info Issues that need more information from the author. labels Dec 4, 2024
@jpobst
Copy link
Contributor

jpobst commented Dec 4, 2024

Just to know, are there any plans on implementing this solution also for .NET8?

I will defer to @simonrozsival and @jonpryor about the risk involved with backporting this to .NET 8.

@filipnavara
Copy link
Member

The implementation on .the .NET runtime side needed to add a Java library to handle the callbacks. This was rather big change that would be non-trivial to backport (especially due to the fact that it required changes across more repositories to account for the extra library).

@jpobst
Copy link
Contributor

jpobst commented Dec 20, 2024

I think this is our official-ish policy on .NET 8 now, so a backport isn't likely:

.NET 8 is "done" in that is will probably only get security fixes from now on. .NET 8 Android may/may not have another release.

@jpobst jpobst closed this as completed Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-attention A xamarin-android contributor needs to review
Projects
None yet
Development

No branches or pull requests

3 participants