Results 1 to 5 of 5

Thread: QPSQL (PostgreSQL): checking servers certificate?

  1. #1
    Join Date
    Sep 2012
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default QPSQL (PostgreSQL): checking servers certificate?

    Hi,

    After trying out SQLite and MS SQL Server and figuring out the actual needs that my program has, I selected PostgreSQL to go forward with. I've got it (temporarily) installed on Windows and I managed to compile the QPSQL driver with help of some posts of this forum. The next step was to establish SSL secured connection to the data base from my app using following functions:

    Qt Code:
    1. db.setDatabaseName("test");
    2. db.setHostName("localhost");
    3. db.setUserName("tester");
    4. db.setPassword("****");
    5. db.setPort(5432);
    6. db.setConnectOptions("requiressl=1");
    7. if (!db.open()) {
    8. QMessageBox::critical(0, tr("Cannot open database"),
    9. tr("Unable to establish a database connection.\n"
    10. ), QMessageBox::Cancel);
    11. return;
    12. }
    To copy to clipboard, switch view to plain text mode 

    So far so good. Connection is established and SQL commands work as expected. However, I was wondering that how to check the servers certificates? Isn't there some function for that? I know that the connection is ciphered even without checking the certificate, but it would good also to know that my app is not connecting to some middle-man. I know checking the certificate is possible if one uses SSL Sockets, but what about with QPSQL?

    Second question: I have enabled the SSL by modifying the postgresql.conf file and as I said the connection succeeds with SSL. But how to force it to use SSL in the server side? Now it accepts both connection requests using SSL and those that are not using SSL. Not sure if this is important, because in my app I'll use the "requiressl=1" option anyway and it does not matter whether some unwanted connection attempts use SSL or not - I think...

    Cheers!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QPSQL (PostgreSQL): checking servers certificate?

    If the server certificate is not valid, i.e. cannot be verified against a CA trusted by the client, an SSL connection will not be established in the first place. If you want to test this try giving the server a self-signed certificate: unless you tell the client to trust that certificate as a CA a connection attempt will fail.

    AFAICT limiting server connections to SSL only is done with an entry in pg_hba.conf like:
    Qt Code:
    1. // For any user from any host trying to access your DB via TCP/IP
    2. hostnossl yourDBName all all reject
    3. // or for all databases
    4. hostnossl all all all reject
    To copy to clipboard, switch view to plain text mode 
    See section 19.1 of the PostgreSQL manual for (much) more

  3. #3
    Join Date
    Sep 2012
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: QPSQL (PostgreSQL): checking servers certificate?

    Thanks! I got the connections now to be limited to support only SSL, other connection requests are rejected.

    About the certificate. I'm actually using at this point a self-signed certificate created by myself. With SSL Socket the same certificate is not accepted unless I use ptr_socket->ignoreSslErrors(); but now I have not done anything else than the lines I added into the first post. I would expect failure in the db.open().

    According to this: http://www.postgresql.org/docs/devel...libpq-ssl.html , I understand I should somehow get the sslmode parameter set to "verify-ca". Is this even possible with QPSQL?
    Last edited by Mobility; 31st January 2013 at 05:25.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QPSQL (PostgreSQL): checking servers certificate?

    Try:
    Qt Code:
    1. db.setConnectOptions("sslmode=verify-ca");
    2. // or
    3. db.setConnectOptions("sslmode=verify-full");
    To copy to clipboard, switch view to plain text mode 
    before you open the connection (requiressl is deprecated according to your manual). Regardless of the Qt docs listing a small set of "supported" options, these values are simply passed to the underlying database client as-is.

  5. #5
    Join Date
    Sep 2012
    Posts
    34
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: QPSQL (PostgreSQL): checking servers certificate?

    Yes, got it working now. Difference between requiressl=1 and sslmode=verify-ca is that with the first one you don't need root certificate file but if it exists, it will behave similar than sslmode=verify-ca.

    Thanks for you time!

Similar Threads

  1. Replies: 1
    Last Post: 29th October 2012, 07:25
  2. Replies: 3
    Last Post: 17th October 2012, 09:30
  3. QPSQL + QT4 (Postgresql driver bug)
    By l2succes in forum Qt Programming
    Replies: 8
    Last Post: 12th March 2011, 23:52
  4. Replies: 2
    Last Post: 11th February 2011, 17:53
  5. Rack Mounted Servers...
    By CuCullin in forum General Discussion
    Replies: 2
    Last Post: 20th January 2006, 22:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.