PageRenderTime 85ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_SSL_STREAM_H__
  2. #define __MORDOR_SSL_STREAM_H__
  3. // Copyright (c) 2009 - Mozy, Inc.
  4. #include "filter.h"
  5. #include <vector>
  6. #include <openssl/ssl.h>
  7. #include <boost/thread.hpp>
  8. #include "buffer.h"
  9. namespace Mordor {
  10. class OpenSSLException : public std::runtime_error
  11. {
  12. public:
  13. OpenSSLException(const std::string &message)
  14. : std::runtime_error(message)
  15. {}
  16. OpenSSLException(); // queries OpenSSL for the error code
  17. };
  18. class CertificateVerificationException : public OpenSSLException
  19. {
  20. public:
  21. CertificateVerificationException(long verifyResult)
  22. : OpenSSLException(constructMessage(verifyResult)),
  23. m_verifyResult(verifyResult)
  24. {}
  25. CertificateVerificationException(long verifyResult,
  26. const std::string &message)
  27. : OpenSSLException(message),
  28. m_verifyResult(verifyResult)
  29. {}
  30. long verifyResult() const { return m_verifyResult; }
  31. private:
  32. static std::string constructMessage(long verifyResult);
  33. private:
  34. long m_verifyResult;
  35. };
  36. class SSLStream : public MutatingFilterStream
  37. {
  38. public:
  39. typedef boost::shared_ptr<SSLStream> ptr;
  40. public:
  41. SSLStream(Stream::ptr parent, bool client = true, bool own = true, SSL_CTX *ctx = NULL);
  42. bool supportsHalfClose() { return false; }
  43. void close(CloseType type = BOTH);
  44. using MutatingFilterStream::read;
  45. size_t read(void *buffer, size_t length);
  46. size_t write(const Buffer &buffer, size_t length);
  47. size_t write(const void *buffer, size_t length);
  48. void flush(bool flushParent = true);
  49. void accept();
  50. void connect();
  51. void serverNameIndication(const std::string &hostname);
  52. void verifyPeerCertificate();
  53. void verifyPeerCertificate(const std::string &hostname);
  54. void clearSSLError();
  55. private:
  56. void wantRead();
  57. int sslCallWithLock(boost::function<int ()> dg, unsigned long *error);
  58. private:
  59. boost::mutex m_mutex;
  60. boost::shared_ptr<SSL_CTX> m_ctx;
  61. boost::shared_ptr<SSL> m_ssl;
  62. Buffer m_readBuffer, m_writeBuffer;
  63. BIO *m_readBio, *m_writeBio;
  64. };
  65. }
  66. #endif