SSLBridge.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #ifndef __SSL_BRIDGE_H__
  20. #define __SSL_BRIDGE_H__
  21. #include <boost/asio.hpp>
  22. #include <boost/shared_ptr.hpp>
  23. #include <openssl/pem.h>
  24. #include <openssl/conf.h>
  25. #include <openssl/x509v3.h>
  26. #include <openssl/ssl.h>
  27. #include <openssl/err.h>
  28. #include <sys/poll.h>
  29. #include <sys/socket.h>
  30. #include <sys/types.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <list>
  34. #include <map>
  35. #include "util/Util.hpp"
  36. #include "certificate/Certificate.hpp"
  37. #include "certificate/CertificateManager.hpp"
  38. #include "SessionCache.hpp"
  39. #include "Logger.hpp"
  40. using namespace boost::asio;
  41. class SSLConnectionError : public std::exception {
  42. public:
  43. virtual const char* what() const throw() {
  44. return "Error with SSL connection...";
  45. }
  46. };
  47. class SSLBridge {
  48. protected:
  49. boost::shared_ptr<ip::tcp::socket> clientSocket;
  50. ip::tcp::socket *serverSocket;
  51. std::string serverName;
  52. bool closed;
  53. SSL *serverSession;
  54. SSL *clientSession;
  55. virtual ip::tcp::endpoint getRemoteEndpoint();
  56. virtual bool readFromClient();
  57. private:
  58. SessionCache *cache;
  59. X509* getServerCertificate();
  60. void buildClientContext(SSL_CTX *context, Certificate *leaf, std::list<Certificate*> *chain);
  61. int isAvailable(int revents);
  62. int isClosed(int revents);
  63. int forwardData(SSL *from, SSL *to);
  64. void setServerName();
  65. bool readFromServer();
  66. public:
  67. SSLBridge(boost::shared_ptr<ip::tcp::socket> clientSocket,
  68. ip::tcp::socket *serverSocket)
  69. : clientSocket(clientSocket), serverSocket(serverSocket),
  70. serverSession(), clientSession(), closed(false)
  71. {
  72. cache = SessionCache::getInstance();
  73. }
  74. ~SSLBridge() {
  75. close();
  76. }
  77. void handshakeWithClient(CertificateManager &manager, bool wildcardOK);
  78. void handshakeWithServer();
  79. void shuttleData();
  80. virtual void close();
  81. };
  82. #endif