Skip to content

Maui -Android certificate pinning using network-security-config ignore incorrect pin #107695

Closed

Description

@DmitryNikiforovCoherent

Description

Added ssl pinning using network-security-config

For test change one letter of base 64 pin, but SslClientAuthenticationOptions.RemoteCertificateValidationCallback still pass SslPolicyErrors.None

Steps to Reproduce

  • Added network_security_config.xml as AndroidResource <network-security-config> <domain-config> <domain includeSubdomains="true">{host}</domain> <pin-set expiration="2025-03-21"> <pin digest="SHA-256">pin</pin> </pin-set> </domain-config> </network-security-config>

  • Added SocketsHttpHandler with SslClientAuthenticationOptions.RemoteCertificateValidationCallback

  • I expect, that if I change base64 pin to incorrect, RemoteCertificateValidationCallback will be called with RemoteCertificateNameMismatch, as for IOS, but receive SslPolicyErrors.None

Link to public reproduction project repository

No response

Version with bug

8.0.70 SR7

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

Any android version

Did you find any workaround?

No response

Relevant log output

No response

Activity

  1. github-actions commented on Sep 6, 2024

    @github-actions
    Contributor

    Hi I'm an AI powered bot that finds similar issues based off the issue title.

    Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

    Open similar issues:

    Closed similar issues:

    Note: You can give me feedback by thumbs upping or thumbs downing this comment.

  2. QianaJiao commented on Sep 9, 2024

    @QianaJiao

    @DmitryNikiforovCoherent Could you share us a repo, I'd like to verify it.

  3. RobTF commented on Sep 9, 2024

    @RobTF

    We have the thumbprint in app config and use ServerCertificateCustomValidationCallback and that seems to work on all platforms.

    Is there a reason certificate pinning has been special cased into a separate config file etc.?

  4. DmitryNikiforovCoherent commented on Sep 9, 2024

    @DmitryNikiforovCoherent
    Author

    @QianaJiao try to prepare demo soon. Just one note:
    Trying 2 httpclient options

    1. HttpClientHandler with ServerCertificateCustomValidationCallback, any sha256 pin(even correct) throw RemoteCertificateChainErrors, for all chains(try first certificate, intermediate and CA)
    2. SocketsHttpHandler with SslClientAuthenticationOptions.RemoteCertificateValidationCallback always returns SslPolicyErrors.None, even if pin incorrect
      At the same time, ios works correctly for both cases
      Could you verify, maybe I do smth wrong?
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config>
            <domain includeSubdomains="true">{host}</domain>
            <pin-set>
                <pin digest="SHA-256">Base 64 pin</pin>
            </pin-set>
        </domain-config>
    </network-security-config>
    
    

    Base 64 pin I receive from ssllabs for my host
    image

  5. QianaJiao commented on Sep 10, 2024

    @QianaJiao

    Thank you for your reply. I will continue to investigate after I get your demo.
    In addition, have you noticed these 2 points?

    1. Ensure that the build action of the network_security_config.xml file is set to AndroidResource
      image
    2. Configure the networkSecurityConfig property on the application node in the Platforms\Android\AndroidManifest.xml file in your .NET MAUI app project

    Here are some documents that may also be useful to you
    https://learn.microsoft.com/en-us/dotnet/maui/data-cloud/local-web-services?view=net-maui-8.0#enable-clear-text-network-traffic-for-the-localhost-domain
    https://developer.android.google.cn/privacy-and-security/security-config

  6. DmitryNikiforovCoherent commented on Sep 10, 2024

    @DmitryNikiforovCoherent
    Author

    @QianaJiao
    Yes, Build action is AndroidResource, network_security_config is mentioned in AndroidManifest.
    Could you check this? HttpClientService contains both cases, with HttpClientHandler and SocketsHttpHandler
    network_security_config.xml contains correct and incorrect pin, please comment what you need
    I use api from https://api-ninjas.com/api/exercises, receive pins from https://www.ssllabs.com/ssltest/analyze.html?d=api.api%2dninjas.com&s=3.168.86.93&latest

    Archive.zip

  7. QianaJiao commented on Sep 11, 2024

    @QianaJiao

    I can repro this issue on both Android and iOS. No matter which pin I comment, it will return SslPolicyErrors.None.
    I'm not confident about my verification results. Could anyone help to take a look?

  8. DmitryNikiforovCoherent commented on Sep 11, 2024

    @DmitryNikiforovCoherent
    Author

    @QianaJiao Sorry, on ios I haven't added ssl pinning, just on android(As ios works correct for me, haven't touched in demo). On android have you checked both button clicks?
    image
    Socket Handler always works successfully, Http Handler always returns error

  9. jfversluis commented on Sep 11, 2024

    @jfversluis
    Member

    @jonathanpeppers is this Android or even runtime?

  10. jonathanpeppers commented on Sep 11, 2024

    @jonathanpeppers
    Member

    There is a bit of confusion on this thread, so I'm not sure I follow what the problem is. Is there a sample that displays a problem?

    One note:

    • Using AndroidMessageHandler (default), probably no RemoteCertificateValidationCallback would ever be called. It would be using the native Android http stack in that case.
    • Using SocketsHttpHandler (you'd have to set UseNativeHttpHandler=false in your .csproj), RemoteCertificateValidationCallback should get called

    @simonrozsival might be able to help, if we think there is a runtime issue here.

  11. transferred this issue fromdotnet/mauion Sep 11, 2024
  12. added
    untriaged
    on Sep 11, 2024
  13. dotnet-policy-service commented on Sep 11, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/ncl
    See info in area-owners.md if you want to be subscribed.

  14. 4 remaining items

  15. dotnet-policy-service commented on Sep 11, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-android': @vitek-karas, @simonrozsival, @steveisok, @akoeplinger
    See info in area-owners.md if you want to be subscribed.

  16. dotnet-policy-service commented on Sep 11, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones
    See info in area-owners.md if you want to be subscribed.

  17. DmitryNikiforovCoherent commented on Sep 12, 2024

    @DmitryNikiforovCoherent
    Author

    There is a bit of confusion on this thread, so I'm not sure I follow what the problem is. Is there a sample that displays a problem?

    One note:

    • Using AndroidMessageHandler (default), probably no RemoteCertificateValidationCallback would ever be called. It would be using the native Android http stack in that case.
    • Using SocketsHttpHandler (you'd have to set UseNativeHttpHandler=false in your .csproj), RemoteCertificateValidationCallback should get called

    @simonrozsival might be able to help, if we think there is a runtime issue here.

    In demo I used HttpClient Implementation: Managed(HttpClientHandler)
    image

    Both SslClientAuthenticationOptions.RemoteCertificateValidationCallback and HttpClientHandler.ServerCertificateCustomValidationCallback called, if make a request via them

    Demo attached in this comment
    #107695 (comment)

  18. simonrozsival commented on Sep 12, 2024

    @simonrozsival
    Member

    @DmitryNikiforovCoherent thanks for reporting this issue. I looked into it today and I think I have a rough idea where the problems in both the HttpClientHandler(internally implemented via AndroidMessageHandler) and SocketsHttpHandler originate.

    Currently the only workaround is to manually check the certificate hash in the ServerCertificateValidationCallback/RemoteCertificateValidationCallback instead of using network_security_config.xml. I would point out that Google doesn't recommend using certificate pinning in Android apps (see https://developer.android.com/privacy-and-security/security-ssl#Pinning).

  19. added 3 commits that reference this issue on Feb 9, 2026
    7f1bf1f
    6243d5b
    06ea804
  20. SidlerD123 commented on Jul 3, 2026

    @SidlerD123

    @simonrozsival We have switched from HttpClientHandler to SocketsHttpHandler in our App. After this change, we noticed that our network_security_config.xml does not seem to be respected anymore. From my (limited) understanding, the linked pull request would fix this. Is this correct?

  21. simonrozsival commented on Jul 11, 2026

    @simonrozsival
    Member

    @SidlerD123 yes, this PR has been merged and it will be part of the .NET 11 release.

  22. locked and limited conversation to collaborators on Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions