~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to third-party/qca/qca/include/QtCrypto/qca_securelayer.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * qca_securelayer.h - Qt Cryptographic Architecture
 
3
 * Copyright (C) 2003-2007  Justin Karneges <justin@affinix.com>
 
4
 * Copyright (C) 2004-2006  Brad Hards <bradh@frogmouth.net>
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
19
 *
 
20
 */
 
21
 
 
22
/**
 
23
   \file qca_securelayer.h
 
24
 
 
25
   Header file for SecureLayer and its subclasses
 
26
 
 
27
   \note You should not use this header directly from an
 
28
   application. You should just use <tt> \#include \<QtCrypto>
 
29
   </tt> instead.
 
30
*/
 
31
#ifndef QCA_SECURELAYER_H
 
32
#define QCA_SECURELAYER_H
 
33
 
 
34
#include <QObject>
 
35
#include "qca_core.h"
 
36
#include "qca_publickey.h"
 
37
#include "qca_cert.h"
 
38
 
 
39
namespace QCA {
 
40
 
 
41
/**
 
42
   Specify the lower-bound for acceptable TLS/SASL security layers
 
43
 
 
44
   For TLS, the interpretation of these levels is:
 
45
   - Any cipher suite that provides non-authenticated communications
 
46
   (usually anonymous Diffie-Hellman) is SL_Integrity. 
 
47
   - Any cipher suite that is limited to 40 bits (export-version
 
48
   crippled forms of RC2, RC4 or DES) is SL_Export. Standard
 
49
   DES (56 bits) and some forms of RC4 (64 bits) are also SL_Export.
 
50
   - Any normal cipher (AES, Camellia, RC4 or similar) with 128 bits, or
 
51
   Elliptic Curve Ciphers with 283 bits, is SL_Baseline
 
52
   - AES or Camellia at least 192 bits, triple-DES and similar
 
53
   ciphers are SL_High.  ECC with 409 or more bits is also SL_High. 
 
54
   - Highest does not have an equivalent strength. It
 
55
   indicates that the provider should use the strongest
 
56
   ciphers available (but not less than SL_High). 
 
57
 */
 
58
enum SecurityLevel
 
59
{
 
60
        SL_None,      ///< indicates that no security is ok
 
61
        SL_Integrity, ///< must at least get integrity protection
 
62
        SL_Export,    ///< must be export level bits or more
 
63
        SL_Baseline,  ///< must be 128 bit or more
 
64
        SL_High,      ///< must be more than 128 bit
 
65
        SL_Highest    ///< SL_High or max possible, whichever is greater
 
66
};
 
67
 
 
68
/**
 
69
   \class SecureLayer qca_securelayer.h QtCrypto
 
70
 
 
71
   Abstract interface to a security layer
 
72
 
 
73
   SecureLayer is normally used between an application and a
 
74
   potentially insecure network. It provides secure
 
75
   communications over that network.
 
76
 
 
77
   The concept is that (after some initial setup), the
 
78
   application can write() some data to the SecureLayer
 
79
   implementation, and that data is encrypted (or otherwise
 
80
   protected, depending on the setup). The SecureLayer
 
81
   implementation then emits the readyReadOutgoing() signal,
 
82
   and the application uses readOutgoing() to retrieve the the
 
83
   encrypted data from the SecureLayer implementation.  The
 
84
   encrypted data is then sent out on the network.
 
85
 
 
86
   When some encrypted data comes back from the network, the
 
87
   application does a writeIncoming() to the SecureLayer
 
88
   implementation. Some time later, the SecureLayer
 
89
   implementation may emit readyRead() to the application,
 
90
   which then read()s the decrypted data from the SecureLayer
 
91
   implementation.
 
92
 
 
93
   Note that sometimes data is sent or received between the
 
94
   SecureLayer implementation and the network without any data
 
95
   being sent between the application and the SecureLayer
 
96
   implementation. This is a result of the initial negotiation
 
97
   activities (which require network traffic to agree a
 
98
   configuration to use) and other overheads associated with
 
99
   the secure link.
 
100
 
 
101
   \ingroup UserAPI
 
102
*/
 
103
class QCA_EXPORT SecureLayer : public QObject
 
104
{
 
105
        Q_OBJECT
 
106
public:
 
107
        /**
 
108
           Constructor for an abstract secure communications
 
109
           layer
 
110
 
 
111
           \param parent the parent object for this object
 
112
        */
 
113
        SecureLayer(QObject *parent = 0);
 
114
 
 
115
        /**
 
116
           Returns true if the layer has a meaningful "close".
 
117
        */
 
118
        virtual bool isClosable() const;
 
119
 
 
120
        /**
 
121
           Returns the number of bytes available to be read()
 
122
           on the application side.
 
123
        */
 
124
        virtual int bytesAvailable() const = 0;
 
125
 
 
126
        /**
 
127
           Returns the number of bytes available to be
 
128
           readOutgoing() on the network side.
 
129
        */
 
130
        virtual int bytesOutgoingAvailable() const = 0;
 
131
 
 
132
        /**
 
133
           Close the link. Note that this may not be
 
134
           meaningful / possible for all implementations. 
 
135
 
 
136
           \sa isClosable() for a test that verifies if the
 
137
           link can be %closed.
 
138
        */
 
139
        virtual void close();
 
140
 
 
141
        /**
 
142
           This method writes unencrypted (plain) data to
 
143
           the SecureLayer implementation. You normally
 
144
           call this function on the application side.
 
145
 
 
146
           \param a the source of the application-side data
 
147
        */
 
148
        virtual void write(const QByteArray &a) = 0;
 
149
 
 
150
        /**
 
151
           This method reads decrypted (plain) data from
 
152
           the SecureLayer implementation. You normally call
 
153
           this function on the application side after receiving
 
154
           the readyRead() signal.
 
155
        */
 
156
        virtual QByteArray read() = 0;
 
157
 
 
158
        /**
 
159
           This method accepts encoded (typically encrypted) data
 
160
           for processing. You normally call this function using
 
161
           data read from the network socket (e.g. using 
 
162
           QTcpSocket::readAll()) after receiving a signal that
 
163
           indicates that the socket has data to read.
 
164
 
 
165
           \param a the ByteArray to take network-side data from
 
166
        */
 
167
        virtual void writeIncoming(const QByteArray &a) = 0;
 
168
 
 
169
        /**
 
170
           This method provides encoded (typically encrypted)
 
171
           data. You normally call this function to get data
 
172
           to write out to the network socket (e.g. using
 
173
           QTcpSocket::write()) after receiving the
 
174
           readyReadOutgoing() signal.
 
175
 
 
176
           \param plainBytes the number of bytes that were read.
 
177
        */
 
178
        virtual QByteArray readOutgoing(int *plainBytes = 0) = 0;
 
179
 
 
180
        /**
 
181
           This allows you to read data without having it
 
182
           decrypted first. This is intended to be used for
 
183
           protocols that close off the connection and return
 
184
           to plain text transfer. You do not normally need to
 
185
           use this function.
 
186
        */
 
187
        virtual QByteArray readUnprocessed();
 
188
 
 
189
        /**
 
190
           Convert encrypted bytes written to plain text bytes written
 
191
 
 
192
           \param encryptedBytes the number of bytes to convert
 
193
        */
 
194
        virtual int convertBytesWritten(qint64 encryptedBytes) = 0;
 
195
 
 
196
Q_SIGNALS:
 
197
        /**
 
198
           This signal is emitted when SecureLayer has
 
199
           decrypted (application side) data ready to be
 
200
           read. Typically you will connect this signal to a
 
201
           slot that reads the data (using read()).
 
202
        */
 
203
        void readyRead();
 
204
 
 
205
        /**
 
206
           This signal is emitted when SecureLayer has encrypted
 
207
           (network side) data ready to be read. Typically you
 
208
           will connect this signal to a slot that reads the data
 
209
           (using readOutgoing()) and writes it to a network socket.
 
210
        */
 
211
        void readyReadOutgoing();
 
212
 
 
213
        /**
 
214
           This signal is emitted when the SecureLayer connection
 
215
           is %closed.
 
216
        */
 
217
        void closed();
 
218
 
 
219
        /**
 
220
           This signal is emitted when an error is detected. You
 
221
           can determine the error type using errorCode().
 
222
        */
 
223
        void error();
 
224
 
 
225
private:
 
226
        Q_DISABLE_COPY(SecureLayer)
 
227
};
 
228
 
 
229
/**
 
230
   \class TLSSession qca_securelayer.h QtCrypto
 
231
 
 
232
   Session token, used for TLS resuming
 
233
 
 
234
   \ingroup UserAPI
 
235
 
 
236
*/
 
237
class QCA_EXPORT TLSSession : public Algorithm
 
238
{
 
239
public:
 
240
        TLSSession();
 
241
 
 
242
        /**
 
243
           Copy constructor
 
244
 
 
245
           \param from the session token to copy from
 
246
        */
 
247
        TLSSession(const TLSSession &from);
 
248
 
 
249
        ~TLSSession();
 
250
 
 
251
        /**
 
252
           Assignment operator
 
253
 
 
254
           \param from the session token to assign from
 
255
        */
 
256
        TLSSession & operator=(const TLSSession &from);
 
257
 
 
258
        /**
 
259
           Test if the session token is valid
 
260
        */
 
261
        bool isNull() const;
 
262
};
 
263
 
 
264
/**
 
265
   \class TLS qca_securelayer.h QtCrypto
 
266
 
 
267
   Transport Layer Security / Secure Socket Layer 
 
268
 
 
269
   Transport Layer Security (%TLS) is the current
 
270
   state-of-the-art in secure transport mechanisms over the
 
271
   internet. It can be used in a way where only one side of
 
272
   the link needs to authenticate to the other. This makes it
 
273
   very useful for servers to provide their identity to
 
274
   clients. Note that is is possible to use %TLS to
 
275
   authenticate both client and server.
 
276
 
 
277
   %TLS is a IETF standard (<a
 
278
   href="http://www.ietf.org/rfc/rfc2712.txt">RFC2712</a> for
 
279
   TLS version 1.0) based on earlier Netscape work on Secure
 
280
   Socket Layer (SSL version 2 and SSL version 3). New
 
281
   applications should use at least TLS 1.0, and SSL version 2
 
282
   should be avoided due to known security problems.
 
283
 
 
284
   \ingroup UserAPI
 
285
*/
 
286
class QCA_EXPORT TLS : public SecureLayer, public Algorithm
 
287
{
 
288
        Q_OBJECT
 
289
public:
 
290
        /**
 
291
           Operating mode
 
292
        */
 
293
        enum Mode
 
294
        {
 
295
                Stream,  ///< stream mode
 
296
                Datagram ///< datagram mode
 
297
        };
 
298
 
 
299
        /**
 
300
           Version of %TLS or SSL
 
301
        */
 
302
        enum Version
 
303
        {
 
304
                TLS_v1, ///< Transport Layer Security, version 1
 
305
                SSL_v3, ///< Secure Socket Layer, version 3
 
306
                SSL_v2, ///< Secure Socket Layer, version 2
 
307
                DTLS_v1 ///< Datagram Transport Layer Security, version 1
 
308
        };
 
309
 
 
310
        /**
 
311
           Type of error
 
312
        */
 
313
        enum Error
 
314
        {
 
315
                ErrorSignerExpired,   ///< local certificate is expired
 
316
                ErrorSignerInvalid,   ///< local certificate is invalid in some way
 
317
                ErrorCertKeyMismatch, ///< certificate and private key don't match
 
318
                ErrorInit,            ///< problem starting up %TLS
 
319
                ErrorHandshake,       ///< problem during the negotiation
 
320
                ErrorCrypt            ///< problem at anytime after
 
321
        };
 
322
 
 
323
        /**
 
324
           Type of identity
 
325
        */
 
326
        enum IdentityResult
 
327
        {
 
328
                Valid,              ///< identity is verified
 
329
                HostMismatch,       ///< valid cert provided, but wrong owner
 
330
                InvalidCertificate, ///< invalid cert
 
331
                NoCertificate       ///< identity unknown
 
332
        };
 
333
 
 
334
        /** 
 
335
            Constructor for Transport Layer Security connection
 
336
 
 
337
            This produces a Stream (normal %TLS) rather than Datagram (DTLS)
 
338
            object.
 
339
            If you want to do DTLS, see below.
 
340
 
 
341
            \param parent the parent object for this object
 
342
            \param provider the name of the provider, if a specific provider
 
343
            is required
 
344
        */
 
345
        explicit TLS(QObject *parent = 0, const QString &provider = QString());
 
346
 
 
347
        /**
 
348
           Constructor for Transport Layer Security connection.
 
349
 
 
350
           This constructor can be used for both normal %TLS (set mode to TLS::Stream)
 
351
           or DTLS (set mode to TLS::Datagram).
 
352
 
 
353
           \param mode the connection Mode
 
354
           \param parent the parent object for this object
 
355
           \param provider the name of the provider, if a specific provider is
 
356
           required
 
357
        */
 
358
        explicit TLS(Mode mode, QObject *parent = 0, const QString &provider = QString());
 
359
 
 
360
        /**
 
361
           Destructor
 
362
        */
 
363
        ~TLS();
 
364
 
 
365
        /**
 
366
           Reset the connection
 
367
        */
 
368
        void reset();
 
369
 
 
370
        /**
 
371
           Get the list of cipher suites that are available for use.
 
372
 
 
373
           A cipher suite is a combination of key exchange,
 
374
           encryption and hashing algorithms that are agreed
 
375
           during the initial handshake between client and
 
376
           server.
 
377
 
 
378
           \param version the protocol Version that the cipher
 
379
           suites are required for
 
380
 
 
381
           \return list of the the names of the cipher suites
 
382
           supported.
 
383
        */
 
384
        QStringList supportedCipherSuites(const Version &version = TLS_v1) const;
 
385
 
 
386
        /**
 
387
           The local certificate to use. This is the
 
388
           certificate that will be provided to the peer. This
 
389
           is almost always required on the server side
 
390
           (because the server has to provide a certificate to
 
391
           the client), and may be used on the client side.
 
392
 
 
393
           \param cert a chain of certificates that
 
394
           link the host certificate to a trusted root
 
395
           certificate.
 
396
           \param key the private key for the certificate
 
397
           chain
 
398
        */
 
399
        void setCertificate(const CertificateChain &cert, const PrivateKey &key);
 
400
 
 
401
        /**
 
402
           \overload
 
403
 
 
404
           Allows setting a certificate from a KeyBundle.
 
405
 
 
406
           \param kb key bundle containing the local certificate
 
407
           and associated private key.
 
408
        */
 
409
        void setCertificate(const KeyBundle &kb);
 
410
 
 
411
        /**
 
412
           Return the trusted certificates set for this object
 
413
        */
 
414
        CertificateCollection trustedCertificates() const;
 
415
 
 
416
        /**
 
417
           Set up the set of trusted certificates that will be used to verify
 
418
           that the certificate provided is valid.
 
419
 
 
420
           Typically, this will be the collection of root certificates from
 
421
           the system, which you can get using QCA::systemStore(), however you
 
422
           may choose to pass whatever certificates match your assurance
 
423
           needs.
 
424
 
 
425
           \param trusted a bundle of trusted certificates.
 
426
        */
 
427
        void setTrustedCertificates(const CertificateCollection &trusted);
 
428
 
 
429
        /**
 
430
           The security level required for this link
 
431
 
 
432
           \param s the level required for this link.
 
433
        */
 
434
        void setConstraints(SecurityLevel s);
 
435
 
 
436
        /**
 
437
           \overload
 
438
 
 
439
           \param minSSF the minimum Security Strength Factor
 
440
           required for this link.
 
441
           \param maxSSF the maximum Security Strength Factor
 
442
           required for this link.
 
443
        */
 
444
        void setConstraints(int minSSF, int maxSSF);
 
445
 
 
446
        /**
 
447
           \overload
 
448
 
 
449
           \param cipherSuiteList a list of the names of
 
450
           cipher suites that can be used for this link.
 
451
 
 
452
           \note the names are the same as the names in the
 
453
           applicable IETF RFCs (or Internet Drafts if there
 
454
           is no applicable RFC).
 
455
        */
 
456
        void setConstraints(const QStringList &cipherSuiteList);
 
457
 
 
458
        /**
 
459
           Retrieve the list of allowed issuers by the server,
 
460
           if the server has provided them.  Only DN types will
 
461
           be present.
 
462
 
 
463
           \code
 
464
Certificate someCert = ...
 
465
PrivateKey someKey = ...
 
466
 
 
467
// see if the server will take our cert
 
468
CertificateInfoOrdered issuerInfo = someCert.issuerInfoOrdered().dnOnly();
 
469
foreach(const CertificateInfoOrdered &info, tls->issuerList())
 
470
{
 
471
        if(info == issuerInfo)
 
472
        {
 
473
                // server will accept someCert, let's present it
 
474
                tls->setCertificate(someCert, someKey);
 
475
                break;
 
476
        }
 
477
}
 
478
           \endcode
 
479
        */
 
480
        QList<CertificateInfoOrdered> issuerList() const;
 
481
 
 
482
        /**
 
483
           Sets the issuer list to present to the client.  For
 
484
           use with servers only.  Only DN types are allowed.
 
485
 
 
486
           \param issuers the list of valid issuers to be used.
 
487
        */
 
488
        void setIssuerList(const QList<CertificateInfoOrdered> &issuers);
 
489
 
 
490
        /**
 
491
           Resume a %TLS session using the given session object
 
492
 
 
493
           \param session the session state to use for resumption.
 
494
        */
 
495
        void setSession(const TLSSession &session);
 
496
 
 
497
        /**
 
498
           Test if the link can use compression
 
499
 
 
500
           \return true if the link can use compression
 
501
        */
 
502
        bool canCompress() const;
 
503
 
 
504
        /**
 
505
           Test if the link can specify a hostname (Server Name
 
506
           Indication)
 
507
 
 
508
           \return true if the link can specify a hostname
 
509
        */
 
510
        bool canSetHostName() const;
 
511
 
 
512
        /**
 
513
           Returns true if compression is enabled
 
514
 
 
515
           This only indicates whether or not the object is configured to use
 
516
           compression, not whether or not the link is actually compressed.
 
517
           Use isCompressed() for that.
 
518
        */
 
519
        bool compressionEnabled() const;
 
520
 
 
521
        /**
 
522
           Set the link to use compression
 
523
 
 
524
           \param b true if the link should use compression, or false to
 
525
           disable compression
 
526
        */
 
527
        void setCompressionEnabled(bool b);
 
528
 
 
529
        /**
 
530
           Returns the host name specified or an empty string if no host
 
531
           name is specified.
 
532
        */
 
533
        QString hostName() const;
 
534
 
 
535
        /**
 
536
           Start the %TLS/SSL connection as a client
 
537
 
 
538
           Typically, you'll want to perform RFC 2818 validation on the
 
539
           server's certificate, based on the hostname you're intending
 
540
           to connect to.  Pass a value for \a host in order to have the
 
541
           validation for you.  If you want to bypass this behavior and
 
542
           do the validation yourself, pass an empty string for \a host.
 
543
 
 
544
           If the host is an internationalized domain name, then it must be
 
545
           provided in unicode format, not in IDNA ACE/punycode format.
 
546
 
 
547
           \param host the hostname that you want to connect to
 
548
 
 
549
           \note The hostname will be used for Server Name Indication
 
550
           extension (see
 
551
           <a href="http://www.ietf.org/rfc/rfc3546.txt">RFC 3546</a> Section
 
552
           3.1) if supported by the backend provider.
 
553
        */
 
554
        void startClient(const QString &host = QString());
 
555
 
 
556
        /**
 
557
           Start the %TLS/SSL connection as a server.
 
558
        */
 
559
        void startServer();
 
560
 
 
561
        /**
 
562
           Resumes %TLS processing.
 
563
 
 
564
           Call this function after hostNameReceived(), certificateRequested()
 
565
           peerCertificateAvailable() or handshaken() is emitted.  By
 
566
           requiring this function to be called in order to proceed,
 
567
           applications are given a chance to perform user interaction between
 
568
           steps in the %TLS process.
 
569
        */
 
570
        void continueAfterStep();
 
571
 
 
572
        /**
 
573
           test if the handshake is complete
 
574
 
 
575
           \return true if the handshake is complete
 
576
 
 
577
           \sa handshaken
 
578
        */
 
579
        bool isHandshaken() const;
 
580
 
 
581
        /**
 
582
           test if the link is compressed
 
583
 
 
584
           \return true if the link is compressed
 
585
        */
 
586
        bool isCompressed() const;
 
587
 
 
588
        /**
 
589
           The protocol version that is in use for this connection.
 
590
        */
 
591
        Version version() const;
 
592
 
 
593
        /**
 
594
           The cipher suite that has been negotiated for this connection.
 
595
 
 
596
           The name returned here is the name used in the applicable RFC
 
597
           (or Internet Draft, where there is no RFC).
 
598
        */
 
599
        QString cipherSuite() const;
 
600
 
 
601
        /**
 
602
           The number of effective bits of security being used for this
 
603
           connection. 
 
604
 
 
605
           This can differ from the actual number of bits in
 
606
           the cipher for certain
 
607
           older "export ciphers" that are deliberately crippled. If you
 
608
           want that information, use cipherMaxBits().
 
609
        */
 
610
        int cipherBits() const;
 
611
 
 
612
        /**
 
613
           The number of bits of security that the cipher could use.
 
614
 
 
615
           This is normally the same as cipherBits(), but can be greater
 
616
           for older "export ciphers".
 
617
        */
 
618
        int cipherMaxBits() const;
 
619
 
 
620
        /**
 
621
           The session object of the %TLS connection, which can be used
 
622
           for resuming.
 
623
        */
 
624
        TLSSession session() const;
 
625
 
 
626
        /**
 
627
           This method returns the type of error that has
 
628
           occurred. You should only need to check this if the
 
629
           error() signal is emitted.
 
630
        */
 
631
        Error errorCode() const;
 
632
 
 
633
        /**
 
634
           After the SSL/%TLS handshake is complete, this
 
635
           method allows you to determine if the other end
 
636
           of the connection (if the application is a client,
 
637
           this is the server; if the application is a server,
 
638
           this is the client) has a valid identity.
 
639
 
 
640
           Note that the security of %TLS/SSL depends on
 
641
           checking this. It is not enough to check that the
 
642
           certificate is valid - you must check that the
 
643
           certificate is valid for the entity that you are
 
644
           trying to communicate with.
 
645
 
 
646
           \note If this returns QCA::TLS::InvalidCertificate,
 
647
           you may wish to use peerCertificateValidity() to
 
648
           determine whether to proceed or not.
 
649
        */
 
650
        IdentityResult peerIdentityResult() const;
 
651
 
 
652
        /**
 
653
           After the SSL/%TLS handshake is valid, this method
 
654
           allows you to check if the received certificate
 
655
           from the other end is valid. As noted in
 
656
           peerIdentityResult(), you also need to check that
 
657
           the certificate matches the entity you are trying
 
658
           to communicate with.
 
659
        */
 
660
        Validity peerCertificateValidity() const;
 
661
 
 
662
        /**
 
663
           The CertificateChain for the local host
 
664
           certificate.
 
665
        */
 
666
        CertificateChain localCertificateChain() const;
 
667
 
 
668
        /**
 
669
           The PrivateKey for the local host
 
670
           certificate.
 
671
        */
 
672
        PrivateKey localPrivateKey() const;
 
673
 
 
674
        /**
 
675
           The CertificateChain from the peer (other end of
 
676
           the connection to the trusted root certificate).
 
677
        */
 
678
        CertificateChain peerCertificateChain() const;
 
679
 
 
680
        // reimplemented
 
681
        virtual bool isClosable() const;
 
682
        virtual int bytesAvailable() const;
 
683
        virtual int bytesOutgoingAvailable() const;
 
684
        virtual void close();
 
685
        virtual void write(const QByteArray &a);
 
686
        virtual QByteArray read();
 
687
        virtual void writeIncoming(const QByteArray &a);
 
688
        virtual QByteArray readOutgoing(int *plainBytes = 0);
 
689
        virtual QByteArray readUnprocessed();
 
690
        virtual int convertBytesWritten(qint64 encryptedBytes);
 
691
 
 
692
        /**
 
693
           Determine the number of packets available to be
 
694
           read on the application side.
 
695
 
 
696
           \note this is only used with DTLS.
 
697
        */
 
698
        int packetsAvailable() const;
 
699
 
 
700
        /**
 
701
           Determine the number of packets available to be
 
702
           read on the network side.
 
703
 
 
704
           \note this is only used with DTLS.
 
705
        */
 
706
        int packetsOutgoingAvailable() const;
 
707
 
 
708
        /**
 
709
           Return the currently configured maximum packet size
 
710
 
 
711
           \note this is only used with DTLS
 
712
        */
 
713
        int packetMTU() const;
 
714
 
 
715
        /**
 
716
           Set the maximum packet size to use.
 
717
 
 
718
           \param size the number of bytes to set as the MTU.
 
719
 
 
720
           \note this is only used with DTLS.
 
721
        */
 
722
        void setPacketMTU(int size) const;
 
723
 
 
724
Q_SIGNALS:
 
725
        /**
 
726
           Emitted if a host name is set by the client.  At
 
727
           this time, the server can inspect the hostName().
 
728
 
 
729
           You must call continueAfterStep() in order for %TLS
 
730
           processing to resume after this signal is emitted.
 
731
 
 
732
           This signal is only emitted in server mode.
 
733
 
 
734
           \sa continueAfterStep
 
735
        */
 
736
        void hostNameReceived();
 
737
 
 
738
        /**
 
739
           Emitted when the server requests a certificate.  At
 
740
           this time, the client can inspect the issuerList().
 
741
 
 
742
           You must call continueAfterStep() in order for %TLS
 
743
           processing to resume after this signal is emitted.
 
744
 
 
745
           This signal is only emitted in client mode.
 
746
 
 
747
           \sa continueAfterStep
 
748
        */
 
749
        void certificateRequested();
 
750
 
 
751
        /**
 
752
           Emitted when a certificate is received from the peer.
 
753
           At this time, you may inspect peerIdentityResult(),
 
754
           peerCertificateValidity(), and peerCertificateChain().
 
755
 
 
756
           You must call continueAfterStep() in order for %TLS
 
757
           processing to resume after this signal is emitted.
 
758
 
 
759
           \sa continueAfterStep
 
760
        */
 
761
        void peerCertificateAvailable();
 
762
 
 
763
        /**
 
764
           Emitted when the protocol handshake is complete.  At
 
765
           this time, all available information about the %TLS
 
766
           session can be inspected.
 
767
 
 
768
           You must call continueAfterStep() in order for %TLS
 
769
           processing to resume after this signal is emitted.
 
770
 
 
771
           \sa continueAfterStep
 
772
           \sa isHandshaken
 
773
        */
 
774
        void handshaken();
 
775
 
 
776
protected:
 
777
        /**
 
778
           Called when a connection is made to a particular signal
 
779
 
 
780
           \param signal the name of the signal that has been
 
781
           connected to.
 
782
        */
 
783
        void connectNotify(const char *signal);
 
784
 
 
785
        /**
 
786
           Called when a connection is removed from a particular signal
 
787
 
 
788
           \param signal the name of the signal that has been
 
789
           disconnected from.
 
790
        */
 
791
        void disconnectNotify(const char *signal);
 
792
 
 
793
private:
 
794
        Q_DISABLE_COPY(TLS)
 
795
 
 
796
        class Private;
 
797
        friend class Private;
 
798
        Private *d;
 
799
};
 
800
 
 
801
/**
 
802
   \class SASL qca_securelayer.h QtCrypto
 
803
 
 
804
   Simple Authentication and Security Layer protocol implementation
 
805
 
 
806
   This class implements the Simple Authenication and Security Layer protocol,
 
807
   which is described in RFC2222 - see
 
808
   <a href="http://www.ietf.org/rfc/rfc2222.txt">http://www.ietf.org/rfc/rfc2222.txt</a>.
 
809
 
 
810
   As the name suggests, %SASL provides authentication (eg, a "login" of some
 
811
   form), for a connection oriented protocol, and can also provide protection
 
812
   for the subsequent connection.
 
813
 
 
814
   The %SASL protocol is designed to be extensible, through a range of
 
815
   "mechanisms", where a mechanism is the actual authentication method.
 
816
   Example mechanisms include Anonymous, LOGIN, Kerberos V4, and GSSAPI.
 
817
   Mechanisms can be added (potentially without restarting the server
 
818
   application) by the system administrator.
 
819
 
 
820
   It is important to understand that %SASL is neither "network aware" nor
 
821
   "protocol aware".  That means that %SASL does not understand how the client
 
822
   connects to the server, and %SASL does not understand the actual
 
823
   application protocol.
 
824
 
 
825
   \ingroup UserAPI
 
826
 
 
827
*/
 
828
class QCA_EXPORT SASL : public SecureLayer, public Algorithm
 
829
{
 
830
        Q_OBJECT
 
831
public:
 
832
        /**
 
833
           Possible errors that may occur when using %SASL
 
834
        */
 
835
        enum Error
 
836
        {
 
837
                ErrorInit,      ///< problem starting up %SASL
 
838
                ErrorHandshake, ///< problem during the authentication process
 
839
                ErrorCrypt      ///< problem at anytime after
 
840
        };
 
841
 
 
842
        /**
 
843
           Possible authentication error states
 
844
        */
 
845
        enum AuthCondition
 
846
        {
 
847
                AuthFail,          ///< Generic authentication failure
 
848
                NoMechanism,       ///< No compatible/appropriate authentication mechanism
 
849
                BadProtocol,       ///< Bad protocol or cancelled
 
850
                BadServer,         ///< Server failed mutual authentication (client side only)
 
851
                BadAuth,           ///< Authentication failure (server side only)
 
852
                NoAuthzid,         ///< Authorization failure (server side only)
 
853
                TooWeak,           ///< Mechanism too weak for this user (server side only)
 
854
                NeedEncrypt,       ///< Encryption is needed in order to use mechanism (server side only)
 
855
                Expired,           ///< Passphrase expired, has to be reset (server side only)
 
856
                Disabled,          ///< Account is disabled (server side only)
 
857
                NoUser,            ///< User not found (server side only)
 
858
                RemoteUnavailable  ///< Remote service needed for auth is gone (server side only)
 
859
        };
 
860
 
 
861
        /**
 
862
           Authentication requirement flag values
 
863
        */
 
864
        enum AuthFlags
 
865
        {
 
866
                AuthFlagsNone          = 0x00,
 
867
                AllowPlain             = 0x01,
 
868
                AllowAnonymous         = 0x02,
 
869
                RequireForwardSecrecy  = 0x04,
 
870
                RequirePassCredentials = 0x08,
 
871
                RequireMutualAuth      = 0x10,
 
872
                RequireAuthzidSupport  = 0x20  // server-only
 
873
        };
 
874
 
 
875
        /**
 
876
           Mode options for client side sending
 
877
        */
 
878
        enum ClientSendMode
 
879
        {
 
880
                AllowClientSendFirst,
 
881
                DisableClientSendFirst
 
882
        };
 
883
 
 
884
        /**
 
885
           Mode options for server side sending
 
886
        */
 
887
        enum ServerSendMode
 
888
        {
 
889
                AllowServerSendLast,
 
890
                DisableServerSendLast
 
891
        };
 
892
 
 
893
        /**
 
894
           \class Params qca_securelayer.h QtCrypto
 
895
 
 
896
           Parameter flags for the %SASL authentication
 
897
 
 
898
           This is used to indicate which parameters are needed by %SASL
 
899
           in order to complete the authentication process.
 
900
 
 
901
           \ingroup UserAPI
 
902
        */
 
903
        class QCA_EXPORT Params
 
904
        {
 
905
        public:
 
906
                Params();
 
907
 
 
908
                /**
 
909
                   Standard constructor. 
 
910
                   
 
911
                   The concept behind this is that you set each of the 
 
912
                   flags depending on which parameters are needed.
 
913
 
 
914
                   \param user the username is required
 
915
                   \param authzid the authorization identity is required
 
916
                   \param pass the password is required
 
917
                   \param realm the realm is required
 
918
                */
 
919
                Params(bool user, bool authzid, bool pass, bool realm);
 
920
 
 
921
                /**
 
922
                   Standard copy constructor
 
923
 
 
924
                   \param from the Params object to copy
 
925
                */
 
926
                Params(const Params &from);
 
927
                ~Params();
 
928
 
 
929
                /**
 
930
                   Standard assignment operator
 
931
 
 
932
                   \param from the Params object to assign from
 
933
                */
 
934
                Params & operator=(const Params &from);
 
935
 
 
936
                /**
 
937
                   User is needed
 
938
                */
 
939
                bool needUsername() const;
 
940
 
 
941
                /**
 
942
                   An Authorization ID can be sent if desired
 
943
                */
 
944
                bool canSendAuthzid() const;
 
945
 
 
946
                /**
 
947
                   Password is needed
 
948
                */
 
949
                bool needPassword() const;
 
950
 
 
951
                /**
 
952
                   A Realm can be sent if desired
 
953
                */
 
954
                bool canSendRealm() const;
 
955
 
 
956
        private:
 
957
                class Private;
 
958
                Private *d;
 
959
        };
 
960
 
 
961
        /**
 
962
           Standard constructor
 
963
 
 
964
           \param parent the parent object for this %SASL connection
 
965
           \param provider if specified, the provider to use. If not 
 
966
           specified, or specified as empty, then any provider is 
 
967
           acceptable.
 
968
        */
 
969
        explicit SASL(QObject *parent = 0, const QString &provider = QString());
 
970
 
 
971
        ~SASL();
 
972
 
 
973
        /**
 
974
           Reset the %SASL mechanism
 
975
        */
 
976
        void reset();
 
977
 
 
978
        /**
 
979
           Specify connection constraints
 
980
 
 
981
           %SASL supports a range of authentication requirements, and
 
982
           a range of security levels. This method allows you to
 
983
           specify the requirements for your connection.
 
984
 
 
985
           \param f the authentication requirements, which you typically
 
986
           build using a binary OR function (eg AllowPlain | AllowAnonymous)
 
987
           \param s the security level of the encryption, if used. See
 
988
           SecurityLevel for details of what each level provides.
 
989
        */
 
990
        void setConstraints(AuthFlags f, SecurityLevel s = SL_None);
 
991
 
 
992
        /**
 
993
           \overload
 
994
 
 
995
           Unless you have a specific reason for directly specifying a
 
996
           strength factor, you probably should use the method above.
 
997
 
 
998
           \param f the authentication requirements, which you typically
 
999
           build using a binary OR function (eg AllowPlain | AllowAnonymous)
 
1000
           \param minSSF the minimum security strength factor that is required
 
1001
           \param maxSSF the maximum security strength factor that is required
 
1002
 
 
1003
           \note Security strength factors are a rough approximation to key
 
1004
           length in the encryption function (eg if you are securing with
 
1005
           plain DES, the security strength factor would be 56).
 
1006
        */
 
1007
        void setConstraints(AuthFlags f, int minSSF, int maxSSF);
 
1008
 
 
1009
        /**
 
1010
           Specify the local address.
 
1011
 
 
1012
           \param addr the address of the local part of the connection
 
1013
           \param port the port number of the local part of the connection
 
1014
        */
 
1015
        void setLocalAddress(const QString &addr, quint16 port);
 
1016
 
 
1017
        /**
 
1018
           Specify the peer address.
 
1019
 
 
1020
           \param addr the address of the peer side of the connection
 
1021
           \param port the port number of the peer side of the connection
 
1022
        */
 
1023
        void setRemoteAddress(const QString &addr, quint16 port);
 
1024
 
 
1025
        /**
 
1026
           Specify the id of the externally secured connection
 
1027
 
 
1028
           \param authid the id of the connection
 
1029
        */
 
1030
        void setExternalAuthId(const QString &authid);
 
1031
 
 
1032
        /**
 
1033
           Specify a security strength factor for an externally secured
 
1034
           connection
 
1035
 
 
1036
           \param strength the security strength factor of the connection
 
1037
        */
 
1038
        void setExternalSSF(int strength);
 
1039
 
 
1040
        /**
 
1041
           Initialise the client side of the connection
 
1042
 
 
1043
           startClient must be called on the client side of the connection.
 
1044
           clientStarted will be emitted when the operation is completed.
 
1045
 
 
1046
           \param service the name of the service
 
1047
           \param host the client side host name
 
1048
           \param mechlist the list of mechanisms which can be used
 
1049
           \param mode the mode to use on the client side
 
1050
        */
 
1051
        void startClient(const QString &service, const QString &host, const QStringList &mechlist, ClientSendMode mode = AllowClientSendFirst);
 
1052
 
 
1053
        /**
 
1054
           Initialise the server side of the connection
 
1055
 
 
1056
           startServer must be called on the server side of the connection.
 
1057
           serverStarted will be emitted when the operation is completed.
 
1058
 
 
1059
           \param service the name of the service
 
1060
           \param host the server side host name
 
1061
           \param realm the realm to use
 
1062
           \param mode which mode to use on the server side
 
1063
        */
 
1064
        void startServer(const QString &service, const QString &host, const QString &realm, ServerSendMode mode = DisableServerSendLast);
 
1065
 
 
1066
        /**
 
1067
           Process the first step in server mode (server)
 
1068
 
 
1069
           Call this with the mechanism selected by the client.  If there
 
1070
           is initial client data, call the other version of this function
 
1071
           instead.
 
1072
 
 
1073
           \param mech the mechanism to be used.
 
1074
        */
 
1075
        void putServerFirstStep(const QString &mech);
 
1076
 
 
1077
        /**
 
1078
           Process the first step in server mode (server)
 
1079
 
 
1080
           Call this with the mechanism selected by the client, and initial
 
1081
           client data.  If there is no initial client data, call the other
 
1082
           version of this function instead.
 
1083
 
 
1084
           \param mech the mechanism to be used
 
1085
           \param clientInit the initial data provided by the client side
 
1086
        */
 
1087
        void putServerFirstStep(const QString &mech, const QByteArray &clientInit);
 
1088
 
 
1089
        /**
 
1090
           Process an authentication step
 
1091
 
 
1092
           Call this with authentication data received from the network.
 
1093
           The only exception is the first step in server mode, in which
 
1094
           case putServerFirstStep must be called.
 
1095
 
 
1096
           \param stepData the authentication data from the network
 
1097
        */
 
1098
        void putStep(const QByteArray &stepData);
 
1099
 
 
1100
        /**
 
1101
           Return the mechanism selected (client)
 
1102
        */
 
1103
        QString mechanism() const;
 
1104
 
 
1105
        /**
 
1106
           Return the mechanism list (server)
 
1107
        */
 
1108
        QStringList mechanismList() const;
 
1109
 
 
1110
        /**
 
1111
           Return the realm list, if available (client)
 
1112
        */
 
1113
        QStringList realmList() const;
 
1114
 
 
1115
        /**
 
1116
           Return the security strength factor of the connection
 
1117
        */
 
1118
        int ssf() const;
 
1119
 
 
1120
        /**
 
1121
           Return the error code
 
1122
        */
 
1123
        Error errorCode() const;
 
1124
 
 
1125
        /**
 
1126
           Return the reason for authentication failure
 
1127
        */
 
1128
        AuthCondition authCondition() const;
 
1129
 
 
1130
        /**
 
1131
           Specify the username to use in authentication
 
1132
 
 
1133
           \param user the username to use
 
1134
        */
 
1135
        void setUsername(const QString &user);
 
1136
 
 
1137
        /**
 
1138
           Specify the authorization identity to use in authentication
 
1139
 
 
1140
           \param auth the authorization identity to use
 
1141
        */
 
1142
        void setAuthzid(const QString &auth);
 
1143
 
 
1144
        /**
 
1145
           Specify the password to use in authentication
 
1146
 
 
1147
           \param pass the password to use
 
1148
        */
 
1149
        void setPassword(const SecureArray &pass);
 
1150
 
 
1151
        /**
 
1152
           Specify the realm to use in authentication
 
1153
 
 
1154
           \param realm the realm to use
 
1155
        */
 
1156
        void setRealm(const QString &realm);
 
1157
 
 
1158
        /**
 
1159
           Continue negotiation after parameters have been set (client)
 
1160
        */
 
1161
        void continueAfterParams();
 
1162
 
 
1163
        /**
 
1164
           Continue negotiation after auth ids have been checked (server)
 
1165
        */
 
1166
        void continueAfterAuthCheck();
 
1167
 
 
1168
        // reimplemented
 
1169
        virtual int bytesAvailable() const;
 
1170
        virtual int bytesOutgoingAvailable() const;
 
1171
        virtual void write(const QByteArray &a);
 
1172
        virtual QByteArray read();
 
1173
        virtual void writeIncoming(const QByteArray &a);
 
1174
        virtual QByteArray readOutgoing(int *plainBytes = 0);
 
1175
        virtual int convertBytesWritten(qint64 encryptedBytes);
 
1176
 
 
1177
Q_SIGNALS:
 
1178
        /**
 
1179
           This signal is emitted when the client has been successfully
 
1180
           started
 
1181
 
 
1182
           \param clientInit true if the client should send an initial
 
1183
           response to the server
 
1184
           \param clientInitData the initial response to send to the server.
 
1185
           Do note that there is a difference in SASL between an empty initial
 
1186
           response and no initial response, and so even if clientInitData is
 
1187
           an empty array, you still need to send an initial response if
 
1188
           clientInit is true.
 
1189
        */
 
1190
        void clientStarted(bool clientInit, const QByteArray &clientInitData);
 
1191
 
 
1192
        /**
 
1193
           This signal is emitted after the server has been
 
1194
           successfully started
 
1195
        */
 
1196
        void serverStarted();
 
1197
 
 
1198
        /**
 
1199
           This signal is emitted when there is data required
 
1200
           to be sent over the network to complete the next
 
1201
           step in the authentication process.
 
1202
 
 
1203
           \param stepData the data to send over the network
 
1204
        */
 
1205
        void nextStep(const QByteArray &stepData);
 
1206
 
 
1207
        /**
 
1208
           This signal is emitted when the client needs
 
1209
           additional parameters
 
1210
 
 
1211
           After receiving this signal, the application should set 
 
1212
           the required parameter values appropriately and then call
 
1213
           continueAfterParams().
 
1214
 
 
1215
           \param params the parameters that are required by the client
 
1216
        */
 
1217
        void needParams(const QCA::SASL::Params &params);
 
1218
 
 
1219
        /**
 
1220
           This signal is emitted when the server needs to
 
1221
           perform the authentication check
 
1222
 
 
1223
           If the user and authzid are valid, call continueAfterAuthCheck().
 
1224
 
 
1225
           \param user the user identification name
 
1226
           \param authzid the user authorization name
 
1227
        */
 
1228
        void authCheck(const QString &user, const QString &authzid);
 
1229
 
 
1230
        /**
 
1231
           This signal is emitted when authentication is complete.
 
1232
        */
 
1233
        void authenticated();
 
1234
 
 
1235
private:
 
1236
        Q_DISABLE_COPY(SASL)
 
1237
 
 
1238
        class Private;
 
1239
        friend class Private;
 
1240
        Private *d;
 
1241
};
 
1242
 
 
1243
}
 
1244
 
 
1245
#endif