Skip to content
This repository has been archived by the owner on Dec 21, 2018. It is now read-only.

Commit

Permalink
Allow to specify the SSL protocol for a requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitallium committed Oct 24, 2012
1 parent 4f17d94 commit 419f997
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static const struct QCommandLineConfigEntry flags[] =
{ QCommandLine::Option, '\0', "proxy-type", "Specifies the proxy type, 'http' (default), 'none' (disable completely), or 'socks5'", QCommandLine::Optional },
{ QCommandLine::Option, '\0', "script-encoding", "Sets the encoding used for the starting script, default is 'utf8'", QCommandLine::Optional },
{ QCommandLine::Option, '\0', "web-security", "Enables web security, 'yes' (default) or 'no'", QCommandLine::Optional },
{ QCommandLine::Option, '\0', "ssl-protocol", "Sets the SSL protocol (supported protocols: 'SSLv3', 'SSLv2', 'TLSv1', 'TlsV1SslV3' (default))", QCommandLine::Optional },

This comment has been minimized.

Copy link
@detro

detro Oct 24, 2012

I think 'TlsV1SslV3' (default) should just read "any".
QSsl::SslProtocol obviously does support that: http://doc.qt.digia.com/qt/qssl.html#SslProtocol-enum

This comment has been minimized.

Copy link
@detro

detro Oct 24, 2012

Also, as mentioned below, SSLv3 should be the default.
Qt has that as default because it's the safest one.

{ QCommandLine::Param, '\0', "script", "Script", QCommandLine::Flags(QCommandLine::Optional|QCommandLine::ParameterFence)},
{ QCommandLine::Param, '\0', "argument", "Script argument", QCommandLine::OptionalMultiple },
{ QCommandLine::Switch, 'h', "help", "Shows this message and quits", QCommandLine::Optional },
Expand Down Expand Up @@ -593,6 +594,9 @@ void Config::handleOption(const QString &option, const QVariant &value)
if (option == "web-security") {
setWebSecurityEnabled(boolValue);
}
if (option == "ssl-protocol") {
setSslProtocol(value.toString().toLower());
}
}

void Config::handleParam(const QString& param, const QVariant &value)
Expand All @@ -610,3 +614,12 @@ void Config::handleError(const QString &error)
setUnknownOption(QString("Error: %1").arg(error));
}

QString Config::sslProtocol() const
{
return m_sslProtocol;
}

void Config::setSslProtocol(const QString& sslProtocolName)
{
m_sslProtocol = sslProtocolName;
}
5 changes: 5 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Config: QObject
Q_PROPERTY(bool printDebugMessages READ printDebugMessages WRITE setPrintDebugMessages)
Q_PROPERTY(bool javascriptCanOpenWindows READ javascriptCanOpenWindows WRITE setJavascriptCanOpenWindows)
Q_PROPERTY(bool javascriptCanCloseWindows READ javascriptCanCloseWindows WRITE setJavascriptCanCloseWindows)
Q_PROPERTY(QString sslProtocol READ sslProtocol WRITE setSslProtocol)

public:
Config(QObject *parent = 0);
Expand Down Expand Up @@ -148,6 +149,9 @@ class Config: QObject
void setJavascriptCanCloseWindows(const bool value);
bool javascriptCanCloseWindows() const;

void setSslProtocol(const QString& sslProtocolName);
QString sslProtocol() const;

public slots:
void handleSwitch(const QString &sw);
void handleOption(const QString &option, const QVariant &value);
Expand Down Expand Up @@ -191,6 +195,7 @@ public slots:
bool m_printDebugMessages;
bool m_javascriptCanOpenWindows;
bool m_javascriptCanCloseWindows;
QString m_sslProtocol;
};

