July 31, 2010

mateuszzz88 mateuszzz88
Lab Rat
1 posts

how to set cert for QSqlDatabase connection with SSL?

 

Hello,

I am trying to connect to MySQL db over SSL, but I get an error: “SSL connection error QMYSQL: Unable to connect”
I followed procedure from here [dev.mysql.com] (example 1) and set up my.cnf, server-side appearrs to work ok:

  1. mysql> show variables like '%ssl%';
  2. +---------------+-----------------------------+
  3. | Variable_name | Value                       |
  4. +---------------+-----------------------------+
  5. | have_openssl  | YES                         |
  6. | have_ssl      | YES                         |
  7. | ssl_ca        | /root/certs/ca-cert.pem     |
  8. | ssl_capath    |                             |
  9. | ssl_cert      | /root/certs/server-cert.pem |
  10. | ssl_cipher    |                             |
  11. | ssl_key       | /root/certs/server-key.pem  |
  12. +---------------+-----------------------------+

I have “QT += core gui sql ssl network” in .pro and connect with following code:

  1.  QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","asterisk");
  2.     db.setHostName(iHostLineEdit->text());
  3.     db.setDatabaseName("asterisk");
  4.     db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");
  5.     db.setUserName(iUserLineEdit->text());
  6.     db.setPassword(iPassLineEdit->text());
  7.     bool ok = db.open();

My guess (based on this [dev.mysql.com] ) is that I have to set CA-cert.pem as certification authority cert. But how do I do that without any QSslSocket instance?

EDIT
in case: I work on windows with mingw; mysql connection without ssl works.

2 replies

August 1, 2010

harryF harryF
Lab Rat
33 posts

According to the MySQL docs, the mysql_ssl_set() function needs to be called before opening the connection.

I suggest that you create a feature request at bugreports.qt.nokia.com stating your problem and requesting additional connection options for setting SSL certificates.

As a workaround, you have to call mysql_ssl_set() manually before calling QSqlDatabase::open(). QSqlDriver::handle() returns a QVariant containing the MYSQL* connection pointer. Pseudo code:

  1. mysql_ssl_set(qvariant_cast<MYSQL *>(mySqlDatabase->driver->handle()), "...", ...

 Signature 

// happy hacking

August 2, 2010

mateuszzz88 mateuszzz88
Lab Rat
1 posts

farryF, thank you for your answer, I’ll try that API. If I succeed I’ll post reqest – for now I’m not sure I set up certs correctly. I can connect to mysqld over ssl from commandline only when specyfying all: ca-cert, client-cert and client-key.

 
  ‹‹ Incorrect CSS handling in Qt Svg module?      Is it possible to use Qt plugins - loaded via QPluginLoader - from QtScript side? How? ››

You must log in to post a reply. Not a member yet? Register here!