~ubuntu-branches/ubuntu/oneiric/psi/oneiric

« back to all changes in this revision

Viewing changes to src/sslfilter.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2005-01-10 17:41:43 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050110174143-ltocv5zapl6blf5d
Tags: 0.9.3-1
* New upstream release
* Cleaned up debian/rules (some things are done by upstream Makefiles now)
* Fixed some lintian warnings:
  - removed executable bit from some .png files
  - moved psi.desktop to /usr/share/applications
* Updated menu files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
** sslfilter.h - simple OpenSSL encryption I/O
3
 
** Copyright (C) 2001, 2002  Justin Karneges
4
 
**
5
 
** This program is free software; you can redistribute it and/or
6
 
** modify it under the terms of the GNU General Public License
7
 
** as published by the Free Software Foundation; either version 2
8
 
** of the License, or (at your option) any later version.
9
 
**
10
 
** This program is distributed in the hope that it will be useful,
11
 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
** GNU General Public License for more details.
14
 
**
15
 
** You should have received a copy of the GNU General Public License
16
 
** along with this program; if not, write to the Free Software
17
 
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 
**
19
 
****************************************************************************/
20
 
 
21
 
#ifndef SSLFILTER_H
22
 
#define SSLFILTER_H
23
 
 
24
 
#include<qobject.h>
25
 
#include<qcstring.h>
26
 
#include<qlibrary.h>
27
 
 
28
 
 
29
 
/****************************************************************************
30
 
  SSLFilter
31
 
 
32
 
  This class handles an OpenSSL session.  It works independent of fds and
33
 
  sockets.  Pass incoming socket data to it via putIncomingSSLData().  When
34
 
  OpenSSL wants to send data out the channel, outgoingSSLDataReady() will
35
 
  be emitted and you can get the data to be sent with getOutgoingSSLData().
36
 
 
37
 
  Once you have these hooks set up, the rest is easy.  Use send() to encrypt
38
 
  data and use recv() to retrieve unencrypted data.
39
 
 
40
 
  When you call send(), the data is encrypted and this class will then signal
41
 
  for you to do the actual sending (as described above).
42
 
 
43
 
  HOWTO:
44
 
 
45
 
  Initialize:
46
 
 
47
 
    SSLFilter ssl;
48
 
 
49
 
    connect(&ssl, SIGNAL(readyRead()), SLOT(ssl_readyRead()));
50
 
    connect(&ssl, SIGNAL(outgoingSSLDataReady()), SLOT(ssl_outgoingReady()));
51
 
 
52
 
  When you are ready to begin negotiation, use begin().  You would probably
53
 
  always do this immediately after you establish a socket connection.  The
54
 
  negotiation happens transparently, and if you send data before the SSL
55
 
  channel is ready then it will be queued.  So it is OK to do something
56
 
  like this:
57
 
 
58
 
    // slot that gets called when QSocket emits connected()
59
 
    void sock_connected()
60
 
    {
61
 
      if(!ssl.begin())
62
 
        return; // error initializing ssl
63
 
 
64
 
      // login (or something)
65
 
      sock->send(array.data(), array.size());
66
 
    }
67
 
 
68
 
  Receiving data requires one extra step than before.  Instead of
69
 
  processing the socket data from your readyRead slot, you need to pass
70
 
  the data on to SSLFilter.  SSLFilter will then emit its own readyRead for
71
 
  real data that you can process.
72
 
 
73
 
    // slot that gets called when QSocket emits readyRead()
74
 
    void sock_readyRead()
75
 
    {
76
 
      QByteArray buf;
77
 
      int size = sock->bytesAvailable();
78
 
      buf.resize(size);
79
 
      sock->readBlock(buf.data(), size);
80
 
      ssl.putIncomingSSLData(buf);
81
 
    }
82
 
 
83
 
    // slot that gets called when SSLFilter emits readyRead()
84
 
    void ssl_readyRead()
85
 
    {
86
 
      QByteArray buf = ssl->recv();
87
 
 
88
 
      // do something
89
 
      processIncomingData(buf);
90
 
    }
91
 
 
92
 
  Often, SSLFilter will want to send data.  This will happen generally
93
 
  after you call SSLFilter::send(), but OpenSSL may have other reasons
94
 
  sometimes.  It will emit outgoingSSLDataReady() when data is ready
95
 
  to be sent.  Simple!
96
 
 
97
 
    void ssl_outgoingReady()
98
 
    {
99
 
      QByteArray buf = ssl->getOutgoingSSLData();
100
 
      sock->writeBlock(buf.data(), buf.size());
101
 
    }
102
 
 
103
 
  To send data, use SSLFilter::send().  SSLFilter will perform the
104
 
  encryption, and signal you to send the encrypted data when ready.
105
 
 
106
 
    QByteArray buf;
107
 
    ssl.send(buf);
108
 
 
109
 
  The OpenSSL library is used dynamically during runtime, and is thus not
110
 
  required to compile or run your program.  Be sure to use the isSupported()
111
 
  function to make sure that the library was loaded properly.
112
 
 
113
 
  On error, SSLFilter will emit error() and reset the class.
114
 
 
115
 
****************************************************************************/
116
 
 
117
 
class SSLFilter : public QObject
118
 
{
119
 
        Q_OBJECT
120
 
 
121
 
public:
122
 
        SSLFilter();
123
 
        ~SSLFilter();
124
 
 
125
 
        bool isSupported();
126
 
        void reset();
127
 
        bool begin();
128
 
 
129
 
        // send data
130
 
        void send(const QByteArray &);
131
 
        // check/recv data
132
 
        bool isRecvData();
133
 
        QByteArray recv();
134
 
 
135
 
        // pass incoming socket data to this function
136
 
        void putIncomingSSLData(const QByteArray &);
137
 
        // check/read outgoing socket data with this function
138
 
        bool isOutgoingSSLData();
139
 
        QByteArray getOutgoingSSLData();
140
 
 
141
 
signals:
142
 
        void readyRead();
143
 
        void outgoingSSLDataReady();
144
 
        void error();
145
 
 
146
 
private slots:
147
 
        void sslUpdate();
148
 
 
149
 
private:
150
 
        enum { SSL_CONNECT, SSL_ACTIVE };
151
 
        int sslAction;
152
 
 
153
 
        bool supported;
154
 
        void *dp;
155
 
 
156
 
        QByteArray sendQueue, recvQueue;
157
 
        void processSendQueue();
158
 
        void sslReadAll();
159
 
        void doError();
160
 
};
161
 
 
162
 
#endif