#endif // CONFIG_H
29 changes: 26 additions & 3 deletions src/networkaccessmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ NetworkAccessManager::NetworkAccessManager(QObject *parent, const Config *config
, m_ignoreSslErrors(config->ignoreSslErrors())
, m_idCounter(0)
, m_networkDiskCache(0)
, m_sslConfiguration(QSslConfiguration::defaultConfiguration())
{
setCookieJar(CookieJar::instance());

Expand All @@ -84,6 +85,17 @@ NetworkAccessManager::NetworkAccessManager(QObject *parent, const Config *config
setCache(m_networkDiskCache);
}

if (QSslSocket::supportsSsl()) {
m_sslConfiguration = QSslConfiguration::defaultConfiguration();

This comment has been minimized.

Copy link
@detro

detro Oct 24, 2012

This is Ssl3. And THIS should be our default configuration.

The "any" mentioned above (that you named "TlsV1Ssl3" or something like that SHOULDN'T be the default one). Plus, it's not really implemented here.

if (config->sslProtocol() == "sslv3") {

This comment has been minimized.

Copy link
@detro

detro Oct 24, 2012

Suggestion: normalize the result of the sslProtocol() call toLower(). This way a user typing mistake will still work.
For example: sslv3, Sslv3 or SSLv3 should all be acceptable ;)

This comment has been minimized.

Copy link
@detro

detro Oct 24, 2012

Of course this is valid for the other if cases here.

m_sslConfiguration.setProtocol(QSsl::SslV3);
} else if (config->sslProtocol() == "sslv2") {
m_sslConfiguration.setProtocol(QSsl::SslV2);
} else if (config->sslProtocol() == "tlsv1") {
m_sslConfiguration.setProtocol(QSsl::TlsV1);
}
}

connect(this, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
connect(this, SIGNAL(finished(QNetworkReply*)), SLOT(handleFinished(QNetworkReply*)));
}
Expand Down Expand Up @@ -126,6 +138,8 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR
if (!QSslSocket::supportsSsl()) {
if (req.url().scheme().toLower() == QLatin1String("https"))
qWarning() << "Request using https scheme without SSL support";
} else {
req.setSslConfiguration(m_sslConfiguration);
}

// Get the URL string before calling the superclass. Seems to work around
Expand All @@ -149,9 +163,6 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR

// Pass duty to the superclass - Nothing special to do here (yet?)
QNetworkReply *reply = QNetworkAccessManager::createRequest(op, req, outgoingData);
if(m_ignoreSslErrors) {
reply->ignoreSslErrors();
}

QVariantList headers;
foreach (QByteArray headerName, req.rawHeaderList()) {
Expand All @@ -172,6 +183,7 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR
data["time"] = QDateTime::currentDateTime();

connect(reply, SIGNAL(readyRead()), this, SLOT(handleStarted()));
connect(reply, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(handleSslErrors(const QList<QSslError> &)));

emit resourceRequested(data);
return reply;
Expand Down Expand Up @@ -243,3 +255,14 @@ void NetworkAccessManager::provideAuthentication(QNetworkReply *reply, QAuthenti
authenticator->setUser(m_userName);
authenticator->setPassword(m_password);
}

void NetworkAccessManager::handleSslErrors(const QList<QSslError> &errors)
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
foreach (QSslError e, errors) {
qDebug()<<"Network - SSL Error:" << e;
}

if (m_ignoreSslErrors)
reply->ignoreSslErrors();
}
4 changes: 4 additions & 0 deletions src/networkaccessmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSet>
#include <QSslConfiguration>

class Config;
class QNetworkDiskCache;
class QSslConfiguration;

class NetworkAccessManager : public QNetworkAccessManager
{
Expand Down Expand Up @@ -66,13 +68,15 @@ private slots:
void handleStarted();
void handleFinished(QNetworkReply *reply);
void provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator);
void handleSslErrors(const QList<QSslError> &errors);

private:
QHash<QNetworkReply*, int> m_ids;
QSet<QNetworkReply*> m_started;
int m_idCounter;
QNetworkDiskCache* m_networkDiskCache;
QVariantMap m_customHeaders;
QSslConfiguration m_sslConfiguration;
};

#endif // NETWORKACCESSMANAGER_H

0 comments on commit 419f997

Please sign in to comment.