SSL Database Connectivity Issue Persisting on Android in Maui App

Sarfaraz Sable 30 Reputation points
2024-01-03T07:35:16.77+00:00

While using SSL for database connectivity in my Maui app on Windows, I successfully established a connection. However, upon attempting to run the same app on Android, I encountered an error.

MySqlConnector.MySqlException (0x80004005): SSL Authentication Error

---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.

---> Interop+AndroidCrypto+SslException: Exception of type 'Interop+AndroidCrypto+SslException' was thrown

Despite creating a network_security_config file with necessary tags, specifying the server's IP address, allowing cleartext traffic, and referencing required certificates in the AndroidManifest file, the error persists. I also installed the required certificate on the mobile device, yet the issue remains when running the Android app.

Developer technologies .NET .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Sarfaraz Sable 30 Reputation points
    2024-01-04T11:04:20.6166667+00:00

    To enable SSL (Secure Sockets Layer) for an Android app's database, a specific process needs to be followed. Here's a breakdown of the steps in a more comprehensive manner:

    Create the required folders and files:

    Start by creating a 'raw' folder in your app's resources if it doesn't exist already. Copy and paste the .pem file (containing the SSL certificate) into this newly created 'raw' folder. Develop the 'network_security_config.xml' file:

    Inside the 'xml' folder of your app's resources, create a file named 'network_security_config.xml.' Use the following XML structure within 'network_security_config.xml': xml Copy code

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">Domain Name/IP</domain>
            <trust-anchors>
                <certificates src="@raw/ca_cert"/> <!-- Referencing the certificate without an extension -->
            </trust-anchors>
        </base-config>
    </network-security-config>
    

    Replace 'Domain Name/IP' with the actual domain name or IP address the SSL certificate is for. Reference the certificate in the 'raw' folder using @raw/ca_cert, adjusting the filename accordingly. Install the certificate on the Android device:

    Access your Android device's settings. Navigate to the 'Security and privacy' section. Scroll down to 'More security and privacy' and choose 'Encryption & Credentials.' Select 'Install from storage' or 'Install a certificate' based on the device's interface. Opt for 'CA Certificate.' Confirm the installation by tapping 'Install anyway' and verify security using your fingerprint, PIN, or another method. Locate and select the downloaded certificate file. Additional Notes:

    During the certificate installation on the Android device, use the name that is mentioned within the certificate itself when prompted. This process establishes a secure connection for your Android app's database using SSL encryption, enhancing data security. By following these steps, you ensure that your Android app's database communication occurs over a secure SSL connection, maintaining data integrity and confidentiality.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.