PageRenderTime 132ms CodeModel.GetById 67ms app.highlight 6ms RepoModel.GetById 37ms app.codeStats 21ms

/mordor/streams/ssl.h

http://github.com/mozy/mordor
C Header | 88 lines | 65 code | 22 blank | 1 comment | 0 complexity | 8589fbdbd1decd23b79627b673838775 MD5 | raw file
 1#ifndef __MORDOR_SSL_STREAM_H__
 2#define __MORDOR_SSL_STREAM_H__
 3// Copyright (c) 2009 - Mozy, Inc.
 4
 5#include "filter.h"
 6
 7#include <vector>
 8
 9#include <openssl/ssl.h>
10#include <boost/thread.hpp>
11
12#include "buffer.h"
13
14namespace Mordor {
15
16class OpenSSLException : public std::runtime_error
17{
18public:
19    OpenSSLException(const std::string &message)
20        : std::runtime_error(message)
21    {}
22
23    OpenSSLException();   // queries OpenSSL for the error code
24};
25
26class CertificateVerificationException : public OpenSSLException
27{
28public:
29    CertificateVerificationException(long verifyResult)
30        : OpenSSLException(constructMessage(verifyResult)),
31          m_verifyResult(verifyResult)
32    {}
33    CertificateVerificationException(long verifyResult,
34        const std::string &message)
35        : OpenSSLException(message),
36          m_verifyResult(verifyResult)
37    {}
38
39    long verifyResult() const { return m_verifyResult; }
40
41private:
42    static std::string constructMessage(long verifyResult);
43
44private:
45    long m_verifyResult;
46};
47
48class SSLStream : public MutatingFilterStream
49{
50public:
51    typedef boost::shared_ptr<SSLStream> ptr;
52
53public:
54    SSLStream(Stream::ptr parent, bool client = true, bool own = true, SSL_CTX *ctx = NULL);
55
56    bool supportsHalfClose() { return false; }
57
58    void close(CloseType type = BOTH);
59    using MutatingFilterStream::read;
60    size_t read(void *buffer, size_t length);
61    size_t write(const Buffer &buffer, size_t length);
62    size_t write(const void *buffer, size_t length);
63    void flush(bool flushParent = true);
64
65    void accept();
66    void connect();
67
68    void serverNameIndication(const std::string &hostname);
69
70    void verifyPeerCertificate();
71    void verifyPeerCertificate(const std::string &hostname);
72    void clearSSLError();
73
74private:
75    void wantRead();
76    int sslCallWithLock(boost::function<int ()> dg, unsigned long *error);
77
78private:
79    boost::mutex m_mutex;
80    boost::shared_ptr<SSL_CTX> m_ctx;
81    boost::shared_ptr<SSL> m_ssl;
82    Buffer m_readBuffer, m_writeBuffer;
83    BIO *m_readBio, *m_writeBio;
84};
85
86}
87
88#endif