RCFProto
 All Classes Functions Typedefs
OpenSslEncryptionFilter.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_OPENSSLENCRYPTIONFILTER_HPP
20 #define INCLUDE_RCF_OPENSSLENCRYPTIONFILTER_HPP
21 
22 #include <memory>
23 #include <string>
24 
25 #include <boost/function.hpp>
26 #include <boost/noncopyable.hpp>
27 #include <boost/shared_ptr.hpp>
28 
29 #include <RCF/Certificate.hpp>
30 #include <RCF/Filter.hpp>
31 #include <RCF/Export.hpp>
32 
33 typedef struct ssl_st SSL;
34 typedef struct ssl_ctx_st SSL_CTX;
35 typedef struct x509_st X509;
36 
37 namespace RCF {
38 
39  // Enumeration describing the role in a SSL conversation that an endpoint is playing.
40  enum SslRole
41  {
42  SslServer,
43  SslClient
44  };
45 
46  class OpenSslEncryptionFilter;
47  class OpenSslEncryptionFilterImpl;
48 
49  typedef boost::function1<bool, Certificate *> CertificateValidationCb;
50 
52  class RCF_EXPORT PemCertificate : public Certificate
53  {
54  public:
55 
56  // *** SWIG BEGIN ***
57 
59  PemCertificate(const std::string & pathToCert, const std::string & password = "");
60 
61  // *** SWIG END ***
62 
63  private:
64 
65  friend class OpenSslEncryptionFilter;
66  friend class OpenSslEncryptionFilterFactory;
67 
68  std::string mPathToCert;
69  std::string mPassword;
70  };
71 
72  class OpenSslDll;
73  class OpenSslCryptoDll;
74 
76  class RCF_EXPORT X509Certificate : public Certificate
77  {
78  public:
79 
80  // *** SWIG BEGIN ***
81 
82  virtual CertificateImplementationType _getType()
83  {
84  return Cit_X509;
85  }
86 
88  std::string getCertificateName();
89 
91  std::string getIssuerName();
92 
93  // *** SWIG END ***
94 
95  X509Certificate(X509 * pX509);
96 
97  X509 * getX509();
98 
99  private:
100  OpenSslDll & mSslDll;
101  OpenSslCryptoDll & mCryptoDll;
102  X509 * mpX509;
103  };
104 
105  typedef boost::shared_ptr<X509Certificate> X509CertificatePtr;
106 
107  class ClientStub;
108 
109  class RCF_EXPORT OpenSslEncryptionFilter : public Filter, boost::noncopyable
110  {
111  public:
112  int getFilterId() const;
113 
114  public:
115 
116  OpenSslEncryptionFilter(
117  ClientStub * pClientStub,
118  SslRole sslRole = SslClient,
119  unsigned int bioBufferSize = 2048);
120 
121  OpenSslEncryptionFilter(
122  const std::string & certificateFile,
123  const std::string & certificateFilePassword,
124  const std::string & caCertificate,
125  const std::string & ciphers,
126  CertificateValidationCb verifyFunctor,
127  SslRole sslRole = SslClient,
128  unsigned int bioBufferSize = 2048);
129 
130  void resetState();
131  void read(const ByteBuffer &byteBuffer, std::size_t bytesRequested);
132  void write(const std::vector<ByteBuffer> &byteBuffers);
133  void onReadCompleted(const ByteBuffer &byteBuffer);
134  void onWriteCompleted(std::size_t bytesTransferred);
135 
136  SSL * getSSL();
137  SSL_CTX * getCTX();
138 
139  CertificatePtr getPeerCertificate();
140 
141  private:
142  friend class OpenSslEncryptionFilterImpl;
143  boost::shared_ptr<OpenSslEncryptionFilterImpl> mImplPtr;
144  };
145 
146  class OpenSslEncryptionFilterFactory : public FilterFactory
147  {
148  public:
149  OpenSslEncryptionFilterFactory();
150 
151  FilterPtr createFilter(RcfServer & server);
152  int getFilterId();
153 
154  private:
155  SslRole mRole;
156  };
157 
158 
159 } // namespace RCF
160 
161 #endif // ! INCLUDE_RCF_OPENSSLENCRYPTIONFILTER_HPP