view 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
line wrap: on
line source
#ifndef SSLCONNECTION_H
#define SSLCONNECTION_H

/**
 * @file sslconnection.h
 * @brief Qt wrapper around polarssl ssl api
 */

#include <QDebug>
#include <QUrl>
#include <QString>
#include <QByteArray>

#include <polarssl/entropy.h>
#include <polarssl/net.h>
#include <polarssl/ssl.h>
#include <polarssl/ctr_drbg.h>
#include <polarssl/error.h>
#include <polarssl/certs.h>

class SSLConnection
{
public:
    enum ErrorCode {
        NoError,
        NoConnection,
        SSLHandshakeFailed,
        InvalidCertificate,
        InvalidPinnedCertificate,
        InvalidResponse,
        ConnectionLost,
        Timeout,
        ErrUnknown
    };

    /**
     * @brief Construct a pinned SSL Connection
     *
     * @param[in] url the Url to connect to
     * @param[in] certificate optional certificate to validate https connection
     */
    SSLConnection(const QString& url,
                  const QByteArray& certificate = QByteArray());

    ~SSLConnection();

    /** @brief write */
    int write(const QByteArray& request);

    /**
     * @brief read at most len bytes
     * and return them as a byte array returns a NULL byte array on error*/
    QByteArray read(size_t len);

    bool initialized() { return mInitialized; }
    bool connected() { return mConnected; }

    ErrorCode getLastError() { return mErrorState; }

    /** @brief: Establish the connection
     *
     * @returns 0 on success otherwise a polarssl error or -1 is returned
     */
    int connect();

private:
    QUrl mUrl;
    QByteArray mPinnedCert;
    x509_crt mX509PinnedCert;
    entropy_context mEntropy;
    ctr_drbg_context mCtr_drbg;
    ssl_context mSSL;
    bool mInitialized;
    bool mConnected;
    int mServerFD;
    SSLConnection::ErrorCode mErrorState;
    /* @brief: Initialize polarssl structures
     *
     * This wraps polarssl initialization functions
     * that can return an error.
     * Sets the error state accordingly.
     *
     * @returns: 0 on success a polarssl error otherwise.
     */
    int init();
};

#endif

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