1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #include <IceSSL/PluginI.h>
6 #include <IceSSL/OpenSSL.h>
7 #include <IceSSL/OpenSSLEngine.h>
8 
9 #include <Ice/Initialize.h>
10 
11 using namespace std;
12 
13 namespace
14 {
15 
16 class PluginI : public IceSSL::PluginI,
17                 public IceSSL::OpenSSL::Plugin
18 {
19 public:
20 
21     PluginI(const Ice::CommunicatorPtr&);
22 
23     virtual Ice::Long getOpenSSLVersion() const;
24     virtual IceSSL::CertificatePtr create(x509_st*) const;
25     virtual IceSSL::CertificatePtr load(const std::string&) const;
26     virtual IceSSL::CertificatePtr decode(const std::string&) const;
27     virtual void setContext(SSL_CTX*);
28     virtual SSL_CTX* getContext();
29 };
30 
31 } // anonymous namespace end
32 
33 //
34 // Plugin implementation.
35 //
PluginI(const Ice::CommunicatorPtr & com)36 PluginI::PluginI(const Ice::CommunicatorPtr& com) :
37     IceSSL::PluginI(com, new IceSSL::OpenSSL::SSLEngine(com))
38 {
39 }
40 
41 Ice::Long
getOpenSSLVersion() const42 PluginI::getOpenSSLVersion() const
43 {
44     return SSLeay();
45 }
46 
47 IceSSL::CertificatePtr
create(x509_st * cert) const48 PluginI::create(x509_st* cert) const
49 {
50     return IceSSL::OpenSSL::Certificate::create(cert);
51 }
52 
53 IceSSL::CertificatePtr
load(const std::string & file) const54 PluginI::load(const std::string& file) const
55 {
56     return IceSSL::OpenSSL::Certificate::load(file);
57 }
58 
59 IceSSL::CertificatePtr
decode(const std::string & encoding) const60 PluginI::decode(const std::string& encoding) const
61 {
62     return IceSSL::OpenSSL::Certificate::load(encoding);
63 }
64 
65 void
setContext(SSL_CTX * context)66 PluginI::setContext(SSL_CTX* context)
67 {
68     IceSSL::OpenSSL::SSLEngine* engine = dynamic_cast<IceSSL::OpenSSL::SSLEngine*>(_engine.get());
69     assert(engine);
70     engine->context(context);
71 }
72 
73 SSL_CTX*
getContext()74 PluginI::getContext()
75 {
76     IceSSL::OpenSSL::SSLEngine* engine = dynamic_cast<IceSSL::OpenSSL::SSLEngine*>(_engine.get());
77     assert(engine);
78     return engine->context();
79 }
80 
81 #ifdef _WIN32
82 //
83 // Plug-in factory function.
84 //
85 extern "C" ICESSL_OPENSSL_API Ice::Plugin*
createIceSSLOpenSSL(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)86 createIceSSLOpenSSL(const Ice::CommunicatorPtr& communicator, const string& /*name*/, const Ice::StringSeq& /*args*/)
87 {
88     return new PluginI(communicator);
89 }
90 
91 namespace Ice
92 {
93 
94 ICESSL_OPENSSL_API void
registerIceSSLOpenSSL(bool loadOnInitialize)95 registerIceSSLOpenSSL(bool loadOnInitialize)
96 {
97     Ice::registerPluginFactory("IceSSL", createIceSSLOpenSSL, loadOnInitialize);
98 }
99 
100 }
101 
102 #else
103 
104 extern "C" ICESSL_API Ice::Plugin*
createIceSSL(const Ice::CommunicatorPtr & communicator,const string &,const Ice::StringSeq &)105 createIceSSL(const Ice::CommunicatorPtr& communicator, const string& /*name*/, const Ice::StringSeq& /*args*/)
106 {
107     return new PluginI(communicator);
108 }
109 
110 //
111 // The following functions are defined only when OpenSSL is the default
112 // implementation. In Windows the default implementation is always
113 // SChannel.
114 //
115 IceSSL::CertificatePtr
load(const std::string & file)116 IceSSL::Certificate::load(const std::string& file)
117 {
118     return IceSSL::OpenSSL::Certificate::load(file);
119 }
120 
121 IceSSL::CertificatePtr
decode(const std::string & encoding)122 IceSSL::Certificate::decode(const std::string& encoding)
123 {
124     return IceSSL::OpenSSL::Certificate::decode(encoding);
125 }
126 
127 #endif
128