summaryrefslogtreecommitdiffstats
path: root/src/net/SslServer.cpp
blob: 9940a61a8cc12223feec0190c33e6c7df3575177 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
# Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# src/net/SslServer.cpp
#    - provide QTcpServer-like behaviour for SSL
# -----------------------------------------------------------------------------
*/

#include "SslServer.h"
#include <QtNetwork/QSslCipher>
#include <QtNetwork/QSslSocket>
#include "src/util/CertManager.h"


SslServer::SslServer()
{
    _tmr = startTimer(7000);
    //QSslSocket::setDefaultCiphers(QSslSocket::supportedCiphers());
}

SslServer::~SslServer()
{
    killTimer((_tmr));
}

void SslServer::incomingConnection(int socketDescriptor)
{
    QSslSocket *serverSocket = new QSslSocket(this);
    connect(serverSocket,
    		SIGNAL(sslErrors(const QList<QSslError> &)),
    		this,
    		SLOT(sslErrors(const QList<QSslError> &))
    		);
    serverSocket->setPrivateKey(CertManager::getPrivateKey("manager"));
    serverSocket->setLocalCertificate(CertManager::getCertificate("manager"));
    serverSocket->setPeerVerifyMode(QSslSocket::VerifyNone);
    serverSocket->setProtocol(QSsl::SslV3);
    //printf("Keylen %d\n", serverSocket->privateKey().length());
    if (serverSocket->setSocketDescriptor(socketDescriptor))
    {
    	// Once the connection is successfully encrypted, raise out newConnection event
        connect(serverSocket, SIGNAL(encrypted()), this, SIGNAL(newConnection()));
        serverSocket->startServerEncryption();
        _pending.push_back(serverSocket);
    }
    else
    {
        delete serverSocket;
    }
}

void SslServer :: sslErrors ( const QList<QSslError> & errors )
{
	qDebug("FIXME: SSL ERRORS on SERVER: %s", qPrintable(errors.begin()->errorString()));
}

void SslServer::timerEvent (QTimerEvent* event)
{
    // Remove all sockets marked for deletion
    for (QList<QSslSocket*>::iterator it = _delete.begin(); it != _delete.end(); it++)
    {
        (*it)->deleteLater();
    }
    _delete.clear();
    // Mark all sockets for deletion where the ssl handshake did not happen after connecting
    /*
    bool flag;
    do
    {
        flag = false;
        for (QList<QSslSocket*>::iterator it = _pending.begin(); it != _pending.end(); it++)
        {
            if ((*it)->state() != QAbstractSocket::ConnectedState || !(*it)->isEncrypted())
            {
                _delete.push_back(*it);
                _pending.removeAll(*it);
                flag = true;
                break;
            }

        }
    }
    while (flag);
    */
    _delete.append(_pending);
    _pending.clear();
}

bool SslServer::hasPendingConnections()
{
    for (QList<QSslSocket*>::iterator it = _pending.begin(); it != _pending.end(); it++)
    {
    	qDebug("State: %d - Encrypted: %d", (int)(*it)->state(), (*it)->isEncrypted());
        if ((*it)->state() == QAbstractSocket::ConnectedState && (*it)->isEncrypted()) return true;
    }
    return false;
}

QTcpSocket* SslServer::nextPendingConnection()
{
    for (QList<QSslSocket*>::iterator it = _pending.begin(); it != _pending.end(); it++)
    {
        if ((*it)->state() == QAbstractSocket::ConnectedState && (*it)->isEncrypted())
        {
            _pending.removeAll(*it);
            _delete.removeAll(*it);
            return *it;
        }
    }
    for (QList<QSslSocket*>::iterator it = _delete.begin(); it != _delete.end(); it++)
    {
        if ((*it)->state() == QAbstractSocket::ConnectedState && (*it)->isEncrypted())
        {
            _pending.removeAll(*it);
            _delete.removeAll(*it);
            return *it;
        }
    }
    return NULL;
}