Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Testing HTTPS with Android Emulator and local API server
Qt 6.11 is out! See what's new in the release blog

Testing HTTPS with Android Emulator and local API server

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
1 Posts 1 Posters 881 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gween
    wrote on last edited by
    #1

    I'm trying to test my API server locally with HTTPS using self signed certificates. When I try:

    request.setUrl(QUrl("https://10.0.2.2:8999/test/"));
    nAccessMang->post(request, p.query().toUtf8());
    

    I get the error:

    "SSL handshake failed: The host name did not match any of the valid hosts for this certificate"
    

    Noting that making an https get request in a similar way works just fine:

    request.setUrl(QUrl("https://www.google.com"));
    nAccessMang->get(request);
    

    Also, http works without errors when making the request to my local API server.

    request.setUrl(QUrl("http://10.0.2.2:8999/test/"));
    nAccessMang->post(request, p.query().toUtf8());
    

    Here are the steps I took to get the above error:

    1. First try this on its own (just adding https to my request):
    request.setUrl(QUrl("https://10.0.2.2:8999/test/"));
    ... # get and set parameters from form.
    nAccessMang->post(request, p.query().toUtf8());
    
    1. Figure I need to add a certificate. So I created a self signed certificate:
    openssl genrsa 2048 > key.key
    chmod 400 key.key
    openssl req -new -x509 -nodes -sha256 -days 365 -key key.key -subj "/CN=localhost" -out crt.crt
    # I have also tried the Android Emulator's IP for the host machine as the CN:
    # openssl req -new -x509 -nodes -sha256 -days 365 -key key.key -subj "/CN=10.0.2.2" -out crt.crt
    

    I have also tried using the following config when creating the certificate:

    # ssl.cnf
    [req]
    req_extensions = v3_req
    distinguished_name = req_distinguished_name
    prompt = no
    [req_distinguished_name]
    C = US
    ST = CA
    L = City
    O = Test
    OU = Division
    CN = Test.com # tried 10.0.2.2 here as well.
    [ v3_req ]
    basicConstraints = CA:FALSE
    keyUsage = nonRepudiation
    subjectAltName = @alt_names
    [alt_names]
    DNS.1 = localhost
    DNS.2 = test_server
    IP.1 = 127.0.0.1
    IP.2 = 10.0.2.2
    
    # then
    
    openssl req -new -x509 -nodes -sha256 -days 365 -key key.key -out crt.crt -config ssl.cnf
    
    1. Add cert to qrc file

    2. Add to code

        QSslConfiguration config = QSslConfiguration();
        config.setProtocol(QSsl::TlsV1_2OrLater);
    
        QFile file(":/ssl/crt.crt");
        file.open(QIODevice::ReadOnly);
        const QByteArray bytes = file.readAll();
        file.close();
    
        const QSslCertificate certificate(bytes);
        QList<QSslCertificate> certs(1);
        certs.append(certificate);
        config.addCaCertificates(certs);
    
        request.setSslConfiguration(config);
    
        request.setUrl(QUrl("https://10.0.2.2:8999/test/"));
        ... # get and set parameters from form.
        nAccessMang->post(request, p.query().toUtf8());
    

    What am I missing to successfully make this https request to my local API server for testing?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved