comparison ui/sslconnection.h @ 45:c6125d73faf4

Move SSLConnection into it's own class
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 14 Mar 2014 16:40:53 +0000
parents
children d28e2624c1d5
comparison
equal deleted inserted replaced
44:b3e8e047bc2c 45:c6125d73faf4
1 #ifndef SSLCONNECTION_H
2 #define SSLCONNECTION_H
3
4 /**
5 * @file sslconnection.h
6 * @brief Qt wrapper around polarssl ssl api
7 */
8
9 #include <QDebug>
10 #include <QUrl>
11 #include <QString>
12 #include <QByteArray>
13
14 #include <polarssl/entropy.h>
15 #include <polarssl/net.h>
16 #include <polarssl/ssl.h>
17 #include <polarssl/ctr_drbg.h>
18 #include <polarssl/error.h>
19 #include <polarssl/certs.h>
20
21 class SSLConnection
22 {
23 public:
24 enum ErrorCode {
25 NoError,
26 NoConnection,
27 SSLHandshakeFailed,
28 InvalidCertificate,
29 InvalidPinnedCertificate,
30 InvalidResponse,
31 ConnectionLost,
32 Timeout,
33 ErrUnknown
34 };
35
36 /**
37 * @brief Construct a pinned SSL Connection
38 *
39 * @param[in] url the Url to connect to
40 * @param[in] certificate optional certificate to validate https connection
41 */
42 SSLConnection(const QString& url,
43 const QByteArray& certificate = QByteArray());
44
45 ~SSLConnection();
46
47 /** @brief write */
48 int write(const QByteArray& request);
49
50 /**
51 * @brief read at most len bytes
52 * and return them as a byte array returns a NULL byte array on error*/
53 QByteArray read(size_t len);
54
55 bool initialized() { return mInitialized; }
56 bool connected() { return mConnected; }
57
58 ErrorCode getLastError() { return mErrorState; }
59
60 /** @brief: Establish the connection
61 *
62 * @returns 0 on success otherwise a polarssl error or -1 is returned
63 */
64 int connect();
65
66 private:
67 QUrl mUrl;
68 QByteArray mPinnedCert;
69 x509_crt mX509PinnedCert;
70 entropy_context mEntropy;
71 ctr_drbg_context mCtr_drbg;
72 ssl_context mSSL;
73 bool mInitialized;
74 bool mConnected;
75 int mServerFD;
76 SSLConnection::ErrorCode mErrorState;
77 /* @brief: Initialize polarssl structures
78 *
79 * This wraps polarssl initialization functions
80 * that can return an error.
81 * Sets the error state accordingly.
82 *
83 * @returns: 0 on success a polarssl error otherwise.
84 */
85 int init();
86 };
87
88 #endif

http://wald.intevation.org/projects/trustbridge/