diff --git a/NetSSL_OpenSSL/Makefile b/NetSSL_OpenSSL/Makefile new file mode 100644 index 000000000..b2ee3d39b --- /dev/null +++ b/NetSSL_OpenSSL/Makefile @@ -0,0 +1,27 @@ +# +# Makefile +# +# $Id: //poco/Main/template/library.make#2 $ +# +# Makefile for Poco NetSSL_OpenSSL +# + +include $(POCO_BASE)/build/rules/global + +SYSLIBS += -lssl -lcrypto + +objects = AcceptCertificateHandler CertificateHandlerFactory \ + CertificateHandlerFactoryMgr ConsoleCertificateHandler \ + Context HTTPSClientSession HTTPSStreamFactory HTTPSSessionInstantiator \ + SSLInitializer InvalidCertificateHandler KeyConsoleHandler \ + KeyFileHandler PrivateKeyFactory PrivateKeyFactoryMgr \ + PrivateKeyPassphraseHandler SecureServerSocket SecureServerSocketImpl \ + SecureSocketImpl SecureStreamSocket SecureStreamSocketImpl \ + SSLException SSLManager Utility VerificationErrorArgs \ + X509Certificate + +target = PocoNetSSL +target_version = $(LIBVERSION) +target_libs = PocoFoundation PocoNet PocoUtil + +include $(POCO_BASE)/build/rules/lib diff --git a/NetSSL_OpenSSL/NetSSL_OpenSSL_vs71.sln b/NetSSL_OpenSSL/NetSSL_OpenSSL_vs71.sln new file mode 100644 index 000000000..543e6fe06 --- /dev/null +++ b/NetSSL_OpenSSL/NetSSL_OpenSSL_vs71.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NetSSL_OpenSSL", "NetSSL_OpenSSL_vs71.vcproj", "{5AECC55E-A469-11DA-8DA6-005056C00008}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestSuite", "testsuite\TestSuite_vs71.vcproj", "{5B023A89-A469-11DA-8DA6-005056C00008}" + ProjectSection(ProjectDependencies) = postProject + {5AECC55E-A469-11DA-8DA6-005056C00008} = {5AECC55E-A469-11DA-8DA6-005056C00008} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + debug_shared = debug_shared + release_shared = release_shared + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {5AECC55E-A469-11DA-8DA6-005056C00008}.debug_shared.ActiveCfg = debug_shared|Win32 + {5AECC55E-A469-11DA-8DA6-005056C00008}.debug_shared.Build.0 = debug_shared|Win32 + {5AECC55E-A469-11DA-8DA6-005056C00008}.release_shared.ActiveCfg = release_shared|Win32 + {5AECC55E-A469-11DA-8DA6-005056C00008}.release_shared.Build.0 = release_shared|Win32 + {5B023A89-A469-11DA-8DA6-005056C00008}.debug_shared.ActiveCfg = debug_shared|Win32 + {5B023A89-A469-11DA-8DA6-005056C00008}.debug_shared.Build.0 = debug_shared|Win32 + {5B023A89-A469-11DA-8DA6-005056C00008}.release_shared.ActiveCfg = release_shared|Win32 + {5B023A89-A469-11DA-8DA6-005056C00008}.release_shared.Build.0 = release_shared|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/NetSSL_OpenSSL/NetSSL_OpenSSL_vs71.vcproj b/NetSSL_OpenSSL/NetSSL_OpenSSL_vs71.vcproj new file mode 100644 index 000000000..ee3b2504e --- /dev/null +++ b/NetSSL_OpenSSL/NetSSL_OpenSSL_vs71.vcproj @@ -0,0 +1,337 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NetSSL_OpenSSL/NetSSL_OpenSSL_vs80.sln b/NetSSL_OpenSSL/NetSSL_OpenSSL_vs80.sln new file mode 100644 index 000000000..9ef5fc007 --- /dev/null +++ b/NetSSL_OpenSSL/NetSSL_OpenSSL_vs80.sln @@ -0,0 +1,29 @@ + +Microsoft Visual Studio Solution File, Format Version 9.00 +# Visual Studio 2005 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NetSSL_OpenSSL", "NetSSL_OpenSSL_vs80.vcproj", "{5AECC55E-A469-11DA-8DA6-005056C00008}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestSuite", "testsuite\TestSuite_vs80.vcproj", "{B2B88092-5BCE-4AC0-941E-88167138B4A7}" + ProjectSection(ProjectDependencies) = postProject + {5AECC55E-A469-11DA-8DA6-005056C00008} = {5AECC55E-A469-11DA-8DA6-005056C00008} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + debug_shared|Win32 = debug_shared|Win32 + release_shared|Win32 = release_shared|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5AECC55E-A469-11DA-8DA6-005056C00008}.debug_shared|Win32.ActiveCfg = debug_shared|Win32 + {5AECC55E-A469-11DA-8DA6-005056C00008}.debug_shared|Win32.Build.0 = debug_shared|Win32 + {5AECC55E-A469-11DA-8DA6-005056C00008}.release_shared|Win32.ActiveCfg = release_shared|Win32 + {5AECC55E-A469-11DA-8DA6-005056C00008}.release_shared|Win32.Build.0 = release_shared|Win32 + {B2B88092-5BCE-4AC0-941E-88167138B4A7}.debug_shared|Win32.ActiveCfg = debug_shared|Win32 + {B2B88092-5BCE-4AC0-941E-88167138B4A7}.debug_shared|Win32.Build.0 = debug_shared|Win32 + {B2B88092-5BCE-4AC0-941E-88167138B4A7}.release_shared|Win32.ActiveCfg = release_shared|Win32 + {B2B88092-5BCE-4AC0-941E-88167138B4A7}.release_shared|Win32.Build.0 = release_shared|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/NetSSL_OpenSSL/NetSSL_OpenSSL_vs80.vcproj b/NetSSL_OpenSSL/NetSSL_OpenSSL_vs80.vcproj new file mode 100644 index 000000000..6216fc5f6 --- /dev/null +++ b/NetSSL_OpenSSL/NetSSL_OpenSSL_vs80.vcproj @@ -0,0 +1,451 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NetSSL_OpenSSL/doc/howtobuild.txt b/NetSSL_OpenSSL/doc/howtobuild.txt new file mode 100644 index 000000000..cc01242c4 --- /dev/null +++ b/NetSSL_OpenSSL/doc/howtobuild.txt @@ -0,0 +1,36 @@ +NetSSL requires OpenSSL release 0.9.8 or later. + +Unix +==== + +Most Unix systems already have OpenSSL preinstalled. +If your system does not have OpenSSL, please get it from +http://www.openssl.org/ or another source. You do not +have to build OpenSSL yourself - a binary distribution +is fine. + +If you do build OpenSSl yourself be sure to call + +./configure --prefix=/usr --openssldir/usr/openssl +make +make install (with superuser rights) + +so that poco works out of the box + + +Windows +======= + +The easiest way to install OpenSSL on Windows is to +use a binary (prebuild) release, for example the +one from Shining Light Productions that comes with +a Windows installer. + +1. Download OpenSSL (at least v0.98a) from: + http://www.slproweb.com/products/Win32OpenSSL.html +2. Install OpenSSL (we assume you install to c:\OpenSSL) +3. Start Visual Studio, go to Tools->Options, under Projects->VC++ Directories + add the following directories: + - Include Files: C:\OpenSSL\include + - Library Files: C:\OpenSSL\lib\VC +4. You are now ready to build NetSSL. diff --git a/NetSSL_OpenSSL/include/Poco/Net/AcceptCertificateHandler.h b/NetSSL_OpenSSL/include/Poco/Net/AcceptCertificateHandler.h new file mode 100644 index 000000000..30b6e85fa --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/AcceptCertificateHandler.h @@ -0,0 +1,81 @@ +// +// AcceptCertificateHandler.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/AcceptCertificateHandler.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: AcceptCertificateHandler +// +// Definition of the AcceptCertificateHandler class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_AcceptCertificateHandler_INCLUDED +#define NetSSL_AcceptCertificateHandler_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/InvalidCertificateHandler.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API AcceptCertificateHandler: public InvalidCertificateHandler + /// A AcceptCertificateHandler is invoked whenever an error + /// occurs verifying the certificate. It always accepts + /// the certificate. Only use this one during testing! +{ +public: + AcceptCertificateHandler(bool handleErrorsOnServerSide); + /// Creates the AcceptCertificateHandler + + virtual ~AcceptCertificateHandler(); + /// Destroys the AcceptCertificateHandler. + + void onInvalidCertificate(const void* pSender, VerificationErrorArgs& errorCert); + /// Receives the questionable certificate in parameter errorCert. If one wants to accept the + /// certificate, call errorCert.setIgnoreError(true). +}; + + +// +// inlines +// +inline void AcceptCertificateHandler::onInvalidCertificate(const void*, VerificationErrorArgs& errorCert) +{ + errorCert.setIgnoreError(true); +} + + +} } // namespace Poco::Net + + +#endif // NetSSL_AcceptCertificateHandler_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/CertificateHandlerFactory.h b/NetSSL_OpenSSL/include/Poco/Net/CertificateHandlerFactory.h new file mode 100644 index 000000000..2b3a17e1c --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/CertificateHandlerFactory.h @@ -0,0 +1,103 @@ +// +// CertificateHandlerFactory.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/CertificateHandlerFactory.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: CertificateHandlerFactory +// +// Definition of the CertificateHandlerFactory class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_CertificateHandlerFactory_INCLUDED +#define NetSSL_CertificateHandlerFactory_INCLUDED + + +#include "Poco/Net/NetSSL.h" + + +namespace Poco { +namespace Net { + + +class InvalidCertificateHandler; + + +class NetSSL_API CertificateHandlerFactory + /// A CertificateHandlerFactory is responsible for creating InvalidCertificateHandlers. + /// You don't need to access this class directly. Use the macro + /// POCO_REGISTER_CHFACTORY(namespace, InvalidCertificateHandlerName) + /// instead (see the documentation of InvalidCertificateHandler for an example). +{ +public: + CertificateHandlerFactory(); + /// Creates the CertificateHandlerFactory. + + virtual ~CertificateHandlerFactory(); + /// Destroys the CertificateHandlerFactory. + + virtual InvalidCertificateHandler* create(bool server) const = 0; + /// Creates a new InvalidCertificateHandler. Set server to true if the certificate handler is used on the server side. +}; + + +class NetSSL_API CertificateHandlerFactoryRegistrar + /// Registrar class which automatically registers CertificateHandlerFactory at the CertificateHandlerFactoryMgr. + /// You don't need to access this class directly. Use the macro + /// POCO_REGISTER_CHFACTORY(namespace, InvalidCertificateHandlerName) + /// instead (see the documentation of InvalidCertificateHandler for an example). +{ +public: + CertificateHandlerFactoryRegistrar(const std::string& name, CertificateHandlerFactory* pFactory); + /// Registers the CertificateHandlerFactory with the given name at the factory manager. + + virtual ~CertificateHandlerFactoryRegistrar(); + /// Destroys the CertificateHandlerFactoryRegistrar. +}; + + +#define POCO_REGISTER_CHFACTORY(API, PKCLS) \ + class API PKCLS##Factory: public Poco::Net::CertificateHandlerFactory \ + { \ + public: \ + PKCLS##Factory(){} \ + ~PKCLS##Factory(){} \ + Poco::Net::InvalidCertificateHandler* create(bool server) const \ + { \ + return new PKCLS(server); \ + } \ + }; \ + static Poco::Net::CertificateHandlerFactoryRegistrar aRegistrar(std::string(#PKCLS), new PKCLS##Factory()); + + +} } // namespace Poco::Net + + +#endif // NetSSL_CertificateHandlerFactory_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/CertificateHandlerFactoryMgr.h b/NetSSL_OpenSSL/include/Poco/Net/CertificateHandlerFactoryMgr.h new file mode 100644 index 000000000..935032ac9 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/CertificateHandlerFactoryMgr.h @@ -0,0 +1,86 @@ +// +// CertificateHandlerFactoryMgr.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/CertificateHandlerFactoryMgr.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: CertificateHandlerFactoryMgr +// +// Definition of the CertificateHandlerFactoryMgr class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_CertificateHandlerFactoryMgr_INCLUDED +#define NetSSL_CertificateHandlerFactoryMgr_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/CertificateHandlerFactory.h" +#include "Poco/SharedPtr.h" +#include + + +namespace Poco { +namespace Net { + + +class NetSSL_API CertificateHandlerFactoryMgr + /// A CertificateHandlerFactoryMgr manages all existing CertificateHandlerFactories. +{ +public: + typedef std::map > FactoriesMap; + + CertificateHandlerFactoryMgr(); + /// Creates the CertificateHandlerFactoryMgr. + + ~CertificateHandlerFactoryMgr(); + /// Destroys the CertificateHandlerFactoryMgr. + + void setFactory(const std::string& name, CertificateHandlerFactory* pFactory); + /// Registers the factory. Class takes ownership of the pointer. + /// If a factory with the same name already exists, an exception is thrown. + + bool hasFactory(const std::string& name) const; + /// Returns true if for the given name a factory is already registered + + const CertificateHandlerFactory* getFactory(const std::string& name) const; + /// Returns NULL if for the given name a factory does not exist, otherwise the factory is returned + + void removeFactory(const std::string& name); + /// Removes the factory from the manager. + +private: + FactoriesMap _factories; +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_CertificateHandlerFactoryMgr_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/ConsoleCertificateHandler.h b/NetSSL_OpenSSL/include/Poco/Net/ConsoleCertificateHandler.h new file mode 100644 index 000000000..87d10645d --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/ConsoleCertificateHandler.h @@ -0,0 +1,71 @@ +// +// ConsoleCertificateHandler.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/ConsoleCertificateHandler.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: ConsoleCertificateHandler +// +// Definition of the ConsoleCertificateHandler class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_ConsoleCertificateHandler_INCLUDED +#define NetSSL_ConsoleCertificateHandler_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/InvalidCertificateHandler.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API ConsoleCertificateHandler: public InvalidCertificateHandler + /// A ConsoleCertificateHandler is invoked whenever an error occurs verifying the certificate. + /// The certificate is printed to stdout and the user is asked via console if he wants to accept it. +{ +public: + ConsoleCertificateHandler(bool handleErrorsOnServerSide); + /// Creates the ConsoleCertificateHandler. + + virtual ~ConsoleCertificateHandler(); + /// Destroys the ConsoleCertificateHandler. + + void onInvalidCertificate(const void* pSender, VerificationErrorArgs& errorCert); + /// Prints the certificate to stdout and waits for user input on the console + /// to decide if a certificate should be accepted/rejected. +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_ConsoleCertificateHandler_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/Context.h b/NetSSL_OpenSSL/include/Poco/Net/Context.h new file mode 100644 index 000000000..dd5345fab --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/Context.h @@ -0,0 +1,119 @@ +// +// Context.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/Context.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: Context +// +// Definition of the Context class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_Context_INCLUDED +#define NetSSL_Context_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include + + +namespace Poco { +namespace Net { + + +class NetSSL_API Context + /// This class encapsulates an SSL Context. +{ +public: + enum VerificationMode + { + VERIFY_NONE = SSL_VERIFY_NONE, + VERIFY_RELAXED = SSL_VERIFY_PEER, + VERIFY_STRICT = SSL_VERIFY_FAIL_IF_NO_PEER_CERT, + VERIFY_ONCE = SSL_VERIFY_CLIENT_ONCE + }; + + Context(const std::string& privateKeyFile, + const std::string& caLocation, + bool isServerContext, + VerificationMode verMode = VERIFY_STRICT, + int verificationDepth = 9, + bool loadCAFromDefaultPath = false, + const std::string& cypherList = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); + /// Creates a context. + /// privateKeyFile contains the key used for encryption, caLocation can either + /// be a directory or a single file containing the certificates for certificate authorities. + /// isServerContext defines if the context belongs to a server or client. + /// verificationDepth sets the upper limit for verification chain sizes. If we encounter + /// a chain larger than that limit, verification will fail. + /// Cypherlist defines which protocols are allowed. + /// Creates the Context. + + ~Context(); + /// Destroys the Context. + + SSL_CTX* sslContext() const; + + Context::VerificationMode verificationMode() const; + + bool serverContext() const; + +private: + SSL_CTX* _pSSLContext; + Context::VerificationMode _mode; + bool _server; +}; + + +// +// inlines +// +inline SSL_CTX* Context::sslContext() const +{ + return _pSSLContext; +} + + +inline Context::VerificationMode Context::verificationMode() const +{ + return _mode; +} + + +inline bool Context::serverContext() const +{ + return _server; +} + + +} } // namespace Poco::Net + + +#endif // NetSSL_Context_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/HTTPSClientSession.h b/NetSSL_OpenSSL/include/Poco/Net/HTTPSClientSession.h new file mode 100644 index 000000000..d63128eee --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/HTTPSClientSession.h @@ -0,0 +1,129 @@ +// +// HTTPSClientSession.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/HTTPSClientSession.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: HTTPSClient +// Module: HTTPSClientSession +// +// Definition of the HTTPSClientSession class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_HTTPSClientSession_INCLUDED +#define NetSSL_HTTPSClientSession_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/Utility.h" +#include "Poco/Net/HTTPClientSession.h" + + +namespace Poco { +namespace Net { + + +class SecureStreamSocket; +class HTTPRequest; +class HTTPResponse; + + +class NetSSL_API HTTPSClientSession: public HTTPClientSession + /// This class implements the client-side of + /// a HTTPS session. + /// + /// To send a HTTPS request to a HTTPS server, first + /// instantiate a HTTPSClientSession object and + /// specify the server's host name and port number. + /// + /// Then create a HTTPRequest object, fill it accordingly, + /// and pass it as argument to the sendRequst() method. + /// + /// sendRequest() will return an output stream that can + /// be used to send the request body, if there is any. + /// + /// After you are done sending the request body, create + /// a HTTPResponse object and pass it to receiveResponse(). + /// + /// This will return an input stream that can be used to + /// read the response body. + /// + /// See RFC 2616 for more + /// information about the HTTP protocol. +{ +public: + HTTPSClientSession(); + /// Creates an unconnected HTTPSClientSession. + + HTTPSClientSession(const SecureStreamSocket& socket); + /// Creates a HTTPSClientSession using the given socket. + /// The socket must not be connected. The session + /// takes ownership of the socket. + + HTTPSClientSession(const std::string& host, Poco::UInt16 port = Utility::HTTPS_PORT); + /// Creates a HTTPSClientSession using the given host and port. + + ~HTTPSClientSession(); + /// Destroys the HTTPSClientSession and closes + /// the underlying socket. + + std::ostream& sendRequest(HTTPRequest& request); + /// Sends the header for the given HTTPS request to + /// the server. + /// + /// The HTTPSClientSession will set the request's + /// Host and Keep-Alive headers accordingly. + /// + /// The returned output stream can be used to write + /// the request body. The stream is valid until + /// receiveResponse() is called or the session + /// is destroyed. + + std::istream& receiveResponse(HTTPResponse& response); + /// Receives the header for the response to the previous + /// HTTPS request. + /// + /// The returned input stream can be used to read + /// the response body. The stream is valid until + /// sendRequest() is called or the session is + /// destroyed. + +protected: + void connect(const SocketAddress& address); + // Connects the socket to the server. + + std::string getHostInfo() const; + /// Returns the target host and port number for proxy requests. +}; + + +} } // namespace Poco::Net + + +#endif // Net_HTTPSClientSession_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/HTTPSSessionInstantiator.h b/NetSSL_OpenSSL/include/Poco/Net/HTTPSSessionInstantiator.h new file mode 100644 index 000000000..ee0952f99 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/HTTPSSessionInstantiator.h @@ -0,0 +1,77 @@ +// +// HTTPSSessionInstantiator.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/HTTPSSessionInstantiator.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: HTTPSClient +// Module: HTTPSSessionInstantiator +// +// Definition of the HTTPSSessionInstantiator class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef Net_HTTPSSessionInstantiator_INCLUDED +#define Net_HTTPSSessionInstantiator_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/Utility.h" +#include "Poco/Net/HTTPSessionInstantiator.h" +#include "Poco/URI.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API HTTPSSessionInstantiator: public HTTPSessionInstantiator + /// The HTTPSessionInstantiator for HTTPSClientSession. +{ +public: + HTTPSSessionInstantiator(); + /// Creates the HTTPSSessionInstantiator. + + ~HTTPSSessionInstantiator(); + /// Destroys the HTTPSSessionInstantiator. + + HTTPClientSession* createClientSession(const Poco::URI& uri); + /// Creates a HTTPSClientSession for the given URI. + + static void registerInstantiator(); + /// Registers the instantiator with the global HTTPSessionFactory. + + static void unregisterInstantiator(); + /// Unregisters the factory with the global HTTPSessionFactory. +}; + + +} } // namespace Poco::Net + + +#endif // Net_HTTPSSessionInstantiator_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/HTTPSStreamFactory.h b/NetSSL_OpenSSL/include/Poco/Net/HTTPSStreamFactory.h new file mode 100644 index 000000000..8296be1a4 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/HTTPSStreamFactory.h @@ -0,0 +1,92 @@ +// +// HTTPSStreamFactory.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/HTTPSStreamFactory.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: HTTPSClient +// Module: HTTPSStreamFactory +// +// Definition of the HTTPSStreamFactory class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_HTTPSStreamFactory_INCLUDED +#define NetSSL_HTTPSStreamFactory_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/HTTPSession.h" +#include "Poco/URIStreamFactory.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API HTTPSStreamFactory: public Poco::URIStreamFactory + /// An implementation of the URIStreamFactory interface + /// that handles Hyper-Text Transfer Protocol (http) URIs. +{ +public: + HTTPSStreamFactory(); + /// Creates the HTTPSStreamFactory. + + HTTPSStreamFactory(const std::string& proxyHost, Poco::UInt16 proxyPort = HTTPSession::HTTP_PORT); + /// Creates the HTTPSStreamFactory. + /// + /// HTTP connections will use the given proxy. + + ~HTTPSStreamFactory(); + /// Destroys the HTTPSStreamFactory. + + std::istream* open(const Poco::URI& uri); + /// Creates and opens a HTTPS stream for the given URI. + /// The URI must be a https://... URI. + /// + /// Throws a NetException if anything goes wrong. + + static void registerFactory(); + /// Registers the HTTPSStreamFactory with the + /// default URIStreamOpener instance. + +private: + enum + { + MAX_REDIRECTS = 10 + }; + + std::string _proxyHost; + Poco::UInt16 _proxyPort; +}; + + +} } // namespace Poco::Net + + +#endif // Net_HTTPSStreamFactory_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/InvalidCertificateHandler.h b/NetSSL_OpenSSL/include/Poco/Net/InvalidCertificateHandler.h new file mode 100644 index 000000000..a1a853a21 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/InvalidCertificateHandler.h @@ -0,0 +1,100 @@ +// +// InvalidCertificateHandler.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/InvalidCertificateHandler.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: InvalidCertificateHandler +// +// Definition of the InvalidCertificateHandler class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_InvalidCertificateHandler_INCLUDED +#define NetSSL_InvalidCertificateHandler_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/VerificationErrorArgs.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API InvalidCertificateHandler + /// A InvalidCertificateHandler is invoked whenever an error occurs verifying the certificate. It allows the user + /// to inspect and accept/reject the certificate. + /// One can install one's own InvalidCertificateHandler by implementing this interface. Note that + /// in the cpp file of the subclass the following code must be present (assuming you use the namespace My_API + /// and the name of your handler class is MyGuiHandler): + /// + /// #include "Poco/Net/CertificateHandlerFactory.h" + /// ... + /// POCO_REGISTER_CHFACTORY(My_API, MyGuiHandler) + /// + /// One can either set the handler directly in the startup code of the main method of ones application by calling + /// + /// SSLManager::instance().initialize(mypassphraseHandler, myguiHandler, mySSLContext) + /// + /// or in case one uses Poco::Util::Application one can rely on an XML configuration and put the following entry + /// under the path openSSL.invalidCertificateHandler: + /// + /// + /// MyGuiHandler + /// + /// [...] // Put optional config params for the handler here + /// + /// + /// Note that the name of the InvalidCertificateHandler must be same as the one provided to the POCO_REGISTER_CHFACTORY macro. +{ +public: + InvalidCertificateHandler(bool handleErrorsOnServerSide); + /// Creates the InvalidCertificateHandler. + /// Set handleErrorsOnServerSide to true if the certificate handler is used on the server side. + /// Automatically registers at one of the SSLManager::VerificationError events. + + virtual ~InvalidCertificateHandler(); + /// Destroys the InvalidCertificateHandler. + + virtual void onInvalidCertificate(const void* pSender, VerificationErrorArgs& errorCert) = 0; + /// Receives the questionable certificate in parameter errorCert. If one wants to accept the + /// certificate, call errorCert.setIgnoreError(true). + +protected: + bool _handleErrorsOnServerSide; + /// Stores if the certificate handler gets invoked by the server (i.e. a client certificate is wrong) + /// or the client (a server certificate is wrong) +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_InvalidCertificateHandler_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/KeyConsoleHandler.h b/NetSSL_OpenSSL/include/Poco/Net/KeyConsoleHandler.h new file mode 100644 index 000000000..7b5bada57 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/KeyConsoleHandler.h @@ -0,0 +1,68 @@ +// +// KeyConsoleHandler.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/KeyConsoleHandler.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: KeyConsoleHandler +// +// Definition of the KeyConsoleHandler class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_KeyConsoleHandler_INCLUDED +#define NetSSL_KeyConsoleHandler_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/PrivateKeyPassphraseHandler.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API KeyConsoleHandler: public PrivateKeyPassphraseHandler + /// Class KeyConsoleHandler. Reads the key for a certificate from the console. +{ +public: + KeyConsoleHandler(bool server); + /// Creates the KeyConsoleHandler. + + ~KeyConsoleHandler(); + /// Destroys the KeyConsoleHandler. + + void onPrivateKeyRequested(const void* pSender, std::string& privateKey); +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_KeyConsoleHandler_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/KeyFileHandler.h b/NetSSL_OpenSSL/include/Poco/Net/KeyFileHandler.h new file mode 100644 index 000000000..69c2f4867 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/KeyFileHandler.h @@ -0,0 +1,72 @@ +// +// KeyFileHandler.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/KeyFileHandler.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: KeyFileHandler +// +// Definition of the KeyFileHandler class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_KeyFileHandler_INCLUDED +#define NetSSL_KeyFileHandler_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/PrivateKeyPassphraseHandler.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API KeyFileHandler: public PrivateKeyPassphraseHandler + /// Class KeyFileHandler. Reads the key for a certificate from a configuration file + /// under the path "openSSL.privateKeyPassphraseHandler.options.password". +{ +public: + KeyFileHandler(bool server); + /// Creates the KeyFileHandler. + + virtual ~KeyFileHandler(); + /// Destroys the KeyFileHandler. + + void onPrivateKeyRequested(const void* pSender, std::string& privateKey); + +private: + static const std::string CFG_PRIV_KEY_FILE; +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_KeyFileHandler_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/NetSSL.h b/NetSSL_OpenSSL/include/Poco/Net/NetSSL.h new file mode 100644 index 000000000..28572d950 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/NetSSL.h @@ -0,0 +1,70 @@ +// +// NetSSL.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/NetSSL.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: OpenSSL +// +// Basic definitions for the Poco OpenSSL library. +// This file must be the first file included by every other OpenSSL +// header file. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_NetSSL_INCLUDED +#define NetSSL_NetSSL_INCLUDED + + +#include "Poco/Net/Net.h" + + +// +// The following block is the standard way of creating macros which make exporting +// from a DLL simpler. All files within this DLL are compiled with the NetSSL_EXPORTS +// symbol defined on the command line. this symbol should not be defined on any project +// that uses this DLL. This way any other project whose source files include this file see +// NetSSL_API functions as being imported from a DLL, wheras this DLL sees symbols +// defined with this macro as being exported. +// +#if (defined(_WIN32) || defined(__CYGWIN__)) && defined(POCO_DLL) + #if defined(NetSSL_EXPORTS) + #define NetSSL_API __declspec(dllexport) + #else + #define NetSSL_API __declspec(dllimport) + #endif +#endif + + +#if !defined(NetSSL_API) + #define NetSSL_API +#endif + + +#endif // NetSSL_NetSSL_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyFactory.h b/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyFactory.h new file mode 100644 index 000000000..a39e98c4c --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyFactory.h @@ -0,0 +1,104 @@ +// +// PrivateKeyFactory.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyFactory.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: PrivateKeyFactory +// +// Definition of the PrivateKeyFactory class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_PrivateKeyFactory_INCLUDED +#define NetSSL_PrivateKeyFactory_INCLUDED + + +#include "Poco/Net/NetSSL.h" + + +namespace Poco { +namespace Net { + + +class PrivateKeyPassphraseHandler; + + +class NetSSL_API PrivateKeyFactory + /// A PrivateKeyFactory is responsible for creating PrivateKeyPassphraseHandlers. + /// You don't need to access this class directly. Use the macro + /// POCO_REGISTER_KEYFACTORY(namespace, PrivateKeyPassphraseHandlerName) + /// instead (see the documentation of PrivateKeyPassphraseHandler for an example). +{ +public: + PrivateKeyFactory(); + /// Creates the PrivateKeyFactory. + + virtual ~PrivateKeyFactory(); + /// Destroys the PrivateKeyFactory. + + virtual PrivateKeyPassphraseHandler* create(bool onServer) const = 0; + /// Creates a new PrivateKeyPassphraseHandler +}; + + +class NetSSL_API PrivateKeyFactoryRegistrar + /// Registrar class which automatically registers PrivateKeyFactories at the PrivateKeyFactoryMgr. + /// You don't need to access this class directly. Use the macro + /// POCO_REGISTER_KEYFACTORY(namespace, PrivateKeyPassphraseHandlerName) + /// instead (see the documentation of PrivateKeyPassphraseHandler for an example). + +{ +public: + PrivateKeyFactoryRegistrar(const std::string& name, PrivateKeyFactory* pFactory); + /// Registers the PrivateKeyFactory with the given name at the factory manager. + + virtual ~PrivateKeyFactoryRegistrar(); + /// Destroys the PrivateKeyFactoryRegistrar. +}; + + +#define POCO_REGISTER_KEYFACTORY(API, PKCLS) \ + class API PKCLS##Factory: public Poco::Net::PrivateKeyFactory \ + { \ + public: \ + PKCLS##Factory(){} \ + ~PKCLS##Factory(){} \ + Poco::Net::PrivateKeyPassphraseHandler* create(bool server) const \ + { \ + return new PKCLS(server); \ + } \ + }; \ + static Poco::Net::PrivateKeyFactoryRegistrar aRegistrar(std::string(#PKCLS), new PKCLS##Factory()); + + +} } // namespace Poco::Net + + +#endif // NetSSL_PrivateKeyFactory_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyFactoryMgr.h b/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyFactoryMgr.h new file mode 100644 index 000000000..87e7991e0 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyFactoryMgr.h @@ -0,0 +1,86 @@ +// +// PrivateKeyFactoryMgr.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyFactoryMgr.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: PrivateKeyFactoryMgr +// +// Definition of the PrivateKeyFactoryMgr class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_PrivateKeyFactoryMgr_INCLUDED +#define NetSSL_PrivateKeyFactoryMgr_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/PrivateKeyFactory.h" +#include "Poco/SharedPtr.h" +#include + + +namespace Poco { +namespace Net { + + +class NetSSL_API PrivateKeyFactoryMgr + /// A PrivateKeyFactoryMgr manages all existing PrivateKeyFactories. +{ +public: + typedef std::map > FactoriesMap; + + PrivateKeyFactoryMgr(); + /// Creates the PrivateKeyFactoryMgr. + + ~PrivateKeyFactoryMgr(); + /// Destroys the PrivateKeyFactoryMgr. + + void setFactory(const std::string& name, PrivateKeyFactory* pFactory); + /// Registers the factory. Class takes ownership of the pointer. + /// If a factory with the same name already exists, an exception is thrown. + + bool hasFactory(const std::string& name) const; + /// Returns true if for the given name a factory is already registered + + const PrivateKeyFactory* getFactory(const std::string& name) const; + /// Returns NULL if for the given name a factory does not exist, otherwise the factory is returned + + void removeFactory(const std::string& name); + /// Removes the factory from the manager. + +private: + FactoriesMap _factories; +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_PrivateKeyFactoryMgr_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyPassphraseHandler.h b/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyPassphraseHandler.h new file mode 100644 index 000000000..20e1308a6 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyPassphraseHandler.h @@ -0,0 +1,105 @@ +// +// PrivateKeyPassphraseHandler.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/PrivateKeyPassphraseHandler.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: PrivateKeyPassphraseHandler +// +// Definition of the PrivateKeyPassphraseHandler class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_PrivateKeyPassphraseHandler_INCLUDED +#define NetSSL_PrivateKeyPassphraseHandler_INCLUDED + + +#include "Poco/Net/NetSSL.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API PrivateKeyPassphraseHandler + /// A passphrase handler is needed whenever the private key of a certificate is loaded and the certificate is protected + /// by a passphrase. The PrivateKeyPassphraseHandler's task is to provide that passphrase. + /// One can install one's own PrivateKeyPassphraseHandler by implementing this interface. Note that + /// in the cpp file of the subclass the following code must be present (assuming you use the namespace My_API + /// and the name of your handler class is MyGuiHandler): + /// + /// #include "Poco/Net/PrivateKeyFactory.h" + /// ... + /// POCO_REGISTER_KEYFACTORY(My_API, MyGuiHandler) + /// + /// One can either set the handler directly in the startup code of the main method of ones application by calling + /// + /// SSLManager::instance().initialize(myguiHandler, myInvalidCertificateHandler, mySSLContext) + /// + /// or in case one's application extends Poco::Util::Application one can use an XML configuration and put the following entry + /// under the path openSSL.privateKeyPassphraseHandler: + /// + /// + /// MyGuiHandler + /// + /// [...] // Put optional config params for the handler here + /// + /// + /// Note that the name of the passphrase handler must be same as the one provided to the POCO_REGISTER_KEYFACTORY macro. +{ +public: + PrivateKeyPassphraseHandler(bool onServerSide); + /// Creates the PrivateKeyPassphraseHandler. Automatically registers at the SSLManager::PrivateKeyPassword event. + + virtual ~PrivateKeyPassphraseHandler(); + /// Destroys the PrivateKeyPassphraseHandler. + + virtual void onPrivateKeyRequested(const void* pSender, std::string& privateKey) = 0; + /// Returns the requested private key in the parameter privateKey. + + bool serverSide() const; + +private: + bool _serverSide; +}; + + +// +// inlines +// +inline bool PrivateKeyPassphraseHandler::serverSide() const +{ + return _serverSide; +} + + +} } // namespace Poco::Net + + +#endif // NetSSL_PrivateKeyPassphraseHandler_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/SSLException.h b/NetSSL_OpenSSL/include/Poco/Net/SSLException.h new file mode 100644 index 000000000..13ad50b51 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/SSLException.h @@ -0,0 +1,59 @@ +// +// SSLException.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/SSLException.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: SSLException +// +// Definition of the SSLException class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_SSLException_INCLUDED +#define NetSSL_SSLException_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/NetException.h" + + +namespace Poco { +namespace Net { + + +POCO_DECLARE_EXCEPTION(NetSSL_API, SSLException, NetException) +POCO_DECLARE_EXCEPTION(NetSSL_API, SSLContextException, SSLException) +POCO_DECLARE_EXCEPTION(NetSSL_API, InvalidCertificateException, SSLException) + + +} } // namespace Poco::Net + + +#endif // NetSSL_SSLException_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/SSLInitializer.h b/NetSSL_OpenSSL/include/Poco/Net/SSLInitializer.h new file mode 100644 index 000000000..df605e9c2 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/SSLInitializer.h @@ -0,0 +1,101 @@ +// +// SSLInitializer.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/SSLInitializer.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: SSLInitializer +// +// Definition of the SSLInitializer class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_SSLInitializer_INCLUDED +#define NetSSL_SSLInitializer_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Mutex.h" + + +extern "C" +{ + struct CRYPTO_dynlock_value + { + Poco::FastMutex _mutex; + }; +} + + +namespace Poco { +namespace Net { + + +class NetSSL_API SSLInitializer + /// Initalizes the OpenSSL library. + /// + /// The class ensures the earliest initialization and the + /// latest shutdown of the OpenSSL library. +{ +public: + SSLInitializer(); + /// Automatically initialize OpenSSL on startup. + + ~SSLInitializer(); + /// Automatically shut down OpenSSL on exit. + + static void initialize(); + /// Initializes the OpenSSL machinery. + + static void uninitialize(); + /// Shuts down the OpenSSL machinery. + +protected: + enum + { + SEEDSIZE = 256 + }; + + // OpenSSL multithreading support + static void lock(int mode, int n, const char* file, int line); + static unsigned long id(); + static struct CRYPTO_dynlock_value* dynlockCreate(const char* file, int line); + static void dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line); + static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line); + +private: + static Poco::FastMutex* _mutexes; + static int _rc; +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_SSLInitializer_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/SSLManager.h b/NetSSL_OpenSSL/include/Poco/Net/SSLManager.h new file mode 100644 index 000000000..93173fc19 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/SSLManager.h @@ -0,0 +1,271 @@ +// +// SSLManager.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/SSLManager.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: SSLManager +// +// Definition of the SSLManager class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_SSLManager_INCLUDED +#define NetSSL_SSLManager_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/VerificationErrorArgs.h" +#include "Poco/Net/Context.h" +#include "Poco/Net/PrivateKeyFactoryMgr.h" +#include "Poco/Net/CertificateHandlerFactoryMgr.h" +#include "Poco/Net/InvalidCertificateHandler.h" +#include "Poco/BasicEvent.h" +#include "Poco/SharedPtr.h" +#include + + +namespace Poco { +namespace Net { + + +class Context; + + +class NetSSL_API SSLManager + /// Class SSLManager. Singleton for holding the default server/client Context and PrivateKeyPassphraseHandler. + /// Either initialize via Poco::Util::Application or via the + /// initialize methods of the singleton. Note that the latter initialization must happen very early + /// during program startup before somebody calls defaultClientContext()/defaultServerContext() + /// or any of the passPhraseHandler methods (which tries to auto-initialize + /// the context and passphrase handler based on an Poco::Util::Application configuration). + /// An exemplary documentation which sets either the server or client defaultcontext and creates a PrivateKeyPassphraseHandler + /// that reads the password from the XML file looks like this: + /// + /// + /// + /// + /// any.pem + /// rootcert.pem + /// relaxed + /// 9 + /// true + /// ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH + /// + /// KeyFileHandler + /// + /// test + /// + /// + /// + /// ConsoleCertificateHandler + /// + /// + /// + /// + /// + /// + /// +{ +public: + typedef Poco::SharedPtr ContextPtr; + typedef Poco::SharedPtr PrivateKeyPassphraseHandlerPtr; + typedef Poco::SharedPtr InvalidCertificateHandlerPtr; + + Poco::BasicEvent ServerVerificationError; + /// Thrown whenever a certificate error is detected by the server during a handshake + + Poco::BasicEvent ClientVerificationError; + /// Thrown whenever a certificate error is detected by the client during a handshake + + Poco::BasicEvent PrivateKeyPassPhrase; + /// Thrown when a encrypted certificate is loaded. Not setting the passwd + /// in the event parameter will result in a failure to load the certificate. + /// Per default the SSLManager checks the configuration.xml file (path openSSL.privateKeyPassphraseHandler.name) + /// which default delegate it should register. If nothing is configured, + /// a KeyConsoleHandler is used. + + static SSLManager& instance(); + /// Returns the instance of the SSLManager singleton. + + void initializeServer(PrivateKeyPassphraseHandlerPtr& ptrPassPhraseHandler, InvalidCertificateHandlerPtr& ptrHandler, ContextPtr ptrContext); + /// Initializes the server side of the SSLManager with a default passphrase handler, a default invalid certificate handler and a default context. If this method + /// is never called the SSLmanager will try to initialize its members from an application configuration. + /// Note: ALWAYS create the handlers before you create the context! + /// Valid initialization code would be: + /// SharedPtr ptrConsole = new KeyConsoleHandler(); + /// SharedPtr ptrCert = new ConsoleCertificateHandler(); + /// SharedPtr ptrContext = new Context("any.pem", "rootcert.pem", Context::Relaxed, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); + /// + /// This method can only be called, if no defaultContext is set yet. + + void initializeClient(PrivateKeyPassphraseHandlerPtr& ptrPassPhraseHandler, InvalidCertificateHandlerPtr& ptrHandler, ContextPtr ptrContext); + /// Initializes the client side of the SSLManager with a default passphrase handler, a default invalid certificate handler and a default context. If this method + /// is never called the SSLmanager will try to initialize its members from an application configuration. + /// Note: ALWAYS create the handlers before you create the context! + /// Valid initialization code would be: + /// SharedPtr ptrConsole = new KeyConsoleHandler(); + /// SharedPtr ptrCert = new ConsoleCertificateHandler(); + /// SharedPtr ptrContext = new Context("any.pem", "rootcert.pem", Context::Relaxed, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); + /// + /// This method can only be called, if no defaultContext is set yet. + + ContextPtr defaultServerContext(); + /// Returns the default context used by the server. The first call to this method initializes the defaultContext + /// from an application configuration. + + ContextPtr defaultClientContext(); + /// Returns the default context used by the client. The first call to this method initializes the defaultContext + /// from an application configuration. + + PrivateKeyPassphraseHandlerPtr serverPassPhraseHandler(); + /// Returns the configured passphrase handler of the server. If none is set, the method will create a default one + /// from an application configuration + + InvalidCertificateHandlerPtr serverCertificateHandler(); + /// Returns an initialized certificate handler (used by the server to verify client cert) which determines how invalid certificates are treated. + /// If none is set, it will try to auto-initialize one from an application configuration. + + PrivateKeyPassphraseHandlerPtr clientPassPhraseHandler(); + /// Returns the configured passphrase handler of the client. If none is set, the method will create a default one + /// from an application configuration + + InvalidCertificateHandlerPtr clientCertificateHandler(); + /// Returns an initialized certificate handler (used by the client to verify server cert) which determines how invalid certificates are treated. + /// If none is set, it will try to auto-initialize one from an application configuration. + + PrivateKeyFactoryMgr& privateKeyFactoryMgr(); + /// Returns the private key factory manager which stores the + /// factories for the different registered passphrase handlers for private keys. + + CertificateHandlerFactoryMgr& certificateHandlerFactoryMgr(); + /// Returns the CertificateHandlerFactoryMgr which stores the + /// factories for the different registered certificate handlers. + + static int verifyClientCallback(int ok, X509_STORE_CTX* pStore); + /// The return value of this method defines how errors in + /// verification are handled. Return 0 to terminate the handshake, + /// or 1 to continue despite the error. + + static int verifyServerCallback(int ok, X509_STORE_CTX* pStore); + /// The return value of this method defines how errors in + /// verification are handled. Return 0 to terminate the handshake, + /// or 1 to continue despite the error. + + static int privateKeyPasswdCallback(char* pBuf, int size, int flag, void* userData); + /// Method is invoked by OpenSSl to retrieve a passwd for an encrypted certificate. + /// The request is delegated to the PrivatekeyPassword event. This method returns the + /// length of the password. + + static const std::string CFG_SERVER_PREFIX; + static const std::string CFG_CLIENT_PREFIX; + +private: + SSLManager(); + /// Creates the SSLManager. + + ~SSLManager(); + /// Destroys the SSLManager. + + void initDefaultContext(bool server); + /// Inits the default context, the first time it is accessed. + + void initEvents(bool server); + /// Registers delegates at the events according to the configuration. + + void initPassPhraseHandler(bool server); + /// Inits the passphrase handler. + + void initCertificateHandler(bool server); + /// Inits the certificate handler. + + static int verifyCallback(bool server, int ok, X509_STORE_CTX* pStore); + /// The return value of this method defines how errors in + /// verification are handled. Return 0 to terminate the handshake, + /// or 1 to continue despite the error. + + PrivateKeyFactoryMgr _factoryMgr; + CertificateHandlerFactoryMgr _certHandlerFactoryMgr; + ContextPtr _ptrDefaultServerContext; + PrivateKeyPassphraseHandlerPtr _ptrServerPassPhraseHandler; + InvalidCertificateHandlerPtr _ptrServerCertificateHandler; + ContextPtr _ptrDefaultClientContext; + PrivateKeyPassphraseHandlerPtr _ptrClientPassPhraseHandler; + InvalidCertificateHandlerPtr _ptrClientCertificateHandler; + + static const std::string CFG_PRIV_KEY_FILE; + static const std::string CFG_CA_LOCATION; + static const std::string CFG_VER_MODE; + static const Context::VerificationMode VAL_VER_MODE; + static const std::string CFG_VER_DEPTH; + static const int VAL_VER_DEPTH; + static const std::string CFG_ENABLE_DEFAULT_CA; + static const bool VAL_ENABLE_DEFAULT_CA; + static const std::string CFG_CYPHER_LIST; + static const std::string VAL_CYPHER_LIST; + static const std::string CFG_DELEGATE_HANDLER; + static const std::string VAL_DELEGATE_HANDLER; + static const std::string CFG_CERTIFICATE_HANDLER; + static const std::string VAL_CERTIFICATE_HANDLER; + + friend class Poco::SingletonHolder; +}; + + +// +// inlines +// +inline PrivateKeyFactoryMgr& SSLManager::privateKeyFactoryMgr() +{ + return _factoryMgr; +} + + +inline CertificateHandlerFactoryMgr& SSLManager::certificateHandlerFactoryMgr() +{ + return _certHandlerFactoryMgr; +} + + +inline int SSLManager::verifyServerCallback(int ok, X509_STORE_CTX* pStore) +{ + return SSLManager::verifyCallback(true, ok, pStore); +} + + +inline int SSLManager::verifyClientCallback(int ok, X509_STORE_CTX* pStore) +{ + return SSLManager::verifyCallback(false, ok, pStore); +} + + +} } // namespace Poco::Net + + +#endif // NetSSL_SSLManager_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/SecureServerSocket.h b/NetSSL_OpenSSL/include/Poco/Net/SecureServerSocket.h new file mode 100644 index 000000000..4f8a79549 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/SecureServerSocket.h @@ -0,0 +1,120 @@ +// +// SecureServerSocket.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/SecureServerSocket.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureServerSocket +// +// Definition of the SecureServerSocket class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_SecureServerSocket_INCLUDED +#define NetSSL_SecureServerSocket_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/ServerSocket.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API SecureServerSocket: public ServerSocket + /// A server socket for secure SSL connections. +{ +public: + SecureServerSocket(); + /// Creates a SSL server socket. + /// + /// The server socket must be bound to + /// an address and put into listening state. + + SecureServerSocket(const Socket& socket); + /// Creates the SecureServerSocket with the SocketImpl + /// from another socket. The SocketImpl must be + /// a ServerSocketImpl, otherwise an InvalidArgumentException + /// will be thrown. + + SecureServerSocket(const SocketAddress& address, int backlog = 64); + /// Creates a server socket, binds it + /// to the given address and puts it in listening + /// state. + /// + /// After successful construction, the server socket + /// is ready to accept connections. + + SecureServerSocket(Poco::UInt16 port, int backlog = 64); + /// Creates a server socket, binds it + /// to the given port and puts it in listening + /// state. + /// + /// After successful construction, the server socket + /// is ready to accept connections. + + virtual ~SecureServerSocket(); + /// Destroys the StreamSocket. + + SecureServerSocket& operator = (const Socket& socket); + /// Assignment operator. + /// + /// Releases the socket's SocketImpl and + /// attaches the SocketImpl from the other socket and + /// increments the reference count of the SocketImpl. + + StreamSocket acceptConnection(SocketAddress& clientAddr); + /// Get the next completed connection from the + /// socket's completed connection queue. + /// + /// If the queue is empty, waits until a connection + /// request completes. + /// + /// Returns a new SSL TCP socket for the connection + /// with the client. + /// + /// The client socket's address is returned in clientAddr. + + StreamSocket acceptConnection(); + /// Get the next completed connection from the + /// socket's completed connection queue. + /// + /// If the queue is empty, waits until a connection + /// request completes. + /// + /// Returns a new TCP socket for the connection + /// with the client. +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_SecureServerSocket_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/SecureServerSocketImpl.h b/NetSSL_OpenSSL/include/Poco/Net/SecureServerSocketImpl.h new file mode 100644 index 000000000..f87ba1745 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/SecureServerSocketImpl.h @@ -0,0 +1,163 @@ +// +// SecureServerSocketImpl.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/SecureServerSocketImpl.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureServerSocketImpl +// +// Definition of the SecureServerSocketImpl class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_SecureServerSocketImpl_INCLUDED +#define NetSSL_SecureServerSocketImpl_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/SecureSocketImpl.h" +#include "Poco/Net/ServerSocketImpl.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API SecureServerSocketImpl: public ServerSocketImpl + /// The SocketImpl class for SecureServerSocket. +{ +public: + SecureServerSocketImpl(); + /// Creates the SecureServerSocketImpl. + + SocketImpl* acceptConnection(SocketAddress& clientAddr); + /// Get the next completed connection from the + /// socket's completed connection queue. + /// + /// If the queue is empty, waits until a connection + /// request completes. + /// + /// Returns a new TCP socket for the connection + /// with the client. + /// + /// The client socket's address is returned in clientAddr. + + void connect(const SocketAddress& address); + /// Initializes the socket and establishes a connection to + /// the TCP server at the given address. + /// + /// Can also be used for UDP sockets. In this case, no + /// connection is established. Instead, incoming and outgoing + /// packets are restricted to the specified address. + + void connect(const SocketAddress& address, const Poco::Timespan& timeout); + /// Initializes the socket, sets the socket timeout and + /// establishes a connection to the TCP server at the given address. + + void connectNB(const SocketAddress& address); + /// Initializes the socket and establishes a connection to + /// the TCP server at the given address. Prior to opening the + /// connection the socket is set to nonblocking mode. + + void bind(const SocketAddress& address, bool reuseAddress = false); + /// Bind a local address to the socket. + /// + /// This is usually only done when establishing a server + /// socket. TCP clients should not bind a socket to a + /// specific address. + /// + /// If reuseAddress is true, sets the SO_REUSEADDR + /// socket option. + + void listen(int backlog = 64); + /// Puts the socket into listening state. + /// + /// The socket becomes a passive socket that + /// can accept incoming connection requests. + /// + /// The backlog argument specifies the maximum + /// number of connections that can be queued + /// for this socket. + + void close(); + /// Close the socket. + + int sendBytes(const void* buffer, int length, int flags = 0); + /// Sends the contents of the given buffer through + /// the socket. Any specified flags are ignored. + /// + /// Returns the number of bytes sent, which may be + /// less than the number of bytes specified. + + int receiveBytes(void* buffer, int length, int flags = 0); + /// Receives data from the socket and stores it + /// in buffer. Up to length bytes are received. + /// + /// Returns the number of bytes received. + + int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0); + /// Sends the contents of the given buffer through + /// the socket to the given address. + /// + /// Returns the number of bytes sent, which may be + /// less than the number of bytes specified. + + int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0); + /// Receives data from the socket and stores it + /// in buffer. Up to length bytes are received. + /// Stores the address of the sender in address. + /// + /// Returns the number of bytes received. + + void sendUrgent(unsigned char data); + /// Sends one byte of urgent data through + /// the socket. + /// + /// The data is sent with the MSG_OOB flag. + /// + /// The preferred way for a socket to receive urgent data + /// is by enabling the SO_OOBINLINE option. + +protected: + ~SecureServerSocketImpl(); + /// Destroys the SecureServerSocketImpl. + +private: + SecureServerSocketImpl(const SecureServerSocketImpl&); + SecureServerSocketImpl& operator = (const SecureServerSocketImpl&); + +private: + SecureSocketImpl _socket; +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_SecureServerSocketImpl_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h b/NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h new file mode 100644 index 000000000..31be7a8d1 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h @@ -0,0 +1,237 @@ +// +// SecureSocketImpl.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/SecureSocketImpl.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureSocketImpl +// +// Definition of the SecureSocketImpl class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_SecureSocketImpl_INCLUDED +#define NetSSL_SecureSocketImpl_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/SocketImpl.h" +#include +#include + + +namespace Poco { +namespace Net { + + +class HostEntry; + + +class NetSSL_API SecureSocketImpl + /// The SocketImpl for SecureStreamSocket. +{ +public: + SecureSocketImpl(); + /// Creates the SecureSocketImpl. + + SecureSocketImpl(SSL* _pSSL); + /// Creates the SecureSocketImpl. + + virtual ~SecureSocketImpl(); + /// Destroys the SecureSocketImpl. + + SocketImpl* acceptConnection(SocketAddress& clientAddr); + /// Get the next completed connection from the + /// socket's completed connection queue. + /// + /// If the queue is empty, waits until a connection + /// request completes. + /// + /// Returns a new TCP socket for the connection + /// with the client. + /// + /// The client socket's address is returned in clientAddr. + + void connect(const SocketAddress& address); + /// Initializes the socket and establishes a connection to + /// the TCP server at the given address. + /// + /// Can also be used for UDP sockets. In this case, no + /// connection is established. Instead, incoming and outgoing + /// packets are restricted to the specified address. + + void connect(const SocketAddress& address, const Poco::Timespan& timeout); + /// Initializes the socket, sets the socket timeout and + /// establishes a connection to the TCP server at the given address. + + void connectNB(const SocketAddress& address); + /// Initializes the socket and establishes a connection to + /// the TCP server at the given address. Prior to opening the + /// connection the socket is set to nonblocking mode. + + void bind(const SocketAddress& address, bool reuseAddress = false); + /// Bind a local address to the socket. + /// + /// This is usually only done when establishing a server + /// socket. TCP clients should not bind a socket to a + /// specific address. + /// + /// If reuseAddress is true, sets the SO_REUSEADDR + /// socket option. + + void listen(int backlog = 64); + /// Puts the socket into listening state. + /// + /// The socket becomes a passive socket that + /// can accept incoming connection requests. + /// + /// The backlog argument specifies the maximum + /// number of connections that can be queued + /// for this socket. + + void close(); + /// Close the socket. + + int sendBytes(const void* buffer, int length, int flags = 0); + /// Sends the contents of the given buffer through + /// the socket. Any specified flags are ignored. + /// + /// Returns the number of bytes sent, which may be + /// less than the number of bytes specified. + + int receiveBytes(void* buffer, int length, int flags = 0); + /// Receives data from the socket and stores it + /// in buffer. Up to length bytes are received. + /// + /// Returns the number of bytes received. + + int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0); + /// Sends the contents of the given buffer through + /// the socket to the given address. + /// + /// Returns the number of bytes sent, which may be + /// less than the number of bytes specified. + + int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0); + /// Receives data from the socket and stores it + /// in buffer. Up to length bytes are received. + /// Stores the address of the sender in address. + /// + /// Returns the number of bytes received. + + void sendUrgent(unsigned char data); + /// Sends one byte of urgent data through + /// the socket. + /// + /// The data is sent with the MSG_OOB flag. + /// + /// The preferred way for a socket to receive urgent data + /// is by enabling the SO_OOBINLINE option. + + poco_socket_t sockfd(); + // Returns the socket. + + void setTunnelEndPoint(const std::string& endHost, Poco::UInt16 endPort); + /// Due to the fact that SSLConnections that run over proxies require + /// a different connect phase (ie send an unencrypted HTTP CONNECT before + /// establishing, we must inform the socket that is only used as a proxy + /// that works as a tunnel to the given endPoint. + /// Only call this method on disconnected sockets. + +protected: + void setSockfd(poco_socket_t sock); + /// Set a socket description iff no socket is already set. + + void invalidate(); + /// Invalidate the current socket. Must only be called on closed sockets. + + static long postConnectionCheck(bool onServer, SSL* pSSL, const std::string& host); + /// PostConnectionCheck to verify that a peer really presented a valid certificate. + /// if onserver is false, used by clients to verify that a server is really the one it claims. + /// if onserver is true, used by the server to verify that a client is really the one it claims. + + void connectSSL(const SocketAddress& address); + /// Creates and connects an SSL connection. Set _pSSL on success or exception otherwise. + + void establishTunnel(); + /// Creates a socket to the proxy and sends CONNECT. + + static bool containsWildcards(const std::string& commonName); + /// Checks if the commonName of a certificate contains wildcards + + static bool matchByAlias(const std::string& alias, const HostEntry& heData); + /// Checks if the alias is contained in heData + +private: + SecureSocketImpl(const SecureSocketImpl&); + SecureSocketImpl& operator = (const SecureSocketImpl&); + +private: + BIO* _pBIO; + SSL* _pSSL; + SocketImpl _socket; + std::string _endHost; + Poco::UInt16 _endPort; +}; + + +// +// inlines +// +inline poco_socket_t SecureSocketImpl::sockfd() +{ + return _socket.sockfd(); +} + + +inline void SecureSocketImpl::setSockfd(poco_socket_t sock) +{ + _socket.setSockfd(sock); +} + + +inline void SecureSocketImpl::invalidate() +{ + _socket.invalidate(); +} + + +inline void SecureSocketImpl::setTunnelEndPoint(const std::string& endHost, Poco::UInt16 endPort) +{ + poco_assert (endPort != 0 && !endHost.empty()); + + _endHost = endHost; + _endPort = endPort; +} + + +} } // namespace Poco::Net + + +#endif // NetSSL_SecureSocketImpl_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocket.h b/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocket.h new file mode 100644 index 000000000..34ade089c --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocket.h @@ -0,0 +1,91 @@ +// +// SecureStreamSocket.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocket.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureStreamSocket +// +// Definition of the SecureStreamSocket class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_SecureStreamSocket_INCLUDED +#define NetSSL_SecureStreamSocket_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/StreamSocket.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API SecureStreamSocket: public StreamSocket + /// A subclass of StreamSocket for secure SSL sockets. +{ +public: + SecureStreamSocket(); + /// Creates an unconnected stream socket. + /// + /// Before sending or receiving data, the socket + /// must be connected with a call to connect(). + + SecureStreamSocket(const SocketAddress& address); + /// Creates a stream socket and connects it to + /// the socket specified by address. + + SecureStreamSocket(const Socket& socket); + /// Creates the StreamSocket with the SocketImpl + /// from another socket. The SocketImpl must be + /// a StreamSocketImpl, otherwise an InvalidArgumentException + /// will be thrown. + + virtual ~SecureStreamSocket(); + /// Destroys the StreamSocket. + + SecureStreamSocket& operator = (const Socket& socket); + /// Assignment operator. + /// + /// Releases the socket's SocketImpl and + /// attaches the SocketImpl from the other socket and + /// increments the reference count of the SocketImpl. + +protected: + SecureStreamSocket(SocketImpl* pImpl); + + friend class SecureServerSocket; +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_SecureStreamSocket_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocketImpl.h b/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocketImpl.h new file mode 100644 index 000000000..c0422da89 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocketImpl.h @@ -0,0 +1,184 @@ +// +// SecureStreamSocketImpl.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/SecureStreamSocketImpl.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureStreamSocketImpl +// +// Definition of the SecureStreamSocketImpl class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_SecureStreamSocketImpl_INCLUDED +#define NetSSL_SecureStreamSocketImpl_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/SecureSocketImpl.h" +#include "Poco/Net/StreamSocketImpl.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API SecureStreamSocketImpl: public StreamSocketImpl + /// This class implements a SSL TCP socket. +{ +public: + SecureStreamSocketImpl(); + /// Creates the SecureStreamSocketImpl. + + SecureStreamSocketImpl(SSL* _pSSL); + /// Creates a SecureStreamSocketImpl using the given native socket. + + SocketImpl* acceptConnection(SocketAddress& clientAddr); + /// Get the next completed connection from the + /// socket's completed connection queue. + /// + /// If the queue is empty, waits until a connection + /// request completes. + /// + /// Returns a new TCP socket for the connection + /// with the client. + /// + /// The client socket's address is returned in clientAddr. + + void connect(const SocketAddress& address); + /// Initializes the socket and establishes a connection to + /// the TCP server at the given address. + /// + /// Can also be used for UDP sockets. In this case, no + /// connection is established. Instead, incoming and outgoing + /// packets are restricted to the specified address. + + void connect(const SocketAddress& address, const Poco::Timespan& timeout); + /// Initializes the socket, sets the socket timeout and + /// establishes a connection to the TCP server at the given address. + + void connectNB(const SocketAddress& address); + /// Initializes the socket and establishes a connection to + /// the TCP server at the given address. Prior to opening the + /// connection the socket is set to nonblocking mode. + + void bind(const SocketAddress& address, bool reuseAddress = false); + /// Bind a local address to the socket. + /// + /// This is usually only done when establishing a server + /// socket. TCP clients should not bind a socket to a + /// specific address. + /// + /// If reuseAddress is true, sets the SO_REUSEADDR + /// socket option. + + void listen(int backlog = 64); + /// Puts the socket into listening state. + /// + /// The socket becomes a passive socket that + /// can accept incoming connection requests. + /// + /// The backlog argument specifies the maximum + /// number of connections that can be queued + /// for this socket. + + void close(); + /// Close the socket. + + int sendBytes(const void* buffer, int length, int flags = 0); + /// Sends the contents of the given buffer through + /// the socket. Any specified flags are ignored. + /// + /// Returns the number of bytes sent, which may be + /// less than the number of bytes specified. + + int receiveBytes(void* buffer, int length, int flags = 0); + /// Receives data from the socket and stores it + /// in buffer. Up to length bytes are received. + /// + /// Returns the number of bytes received. + + int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0); + /// Sends the contents of the given buffer through + /// the socket to the given address. + /// + /// Returns the number of bytes sent, which may be + /// less than the number of bytes specified. + + int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0); + /// Receives data from the socket and stores it + /// in buffer. Up to length bytes are received. + /// Stores the address of the sender in address. + /// + /// Returns the number of bytes received. + + void sendUrgent(unsigned char data); + /// Sends one byte of urgent data through + /// the socket. + /// + /// The data is sent with the MSG_OOB flag. + /// + /// The preferred way for a socket to receive urgent data + /// is by enabling the SO_OOBINLINE option. + + void setTunnelEndPoint(const std::string& host, Poco::UInt16 port); + /// Due to the fact that SSLConnections that run over proxies require + /// a different connect phase (ie send an unencrypted HTTP CONNECT before + /// establishing, we must inform the socket that it is only used as a proxy + /// that works as a tunnel to the given endPoint. + /// Only call this method on disconnected sockets. + +protected: + ~SecureStreamSocketImpl(); + /// Destroys the SecureStreamSocketImpl. + +private: + SecureStreamSocketImpl(const SecureStreamSocketImpl&); + SecureStreamSocketImpl& operator = (const SecureStreamSocketImpl&); + +private: + SecureSocketImpl _socket; + + friend class SecureSocketImpl; +}; + + +// +// inlines +// +inline void SecureStreamSocketImpl::setTunnelEndPoint(const std::string& host, Poco::UInt16 port) +{ + _socket.setTunnelEndPoint(host, port); +} + + +} } // namespace Poco::Net + + +#endif // NetSSL_SecureStreamSocketImpl_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/Utility.h b/NetSSL_OpenSSL/include/Poco/Net/Utility.h new file mode 100644 index 000000000..63f8e558d --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/Utility.h @@ -0,0 +1,72 @@ +// +// Utility.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/Utility.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: Utility +// +// Definition of the Utility class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_Utility_INCLUDED +#define NetSSL_Utility_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/Context.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API Utility + /// Class Utility. helper class for init & shutdown of the OpenSSL library +{ +public: + static int HTTPS_PORT; + /// Default port value for HHTPS + + static Context::VerificationMode convertVerificationMode(const std::string& verMode); + /// Non-case sensitive conversion of a string to a VerificationMode enum. + /// If verMode is illegal an OptionException is thrown. + + static std::string convertCertificateError(long errCode); + /// Converts an SSL error code into human readable form + + static std::string convertSSLError(SSL* pSSL, int errCode); +}; + + +} } // namespace Poco::Net + + +#endif // NetSSL_Utility_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/VerificationErrorArgs.h b/NetSSL_OpenSSL/include/Poco/Net/VerificationErrorArgs.h new file mode 100644 index 000000000..a55af340e --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/VerificationErrorArgs.h @@ -0,0 +1,130 @@ +// +// VerificationErrorArgs.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/VerificationErrorArgs.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: VerificationErrorArgs +// +// Definition of the VerificationErrorArgs class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_VerificationErrorArgs_INCLUDED +#define NetSSL_VerificationErrorArgs_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include "Poco/Net/X509Certificate.h" + + +namespace Poco { +namespace Net { + + +class NetSSL_API VerificationErrorArgs + /// A utility class for certificate error handling. +{ +public: + VerificationErrorArgs(const X509Certificate& cert, int errDepth, int errNum, const std::string& errMsg); + /// Creates the VerificationErrorArgs. _ignoreError is per default set to false. + + ~VerificationErrorArgs(); + /// Destroys the VerificationErrorArgs. + + const X509Certificate& certificate() const; + /// Returns the certificate that caused the error. + + int errorDepth() const; + /// Returns the position of the certificate in the certificate chain. + + int errorNumber() const; + /// Returns the id of the error + + const std::string& errorMessage() const; + /// Returns the textual presentation of the errorNumber. + + void setIgnoreError(bool ignoreError); + /// setIgnoreError to true, if a verification error is judged non-fatal by the user. + + bool getIgnoreError() const; + /// returns the value of _ignoreError + +private: + X509Certificate _cert; + int _errorDepth; + int _errorNumber; + std::string _errorMessage; /// Textual representation of the _errorNumber + bool _ignoreError; +}; + + +// +// inlines +// +inline const X509Certificate& VerificationErrorArgs::certificate() const +{ + return _cert; +} + + +inline int VerificationErrorArgs::errorDepth() const +{ + return _errorDepth; +} + + +inline int VerificationErrorArgs::errorNumber() const +{ + return _errorNumber; +} + + +inline const std::string& VerificationErrorArgs::errorMessage() const +{ + return _errorMessage; +} + + +inline void VerificationErrorArgs::setIgnoreError(bool ignoreError) +{ + _ignoreError = ignoreError; +} + + +inline bool VerificationErrorArgs::getIgnoreError() const +{ + return _ignoreError; +} + + +} } // namespace Poco::Net + + +#endif // NetSSL_VerificationErrorArgs_INCLUDED diff --git a/NetSSL_OpenSSL/include/Poco/Net/X509Certificate.h b/NetSSL_OpenSSL/include/Poco/Net/X509Certificate.h new file mode 100644 index 000000000..369743911 --- /dev/null +++ b/NetSSL_OpenSSL/include/Poco/Net/X509Certificate.h @@ -0,0 +1,105 @@ +// +// X509Certificate.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/include/Poco/Net/X509Certificate.h#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: X509Certificate +// +// Definition of the X509Certificate class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSL_X509Certificate_INCLUDED +#define NetSSL_X509Certificate_INCLUDED + + +#include "Poco/Net/NetSSL.h" +#include + + +namespace Poco { +namespace Net { + + +class NetSSL_API X509Certificate + /// This class represents an X509 Certificate. +{ +public: + X509Certificate(X509* pCert); + /// Creates the X509Certificate. + + ~X509Certificate(); + /// Destroys the X509Certificate. + + const std::string& issuerName() const; + /// Returns the certificate issuer name. + + const std::string& subjectName() const; + /// Returns the certificate subject name. + + const X509* certificate() const; + /// Returns the OpenSSL certificate. + +private: + void initialize(); + /// Extracts data from _pCert. Assumes _pCert != 0. + +private: + std::string _issuerName; + std::string _subjectName; + X509* _pCert; +}; + + +// +// inlines +// +inline const std::string& X509Certificate::issuerName() const +{ + return _issuerName; +} + + +inline const std::string& X509Certificate::subjectName() const +{ + return _subjectName; +} + + +inline const X509* X509Certificate::certificate() const +{ + return _pCert; +} + + +} } // namespace Poco::Net + + +#endif // NetSSL_X509Certificate_INCLUDED diff --git a/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer.properties b/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer.properties new file mode 100644 index 000000000..0d5956fad --- /dev/null +++ b/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer.properties @@ -0,0 +1,29 @@ +# This is a sample configuration file for HTTPTimeServer + +logging.loggers.root.channel.class = ConsoleChannel +logging.loggers.app.name = Application +logging.loggers.app.channel = c1 +logging.formatters.f1.class = PatternFormatter +logging.formatters.f1.pattern = [%p] %t +logging.channels.c1.class = ConsoleChannel +logging.channels.c1.formatter = f1 +# HTTPSTimeServer.format = %W, %e %b %y %H:%M:%S %Z +# HTTPSTimeServer.port = 9980 +# openSSL.client.privateKeyFile = ${application.configDir}any.pem +# openSSL.client.caConfig = ${application.configDir}rootcert.pem +# openSSL.client.verificationMode = relaxed +# openSSL.client.verificationDepth = 9 +# openSSL.client.loadDefaultCAFile = false +# openSSL.client.cypherList = ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH +# openSSL.client.privateKeyPassphraseHandler.name = KeyFileHandler +# openSSL.client.privateKeyPassphraseHandler.options.password = test +# openSSL.client.invalidCertificateHandler = AcceptCertificateHandler +openSSL.server.privateKeyFile = ${application.configDir}any.pem +openSSL.server.caConfig = ${application.configDir}rootcert.pem +openSSL.server.verificationMode = none +openSSL.server.verificationDepth = 9 +openSSL.server.loadDefaultCAFile = false +openSSL.server.cypherList = ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH +openSSL.server.privateKeyPassphraseHandler.name = KeyFileHandler +openSSL.server.privateKeyPassphraseHandler.options.password = test +openSSL.server.invalidCertificateHandler = AcceptCertificateHandler diff --git a/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer.vmsbuild b/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer.vmsbuild new file mode 100644 index 000000000..6d46956d6 --- /dev/null +++ b/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer.vmsbuild @@ -0,0 +1,7 @@ +# +# sample.vmsbuild +# +# $Id: //poco/Main/Net/samples/HTTPTimeServer/HTTPTimeServer.vmsbuild#1 $ +# +EXE=HTTPSTimeServer +HTTPSTimeServer diff --git a/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer_vs71.vcproj b/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer_vs71.vcproj new file mode 100644 index 000000000..d35f40fba --- /dev/null +++ b/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer_vs71.vcproj @@ -0,0 +1,152 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer_vs80.vcproj b/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer_vs80.vcproj new file mode 100644 index 000000000..ff6685e77 --- /dev/null +++ b/NetSSL_OpenSSL/samples/HTTPSTimeServer/HTTPSTimeServer_vs80.vcproj @@ -0,0 +1,217 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NetSSL_OpenSSL/samples/HTTPSTimeServer/Makefile b/NetSSL_OpenSSL/samples/HTTPSTimeServer/Makefile new file mode 100644 index 000000000..4f0e60c79 --- /dev/null +++ b/NetSSL_OpenSSL/samples/HTTPSTimeServer/Makefile @@ -0,0 +1,19 @@ +# +# Makefile +# +# $Id: //poco/Main/Net/samples/HTTPTimeServer/Makefile#1 $ +# +# Makefile for Poco HTTPSTimeServer +# + +include $(POCO_BASE)/build/rules/global + +SYSLIBS += -lssl -lcrypto -ldl + +objects = HTTPSTimeServer + +target = HTTPSTimeServer +target_version = 1 +target_libs = PocoNetSSL PocoNet PocoUtil PocoXML PocoFoundation + +include $(POCO_BASE)/build/rules/exec diff --git a/NetSSL_OpenSSL/samples/HTTPSTimeServer/any.pem b/NetSSL_OpenSSL/samples/HTTPSTimeServer/any.pem new file mode 100644 index 000000000..b17a97568 --- /dev/null +++ b/NetSSL_OpenSSL/samples/HTTPSTimeServer/any.pem @@ -0,0 +1,48 @@ +-----BEGIN CERTIFICATE----- +MIICaDCCAdECCQCzfxSsk7yaLjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAzMDExMzA3MzFaFw0wNjAzMzExMzA3MzFaMH4xCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExETAPBgNVBAcTCFN0IEpha29iMRww +GgYDVQQKExNBcHBsaWVkIEluZm9ybWF0aWNzMQowCAYDVQQDFAEqMR4wHAYJKoZI +hvcNAQkBFg9pbmZvQGFwcGluZi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ +AoGBAJHGyXDHyCYoWz+65ltNwwZbhwOGnxr9P1WMATuFJh0bPBZxKbZRdbTm9KhZ +OlvsEIsfgiYdsxURYIqXfEgISYLZcZY0pQwGEOmB+0NeC/+ENSfOlNSthx6zSVlc +zhJ7+dJOGwepHAiLr1fRuc5jogYLraE+lKTnqAAFfzwvti77AgMBAAEwDQYJKoZI +hvcNAQEFBQADgYEAY/ZoeY1ukkEJX7259NeoVM0oahlulWV0rlCqyaeosOiDORPT +m6X1w/5MTCf9VyaD1zukoSZ4QqNVjHFXcXidbB7Tgt3yRuZ5PC5LIFCDPv9mgPne +mUA70yfctNfza2z3ZiQ6NDkW3mZX+1tmxYIrJQIrkVeYeqf1Gh2nyZrUMcE= +-----END CERTIFICATE----- +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,E7AE93C9E49184EA + +A2IqzNcWs+I5vzV+i+woDk56+yr58eU0Onw8eEvXkLjnSc58JU4327IF7yUbKWdW +Q7BYGGOkVFiZ7ANOwviDg5SUhxRDWCcW8dS6/p1vfdQ1C3qj2OwJjkpg0aDBIzJn +FzgguT3MF3ama77vxv0S3kOfmCj62MLqPGpj5pQ0/1hefRFbL8oAX8bXUN7/rmGM +Zc0QyzFZv2iQ04dY/6TNclwKPB4H0On4K+8BMs3PRkWA0clCaQaFO2+iwnk3XZfe ++MsKUEbLCpAQeYspYv1cw38dCdWq1KTP5aJk+oXgwjfX5cAaPTz74NTqTIsCcaTD +3vy7ukJYFlDR9Kyo7z8rMazYrKJslhnuRH0BhK9st9McwL957j5tZmrKyraCcmCx +dMAGcsis1va3ayYZpIpFqA4EhYrTM+6N8ZRfUap20+b5IQwHfTQDejUhL6rBwy7j +Ti5yD83/itoOMyXq2sV/XWfVD5zk/P5iv22O1EAQMhhnPB9K/I/JhuSGQJfn3cNh +ykOUYT0+vDeSeEVa+FVEP1W35G0alTbKbNs5Tb8KxJ3iDJUxokM//SvPXZy9hOVX +Y05imB04J15DaGbAHlNzunhuJi7121WV/JRXZRW9diE6hwpD8rwqi3FMuRUmy7U9 +aFA5poKRAYlo9YtZ3YpFyjGKB6MfCQcB2opuSnQ/gbugV41m67uQ4CDwWLaNRkTb +GlsMBNcHnidg15Bsat5HaB7l250ukrI13Uw1MYdDUzaS3gPfw9aC4F2w0p3U+DPH +80/zePxtroR7T4/+rI136Rl+aMXDMOEGCX1TVP8rjuZzuRyUSUKC8Q== +-----END RSA PRIVATE KEY----- +-----BEGIN CERTIFICATE----- +MIICXTCCAcYCCQC1Vk/N8qR4AjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAyMjcxMzI3MThaFw0wNjAzMjkxMzI3MThaMHMxCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExEjAQBgNVBAcTCVN0LiBKYWtvYjEP +MA0GA1UEChMGQXBwSW5mMQ8wDQYDVQQDEwZBcHBJbmYxGjAYBgkqhkiG9w0BCQEW +C2FwcEBpbmYuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsFXiPuicN +Im4oJwF8NuaFN+lgYwcZ6dAO3ILIR3kLA2PxF8HSQLfF8J8a4odZhLhctIMAKTxm +k0w8TW5qhL8QLdGzY9vzvkgdKOkan2t3sMeXJAfrM1AphTsmgntAQazGZjOj5p4W +jDnxQ+VXAylqwjHh49eSBxM3wgoscF4iLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GB +AIpfLdXiKchPvFMhQS8xTtXvrw5dVL3yImUMYs4GQi8RrjGmfGB3yMAR7B/b8v4a ++ztfusgWAWiUKuSGTk4S8YB0fsFlmOv0WDr+PyZ4Lui/a8opbyzGE7rqpnF/s0GO +M7uLCNNwIN7WhmxcWV0KZU1wTppoSWPJda1yTbBzF9XP +-----END CERTIFICATE----- diff --git a/NetSSL_OpenSSL/samples/HTTPSTimeServer/rootcert.pem b/NetSSL_OpenSSL/samples/HTTPSTimeServer/rootcert.pem new file mode 100644 index 000000000..c26aeb244 --- /dev/null +++ b/NetSSL_OpenSSL/samples/HTTPSTimeServer/rootcert.pem @@ -0,0 +1,15 @@ +-----BEGIN CERTIFICATE----- +MIICXTCCAcYCCQC1Vk/N8qR4AjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAyMjcxMzI3MThaFw0wNjAzMjkxMzI3MThaMHMxCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExEjAQBgNVBAcTCVN0LiBKYWtvYjEP +MA0GA1UEChMGQXBwSW5mMQ8wDQYDVQQDEwZBcHBJbmYxGjAYBgkqhkiG9w0BCQEW +C2FwcEBpbmYuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsFXiPuicN +Im4oJwF8NuaFN+lgYwcZ6dAO3ILIR3kLA2PxF8HSQLfF8J8a4odZhLhctIMAKTxm +k0w8TW5qhL8QLdGzY9vzvkgdKOkan2t3sMeXJAfrM1AphTsmgntAQazGZjOj5p4W +jDnxQ+VXAylqwjHh49eSBxM3wgoscF4iLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GB +AIpfLdXiKchPvFMhQS8xTtXvrw5dVL3yImUMYs4GQi8RrjGmfGB3yMAR7B/b8v4a ++ztfusgWAWiUKuSGTk4S8YB0fsFlmOv0WDr+PyZ4Lui/a8opbyzGE7rqpnF/s0GO +M7uLCNNwIN7WhmxcWV0KZU1wTppoSWPJda1yTbBzF9XP +-----END CERTIFICATE----- diff --git a/NetSSL_OpenSSL/samples/HTTPSTimeServer/src/HTTPSTimeServer.cpp b/NetSSL_OpenSSL/samples/HTTPSTimeServer/src/HTTPSTimeServer.cpp new file mode 100644 index 000000000..b96833fc1 --- /dev/null +++ b/NetSSL_OpenSSL/samples/HTTPSTimeServer/src/HTTPSTimeServer.cpp @@ -0,0 +1,238 @@ +// +// TimeServer.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/samples/HTTPSTimeServer/src/HTTPSTimeServer.cpp#1 $ +// +// This sample demonstrates the HTTPServer and related classes. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/HTTPServer.h" +#include "Poco/Net/HTTPRequestHandler.h" +#include "Poco/Net/HTTPRequestHandlerFactory.h" +#include "Poco/Net/HTTPServerParams.h" +#include "Poco/Net/HTTPServerRequest.h" +#include "Poco/Net/HTTPServerResponse.h" +#include "Poco/Net/HTTPServerParams.h" +#include "Poco/Net/SecureServerSocket.h" +#include "Poco/Timestamp.h" +#include "Poco/DateTimeFormatter.h" +#include "Poco/DateTimeFormat.h" +#include "Poco/Exception.h" +#include "Poco/SharedPtr.h" +#include "Poco/Util/ServerApplication.h" +#include "Poco/Util/Option.h" +#include "Poco/Util/OptionSet.h" +#include "Poco/Util/HelpFormatter.h" +#include "Poco/Net/SSLManager.h" +#include "Poco/Net/KeyConsoleHandler.h" +#include "Poco/Net/AcceptCertificateHandler.h" +#include + + +using Poco::Net::SecureServerSocket; +using Poco::Net::HTTPRequestHandler; +using Poco::Net::HTTPRequestHandlerFactory; +using Poco::Net::HTTPServer; +using Poco::Net::HTTPServerRequest; +using Poco::Net::HTTPServerResponse; +using Poco::Net::HTTPServerParams; +using Poco::Timestamp; +using Poco::DateTimeFormatter; +using Poco::DateTimeFormat; +using Poco::SharedPtr; +using Poco::Util::ServerApplication; +using Poco::Util::Application; +using Poco::Util::Option; +using Poco::Util::OptionSet; +using Poco::Util::HelpFormatter; +using Poco::Net::SSLManager; +using Poco::Net::Context; +using Poco::Net::KeyConsoleHandler; +using Poco::Net::PrivateKeyPassphraseHandler; +using Poco::Net::InvalidCertificateHandler; +using Poco::Net::AcceptCertificateHandler; + + +class TimeRequestHandler: public HTTPRequestHandler + /// Return a HTML document with the current date and time. +{ +public: + TimeRequestHandler(const std::string& format): + _format(format) + { + } + + void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) + { + Application& app = Application::instance(); + app.logger().information("Request from " + request.clientAddress().toString()); + + Timestamp now; + std::string dt(DateTimeFormatter::format(now, _format)); + + response.setChunkedTransferEncoding(true); + response.setContentType("text/html"); + + std::ostream& ostr = response.send(); + ostr << "HTTPTimeServer powered by C++ Portable Components"; + ostr << ""; + ostr << "

"; + ostr << dt; + ostr << "

"; + } + +private: + std::string _format; +}; + + +class TimeRequestHandlerFactory: public HTTPRequestHandlerFactory +{ +public: + TimeRequestHandlerFactory(const std::string& format): + _format(format) + { + } + + HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) + { + if (request.getURI() == "/") + return new TimeRequestHandler(_format); + else + return 0; + } + +private: + std::string _format; +}; + + +class HTTPSTimeServer: public Poco::Util::ServerApplication + /// The main application class. + /// + /// This class handles command-line arguments and + /// configuration files. + /// Start the HTTPTimeServer executable with the help + /// option (/help on Windows, --help on Unix) for + /// the available command line options. + /// + /// To use the sample configuration file (HTTPTimeServer.properties), + /// copy the file to the directory where the HTTPTimeServer executable + /// resides. If you start the debug version of the HTTPTimeServer + /// (HTTPTimeServerd[.exe]), you must also create a copy of the configuration + /// file named HTTPTimeServerd.properties. In the configuration file, you + /// can specify the port on which the server is listening (default + /// 9980) and the format of the date/time string sent back to the client. + /// + /// To test the TimeServer you can use any web browser (https://localhost:9980/). +{ +public: + HTTPSTimeServer(): _helpRequested(false) + { + } + + ~HTTPSTimeServer() + { + } + +protected: + void initialize(Application& self) + { + loadConfiguration(); // load default configuration files, if present + ServerApplication::initialize(self); + } + + void uninitialize() + { + ServerApplication::uninitialize(); + } + + void defineOptions(OptionSet& options) + { + ServerApplication::defineOptions(options); + + options.addOption( + Option("help", "h", "display help information on command line arguments") + .required(false) + .repeatable(false)); + } + + void handleOption(const std::string& name, const std::string& value) + { + ServerApplication::handleOption(name, value); + + if (name == "help") + _helpRequested = true; + } + + void displayHelp() + { + HelpFormatter helpFormatter(options()); + helpFormatter.setCommand(commandName()); + helpFormatter.setUsage("OPTIONS"); + helpFormatter.setHeader("A web server that serves the current date and time."); + helpFormatter.format(std::cout); + } + + int main(const std::vector& args) + { + if (_helpRequested) + { + displayHelp(); + } + else + { + // get parameters from configuration file + unsigned short port = (unsigned short) config().getInt("HTTPTimeServer.port", 9980); + std::string format(config().getString("HTTPTimeServer.format", DateTimeFormat::SORTABLE_FORMAT)); + + // set-up a server socket + SecureServerSocket svs(port); + // set-up a HTTPServer instance + HTTPServer srv(new TimeRequestHandlerFactory(format), svs, new HTTPServerParams); + // start the HTTPServer + srv.start(); + // wait for CTRL-C or kill + waitForTerminationRequest(); + // Stop the HTTPServer + srv.stop(); + } + return Application::EXIT_OK; + } + +private: + bool _helpRequested; +}; + + +int main(int argc, char** argv) +{ + HTTPSTimeServer app; + return app.run(argc, argv); +} diff --git a/NetSSL_OpenSSL/samples/Makefile b/NetSSL_OpenSSL/samples/Makefile new file mode 100644 index 000000000..31c2fccf0 --- /dev/null +++ b/NetSSL_OpenSSL/samples/Makefile @@ -0,0 +1,11 @@ +# +# Makefile +# +# $Id: //poco/1.3/NetSSL_OpenSSL/samples/Makefile#1 $ +# +# Makefile for Poco NetSSL Samples +# + +.DEFAULT all: + $(MAKE) -C HTTPSTimeServer $(MAKECMDGOALS) + $(MAKE) -C download $(MAKECMDGOALS) diff --git a/NetSSL_OpenSSL/samples/download/Makefile b/NetSSL_OpenSSL/samples/download/Makefile new file mode 100644 index 000000000..ad455cfae --- /dev/null +++ b/NetSSL_OpenSSL/samples/download/Makefile @@ -0,0 +1,19 @@ +# +# Makefile +# +# $Id: //poco/Main/template/sample.make#4 $ +# +# Makefile for Poco download +# + +include $(POCO_BASE)/build/rules/global + +SYSLIBS += -lssl -lcrypto -ldl + +objects = download + +target = download +target_version = 1 +target_libs = PocoNetSSL PocoNet PocoUtil PocoXML PocoFoundation + +include $(POCO_BASE)/build/rules/exec diff --git a/NetSSL_OpenSSL/samples/download/any.pem b/NetSSL_OpenSSL/samples/download/any.pem new file mode 100644 index 000000000..b17a97568 --- /dev/null +++ b/NetSSL_OpenSSL/samples/download/any.pem @@ -0,0 +1,48 @@ +-----BEGIN CERTIFICATE----- +MIICaDCCAdECCQCzfxSsk7yaLjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAzMDExMzA3MzFaFw0wNjAzMzExMzA3MzFaMH4xCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExETAPBgNVBAcTCFN0IEpha29iMRww +GgYDVQQKExNBcHBsaWVkIEluZm9ybWF0aWNzMQowCAYDVQQDFAEqMR4wHAYJKoZI +hvcNAQkBFg9pbmZvQGFwcGluZi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ +AoGBAJHGyXDHyCYoWz+65ltNwwZbhwOGnxr9P1WMATuFJh0bPBZxKbZRdbTm9KhZ +OlvsEIsfgiYdsxURYIqXfEgISYLZcZY0pQwGEOmB+0NeC/+ENSfOlNSthx6zSVlc +zhJ7+dJOGwepHAiLr1fRuc5jogYLraE+lKTnqAAFfzwvti77AgMBAAEwDQYJKoZI +hvcNAQEFBQADgYEAY/ZoeY1ukkEJX7259NeoVM0oahlulWV0rlCqyaeosOiDORPT +m6X1w/5MTCf9VyaD1zukoSZ4QqNVjHFXcXidbB7Tgt3yRuZ5PC5LIFCDPv9mgPne +mUA70yfctNfza2z3ZiQ6NDkW3mZX+1tmxYIrJQIrkVeYeqf1Gh2nyZrUMcE= +-----END CERTIFICATE----- +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,E7AE93C9E49184EA + +A2IqzNcWs+I5vzV+i+woDk56+yr58eU0Onw8eEvXkLjnSc58JU4327IF7yUbKWdW +Q7BYGGOkVFiZ7ANOwviDg5SUhxRDWCcW8dS6/p1vfdQ1C3qj2OwJjkpg0aDBIzJn +FzgguT3MF3ama77vxv0S3kOfmCj62MLqPGpj5pQ0/1hefRFbL8oAX8bXUN7/rmGM +Zc0QyzFZv2iQ04dY/6TNclwKPB4H0On4K+8BMs3PRkWA0clCaQaFO2+iwnk3XZfe ++MsKUEbLCpAQeYspYv1cw38dCdWq1KTP5aJk+oXgwjfX5cAaPTz74NTqTIsCcaTD +3vy7ukJYFlDR9Kyo7z8rMazYrKJslhnuRH0BhK9st9McwL957j5tZmrKyraCcmCx +dMAGcsis1va3ayYZpIpFqA4EhYrTM+6N8ZRfUap20+b5IQwHfTQDejUhL6rBwy7j +Ti5yD83/itoOMyXq2sV/XWfVD5zk/P5iv22O1EAQMhhnPB9K/I/JhuSGQJfn3cNh +ykOUYT0+vDeSeEVa+FVEP1W35G0alTbKbNs5Tb8KxJ3iDJUxokM//SvPXZy9hOVX +Y05imB04J15DaGbAHlNzunhuJi7121WV/JRXZRW9diE6hwpD8rwqi3FMuRUmy7U9 +aFA5poKRAYlo9YtZ3YpFyjGKB6MfCQcB2opuSnQ/gbugV41m67uQ4CDwWLaNRkTb +GlsMBNcHnidg15Bsat5HaB7l250ukrI13Uw1MYdDUzaS3gPfw9aC4F2w0p3U+DPH +80/zePxtroR7T4/+rI136Rl+aMXDMOEGCX1TVP8rjuZzuRyUSUKC8Q== +-----END RSA PRIVATE KEY----- +-----BEGIN CERTIFICATE----- +MIICXTCCAcYCCQC1Vk/N8qR4AjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAyMjcxMzI3MThaFw0wNjAzMjkxMzI3MThaMHMxCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExEjAQBgNVBAcTCVN0LiBKYWtvYjEP +MA0GA1UEChMGQXBwSW5mMQ8wDQYDVQQDEwZBcHBJbmYxGjAYBgkqhkiG9w0BCQEW +C2FwcEBpbmYuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsFXiPuicN +Im4oJwF8NuaFN+lgYwcZ6dAO3ILIR3kLA2PxF8HSQLfF8J8a4odZhLhctIMAKTxm +k0w8TW5qhL8QLdGzY9vzvkgdKOkan2t3sMeXJAfrM1AphTsmgntAQazGZjOj5p4W +jDnxQ+VXAylqwjHh49eSBxM3wgoscF4iLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GB +AIpfLdXiKchPvFMhQS8xTtXvrw5dVL3yImUMYs4GQi8RrjGmfGB3yMAR7B/b8v4a ++ztfusgWAWiUKuSGTk4S8YB0fsFlmOv0WDr+PyZ4Lui/a8opbyzGE7rqpnF/s0GO +M7uLCNNwIN7WhmxcWV0KZU1wTppoSWPJda1yTbBzF9XP +-----END CERTIFICATE----- diff --git a/NetSSL_OpenSSL/samples/download/download.vmsbuild b/NetSSL_OpenSSL/samples/download/download.vmsbuild new file mode 100644 index 000000000..7bfad4474 --- /dev/null +++ b/NetSSL_OpenSSL/samples/download/download.vmsbuild @@ -0,0 +1,8 @@ +# +# sample.vmsbuild +# +# $Id: //poco/Main/template/sample.vmsbuild#3 $ +# +EXE=download +download + diff --git a/NetSSL_OpenSSL/samples/download/download_vs71.vcproj b/NetSSL_OpenSSL/samples/download/download_vs71.vcproj new file mode 100644 index 000000000..19f683e64 --- /dev/null +++ b/NetSSL_OpenSSL/samples/download/download_vs71.vcproj @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NetSSL_OpenSSL/samples/download/download_vs80.vcproj b/NetSSL_OpenSSL/samples/download/download_vs80.vcproj new file mode 100644 index 000000000..87956a5f4 --- /dev/null +++ b/NetSSL_OpenSSL/samples/download/download_vs80.vcproj @@ -0,0 +1,210 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NetSSL_OpenSSL/samples/download/rootcert.pem b/NetSSL_OpenSSL/samples/download/rootcert.pem new file mode 100644 index 000000000..c26aeb244 --- /dev/null +++ b/NetSSL_OpenSSL/samples/download/rootcert.pem @@ -0,0 +1,15 @@ +-----BEGIN CERTIFICATE----- +MIICXTCCAcYCCQC1Vk/N8qR4AjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAyMjcxMzI3MThaFw0wNjAzMjkxMzI3MThaMHMxCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExEjAQBgNVBAcTCVN0LiBKYWtvYjEP +MA0GA1UEChMGQXBwSW5mMQ8wDQYDVQQDEwZBcHBJbmYxGjAYBgkqhkiG9w0BCQEW +C2FwcEBpbmYuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsFXiPuicN +Im4oJwF8NuaFN+lgYwcZ6dAO3ILIR3kLA2PxF8HSQLfF8J8a4odZhLhctIMAKTxm +k0w8TW5qhL8QLdGzY9vzvkgdKOkan2t3sMeXJAfrM1AphTsmgntAQazGZjOj5p4W +jDnxQ+VXAylqwjHh49eSBxM3wgoscF4iLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GB +AIpfLdXiKchPvFMhQS8xTtXvrw5dVL3yImUMYs4GQi8RrjGmfGB3yMAR7B/b8v4a ++ztfusgWAWiUKuSGTk4S8YB0fsFlmOv0WDr+PyZ4Lui/a8opbyzGE7rqpnF/s0GO +M7uLCNNwIN7WhmxcWV0KZU1wTppoSWPJda1yTbBzF9XP +-----END CERTIFICATE----- diff --git a/NetSSL_OpenSSL/samples/download/src/download.cpp b/NetSSL_OpenSSL/samples/download/src/download.cpp new file mode 100644 index 000000000..da24907a4 --- /dev/null +++ b/NetSSL_OpenSSL/samples/download/src/download.cpp @@ -0,0 +1,109 @@ +// +// download.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/samples/download/src/download.cpp#1 $ +// +// This sample demonstrates the URIStreamOpener class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/URIStreamOpener.h" +#include "Poco/StreamCopier.h" +#include "Poco/Path.h" +#include "Poco/URI.h" +#include "Poco/SharedPtr.h" +#include "Poco/Exception.h" +#include "Poco/Net/HTTPStreamFactory.h" +#include "Poco/Net/HTTPSStreamFactory.h" +#include "Poco/Net/FTPStreamFactory.h" +#include "Poco/Net/SSLManager.h" +#include "Poco/Net/KeyConsoleHandler.h" +#include "Poco/Net/ConsoleCertificateHandler.h" +#include +#include + + +using Poco::URIStreamOpener; +using Poco::StreamCopier; +using Poco::Path; +using Poco::URI; +using Poco::SharedPtr; +using Poco::Exception; +using Poco::Net::HTTPStreamFactory; +using Poco::Net::HTTPSStreamFactory; +using Poco::Net::FTPStreamFactory; +using Poco::Net::SSLManager; +using Poco::Net::Context; +using Poco::Net::KeyConsoleHandler; +using Poco::Net::PrivateKeyPassphraseHandler; +using Poco::Net::InvalidCertificateHandler; +using Poco::Net::ConsoleCertificateHandler; + + +int main(int argc, char** argv) +{ + HTTPStreamFactory::registerFactory(); + HTTPSStreamFactory::registerFactory(); + FTPStreamFactory::registerFactory(); + + if (argc != 2) + { + Path p(argv[0]); + std::cerr << "usage: " << p.getBaseName() << " " << std::endl; + std::cerr << " Download to standard output." << std::endl; + std::cerr << " Works with http, https, ftp and file URIs." << std::endl; + return 1; + } + + // Note: we must create the passphrase handler prior Context + SharedPtr ptrConsole = new KeyConsoleHandler(false); // ask the user via console for the pwd + SharedPtr ptrCert = new ConsoleCertificateHandler(false); // ask the user via console + SharedPtr ptrContext = new Context("any.pem", "rootcert.pem", false, Context::VERIFY_RELAXED, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); + SSLManager::instance().initializeClient(ptrConsole, ptrCert, ptrContext); + + // init of server part is not required, but we keep the code here as an example + /* + ptrConsole = new KeyConsoleHandler(true); // ask the user via console for the pwd + ptrCert = new ConsoleCertificateHandler(true); // ask the user via console + ptrContext = new Context("any.pem", "rootcert.pem", true, Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); + SSLManager::instance().initializeServer(ptrConsole, ptrCert, ptrContext); + */ + try + { + URI uri(argv[1]); + std::auto_ptr pStr(URIStreamOpener::defaultOpener().open(uri)); + StreamCopier::copyStream(*pStr.get(), std::cout); + } + catch (Exception& exc) + { + std::cerr << exc.displayText() << std::endl; + return 1; + } + + return 0; +} diff --git a/NetSSL_OpenSSL/samples/samples_vs71.sln b/NetSSL_OpenSSL/samples/samples_vs71.sln new file mode 100644 index 000000000..6e3306a16 --- /dev/null +++ b/NetSSL_OpenSSL/samples/samples_vs71.sln @@ -0,0 +1,29 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HTTPSTimeServer", "HTTPSTimeServer\HTTPSTimeServer_vs71.vcproj", "{7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "download", "download\download_vs71.vcproj", "{22F6B2E6-A532-4156-A49A-835A21BFF3F6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + debug_shared = debug_shared + release_shared = release_shared + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}.debug_shared.ActiveCfg = debug_shared|Win32 + {7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}.debug_shared.Build.0 = debug_shared|Win32 + {7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}.release_shared.ActiveCfg = release_shared|Win32 + {7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}.release_shared.Build.0 = release_shared|Win32 + {22F6B2E6-A532-4156-A49A-835A21BFF3F6}.debug_shared.ActiveCfg = debug_shared|Win32 + {22F6B2E6-A532-4156-A49A-835A21BFF3F6}.debug_shared.Build.0 = debug_shared|Win32 + {22F6B2E6-A532-4156-A49A-835A21BFF3F6}.release_shared.ActiveCfg = release_shared|Win32 + {22F6B2E6-A532-4156-A49A-835A21BFF3F6}.release_shared.Build.0 = release_shared|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/NetSSL_OpenSSL/samples/samples_vs80.sln b/NetSSL_OpenSSL/samples/samples_vs80.sln new file mode 100644 index 000000000..2ec513e8a --- /dev/null +++ b/NetSSL_OpenSSL/samples/samples_vs80.sln @@ -0,0 +1,25 @@ +Microsoft Visual Studio Solution File, Format Version 9.00 +# Visual Studio 2005 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HTTPSTimeServer", "HTTPSTimeServer\HTTPSTimeServer_vs80.vcproj", "{7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "download", "download\download_vs80.vcproj", "{22F6B2E6-A532-4156-A49A-835A21BFF3F6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + debug_shared|Win32 = debug_shared|Win32 + release_shared|Win32 = release_shared|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}.debug_shared|Win32.ActiveCfg = debug_shared|Win32 + {7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}.debug_shared|Win32.Build.0 = debug_shared|Win32 + {7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}.release_shared|Win32.ActiveCfg = release_shared|Win32 + {7C4285ED-F0BC-4CBA-B1F0-61FCB7FA26C4}.release_shared|Win32.Build.0 = release_shared|Win32 + {22F6B2E6-A532-4156-A49A-835A21BFF3F6}.debug_shared|Win32.ActiveCfg = debug_shared|Win32 + {22F6B2E6-A532-4156-A49A-835A21BFF3F6}.debug_shared|Win32.Build.0 = debug_shared|Win32 + {22F6B2E6-A532-4156-A49A-835A21BFF3F6}.release_shared|Win32.ActiveCfg = release_shared|Win32 + {22F6B2E6-A532-4156-A49A-835A21BFF3F6}.release_shared|Win32.Build.0 = release_shared|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/NetSSL_OpenSSL/src/AcceptCertificateHandler.cpp b/NetSSL_OpenSSL/src/AcceptCertificateHandler.cpp new file mode 100644 index 000000000..e2c333687 --- /dev/null +++ b/NetSSL_OpenSSL/src/AcceptCertificateHandler.cpp @@ -0,0 +1,58 @@ +// +// AcceptCertificateHandler.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/AcceptCertificateHandler.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: AcceptCertificateHandler +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/AcceptCertificateHandler.h" +#include "Poco/Net/CertificateHandlerFactory.h" + + +namespace Poco { +namespace Net { + + +AcceptCertificateHandler::AcceptCertificateHandler(bool server):InvalidCertificateHandler(server) +{ +} + + +AcceptCertificateHandler::~AcceptCertificateHandler() +{ +} + + +POCO_REGISTER_CHFACTORY(NetSSL_API, AcceptCertificateHandler) + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/CertificateHandlerFactory.cpp b/NetSSL_OpenSSL/src/CertificateHandlerFactory.cpp new file mode 100644 index 000000000..6d897c1ff --- /dev/null +++ b/NetSSL_OpenSSL/src/CertificateHandlerFactory.cpp @@ -0,0 +1,66 @@ +// +// CertificateHandlerFactory.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/CertificateHandlerFactory.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: CertificateHandlerFactory +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/CertificateHandlerFactory.h" +#include "Poco/Net/SSLManager.h" + + +namespace Poco { +namespace Net { + + +CertificateHandlerFactory::CertificateHandlerFactory() +{ +} + + +CertificateHandlerFactory::~CertificateHandlerFactory() +{ +} + + +CertificateHandlerFactoryRegistrar::CertificateHandlerFactoryRegistrar(const std::string& name, CertificateHandlerFactory* pFactory) +{ + SSLManager::instance().certificateHandlerFactoryMgr().setFactory(name, pFactory); +} + + +CertificateHandlerFactoryRegistrar::~CertificateHandlerFactoryRegistrar() +{ +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/CertificateHandlerFactoryMgr.cpp b/NetSSL_OpenSSL/src/CertificateHandlerFactoryMgr.cpp new file mode 100644 index 000000000..eed5365b4 --- /dev/null +++ b/NetSSL_OpenSSL/src/CertificateHandlerFactoryMgr.cpp @@ -0,0 +1,85 @@ +// +// CertificateHandlerFactoryMgr.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/CertificateHandlerFactoryMgr.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: CertificateHandlerFactoryMgr +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/CertificateHandlerFactoryMgr.h" + + +namespace Poco { +namespace Net { + + +CertificateHandlerFactoryMgr::CertificateHandlerFactoryMgr() +{ +} + + +CertificateHandlerFactoryMgr::~CertificateHandlerFactoryMgr() +{ +} + + +void CertificateHandlerFactoryMgr::setFactory(const std::string& name, CertificateHandlerFactory* pFactory) +{ + bool success = _factories.insert(make_pair(name, Poco::SharedPtr(pFactory))).second; + if (!success) + delete pFactory; + poco_assert(success); +} + + +bool CertificateHandlerFactoryMgr::hasFactory(const std::string& name) const +{ + return _factories.find(name) != _factories.end(); +} + + +const CertificateHandlerFactory* CertificateHandlerFactoryMgr::getFactory(const std::string& name) const +{ + FactoriesMap::const_iterator it = _factories.find(name); + if (it != _factories.end()) + return it->second; + else + return 0; +} + + +void CertificateHandlerFactoryMgr::removeFactory(const std::string& name) +{ + _factories.erase(name); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/ConsoleCertificateHandler.cpp b/NetSSL_OpenSSL/src/ConsoleCertificateHandler.cpp new file mode 100644 index 000000000..ce5400c9d --- /dev/null +++ b/NetSSL_OpenSSL/src/ConsoleCertificateHandler.cpp @@ -0,0 +1,78 @@ +// +// ConsoleCertificateHandler.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/ConsoleCertificateHandler.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: ConsoleCertificateHandler +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/ConsoleCertificateHandler.h" +#include +#include "Poco/Net/CertificateHandlerFactory.h" + + +namespace Poco { +namespace Net { + + +ConsoleCertificateHandler::ConsoleCertificateHandler(bool server):InvalidCertificateHandler(server) +{ +} + + +ConsoleCertificateHandler::~ConsoleCertificateHandler() +{ +} + + +void ConsoleCertificateHandler::onInvalidCertificate(const void*, VerificationErrorArgs& errorCert) +{ + const X509Certificate& aCert = errorCert.certificate(); + std::cout << " Certificate:\n"; + std::cout << "----------------\n"; + std::cout << " IssuerName: \t" << aCert.issuerName() << "\n"; + std::cout << " SubjectName:\t" << aCert.subjectName() << "\n\n"; + std::cout << "The certificate yielded the error: " << errorCert.errorMessage() << "\n\n"; + std::cout << "The error occurred at in the certificate chain at position " << errorCert.errorDepth() << "\n"; + std::cout << "Accept the certificate? (y,n)"; + char c; + std::cin >> c; + if (c == 'y' || c == 'Y') + errorCert.setIgnoreError(true); + else + errorCert.setIgnoreError(false); +} + + +POCO_REGISTER_CHFACTORY(NetSSL_API, ConsoleCertificateHandler) + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/Context.cpp b/NetSSL_OpenSSL/src/Context.cpp new file mode 100644 index 000000000..de24d132e --- /dev/null +++ b/NetSSL_OpenSSL/src/Context.cpp @@ -0,0 +1,129 @@ +// +// Context.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/Context.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: Context +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/Context.h" +#include "Poco/Net/SSLManager.h" +#include "Poco/Net/SSLException.h" +#include "Poco/File.h" +#include +#include +#include +#include + + +using Poco::File; + + +namespace Poco { +namespace Net { + + +Context::Context( + const std::string& privateKeyFile, + const std::string& caLocation, + bool isServerContext, + VerificationMode verMode, + int verificationDepth, + bool loadCAFromDefaultPath, + const std::string& cypherList):_pSSLContext(0), _mode(verMode), _server(isServerContext) +{ + + _pSSLContext = SSL_CTX_new(SSLv23_method()); + SSL_CTX_set_default_passwd_cb(_pSSLContext, &SSLManager::privateKeyPasswdCallback); + + File aFile(caLocation); + int errCode = 0; + if (aFile.isDirectory()) + errCode = SSL_CTX_load_verify_locations(_pSSLContext, 0, caLocation.c_str()); + else + errCode = SSL_CTX_load_verify_locations(_pSSLContext, caLocation.c_str(), 0); + if (errCode != 1) + { + SSL_CTX_free(_pSSLContext); + _pSSLContext = 0; + throw SSLContextException(std::string("Failed to load CA file/directory from ") + caLocation); + } + + if (loadCAFromDefaultPath) + { + errCode = SSL_CTX_set_default_verify_paths(_pSSLContext); + if (errCode != 1) + { + SSL_CTX_free(_pSSLContext); + _pSSLContext = 0; + throw SSLContextException(std::string("Failed to load CA file/directory from default location")); + } + } + + errCode = SSL_CTX_use_certificate_chain_file(_pSSLContext, privateKeyFile.c_str()); + if (errCode != 1) + { + SSL_CTX_free(_pSSLContext); + _pSSLContext = 0; + throw SSLContextException(std::string("Error loading certificate from file ") + privateKeyFile); + } + File tmp(privateKeyFile); + poco_assert (tmp.exists()); + errCode = SSL_CTX_use_PrivateKey_file(_pSSLContext, privateKeyFile.c_str(), SSL_FILETYPE_PEM); + if (errCode != 1) + { + SSL_CTX_free(_pSSLContext); + _pSSLContext = 0; + throw SSLContextException(std::string("Error loading private key from file ") + privateKeyFile); + } + int flags = (int)verMode; + if (verMode == VERIFY_STRICT || verMode == VERIFY_ONCE) + flags |= SSL_VERIFY_PEER; + if (serverContext()) + SSL_CTX_set_verify(_pSSLContext, flags, &SSLManager::verifyServerCallback); + else + SSL_CTX_set_verify(_pSSLContext, flags, &SSLManager::verifyClientCallback); + SSL_CTX_set_verify_depth(_pSSLContext, verificationDepth); + SSL_CTX_set_mode(_pSSLContext, SSL_MODE_AUTO_RETRY); +} + + +Context::~Context() +{ + if (_pSSLContext) + { + SSL_CTX_free(_pSSLContext); + _pSSLContext = 0; + } +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/HTTPSClientSession.cpp b/NetSSL_OpenSSL/src/HTTPSClientSession.cpp new file mode 100644 index 000000000..6c6ebbb8b --- /dev/null +++ b/NetSSL_OpenSSL/src/HTTPSClientSession.cpp @@ -0,0 +1,175 @@ +// +// HTTPSClientSession.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/HTTPSClientSession.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: HTTPSClient +// Module: HTTPSClientSession +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/HTTPSClientSession.h" +#include "Poco/Net/SecureStreamSocket.h" +#include "Poco/Net/SecureStreamSocketImpl.h" +#include "Poco/Net/HTTPRequest.h" +#include "Poco/Net/HTTPResponse.h" +#include "Poco/Net/HTTPHeaderStream.h" +#include "Poco/Net/HTTPStream.h" +#include "Poco/Net/HTTPFixedLengthStream.h" +#include "Poco/Net/HTTPChunkedStream.h" +#include "Poco/Net/NetException.h" +#include "Poco/NumberFormatter.h" + + +using Poco::NumberFormatter; +using Poco::IllegalStateException; + + +namespace Poco { +namespace Net { + + +HTTPSClientSession::HTTPSClientSession(): + HTTPClientSession(SecureStreamSocket()) +{ + setPort(Utility::HTTPS_PORT); +} + + +HTTPSClientSession::HTTPSClientSession(const SecureStreamSocket& socket): + HTTPClientSession(socket) +{ + setPort(Utility::HTTPS_PORT); +} + + +HTTPSClientSession::HTTPSClientSession(const std::string& host, Poco::UInt16 port): + HTTPClientSession(SecureStreamSocket()) +{ + setHost(host); + setPort(port); +} + + +HTTPSClientSession::~HTTPSClientSession() +{ +} + + +std::ostream& HTTPSClientSession::sendRequest(HTTPRequest& request) +{ + deleteResponseStream(); + + bool keepAlive = getKeepAlive(); + if (connected() && !keepAlive) + close(); + if (!connected()) + reconnect(); + request.setKeepAlive(keepAlive); + request.setHost(getHost(), getPort()); + + { + HTTPHeaderOutputStream hos(*this); + setReconnect(keepAlive); + request.write(hos); + setReconnect(false); + setExpectResponseBody(request.getMethod() != HTTPRequest::HTTP_HEAD); + } + if (request.getChunkedTransferEncoding()) + setRequestStream(new HTTPChunkedOutputStream(*this)); + else if (request.getContentLength() != HTTPMessage::UNKNOWN_CONTENT_LENGTH) + setRequestStream(new HTTPFixedLengthOutputStream(*this, request.getContentLength())); + else if (request.getMethod() == HTTPRequest::HTTP_GET || request.getMethod() == HTTPRequest::HTTP_HEAD) + setRequestStream(new HTTPFixedLengthOutputStream(*this, 0)); + else + setRequestStream(new HTTPOutputStream(*this)); + + return *getRequestStream(); +} + + +std::istream& HTTPSClientSession::receiveResponse(HTTPResponse& response) +{ + deleteRequestStream(); + + do + { + response.clear(); + HTTPHeaderInputStream his(*this); + try + { + response.read(his); + } + catch (MessageException&) + { + if (networkException()) + networkException()->rethrow(); + else + throw; + } + } + while (response.getStatus() == HTTPResponse::HTTP_CONTINUE); + + if (!getExpectResponseBody()) + setResponseStream(new HTTPFixedLengthInputStream(*this, 0)); + else if (response.getChunkedTransferEncoding()) + setResponseStream(new HTTPChunkedInputStream(*this)); + else if (response.getContentLength() != HTTPMessage::UNKNOWN_CONTENT_LENGTH) + setResponseStream(new HTTPFixedLengthInputStream(*this, response.getContentLength())); + else + setResponseStream(new HTTPInputStream(*this)); + + return *getResponseStream(); +} + + +std::string HTTPSClientSession::getHostInfo() const +{ + std::string result("https://"); + result.append(getHost()); + result.append(":"); + result.append(NumberFormatter::format(getPort())); + return result; +} + + +void HTTPSClientSession::connect(const SocketAddress& address) +{ + if (!getProxyHost().empty()) + { + StreamSocket& aSock = socket(); + SecureStreamSocketImpl* pImplSock = dynamic_cast(aSock.impl()); + poco_check_ptr (pImplSock); + pImplSock->setTunnelEndPoint(getHost(), getPort()); + } + HTTPSession::connect(address); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/HTTPSSessionInstantiator.cpp b/NetSSL_OpenSSL/src/HTTPSSessionInstantiator.cpp new file mode 100644 index 000000000..fe4b5e6bb --- /dev/null +++ b/NetSSL_OpenSSL/src/HTTPSSessionInstantiator.cpp @@ -0,0 +1,77 @@ +// +// HTTPSSessionInstantiator.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/HTTPSSessionInstantiator.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: HTTPSClient +// Module: HTTPSSessionInstantiator +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/HTTPSSessionInstantiator.h" +#include "Poco/Net/HTTPSessionFactory.h" +#include "Poco/Net/HTTPSClientSession.h" + + +namespace Poco { +namespace Net { + + +HTTPSSessionInstantiator::HTTPSSessionInstantiator() +{ +} + + +HTTPSSessionInstantiator::~HTTPSSessionInstantiator() +{ +} + + +HTTPClientSession* HTTPSSessionInstantiator::createClientSession(const Poco::URI& uri) +{ + poco_assert (uri.getScheme() == "https"); + HTTPSClientSession* pSession = new HTTPSClientSession(uri.getHost(), uri.getPort()); + pSession->setProxy(proxyHost(), proxyPort()); + return pSession; +} + + +void HTTPSSessionInstantiator::registerInstantiator() +{ + HTTPSessionFactory::defaultFactory().registerProtocol("https", new HTTPSSessionInstantiator); +} + + +void HTTPSSessionInstantiator::unregisterInstantiator() +{ + HTTPSessionFactory::defaultFactory().unregisterProtocol("https"); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/HTTPSStreamFactory.cpp b/NetSSL_OpenSSL/src/HTTPSStreamFactory.cpp new file mode 100644 index 000000000..41ca73078 --- /dev/null +++ b/NetSSL_OpenSSL/src/HTTPSStreamFactory.cpp @@ -0,0 +1,128 @@ +// +// HTTPSStreamFactory.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/HTTPSStreamFactory.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: HTTPSClient +// Module: HTTPSStreamFactory +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/HTTPSStreamFactory.h" +#include "Poco/Net/HTTPSClientSession.h" +#include "Poco/Net/HTTPIOStream.h" +#include "Poco/Net/HTTPRequest.h" +#include "Poco/Net/HTTPResponse.h" +#include "Poco/Net/NetException.h" +#include "Poco/URI.h" +#include "Poco/URIStreamOpener.h" +#include "Poco/UnbufferedStreamBuf.h" + + +using Poco::URIStreamFactory; +using Poco::URI; +using Poco::URIStreamOpener; +using Poco::UnbufferedStreamBuf; + + +namespace Poco { +namespace Net { + + +HTTPSStreamFactory::HTTPSStreamFactory(): + _proxyPort(HTTPSession::HTTP_PORT) +{ +} + + +HTTPSStreamFactory::HTTPSStreamFactory(const std::string& proxyHost, Poco::UInt16 proxyPort): + _proxyHost(proxyHost), + _proxyPort(proxyPort) +{ +} + + +HTTPSStreamFactory::~HTTPSStreamFactory() +{ +} + + +std::istream* HTTPSStreamFactory::open(const URI& uri) +{ + poco_assert (uri.getScheme() == "https"); + + URI resolvedURI(uri); + HTTPClientSession* pSession = 0; + try + { + int redirects = 0; + do + { + pSession = new HTTPSClientSession(resolvedURI.getHost(), resolvedURI.getPort()); + pSession->setProxy(_proxyHost, _proxyPort); + std::string path = resolvedURI.getPathAndQuery(); + if (path.empty()) path = "/"; + HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); + pSession->sendRequest(req); + HTTPResponse res; + std::istream& rs = pSession->receiveResponse(res); + bool moved = (res.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY || + res.getStatus() == HTTPResponse::HTTP_FOUND || + res.getStatus() == HTTPResponse::HTTP_SEE_OTHER); + if (moved) + { + resolvedURI.resolve(res.get("Location")); + delete pSession; + ++redirects; + } + else if (res.getStatus() == HTTPResponse::HTTP_OK) + { + return new HTTPResponseStream(rs, pSession); + } + else throw HTTPException(res.getReason(), uri.toString()); + } + while (redirects < MAX_REDIRECTS); + throw HTTPException("Too many redirects", uri.toString()); + } + catch (...) + { + delete pSession; + throw; + } +} + + +void HTTPSStreamFactory::registerFactory() +{ + std::string https("https"); + URIStreamOpener::defaultOpener().registerStreamFactory(https, new HTTPSStreamFactory); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/InvalidCertificateHandler.cpp b/NetSSL_OpenSSL/src/InvalidCertificateHandler.cpp new file mode 100644 index 000000000..6697b8f89 --- /dev/null +++ b/NetSSL_OpenSSL/src/InvalidCertificateHandler.cpp @@ -0,0 +1,67 @@ +// +// InvalidCertificateHandler.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/InvalidCertificateHandler.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: InvalidCertificateHandler +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/InvalidCertificateHandler.h" +#include "Poco/Net/SSLManager.h" +#include "Poco/Delegate.h" + + +using Poco::Delegate; + + +namespace Poco { +namespace Net { + + +InvalidCertificateHandler::InvalidCertificateHandler(bool handleErrorsOnServerSide): _handleErrorsOnServerSide(handleErrorsOnServerSide) +{ + if (_handleErrorsOnServerSide) + SSLManager::instance().ServerVerificationError += Delegate(this, &InvalidCertificateHandler::onInvalidCertificate); + else + SSLManager::instance().ClientVerificationError += Delegate(this, &InvalidCertificateHandler::onInvalidCertificate); +} + + +InvalidCertificateHandler::~InvalidCertificateHandler() +{ + if (_handleErrorsOnServerSide) + SSLManager::instance().ServerVerificationError -= Delegate(this, &InvalidCertificateHandler::onInvalidCertificate); + else + SSLManager::instance().ClientVerificationError -= Delegate(this, &InvalidCertificateHandler::onInvalidCertificate); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/KeyConsoleHandler.cpp b/NetSSL_OpenSSL/src/KeyConsoleHandler.cpp new file mode 100644 index 000000000..99308aec0 --- /dev/null +++ b/NetSSL_OpenSSL/src/KeyConsoleHandler.cpp @@ -0,0 +1,66 @@ +// +// KeyConsoleHandler.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/KeyConsoleHandler.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: KeyConsoleHandler +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/KeyConsoleHandler.h" +#include "Poco/Net/PrivateKeyFactory.h" +#include + + +namespace Poco { +namespace Net { + + +KeyConsoleHandler::KeyConsoleHandler(bool server):PrivateKeyPassphraseHandler(server) +{ +} + + +KeyConsoleHandler::~KeyConsoleHandler() +{ +} + + +void KeyConsoleHandler::onPrivateKeyRequested(const void* pSender, std::string& privateKey) +{ + std::cout << "Please enter the pass-phrase for the private key: "; + std::cin >> privateKey; +} + + +POCO_REGISTER_KEYFACTORY(NetSSL_API, KeyConsoleHandler) + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/KeyFileHandler.cpp b/NetSSL_OpenSSL/src/KeyFileHandler.cpp new file mode 100644 index 000000000..195c1d7bc --- /dev/null +++ b/NetSSL_OpenSSL/src/KeyFileHandler.cpp @@ -0,0 +1,79 @@ +// +// KeyFileHandler.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/KeyFileHandler.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: KeyFileHandler +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/KeyFileHandler.h" +#include "Poco/Net/PrivateKeyFactory.h" +#include "Poco/Net/SSLManager.h" +#include "Poco/File.h" +#include "Poco/Util/LayeredConfiguration.h" +#include "Poco/Util/Application.h" +#include "Poco/Util/OptionException.h" + + +namespace Poco { +namespace Net { + + +const std::string KeyFileHandler::CFG_PRIV_KEY_FILE("privateKeyPassphraseHandler.options.password"); + + +KeyFileHandler::KeyFileHandler(bool server):PrivateKeyPassphraseHandler(server) +{ +} + + +KeyFileHandler::~KeyFileHandler() +{ +} + + +void KeyFileHandler::onPrivateKeyRequested(const void* pSender, std::string& privateKey) +{ + Poco::Util::LayeredConfiguration& config = Poco::Util::Application::instance().config(); + std::string prefix = serverSide() ? SSLManager::CFG_SERVER_PREFIX : SSLManager::CFG_CLIENT_PREFIX; + if (!config.hasProperty(prefix+CFG_PRIV_KEY_FILE)) + { + throw Poco::Util::EmptyOptionException(std::string("Missing Configuration Entry: ") + prefix+CFG_PRIV_KEY_FILE); + } + + privateKey = config.getString(prefix+CFG_PRIV_KEY_FILE); +} + + +POCO_REGISTER_KEYFACTORY(NetSSL_API,KeyFileHandler) + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/PrivateKeyFactory.cpp b/NetSSL_OpenSSL/src/PrivateKeyFactory.cpp new file mode 100644 index 000000000..5e340372e --- /dev/null +++ b/NetSSL_OpenSSL/src/PrivateKeyFactory.cpp @@ -0,0 +1,66 @@ +// +// PrivateKeyFactory.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/PrivateKeyFactory.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: PrivateKeyFactory +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/PrivateKeyFactory.h" +#include "Poco/Net/SSLManager.h" + + +namespace Poco { +namespace Net { + + +PrivateKeyFactory::PrivateKeyFactory() +{ +} + + +PrivateKeyFactory::~PrivateKeyFactory() +{ +} + + +PrivateKeyFactoryRegistrar::PrivateKeyFactoryRegistrar(const std::string& name, PrivateKeyFactory* pFactory) +{ + SSLManager::instance().privateKeyFactoryMgr().setFactory(name, pFactory); +} + + +PrivateKeyFactoryRegistrar::~PrivateKeyFactoryRegistrar() +{ +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/PrivateKeyFactoryMgr.cpp b/NetSSL_OpenSSL/src/PrivateKeyFactoryMgr.cpp new file mode 100644 index 000000000..ee5a07d20 --- /dev/null +++ b/NetSSL_OpenSSL/src/PrivateKeyFactoryMgr.cpp @@ -0,0 +1,85 @@ +// +// PrivateKeyFactoryMgr.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/PrivateKeyFactoryMgr.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: PrivateKeyFactoryMgr +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/PrivateKeyFactoryMgr.h" + + +namespace Poco { +namespace Net { + + +PrivateKeyFactoryMgr::PrivateKeyFactoryMgr() +{ +} + + +PrivateKeyFactoryMgr::~PrivateKeyFactoryMgr() +{ +} + + +void PrivateKeyFactoryMgr::setFactory(const std::string& name, PrivateKeyFactory* pFactory) +{ + bool success = _factories.insert(make_pair(name, Poco::SharedPtr(pFactory))).second; + if (!success) + delete pFactory; + poco_assert(success); +} + + +bool PrivateKeyFactoryMgr::hasFactory(const std::string& name) const +{ + return _factories.find(name) != _factories.end(); +} + + +const PrivateKeyFactory* PrivateKeyFactoryMgr::getFactory(const std::string& name) const +{ + FactoriesMap::const_iterator it = _factories.find(name); + if (it != _factories.end()) + return it->second; + else + return 0; +} + + +void PrivateKeyFactoryMgr::removeFactory(const std::string& name) +{ + _factories.erase(name); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/PrivateKeyPassphraseHandler.cpp b/NetSSL_OpenSSL/src/PrivateKeyPassphraseHandler.cpp new file mode 100644 index 000000000..5f2e1f128 --- /dev/null +++ b/NetSSL_OpenSSL/src/PrivateKeyPassphraseHandler.cpp @@ -0,0 +1,61 @@ +// +// PrivateKeyPassphraseHandler.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/PrivateKeyPassphraseHandler.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: PrivateKeyPassphraseHandler +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/PrivateKeyPassphraseHandler.h" +#include "Poco/Net/SSLManager.h" +#include "Poco/Delegate.h" + + +using Poco::Delegate; + + +namespace Poco { +namespace Net { + + +PrivateKeyPassphraseHandler::PrivateKeyPassphraseHandler(bool onServerSide): _serverSide(onServerSide) +{ + SSLManager::instance().PrivateKeyPassPhrase += Delegate(this, &PrivateKeyPassphraseHandler::onPrivateKeyRequested); +} + + +PrivateKeyPassphraseHandler::~PrivateKeyPassphraseHandler() +{ + SSLManager::instance().PrivateKeyPassPhrase -= Delegate(this, &PrivateKeyPassphraseHandler::onPrivateKeyRequested); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/SSLException.cpp b/NetSSL_OpenSSL/src/SSLException.cpp new file mode 100644 index 000000000..edb5dcbbb --- /dev/null +++ b/NetSSL_OpenSSL/src/SSLException.cpp @@ -0,0 +1,50 @@ +// +// SSLException.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/SSLException.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: SSLException +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/SSLException.h" +#include + + +namespace Poco { +namespace Net { + + +POCO_IMPLEMENT_EXCEPTION(SSLException, NetException, "SSLException") +POCO_IMPLEMENT_EXCEPTION(SSLContextException, SSLException, "SSLContextException") +POCO_IMPLEMENT_EXCEPTION(InvalidCertificateException, SSLException, "InvalidCertificateException") + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/SSLInitializer.cpp b/NetSSL_OpenSSL/src/SSLInitializer.cpp new file mode 100644 index 000000000..e26ad44bb --- /dev/null +++ b/NetSSL_OpenSSL/src/SSLInitializer.cpp @@ -0,0 +1,146 @@ +// +// SSLInitializer.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/SSLInitializer.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: SSLInitializer +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/SSLInitializer.h" +#include "Poco/Net/KeyConsoleHandler.h" +#include "Poco/Net/KeyFileHandler.h" +#include "Poco/RandomStream.h" +#include "Poco/Thread.h" +#include +#include +#include + + +using Poco::RandomInputStream; +using Poco::Thread; +using Poco::FastMutex; + + +namespace Poco { +namespace Net { + + +FastMutex* SSLInitializer::_mutexes(0); +int SSLInitializer::_rc(0); + + +static SSLInitializer initializer; + + +SSLInitializer::SSLInitializer() +{ + initialize(); +} + + +SSLInitializer::~SSLInitializer() +{ + uninitialize(); +} + + +void SSLInitializer::initialize() +{ + if (++_rc == 1) + { + poco_assert (1 == SSL_library_init()); // always returns 1 + SSL_load_error_strings(); + + char seed[SEEDSIZE]; + RandomInputStream rnd; + rnd.read(seed, sizeof(seed)); + RAND_seed(seed, SEEDSIZE); + + int nMutexes = CRYPTO_num_locks(); + _mutexes = new FastMutex[nMutexes]; + CRYPTO_set_locking_callback(&SSLInitializer::lock); + CRYPTO_set_id_callback(&SSLInitializer::id); + CRYPTO_set_dynlock_create_callback(&SSLInitializer::dynlockCreate); + CRYPTO_set_dynlock_lock_callback(&SSLInitializer::dynlock); + CRYPTO_set_dynlock_destroy_callback(&SSLInitializer::dynlockDestroy); + } +} + + +void SSLInitializer::uninitialize() +{ + if (--_rc == 0) + { + delete [] _mutexes; + } +} + + +void SSLInitializer::lock(int mode, int n, const char* file, int line) +{ + if (mode & CRYPTO_LOCK) + _mutexes[n].lock(); + else + _mutexes[n].unlock(); +} + + +unsigned long SSLInitializer::id() +{ + Thread* pThread = Thread::current(); + return pThread ? pThread->id() : 0; +} + + +struct CRYPTO_dynlock_value* SSLInitializer::dynlockCreate(const char* file, int line) +{ + return new CRYPTO_dynlock_value; +} + + +void SSLInitializer::dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line) +{ + poco_check_ptr (lock); + + if (mode & CRYPTO_LOCK) + lock->_mutex.lock(); + else + lock->_mutex.unlock(); +} + + +void SSLInitializer::dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line) +{ + delete lock; +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/SSLManager.cpp b/NetSSL_OpenSSL/src/SSLManager.cpp new file mode 100644 index 000000000..c872caffb --- /dev/null +++ b/NetSSL_OpenSSL/src/SSLManager.cpp @@ -0,0 +1,305 @@ +// +// SSLManager.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/SSLManager.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: SSLManager +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/SSLManager.h" +#include "Poco/Net/Context.h" +#include "Poco/Net/Utility.h" +#include "Poco/Net/PrivateKeyPassphraseHandler.h" +#include "Poco/Net/SSLInitializer.h" +#include "Poco/SingletonHolder.h" +#include "Poco/Delegate.h" +#include "Poco/Util/Application.h" +#include "Poco/Util/OptionException.h" +#include "Poco/Util/LayeredConfiguration.h" + + +namespace Poco { +namespace Net { + + +const std::string SSLManager::CFG_PRIV_KEY_FILE("privateKeyFile"); +const std::string SSLManager::CFG_CA_LOCATION("caConfig"); +const std::string SSLManager::CFG_VER_MODE("verificationMode"); +const Context::VerificationMode SSLManager::VAL_VER_MODE(Context::VERIFY_STRICT); +const std::string SSLManager::CFG_VER_DEPTH("verificationDepth"); +const int SSLManager::VAL_VER_DEPTH(9); +const std::string SSLManager::CFG_ENABLE_DEFAULT_CA("loadDefaultCAFile"); +const bool SSLManager::VAL_ENABLE_DEFAULT_CA(false); +const std::string SSLManager::CFG_CYPHER_LIST("cypherList"); +const std::string SSLManager::VAL_CYPHER_LIST("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); +const std::string SSLManager::CFG_DELEGATE_HANDLER("privateKeyPassphraseHandler.name"); +const std::string SSLManager::VAL_DELEGATE_HANDLER("KeyConsoleHandler"); +const std::string SSLManager::CFG_CERTIFICATE_HANDLER("invalidCertificateHandler.name"); +const std::string SSLManager::VAL_CERTIFICATE_HANDLER("ConsoleCertificateHandler"); +const std::string SSLManager::CFG_SERVER_PREFIX("openSSL.server."); +const std::string SSLManager::CFG_CLIENT_PREFIX("openSSL.client."); + + +SSLManager::SSLManager() +{ + SSLInitializer::initialize(); +} + + +SSLManager::~SSLManager() +{ + PrivateKeyPassPhrase.clear(); + ClientVerificationError.clear(); + ServerVerificationError.clear(); + _ptrServerPassPhraseHandler = 0; + _ptrServerCertificateHandler = 0; + _ptrDefaultServerContext = 0; + _ptrClientPassPhraseHandler = 0; + _ptrClientCertificateHandler = 0; + _ptrDefaultClientContext = 0; + SSLInitializer::uninitialize(); +} + + +SSLManager& SSLManager::instance() +{ + static Poco::SingletonHolder singleton; + return *singleton.get(); +} + + +void SSLManager::initializeServer(PrivateKeyPassphraseHandlerPtr& ptrPassPhraseHandler, InvalidCertificateHandlerPtr& ptrHandler, ContextPtr ptrContext) +{ + _ptrServerPassPhraseHandler = ptrPassPhraseHandler; + _ptrServerCertificateHandler = ptrHandler; + _ptrDefaultServerContext = ptrContext; +} + + +void SSLManager::initializeClient(PrivateKeyPassphraseHandlerPtr& ptrPassPhraseHandler, InvalidCertificateHandlerPtr& ptrHandler, ContextPtr ptrContext) +{ + _ptrClientPassPhraseHandler = ptrPassPhraseHandler; + _ptrClientCertificateHandler = ptrHandler; + _ptrDefaultClientContext = ptrContext; +} + + +SSLManager::ContextPtr SSLManager::defaultServerContext() +{ + if (!_ptrDefaultServerContext) + initDefaultContext(true); + + return _ptrDefaultServerContext; +} + + +SSLManager::ContextPtr SSLManager::defaultClientContext() +{ + if (!_ptrDefaultClientContext) + initDefaultContext(false); + + return _ptrDefaultClientContext; +} + + +SSLManager::PrivateKeyPassphraseHandlerPtr SSLManager::serverPassPhraseHandler() +{ + if (!_ptrServerPassPhraseHandler) + initPassPhraseHandler(true); + + return _ptrServerPassPhraseHandler; +} + + +SSLManager::PrivateKeyPassphraseHandlerPtr SSLManager::clientPassPhraseHandler() +{ + if (!_ptrClientPassPhraseHandler) + initPassPhraseHandler(false); + + return _ptrClientPassPhraseHandler; +} + + +SSLManager::InvalidCertificateHandlerPtr SSLManager::serverCertificateHandler() +{ + if (!_ptrServerCertificateHandler) + initCertificateHandler(true); + + return _ptrServerCertificateHandler; +} + + +SSLManager::InvalidCertificateHandlerPtr SSLManager::clientCertificateHandler() +{ + if (!_ptrClientCertificateHandler) + initCertificateHandler(false); + + return _ptrClientCertificateHandler; +} + + +int SSLManager::verifyCallback(bool server, int ok, X509_STORE_CTX* pStore) +{ + if (!ok) + { + X509* pCert = X509_STORE_CTX_get_current_cert(pStore); + X509Certificate x509(pCert); + int depth = X509_STORE_CTX_get_error_depth(pStore); + int err = X509_STORE_CTX_get_error(pStore); + std::string error(X509_verify_cert_error_string(err)); + VerificationErrorArgs args(x509, depth, err, error); + if (server) + SSLManager::instance().ServerVerificationError.notify(&SSLManager::instance(), args); + else + SSLManager::instance().ClientVerificationError.notify(&SSLManager::instance(), args); + ok = args.getIgnoreError() ? 1 : 0; + } + + return ok; +} + + +int SSLManager::privateKeyPasswdCallback(char* pBuf, int size, int flag, void* userData) +{ + std::string pwd; + SSLManager::instance().PrivateKeyPassPhrase.notify(&SSLManager::instance(), pwd); + + strncpy(pBuf, (char *)(pwd.c_str()), size); + pBuf[size - 1] = '\0'; + if (size > pwd.length()) + size = (int) pwd.length(); + + return size; +} + + +void SSLManager::initDefaultContext(bool server) +{ + if (server && _ptrDefaultServerContext) return; + if (!server && _ptrDefaultClientContext) return; + + initEvents(server); + + Poco::Util::LayeredConfiguration& config = Poco::Util::Application::instance().config(); + std::string prefix = server ? CFG_SERVER_PREFIX : CFG_CLIENT_PREFIX; + if (!config.hasProperty(prefix+CFG_PRIV_KEY_FILE)) + { + throw Poco::Util::EmptyOptionException(std::string("Missing Configuration Entry: ") + prefix+CFG_PRIV_KEY_FILE); + } + // mandatory options + std::string privKeyFile = config.getString(prefix+CFG_PRIV_KEY_FILE); + std::string caLocation = config.getString(prefix+CFG_CA_LOCATION); + + // optional options for which we have defaults defined + Context::VerificationMode verMode = VAL_VER_MODE; + if (config.hasProperty(prefix+CFG_VER_MODE)) + { + // either: none, relaxed, strict, once + std::string mode = config.getString(prefix+CFG_VER_MODE); + verMode = Utility::convertVerificationMode(mode); + } + + int verDepth = config.getInt(prefix+CFG_VER_DEPTH, VAL_VER_DEPTH); + bool loadDefCA = config.getBool(prefix+CFG_ENABLE_DEFAULT_CA, VAL_ENABLE_DEFAULT_CA); + std::string cypherList = config.getString(prefix+CFG_CYPHER_LIST, VAL_CYPHER_LIST); + if (server) + { + _ptrDefaultServerContext = new Context(privKeyFile, caLocation, server, verMode, verDepth, loadDefCA, cypherList); + } + else + { + _ptrDefaultClientContext = new Context(privKeyFile, caLocation, server, verMode, verDepth, loadDefCA, cypherList); + } +} + + +void SSLManager::initEvents(bool server) +{ + initPassPhraseHandler(server); + initCertificateHandler(server); +} + + +void SSLManager::initPassPhraseHandler(bool server) +{ + if (server && _ptrServerPassPhraseHandler) return; + if (!server && _ptrClientPassPhraseHandler) return; + + std::string prefix = server ? CFG_SERVER_PREFIX : CFG_CLIENT_PREFIX; + Poco::Util::LayeredConfiguration& config = Poco::Util::Application::instance().config(); + + std::string className(config.getString(prefix+CFG_DELEGATE_HANDLER, VAL_DELEGATE_HANDLER)); + + const PrivateKeyFactory* pFactory = 0; + if (privateKeyFactoryMgr().hasFactory(className)) + { + pFactory = privateKeyFactoryMgr().getFactory(className); + } + + if (pFactory) + { + if (server) + _ptrServerPassPhraseHandler = pFactory->create(server); + else + _ptrClientPassPhraseHandler = pFactory->create(server); + } + else throw Poco::Util::UnknownOptionException(std::string("No PassPhrasehandler known with the name ") + className); +} + + +void SSLManager::initCertificateHandler(bool server) +{ + if (server && _ptrServerCertificateHandler) return; + if (!server && _ptrClientCertificateHandler) return; + + std::string prefix = server ? CFG_SERVER_PREFIX : CFG_CLIENT_PREFIX; + Poco::Util::LayeredConfiguration& config = Poco::Util::Application::instance().config(); + + std::string className(config.getString(prefix+CFG_CERTIFICATE_HANDLER, VAL_CERTIFICATE_HANDLER)); + + const CertificateHandlerFactory* pFactory = 0; + if (certificateHandlerFactoryMgr().hasFactory(className)) + { + pFactory = certificateHandlerFactoryMgr().getFactory(className); + } + + if (pFactory) + { + if (server) + _ptrServerCertificateHandler = pFactory->create(true); + else + _ptrClientCertificateHandler = pFactory->create(false); + } + else throw Poco::Util::UnknownOptionException(std::string("No InvalidCertificate handler known with the name ") + className); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/SecureServerSocket.cpp b/NetSSL_OpenSSL/src/SecureServerSocket.cpp new file mode 100644 index 000000000..e634f4daa --- /dev/null +++ b/NetSSL_OpenSSL/src/SecureServerSocket.cpp @@ -0,0 +1,113 @@ +// +// SecureServerSocket.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/SecureServerSocket.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureServerSocket +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/SecureServerSocket.h" +#include "Poco/Net/SecureServerSocketImpl.h" +#include "Poco/Net/SecureStreamSocket.h" +#include "Poco/Exception.h" + + +using Poco::InvalidArgumentException; + + +namespace Poco { +namespace Net { + + +SecureServerSocket::SecureServerSocket(): + ServerSocket(new SecureServerSocketImpl, true) +{ +} + + +SecureServerSocket::SecureServerSocket(const Socket& socket): + ServerSocket(socket) +{ + if (!dynamic_cast(impl())) + throw InvalidArgumentException("Cannot assign incompatible socket"); +} + + +SecureServerSocket::SecureServerSocket(const SocketAddress& address, int backlog): + ServerSocket(new SecureServerSocketImpl, true) +{ + impl()->bind(address, true); + impl()->listen(backlog); +} + + +SecureServerSocket::SecureServerSocket(Poco::UInt16 port, int backlog): + ServerSocket(new SecureServerSocketImpl, true) +{ + IPAddress wildcardAddr; + SocketAddress address(wildcardAddr, port); + impl()->bind(address, true); + impl()->listen(backlog); +} + + +SecureServerSocket::~SecureServerSocket() +{ +} + + +SecureServerSocket& SecureServerSocket::operator = (const Socket& socket) +{ + if (&socket != this) + { + if (dynamic_cast(socket.impl())) + ServerSocket::operator = (socket); + else + throw InvalidArgumentException("Cannot assign incompatible socket"); + } + return *this; +} + + +StreamSocket SecureServerSocket::acceptConnection(SocketAddress& clientAddr) +{ + return SecureStreamSocket(impl()->acceptConnection(clientAddr)); +} + + +StreamSocket SecureServerSocket::acceptConnection() +{ + SocketAddress clientAddr; + return SecureStreamSocket(impl()->acceptConnection(clientAddr)); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/SecureServerSocketImpl.cpp b/NetSSL_OpenSSL/src/SecureServerSocketImpl.cpp new file mode 100644 index 000000000..0f12b5ebd --- /dev/null +++ b/NetSSL_OpenSSL/src/SecureServerSocketImpl.cpp @@ -0,0 +1,131 @@ +// +// SecureServerSocketImpl.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/SecureServerSocketImpl.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureServerSocketImpl +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/SecureServerSocketImpl.h" + + +namespace Poco { +namespace Net { + + +SecureServerSocketImpl::SecureServerSocketImpl() +{ +} + + +SecureServerSocketImpl::~SecureServerSocketImpl() +{ +} + + +SocketImpl* SecureServerSocketImpl::acceptConnection(SocketAddress& clientAddr) +{ + return _socket.acceptConnection(clientAddr); +} + + +void SecureServerSocketImpl::connect(const SocketAddress& address) +{ + _socket.connect(address); + setSockfd(_socket.sockfd()); +} + + +void SecureServerSocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout) +{ + _socket.connect(address, timeout); + setSockfd(_socket.sockfd()); +} + + +void SecureServerSocketImpl::connectNB(const SocketAddress& address) +{ + _socket.connectNB(address); + setSockfd(_socket.sockfd()); +} + + +void SecureServerSocketImpl::bind(const SocketAddress& address, bool reuseAddress) +{ + _socket.bind(address, reuseAddress); +} + + +void SecureServerSocketImpl::listen(int backlog) +{ + _socket.listen(backlog); + setSockfd(_socket.sockfd()); +} + + +void SecureServerSocketImpl::close() +{ + invalidate(); + _socket.close(); +} + + +int SecureServerSocketImpl::sendBytes(const void* buffer, int length, int flags) +{ + return _socket.sendBytes(buffer, length, flags); +} + + +int SecureServerSocketImpl::receiveBytes(void* buffer, int length, int flags) +{ + return _socket.receiveBytes(buffer, length, flags); +} + + +int SecureServerSocketImpl::sendTo(const void* buffer, int length, const SocketAddress& address, int flags) +{ + return _socket.sendTo(buffer, length, address, flags); +} + + +int SecureServerSocketImpl::receiveFrom(void* buffer, int length, SocketAddress& address, int flags) +{ + return _socket.receiveFrom(buffer, length, address, flags); +} + + +void SecureServerSocketImpl::sendUrgent(unsigned char data) +{ + return _socket.sendUrgent(data); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/SecureSocketImpl.cpp b/NetSSL_OpenSSL/src/SecureSocketImpl.cpp new file mode 100644 index 000000000..3ee4e7dd0 --- /dev/null +++ b/NetSSL_OpenSSL/src/SecureSocketImpl.cpp @@ -0,0 +1,603 @@ +// +// SecureSocketImpl.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/SecureSocketImpl.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureSocketImpl +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/SecureSocketImpl.h" +#include "Poco/Net/SSLException.h" +#include "Poco/Net/SSLManager.h" +#include "Poco/Net/Utility.h" +#include "Poco/Net/SecureStreamSocketImpl.h" +#include "Poco/Net/StreamSocketImpl.h" +#include "Poco/Net/NetException.h" +#include "Poco/Net/DNS.h" +#include "Poco/NumberFormatter.h" +#include "Poco/NumberParser.h" +#include "Poco/String.h" +#include "Poco/RegularExpression.h" +#include +#include + + +using Poco::IOException; +using Poco::TimeoutException; +using Poco::InvalidArgumentException; +using Poco::NumberFormatter; +using Poco::Timespan; + + +// workaround for C++-incompatible macro +#define POCO_BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(void*)((n)?"a":NULL)) + + + +namespace Poco { +namespace Net { + + +SecureSocketImpl::SecureSocketImpl():_pBIO(0), _pSSL(0) +{ +} + + +SecureSocketImpl::SecureSocketImpl(SSL *pSSL): _pSSL(pSSL) +{ + poco_check_ptr (_pSSL); + + _pBIO = SSL_get_rbio(_pSSL); + poco_check_ptr (_pBIO); + int tmpSocket = 0; + BIO_get_fd(_pBIO, &tmpSocket); + setSockfd(tmpSocket); + +} + + +SecureSocketImpl::~SecureSocketImpl() +{ + close(); +} + + +SocketImpl* SecureSocketImpl::acceptConnection(SocketAddress& clientAddr) +{ + poco_assert (sockfd() != POCO_INVALID_SOCKET); + poco_check_ptr (_pBIO); + + BIO* pClient = 0; + int rc = 0; + + do + { + rc = BIO_do_accept(_pBIO); + } + while (rc <= 0 && _socket.lastError() == POCO_EINTR); + + if (rc > 0) + { + pClient = BIO_pop(_pBIO); + poco_check_ptr (pClient); + + SSL* pSSL = SSL_new(SSLManager::instance().defaultServerContext()->sslContext()); + if (pSSL) + { + SSL_set_accept_state(pSSL); + SSL_set_bio(pSSL, pClient, pClient); + int err = SSL_accept(pSSL); + + if (err > 0) + { + SecureStreamSocketImpl* pSI = new SecureStreamSocketImpl(pSSL); + clientAddr = pSI->peerAddress(); + std::string clientName = clientAddr.host().toString(); + + if (X509_V_OK != postConnectionCheck(true, pSSL, clientName)) + { + delete pSI; + pSI = 0; + SSL_shutdown(pSSL); + SSL_free(pSSL); + pClient = 0; + SocketImpl::error("postConnectionCheck failed"); // will throw + } + + return pSI; + } + else + { + std::string errMsg = Utility::convertSSLError(pSSL, err); + SSL_shutdown(pSSL); + SSL_free(pSSL); + SocketImpl::error(std::string("failed to acceptConnection: ") + errMsg); + } + } + else + { + BIO_free(pClient); + } + + } + SocketImpl::error(); // will throw + return 0; +} + + +void SecureSocketImpl::connect(const SocketAddress& address) +{ + if (sockfd() == POCO_INVALID_SOCKET) + { + if (!_pBIO) + _pBIO = BIO_new(BIO_s_connect()); + } + + int rc = 0; + do + { + BIO_set_conn_hostname(_pBIO, address.host().toString().c_str()); + int tmp = address.port(); + BIO_set_conn_int_port(_pBIO, &tmp); + rc = BIO_do_connect(_pBIO); // returns 1 in case of ok! + } + while (rc != 1 && _socket.lastError() == POCO_EINTR); + + if (rc != 1) SocketImpl::error(address.toString()); + + establishTunnel(); + connectSSL(address); + poco_check_ptr (_pSSL); +} + + +void SecureSocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout) +{ + poco_assert (sockfd() == POCO_INVALID_SOCKET); + poco_assert (_pSSL == 0); + poco_assert (_pBIO == 0); + + _pBIO = BIO_new(BIO_s_connect()); + POCO_BIO_set_nbio_accept(_pBIO, 1); // set nonblocking + + try + { + BIO_set_conn_hostname(_pBIO, address.host().toString().c_str()); + int tmp = address.port(); + BIO_set_conn_int_port(_pBIO, &tmp); + int rc = BIO_do_connect(_pBIO); // returns 1 in case of ok! + + if (rc != 1) + { + if (_socket.lastError() != POCO_EINPROGRESS && _socket.lastError() != POCO_EWOULDBLOCK) + SocketImpl::error(address.toString()); + if (!_socket.poll(timeout, SocketImpl::SELECT_READ | SocketImpl::SELECT_WRITE)) + throw Poco::TimeoutException("connect timed out", address.toString()); + int err = _socket.socketError(); + if (err != 0) SocketImpl::error(err); + } + + establishTunnel(); + connectSSL(address); + poco_check_ptr (_pSSL); + } + catch (Poco::Exception&) + { + POCO_BIO_set_nbio_accept(_pBIO, 0); + throw; + } + POCO_BIO_set_nbio_accept(_pBIO, 0); +} + + +void SecureSocketImpl::connectNB(const SocketAddress& address) +{ + if (sockfd() == POCO_INVALID_SOCKET) + { + if(!_pBIO) + _pBIO = BIO_new(BIO_s_connect()); + } + + POCO_BIO_set_nbio_accept(_pBIO, 1); //setnonBlocking + BIO_set_conn_hostname(_pBIO, address.host().toString().c_str()); + int tmp = address.port(); + BIO_set_conn_int_port(_pBIO, &tmp); + + int rc = BIO_do_connect(_pBIO); // returns 1 in case of ok! + + if (rc != 1) + { + if (_socket.lastError() != POCO_EINPROGRESS && _socket.lastError() != POCO_EWOULDBLOCK) + SocketImpl::error(address.toString()); + } + else + { + int tmpSocket=0; + BIO_get_fd(_pBIO,&tmpSocket); + poco_assert (-1 != tmpSocket); + setSockfd(tmpSocket); + establishTunnel(); + connectSSL(address); + poco_check_ptr (_pSSL); + } +} + + +void SecureSocketImpl::bind(const SocketAddress& address, bool reuseAddress) +{ + _socket.bind(address, reuseAddress); +} + + +void SecureSocketImpl::listen(int backlog) +{ + _socket.listen(backlog); + _pBIO = BIO_new (BIO_s_accept()); + BIO_set_fd(_pBIO, (int)sockfd(), BIO_CLOSE); +} + + +void SecureSocketImpl::close() +{ + if (_pSSL) + { + if (SSL_get_shutdown(_pSSL) & SSL_RECEIVED_SHUTDOWN) + { + SSL_shutdown(_pSSL); + } + else + { + SSL_clear(_pSSL); + } + SSL_free(_pSSL); // frees _pBIO + _pSSL = 0; + _pBIO = 0; + } + + if (_pBIO) + { + BIO_free_all(_pBIO); //free all, even BIOs for pending connections + _pBIO = 0; + } + invalidate(); // the socket is already invalid, although the fd still contains a meaningful value, correct that +} + + +int SecureSocketImpl::sendBytes(const void* buffer, int length, int flags) +{ + poco_assert (sockfd() != POCO_INVALID_SOCKET); + poco_check_ptr (_pSSL); + + int rc; + do + { + rc = SSL_write(_pSSL, buffer, length); + if (rc < 0) + { + std::string errMsg = Utility::convertSSLError(_pSSL, rc); + } + } + while (rc < 0 && _socket.lastError() == POCO_EINTR); + if (rc < 0) SocketImpl::error(); + return rc; +} + + +int SecureSocketImpl::receiveBytes(void* buffer, int length, int flags) +{ + poco_assert (sockfd() != POCO_INVALID_SOCKET); + poco_check_ptr (_pSSL); + +#if defined(POCO_BROKEN_TIMEOUTS) + Poco::Timespan recvTimeout = _socket.getReceiveTimeout(); + if (recvTimeout.totalMicroseconds() != 0) + { + if (!_socket.poll(recvTimeout, SocketImpl::SELECT_READ)) + throw TimeoutException(); + } +#endif + + int rc; + do + { + rc = SSL_read(_pSSL, buffer, length); + if (rc <= 0) + { + switch (SSL_get_error(_pSSL, rc)) + { + case SSL_ERROR_ZERO_RETURN: + // connection closed + close(); + break; + case SSL_ERROR_NONE: + case SSL_ERROR_WANT_WRITE: //renegotiation + case SSL_ERROR_WANT_READ: //renegotiation + default: + ; + } + } + } + while (rc < 0 && _socket.lastError() == POCO_EINTR); + if (rc < 0) + { + if (_socket.lastError() == POCO_EAGAIN || _socket.lastError() == POCO_ETIMEDOUT) + throw TimeoutException(); + else + SocketImpl::error("failed to read bytes"); + } + return rc; +} + + +int SecureSocketImpl::sendTo(const void* buffer, int length, const SocketAddress& address, int flags) +{ + throw NetException("sendTo not possible with SSL"); +} + + +int SecureSocketImpl::receiveFrom(void* buffer, int length, SocketAddress& address, int flags) +{ + throw NetException("receiveFrom not possible with SSL"); +} + + +void SecureSocketImpl::sendUrgent(unsigned char data) +{ + // SSL doesn't support out-of-band data + sendBytes(reinterpret_cast(&data), sizeof(data)); +} + + +long SecureSocketImpl::postConnectionCheck(bool server, SSL* pSSL, const std::string& hostName) +{ + static std::string locHost("127.0.0.1"); + + SSLManager& mgr = SSLManager::instance(); + Context::VerificationMode mode = server? mgr.defaultServerContext()->verificationMode() : mgr.defaultClientContext()->verificationMode(); + if (hostName == locHost && mode != Context::VERIFY_STRICT) + return X509_V_OK; + + X509* cert = 0; + X509_NAME* subj = 0; + char* host = const_cast(hostName.c_str()); + + int extcount=0; + + if (mode == Context::VERIFY_NONE) // should we allow none on the client side? + { + return X509_V_OK; + } + + cert = SSL_get_peer_certificate(pSSL); + + // note: the check is used by the client, so as long we don't set None at the client we reject + // cases where no certificate/incomplete info is presented by the server + if ((!cert || !host) && mode != Context::VERIFY_NONE) + { + if (cert) + X509_free(cert); + return X509_V_ERR_APPLICATION_VERIFICATION; + } + + bool ok = false; + + if ((extcount = X509_get_ext_count(cert)) > 0) + { + for (int i = 0; i < extcount && !ok; ++i) + { + const char* extstr = 0; + X509_EXTENSION* ext; + ext = X509_get_ext(cert, i); + extstr = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext))); + + if (!strcmp(extstr, "subjectAltName")) + { + X509V3_EXT_METHOD* meth = X509V3_EXT_get(ext); + if (!meth) + break; + +#if OPENSSL_VERSION_NUMBER >= 0x00908000 + const unsigned char* pData = ext->value->data; + const unsigned char** ppData = &pData; +#else + unsigned char* pData = ext->value->data; + unsigned char** ppData = &pData; +#endif + STACK_OF(CONF_VALUE)* val = meth->i2v(meth, meth->d2i(0, ppData, ext->value->length), 0); + + for (int j = 0; j < sk_CONF_VALUE_num(val) && !ok; ++j) + { + CONF_VALUE* nval = sk_CONF_VALUE_value(val, j); + if (!strcmp(nval->name, "DNS") && !strcmp(nval->value, host)) + { + ok = true; + } + } + } + } + } + + char data[256]; + if (!ok && (subj = X509_get_subject_name(cert)) && X509_NAME_get_text_by_NID(subj, NID_commonName, data, 256) > 0) + { + data[255] = 0; + + std::string strData(data); // commonName can contain wildcards like *.appinf.com + try + { + // two cases: strData contains wildcards or not + if (SecureSocketImpl::containsWildcards(strData)) + { + // a compare by IPAddress is not possible with wildcards + // only allow compare by name + const HostEntry& heData = DNS::resolve(hostName); + ok = SecureSocketImpl::matchByAlias(strData, heData); + } + else + { + // it depends on hostname if we compare by IP or by alias + IPAddress ip; + if (IPAddress::tryParse(hostName, ip)) + { + // compare by IP + const HostEntry& heData = DNS::resolve(strData); + const HostEntry::AddressList& addr = heData.addresses(); + HostEntry::AddressList::const_iterator it = addr.begin(); + HostEntry::AddressList::const_iterator itEnd = addr.end(); + for (; it != itEnd && !ok; ++it) + { + ok = (*it == ip); + } + } + else + { + // compare by name + const HostEntry& heData = DNS::resolve(hostName); + ok = SecureSocketImpl::matchByAlias(strData, heData); + } + } + } + catch(HostNotFoundException&) + { + if (cert) + X509_free(cert); + return X509_V_ERR_APPLICATION_VERIFICATION; + } + } + + if (cert) + X509_free(cert); + + // we already have a verify callback registered so no need to ask twice SSL_get_verify_result(pSSL); + if (ok) + return X509_V_OK; + + return X509_V_ERR_APPLICATION_VERIFICATION; +} + + +void SecureSocketImpl::connectSSL(const SocketAddress& address) +{ + if (!_pSSL) + { + _pSSL = SSL_new(SSLManager::instance().defaultClientContext()->sslContext()); + SSL_set_bio(_pSSL, _pBIO, _pBIO); + } + std::string errMsg; + + int ret = SSL_connect(_pSSL); + + if (ret <= 0) + { + errMsg = Utility::convertSSLError(_pSSL, ret); + throw SSLException(errMsg); + } + + std::string serverName = address.host().toString(); + long errCode = 0; + if (_endHost.empty()) + postConnectionCheck(false, _pSSL, serverName); + else + postConnectionCheck(false, _pSSL, _endHost); + bool err = false; + + if (errCode != X509_V_OK) + { + err = true; + errMsg = Utility::convertCertificateError(errCode); + } + else + { + int tmpSocket=0; + BIO_get_fd(_pBIO,&tmpSocket); + poco_assert (-1 != tmpSocket); + setSockfd(tmpSocket); + } + + if (err) + { + SSL_free(_pSSL); // dels _pBIO too + _pSSL = 0; + _pBIO = 0; + invalidate(); + throw InvalidCertificateException(errMsg); + } +} + + +void SecureSocketImpl::establishTunnel() +{ + if (!_endHost.empty()) + { + poco_check_ptr (_pBIO); + // send CONNECT proxyHost:proxyPort HTTP/1.0\r\n\r\n + std::string connect("CONNECT "); + connect.append(_endHost); + connect.append(":"); + connect.append(Poco::NumberFormatter::format(_endPort)); + connect.append(" HTTP/1.0\r\n\r\n"); + int rc = BIO_write(_pBIO, (const void*) connect.c_str(), (int)(connect.length()*sizeof(char))); + if (rc != connect.length()) + throw SSLException("Failed to establish connection to proxy"); + // get the response + char resp[512]; + rc = BIO_read(_pBIO, resp, 512*sizeof(char)); + std::string response(resp); + if (response.find("200") == std::string::npos) + throw SSLException("Failed to establish connection to proxy"); + } +} + + +bool SecureSocketImpl::containsWildcards(const std::string& commonName) +{ + return (commonName.find('*') != std::string::npos || commonName.find('?') != std::string::npos); +} + + +bool SecureSocketImpl::matchByAlias(const std::string& alias, const HostEntry& heData) +{ + // compare by name + Poco::RegularExpression expr(alias); + bool found = false; + const HostEntry::AliasList& aliases = heData.aliases(); + HostEntry::AliasList::const_iterator it = aliases.begin(); + HostEntry::AliasList::const_iterator itEnd = aliases.end(); + for (; it != itEnd && !found; ++it) + { + found = expr.match(*it); + } + + return found; +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/SecureStreamSocket.cpp b/NetSSL_OpenSSL/src/SecureStreamSocket.cpp new file mode 100644 index 000000000..c3092861e --- /dev/null +++ b/NetSSL_OpenSSL/src/SecureStreamSocket.cpp @@ -0,0 +1,94 @@ +// +// SecureStreamSocket.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/SecureStreamSocket.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureStreamSocket +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/SecureStreamSocket.h" +#include "Poco/Net/SecureStreamSocketImpl.h" +#include "Poco/Net/SocketImpl.h" +#include "Poco/Exception.h" + + +using Poco::InvalidArgumentException; + + +namespace Poco { +namespace Net { + + +SecureStreamSocket::SecureStreamSocket(): + StreamSocket(new SecureStreamSocketImpl) +{ +} + + +SecureStreamSocket::SecureStreamSocket(const SocketAddress& address): + StreamSocket(new SecureStreamSocketImpl) +{ + connect(address); +} + + +SecureStreamSocket::SecureStreamSocket(const Socket& socket): + StreamSocket(socket) +{ + if (!dynamic_cast(impl())) + throw InvalidArgumentException("Cannot assign incompatible socket"); +} + + +SecureStreamSocket::SecureStreamSocket(SocketImpl* pImpl): + StreamSocket(pImpl) +{ + if (!dynamic_cast(impl())) + throw InvalidArgumentException("Cannot assign incompatible socket"); +} + + +SecureStreamSocket::~SecureStreamSocket() +{ +} + + +SecureStreamSocket& SecureStreamSocket::operator = (const Socket& socket) +{ + if (dynamic_cast(socket.impl())) + StreamSocket::operator = (socket); + else + throw InvalidArgumentException("Cannot assign incompatible socket"); + return *this; +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/SecureStreamSocketImpl.cpp b/NetSSL_OpenSSL/src/SecureStreamSocketImpl.cpp new file mode 100644 index 000000000..d59a2e517 --- /dev/null +++ b/NetSSL_OpenSSL/src/SecureStreamSocketImpl.cpp @@ -0,0 +1,145 @@ +// +// SecureStreamSocketImpl.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/SecureStreamSocketImpl.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLSockets +// Module: SecureStreamSocketImpl +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/SecureStreamSocketImpl.h" + + +namespace Poco { +namespace Net { + + +SecureStreamSocketImpl::SecureStreamSocketImpl() +{ +} + + +SecureStreamSocketImpl::SecureStreamSocketImpl(SSL* _pSSL): _socket(_pSSL) +{ + setSockfd(_socket.sockfd()); +} + + +SecureStreamSocketImpl::~SecureStreamSocketImpl() +{ +} + + +SocketImpl* SecureStreamSocketImpl::acceptConnection(SocketAddress& clientAddr) +{ + return _socket.acceptConnection(clientAddr); +} + + +void SecureStreamSocketImpl::connect(const SocketAddress& address) +{ + _socket.connect(address); + setSockfd(_socket.sockfd()); +} + + +void SecureStreamSocketImpl::connect(const SocketAddress& address, const Poco::Timespan& timeout) +{ + _socket.connect(address, timeout); + setSockfd(_socket.sockfd()); +} + + +void SecureStreamSocketImpl::connectNB(const SocketAddress& address) +{ + _socket.connectNB(address); + setSockfd(_socket.sockfd()); +} + + +void SecureStreamSocketImpl::bind(const SocketAddress& address, bool reuseAddress) +{ + _socket.bind(address, reuseAddress); +} + + +void SecureStreamSocketImpl::listen(int backlog) +{ + _socket.listen(backlog); + setSockfd(_socket.sockfd()); +} + + +void SecureStreamSocketImpl::close() +{ + invalidate(); + _socket.close(); +} + + +int SecureStreamSocketImpl::sendBytes(const void* buffer, int length, int flags) +{ + const char* p = reinterpret_cast(buffer); + int remaining = length; + while (remaining > 0) + { + int n = _socket.sendBytes(p, length, flags); + p += n; + remaining -= n; + } + return length; +} + + +int SecureStreamSocketImpl::receiveBytes(void* buffer, int length, int flags) +{ + return _socket.receiveBytes(buffer, length, flags); +} + + +int SecureStreamSocketImpl::sendTo(const void* buffer, int length, const SocketAddress& address, int flags) +{ + return _socket.sendTo(buffer, length, address, flags); +} + + +int SecureStreamSocketImpl::receiveFrom(void* buffer, int length, SocketAddress& address, int flags) +{ + return _socket.receiveFrom(buffer, length, address, flags); +} + + +void SecureStreamSocketImpl::sendUrgent(unsigned char data) +{ + return _socket.sendUrgent(data); +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/Utility.cpp b/NetSSL_OpenSSL/src/Utility.cpp new file mode 100644 index 000000000..c7d18f7db --- /dev/null +++ b/NetSSL_OpenSSL/src/Utility.cpp @@ -0,0 +1,135 @@ +// +// Utility.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/Utility.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: Utility +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/Utility.h" +#include "Poco/String.h" +#include "Poco/Util/OptionException.h" +#include + + +namespace Poco { +namespace Net { + + +int Utility::HTTPS_PORT = 443; + + +Context::VerificationMode Utility::convertVerificationMode(const std::string& vMode) +{ + std::string mode = Poco::toLower(vMode); + Context::VerificationMode verMode = Context::VERIFY_STRICT; + + if (mode == "none") + verMode = Context::VERIFY_NONE; + else if (mode == "relaxed") + verMode = Context::VERIFY_RELAXED; + else if (mode == "strict") + verMode = Context::VERIFY_STRICT; + else if (mode == "once") + verMode = Context::VERIFY_ONCE; + else + throw Poco::Util::OptionException(std::string("Wrong value >") + vMode + std::string("< for a verificationMode. Can only be none, relaxed, strict or once.")); + + return verMode; +} + + +std::string Utility::convertCertificateError(long errCode) +{ + std::string errMsg(X509_verify_cert_error_string(errCode)); + return errMsg; +} + + +std::string Utility::convertSSLError(SSL* pSSL, int errCode) +{ + + std::string errMsg; + if (errCode > 0) return "no error"; + + int connectErr = SSL_get_error(pSSL, errCode); + long lErr = 0; + char buf[512]; + + switch (connectErr) + { + case SSL_ERROR_ZERO_RETURN: + // connection closed + errMsg = "connection closed by server"; + break; + case SSL_ERROR_WANT_READ: + errMsg = "want read"; + break; + case SSL_ERROR_WANT_WRITE: + errMsg = "want write"; + break; + case SSL_ERROR_WANT_CONNECT: + errMsg = "want connect"; + break; + case SSL_ERROR_WANT_ACCEPT: + errMsg = "want accept"; + break; + case SSL_ERROR_WANT_X509_LOOKUP: + errMsg = "want lookup"; + break; + case SSL_ERROR_SYSCALL: + errMsg = "syscall"; + break; + case SSL_ERROR_SSL: + lErr = ERR_get_error(); + if (errCode == 0) + { + errMsg = "EOF was observed"; + } + else if (errCode == -1) + { + errMsg = "The underlying BIO reported an I/O error"; + } + else + { + ERR_error_string_n(lErr, buf, 512); + errMsg = buf; + } + break; + default: + errMsg = "none"; + } + + return errMsg; +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/VerificationErrorArgs.cpp b/NetSSL_OpenSSL/src/VerificationErrorArgs.cpp new file mode 100644 index 000000000..1427f9a71 --- /dev/null +++ b/NetSSL_OpenSSL/src/VerificationErrorArgs.cpp @@ -0,0 +1,59 @@ +// +// VerificationErrorArgs.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/VerificationErrorArgs.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: VerificationErrorArgs +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/VerificationErrorArgs.h" + + +namespace Poco { +namespace Net { + + +VerificationErrorArgs::VerificationErrorArgs(const X509Certificate& cert, int errDepth, int errNum, const std::string& errMsg): + _cert(cert), + _errorDepth(errDepth), + _errorNumber(errNum), + _errorMessage(errMsg), + _ignoreError(false) +{ +} + + +VerificationErrorArgs::~VerificationErrorArgs() +{ +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/src/X509Certificate.cpp b/NetSSL_OpenSSL/src/X509Certificate.cpp new file mode 100644 index 000000000..00ed23f84 --- /dev/null +++ b/NetSSL_OpenSSL/src/X509Certificate.cpp @@ -0,0 +1,66 @@ +// +// X509Certificate.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/src/X509Certificate.cpp#1 $ +// +// Library: NetSSL_OpenSSL +// Package: SSLCore +// Module: X509Certificate +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "Poco/Net/X509Certificate.h" + + +namespace Poco { +namespace Net { + + +X509Certificate::X509Certificate(X509* pCert):_pCert(pCert) +{ + poco_check_ptr(_pCert); + initialize(); +} + + +X509Certificate::~X509Certificate() +{ +} + + +void X509Certificate::initialize() +{ + char data[256]; + X509_NAME_oneline(X509_get_issuer_name(_pCert), data, 256); + _issuerName = data; + X509_NAME_oneline(X509_get_subject_name(_pCert), data, 256); + _subjectName = data; +} + + +} } // namespace Poco::Net diff --git a/NetSSL_OpenSSL/testsuite/Makefile b/NetSSL_OpenSSL/testsuite/Makefile new file mode 100644 index 000000000..eba98121a --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/Makefile @@ -0,0 +1,21 @@ +# +# Makefile +# +# $Id: //poco/Main/template/TestSuite.make#3 $ +# +# Makefile for Poco NetSSL testsuite +# + +include $(POCO_BASE)/build/rules/global + +SYSLIBS += -lssl -lcrypto -ldl + +objects = NetSSLTestSuite Driver \ + HTTPSClientSessionTest HTTPSClientTestSuite HTTPSServerTest HTTPSServerTestSuite \ + HTTPSStreamFactoryTest HTTPSTestServer TCPServerTest TCPServerTestSuite + +target = testrunner +target_version = 1 +target_libs = PocoNetSSL PocoNet PocoUtil PocoXML PocoFoundation CppUnit + +include $(POCO_BASE)/build/rules/exec diff --git a/NetSSL_OpenSSL/testsuite/TestSuite.vmsbuild b/NetSSL_OpenSSL/testsuite/TestSuite.vmsbuild new file mode 100644 index 000000000..32d8d588f --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/TestSuite.vmsbuild @@ -0,0 +1,9 @@ +# +# TestSuite.vmsbuild +# +# $Id: //poco/Main/template/TestSuite.vmsbuild#3 $ +# +EXE=TestRunner +OpenSSLTestSuite +Driver + diff --git a/NetSSL_OpenSSL/testsuite/TestSuite.xml b/NetSSL_OpenSSL/testsuite/TestSuite.xml new file mode 100644 index 000000000..ed8b08790 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/TestSuite.xml @@ -0,0 +1,42 @@ + + + + ${application.configDir}any.pem + ${application.configDir}rootcert.pem + none + 9 + true + ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH + + KeyFileHandler + + test + + + + AcceptCertificateHandler + + + + + + ${application.configDir}any.pem + ${application.configDir}rootcert.pem + relaxed + 9 + true + ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH + + KeyFileHandler + + test + + + + AcceptCertificateHandler + + + + + + diff --git a/NetSSL_OpenSSL/testsuite/TestSuite_vs71.vcproj b/NetSSL_OpenSSL/testsuite/TestSuite_vs71.vcproj new file mode 100644 index 000000000..43f4b61c4 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/TestSuite_vs71.vcproj @@ -0,0 +1,263 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NetSSL_OpenSSL/testsuite/TestSuite_vs80.vcproj b/NetSSL_OpenSSL/testsuite/TestSuite_vs80.vcproj new file mode 100644 index 000000000..1c60f9173 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/TestSuite_vs80.vcproj @@ -0,0 +1,345 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/NetSSL_OpenSSL/testsuite/any.pem b/NetSSL_OpenSSL/testsuite/any.pem new file mode 100644 index 000000000..b17a97568 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/any.pem @@ -0,0 +1,48 @@ +-----BEGIN CERTIFICATE----- +MIICaDCCAdECCQCzfxSsk7yaLjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAzMDExMzA3MzFaFw0wNjAzMzExMzA3MzFaMH4xCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExETAPBgNVBAcTCFN0IEpha29iMRww +GgYDVQQKExNBcHBsaWVkIEluZm9ybWF0aWNzMQowCAYDVQQDFAEqMR4wHAYJKoZI +hvcNAQkBFg9pbmZvQGFwcGluZi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ +AoGBAJHGyXDHyCYoWz+65ltNwwZbhwOGnxr9P1WMATuFJh0bPBZxKbZRdbTm9KhZ +OlvsEIsfgiYdsxURYIqXfEgISYLZcZY0pQwGEOmB+0NeC/+ENSfOlNSthx6zSVlc +zhJ7+dJOGwepHAiLr1fRuc5jogYLraE+lKTnqAAFfzwvti77AgMBAAEwDQYJKoZI +hvcNAQEFBQADgYEAY/ZoeY1ukkEJX7259NeoVM0oahlulWV0rlCqyaeosOiDORPT +m6X1w/5MTCf9VyaD1zukoSZ4QqNVjHFXcXidbB7Tgt3yRuZ5PC5LIFCDPv9mgPne +mUA70yfctNfza2z3ZiQ6NDkW3mZX+1tmxYIrJQIrkVeYeqf1Gh2nyZrUMcE= +-----END CERTIFICATE----- +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: DES-EDE3-CBC,E7AE93C9E49184EA + +A2IqzNcWs+I5vzV+i+woDk56+yr58eU0Onw8eEvXkLjnSc58JU4327IF7yUbKWdW +Q7BYGGOkVFiZ7ANOwviDg5SUhxRDWCcW8dS6/p1vfdQ1C3qj2OwJjkpg0aDBIzJn +FzgguT3MF3ama77vxv0S3kOfmCj62MLqPGpj5pQ0/1hefRFbL8oAX8bXUN7/rmGM +Zc0QyzFZv2iQ04dY/6TNclwKPB4H0On4K+8BMs3PRkWA0clCaQaFO2+iwnk3XZfe ++MsKUEbLCpAQeYspYv1cw38dCdWq1KTP5aJk+oXgwjfX5cAaPTz74NTqTIsCcaTD +3vy7ukJYFlDR9Kyo7z8rMazYrKJslhnuRH0BhK9st9McwL957j5tZmrKyraCcmCx +dMAGcsis1va3ayYZpIpFqA4EhYrTM+6N8ZRfUap20+b5IQwHfTQDejUhL6rBwy7j +Ti5yD83/itoOMyXq2sV/XWfVD5zk/P5iv22O1EAQMhhnPB9K/I/JhuSGQJfn3cNh +ykOUYT0+vDeSeEVa+FVEP1W35G0alTbKbNs5Tb8KxJ3iDJUxokM//SvPXZy9hOVX +Y05imB04J15DaGbAHlNzunhuJi7121WV/JRXZRW9diE6hwpD8rwqi3FMuRUmy7U9 +aFA5poKRAYlo9YtZ3YpFyjGKB6MfCQcB2opuSnQ/gbugV41m67uQ4CDwWLaNRkTb +GlsMBNcHnidg15Bsat5HaB7l250ukrI13Uw1MYdDUzaS3gPfw9aC4F2w0p3U+DPH +80/zePxtroR7T4/+rI136Rl+aMXDMOEGCX1TVP8rjuZzuRyUSUKC8Q== +-----END RSA PRIVATE KEY----- +-----BEGIN CERTIFICATE----- +MIICXTCCAcYCCQC1Vk/N8qR4AjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAyMjcxMzI3MThaFw0wNjAzMjkxMzI3MThaMHMxCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExEjAQBgNVBAcTCVN0LiBKYWtvYjEP +MA0GA1UEChMGQXBwSW5mMQ8wDQYDVQQDEwZBcHBJbmYxGjAYBgkqhkiG9w0BCQEW +C2FwcEBpbmYuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsFXiPuicN +Im4oJwF8NuaFN+lgYwcZ6dAO3ILIR3kLA2PxF8HSQLfF8J8a4odZhLhctIMAKTxm +k0w8TW5qhL8QLdGzY9vzvkgdKOkan2t3sMeXJAfrM1AphTsmgntAQazGZjOj5p4W +jDnxQ+VXAylqwjHh49eSBxM3wgoscF4iLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GB +AIpfLdXiKchPvFMhQS8xTtXvrw5dVL3yImUMYs4GQi8RrjGmfGB3yMAR7B/b8v4a ++ztfusgWAWiUKuSGTk4S8YB0fsFlmOv0WDr+PyZ4Lui/a8opbyzGE7rqpnF/s0GO +M7uLCNNwIN7WhmxcWV0KZU1wTppoSWPJda1yTbBzF9XP +-----END CERTIFICATE----- diff --git a/NetSSL_OpenSSL/testsuite/rootcert.pem b/NetSSL_OpenSSL/testsuite/rootcert.pem new file mode 100644 index 000000000..c26aeb244 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/rootcert.pem @@ -0,0 +1,15 @@ +-----BEGIN CERTIFICATE----- +MIICXTCCAcYCCQC1Vk/N8qR4AjANBgkqhkiG9w0BAQUFADBzMQswCQYDVQQGEwJB +VDESMBAGA1UECBMJQ2FyaW50aGlhMRIwEAYDVQQHEwlTdC4gSmFrb2IxDzANBgNV +BAoTBkFwcEluZjEPMA0GA1UEAxMGQXBwSW5mMRowGAYJKoZIhvcNAQkBFgthcHBA +aW5mLmNvbTAeFw0wNjAyMjcxMzI3MThaFw0wNjAzMjkxMzI3MThaMHMxCzAJBgNV +BAYTAkFUMRIwEAYDVQQIEwlDYXJpbnRoaWExEjAQBgNVBAcTCVN0LiBKYWtvYjEP +MA0GA1UEChMGQXBwSW5mMQ8wDQYDVQQDEwZBcHBJbmYxGjAYBgkqhkiG9w0BCQEW +C2FwcEBpbmYuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsFXiPuicN +Im4oJwF8NuaFN+lgYwcZ6dAO3ILIR3kLA2PxF8HSQLfF8J8a4odZhLhctIMAKTxm +k0w8TW5qhL8QLdGzY9vzvkgdKOkan2t3sMeXJAfrM1AphTsmgntAQazGZjOj5p4W +jDnxQ+VXAylqwjHh49eSBxM3wgoscF4iLQIDAQABMA0GCSqGSIb3DQEBBQUAA4GB +AIpfLdXiKchPvFMhQS8xTtXvrw5dVL3yImUMYs4GQi8RrjGmfGB3yMAR7B/b8v4a ++ztfusgWAWiUKuSGTk4S8YB0fsFlmOv0WDr+PyZ4Lui/a8opbyzGE7rqpnF/s0GO +M7uLCNNwIN7WhmxcWV0KZU1wTppoSWPJda1yTbBzF9XP +-----END CERTIFICATE----- diff --git a/NetSSL_OpenSSL/testsuite/src/Driver.cpp b/NetSSL_OpenSSL/testsuite/src/Driver.cpp new file mode 100644 index 000000000..658e27c7c --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/Driver.cpp @@ -0,0 +1,84 @@ +// +// Driver.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/Driver.cpp#1 $ +// +// Console-based test driver for Poco NetSSL. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "CppUnit/TestRunner.h" +#include "NetSSLTestSuite.h" +#include "Poco/Util/Application.h" +#include "Poco/Net/HTTPStreamFactory.h" +#include "Poco/Net/HTTPSStreamFactory.h" +#include + + +class NetSSLApp: public Poco::Util::Application +{ +public: + NetSSLApp() + { + } + + ~NetSSLApp() + { + } + +protected: + void initialize(Poco::Util::Application& self) + { + loadConfiguration(); // load default configuration files, if present + Poco::Util::Application::initialize(self); + } +}; + + +int main(int ac, char **av) +{ + Poco::Net::HTTPStreamFactory::registerFactory(); + Poco::Net::HTTPSStreamFactory::registerFactory(); + NetSSLApp app; + try + { + app.init(1, av); + } + catch (Poco::Exception& exc) + { + std::cout << exc.displayText() << std::endl; + return 1; + } + + std::vector args; + for (int i = 0; i < ac; ++i) + args.push_back(std::string(av[i])); + CppUnit::TestRunner runner; + runner.addTest("NetSSLTestSuite", NetSSLTestSuite::suite()); + return runner.run(args) ? 0 : 1; +} diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.cpp b/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.cpp new file mode 100644 index 000000000..4c8cca61e --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.cpp @@ -0,0 +1,306 @@ +// +// HTTPSClientSessionTest.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "HTTPSClientSessionTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Net/HTTPSClientSession.h" +#include "Poco/Net/HTTPRequest.h" +#include "Poco/Net/HTTPResponse.h" +#include "Poco/StreamCopier.h" +#include "Poco/Exception.h" +#include "HTTPSTestServer.h" +#include +#include +#include + + +using Poco::Net::HTTPSClientSession; +using Poco::Net::HTTPRequest; +using Poco::Net::HTTPResponse; +using Poco::Net::HTTPMessage; +using Poco::StreamCopier; + + +HTTPSClientSessionTest::HTTPSClientSessionTest(const std::string& name): CppUnit::TestCase(name) +{ +} + + +HTTPSClientSessionTest::~HTTPSClientSessionTest() +{ +} + + +void HTTPSClientSessionTest::testGetSmall() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_GET, "/small"); + s.sendRequest(request); + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length()); + assert (response.getContentType() == "text/plain"); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str() == HTTPSTestServer::SMALL_BODY); +} + + +void HTTPSClientSessionTest::testGetLarge() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_GET, "/large"); + s.sendRequest(request); + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length()); + assert (response.getContentType() == "text/plain"); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str() == HTTPSTestServer::LARGE_BODY); +} + + +void HTTPSClientSessionTest::testHead() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_HEAD, "/large"); + s.sendRequest(request); + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length()); + assert (response.getContentType() == "text/plain"); + std::ostringstream ostr; + assert (StreamCopier::copyStream(rs, ostr) == 0); +} + + +void HTTPSClientSessionTest::testPostSmallIdentity() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_POST, "/echo"); + std::string body("this is a random request body\r\n0\r\n"); + request.setContentLength((int) body.length()); + s.sendRequest(request) << body; + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (response.getContentLength() == body.length()); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str() == body); +} + + +void HTTPSClientSessionTest::testPostLargeIdentity() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_POST, "/echo"); + std::string body(8000, 'x'); + body.append("\r\n0\r\n"); + request.setContentLength((int) body.length()); + s.sendRequest(request) << body; + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (response.getContentLength() == body.length()); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str() == body); +} + + +void HTTPSClientSessionTest::testPostSmallChunked() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_POST, "/echo"); + std::string body("this is a random request body"); + request.setChunkedTransferEncoding(true); + s.sendRequest(request) << body; + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (response.getChunkedTransferEncoding()); + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str() == body); +} + + +void HTTPSClientSessionTest::testPostLargeChunked() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_POST, "/echo"); + std::string body(16000, 'x'); + request.setChunkedTransferEncoding(true); + s.sendRequest(request) << body; + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (response.getChunkedTransferEncoding()); + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str() == body); +} + + +void HTTPSClientSessionTest::testPostSmallClose() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_POST, "/echo"); + std::string body("this is a random request body"); + s.sendRequest(request) << body; + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (!response.getChunkedTransferEncoding()); + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str() == body); +} + + +void HTTPSClientSessionTest::testPostLargeClose() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + HTTPRequest request(HTTPRequest::HTTP_POST, "/echo"); + std::string body(8000, 'x'); + s.sendRequest(request) << body; + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + assert (!response.getChunkedTransferEncoding()); + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str() == body); +} + + +void HTTPSClientSessionTest::testKeepAlive() +{ + HTTPSTestServer srv; + HTTPSClientSession s("localhost", srv.port()); + s.setKeepAlive(true); + HTTPRequest request(HTTPRequest::HTTP_HEAD, "/keepAlive", HTTPMessage::HTTP_1_1); + s.sendRequest(request); + HTTPResponse response; + std::istream& rs1 = s.receiveResponse(response); + assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length()); + assert (response.getContentType() == "text/plain"); + assert (response.getKeepAlive()); + std::ostringstream ostr1; + assert (StreamCopier::copyStream(rs1, ostr1) == 0); + + request.setMethod(HTTPRequest::HTTP_GET); + request.setURI("/small"); + s.sendRequest(request); + std::istream& rs2 = s.receiveResponse(response); + assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length()); + assert (response.getKeepAlive()); + std::ostringstream ostr2; + StreamCopier::copyStream(rs2, ostr2); + assert (ostr2.str() == HTTPSTestServer::SMALL_BODY); + + request.setMethod(HTTPRequest::HTTP_GET); + request.setURI("/large"); + s.sendRequest(request); + std::istream& rs3 = s.receiveResponse(response); + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + assert (response.getChunkedTransferEncoding()); + assert (response.getKeepAlive()); + std::ostringstream ostr3; + int n = StreamCopier::copyStream(rs3, ostr3); + assert (ostr3.str() == HTTPSTestServer::LARGE_BODY); + + request.setMethod(HTTPRequest::HTTP_HEAD); + request.setURI("/large"); + s.sendRequest(request); + std::istream& rs4= s.receiveResponse(response); + assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length()); + assert (response.getContentType() == "text/plain"); + assert (!response.getKeepAlive()); + std::ostringstream ostr4; + assert (StreamCopier::copyStream(rs4, ostr4) == 0); +} + + +void HTTPSClientSessionTest::testProxy() +{ + HTTPSTestServer srv; + HTTPSClientSession s("wwws.appinf.com"); + s.setProxy("proxy.aon.at", 8080); + HTTPRequest request(HTTPRequest::HTTP_GET, "/"); + s.sendRequest(request); + HTTPResponse response; + std::istream& rs = s.receiveResponse(response); + std::ostringstream ostr; + StreamCopier::copyStream(rs, ostr); + assert (ostr.str().length() > 0); +} + + +void HTTPSClientSessionTest::setUp() +{ +} + + +void HTTPSClientSessionTest::tearDown() +{ +} + + +CppUnit::Test* HTTPSClientSessionTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSClientSessionTest"); + + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetSmall); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetLarge); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testHead); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostSmallIdentity); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeIdentity); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostSmallChunked); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeChunked); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostSmallClose); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeClose); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testKeepAlive); + CppUnit_addTest(pSuite, HTTPSClientSessionTest, testProxy); + + return pSuite; +} diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.h b/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.h new file mode 100644 index 000000000..689bd87a0 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.h @@ -0,0 +1,70 @@ +// +// HTTPSClientSessionTest.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.h#1 $ +// +// Definition of the HTTPSClientSessionTest class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef HTTPSClientSessionTest_INCLUDED +#define HTTPSClientSessionTest_INCLUDED + + +#include "Poco/Net/Net.h" +#include "CppUnit/TestCase.h" + + +class HTTPSClientSessionTest: public CppUnit::TestCase +{ +public: + HTTPSClientSessionTest(const std::string& name); + ~HTTPSClientSessionTest(); + + void testGetSmall(); + void testGetLarge(); + void testHead(); + void testPostSmallIdentity(); + void testPostLargeIdentity(); + void testPostSmallChunked(); + void testPostLargeChunked(); + void testPostSmallClose(); + void testPostLargeClose(); + void testKeepAlive(); + void testProxy(); + + void setUp(); + void tearDown(); + + static CppUnit::Test* suite(); + +private: +}; + + +#endif // HTTPSClientSessionTest_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSClientTestSuite.cpp b/NetSSL_OpenSSL/testsuite/src/HTTPSClientTestSuite.cpp new file mode 100644 index 000000000..f162c4850 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSClientTestSuite.cpp @@ -0,0 +1,46 @@ +// +// HTTPSClientTestSuite.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSClientTestSuite.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "HTTPSClientTestSuite.h" +#include "HTTPSClientSessionTest.h" +#include "HTTPSStreamFactoryTest.h" + + +CppUnit::Test* HTTPSClientTestSuite::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSClientTestSuite"); + + pSuite->addTest(HTTPSClientSessionTest::suite()); + pSuite->addTest(HTTPSStreamFactoryTest::suite()); + + return pSuite; +} diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSClientTestSuite.h b/NetSSL_OpenSSL/testsuite/src/HTTPSClientTestSuite.h new file mode 100644 index 000000000..264186194 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSClientTestSuite.h @@ -0,0 +1,49 @@ +// +// HTTPSClientTestSuite.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSClientTestSuite.h#1 $ +// +// Definition of the HTTPSClientTestSuite class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef HTTPSClientTestSuite_INCLUDED +#define HTTPSClientTestSuite_INCLUDED + + +#include "CppUnit/TestSuite.h" + + +class HTTPSClientTestSuite +{ +public: + static CppUnit::Test* suite(); +}; + + +#endif // HTTPSClientTestSuite_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.cpp b/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.cpp new file mode 100644 index 000000000..519b63e62 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.cpp @@ -0,0 +1,421 @@ +// +// HTTPSServerTest.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "HTTPSServerTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Net/HTTPServer.h" +#include "Poco/Net/HTTPServerParams.h" +#include "Poco/Net/HTTPRequestHandler.h" +#include "Poco/Net/HTTPRequestHandlerFactory.h" +#include "Poco/Net/HTTPSClientSession.h" +#include "Poco/Net/HTTPRequest.h" +#include "Poco/Net/HTTPServerRequest.h" +#include "Poco/Net/HTTPResponse.h" +#include "Poco/Net/HTTPServerResponse.h" +#include "Poco/Net/SecureServerSocket.h" +#include "Poco/StreamCopier.h" +#include + + +using Poco::Net::HTTPServer; +using Poco::Net::HTTPServerParams; +using Poco::Net::HTTPRequestHandler; +using Poco::Net::HTTPRequestHandlerFactory; +using Poco::Net::HTTPSClientSession; +using Poco::Net::HTTPRequest; +using Poco::Net::HTTPServerRequest; +using Poco::Net::HTTPResponse; +using Poco::Net::HTTPServerResponse; +using Poco::Net::HTTPMessage; +using Poco::Net::SecureServerSocket; +using Poco::StreamCopier; + + +namespace +{ + class EchoBodyRequestHandler: public HTTPRequestHandler + { + public: + void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) + { + if (request.getChunkedTransferEncoding()) + response.setChunkedTransferEncoding(true); + else if (request.getContentLength() != HTTPMessage::UNKNOWN_CONTENT_LENGTH) + response.setContentLength(request.getContentLength()); + + response.setContentType(request.getContentType()); + + std::istream& istr = request.stream(); + std::ostream& ostr = response.send(); + int n = StreamCopier::copyStream(istr, ostr); + } + }; + + class EchoHeaderRequestHandler: public HTTPRequestHandler + { + public: + void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) + { + std::ostringstream osstr; + request.write(osstr); + int n = (int) osstr.str().length(); + response.setContentLength(n); + std::ostream& ostr = response.send(); + if (request.getMethod() != HTTPRequest::HTTP_HEAD) + request.write(ostr); + } + }; + + class RedirectRequestHandler: public HTTPRequestHandler + { + public: + void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) + { + response.redirect("http://www.appinf.com/"); + } + }; + + class AuthRequestHandler: public HTTPRequestHandler + { + public: + void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) + { + response.requireAuthentication("/auth"); + response.send(); + } + }; + + class RequestHandlerFactory: public HTTPRequestHandlerFactory + { + public: + HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) + { + if (request.getURI() == "/echoBody") + return new EchoBodyRequestHandler; + else if (request.getURI() == "/echoHeader") + return new EchoHeaderRequestHandler; + else if (request.getURI() == "/redirect") + return new RedirectRequestHandler(); + else if (request.getURI() == "/auth") + return new AuthRequestHandler(); + else + return 0; + } + }; +} + + +HTTPSServerTest::HTTPSServerTest(const std::string& name): CppUnit::TestCase(name) +{ +} + + +HTTPSServerTest::~HTTPSServerTest() +{ +} + + +void HTTPSServerTest::testIdentityRequest() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(false); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + std::string body(5000, 'x'); + HTTPRequest request("POST", "/echoBody"); + request.setContentLength((int) body.length()); + request.setContentType("text/plain"); + cs.sendRequest(request) << body; + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == body.size()); + assert (response.getContentType() == "text/plain"); + assert (rbody == body); +} + + +void HTTPSServerTest::testChunkedRequest() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(false); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + std::string body(5000, 'x'); + HTTPRequest request("POST", "/echoBody"); + request.setContentType("text/plain"); + request.setChunkedTransferEncoding(true); + cs.sendRequest(request) << body; + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + assert (response.getContentType() == "text/plain"); + assert (response.getChunkedTransferEncoding()); + assert (rbody == body); +} + + +void HTTPSServerTest::testClosedRequest() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(false); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + std::string body(5000, 'x'); + HTTPRequest request("POST", "/echoBody"); + request.setContentType("text/plain"); + cs.sendRequest(request) << body; + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + assert (response.getContentType() == "text/plain"); + assert (!response.getChunkedTransferEncoding()); + assert (rbody == body); +} + + +void HTTPSServerTest::testIdentityRequestKeepAlive() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(true); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + cs.setKeepAlive(true); + std::string body(5000, 'x'); + HTTPRequest request("POST", "/echoBody"); + request.setContentLength((int) body.length()); + request.setContentType("text/plain"); + cs.sendRequest(request) << body; + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == body.size()); + assert (response.getContentType() == "text/plain"); + assert (response.getKeepAlive()); + assert (rbody == body); + + body.assign(1000, 'y'); + request.setContentLength((int) body.length()); + cs.setKeepAlive(false); + cs.sendRequest(request) << body; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == body.size()); + assert (response.getContentType() == "text/plain"); + assert (!response.getKeepAlive()); + assert (rbody == body);} + + +void HTTPSServerTest::testChunkedRequestKeepAlive() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(true); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + cs.setKeepAlive(true); + std::string body(5000, 'x'); + HTTPRequest request("POST", "/echoBody"); + request.setContentType("text/plain"); + request.setChunkedTransferEncoding(true); + cs.sendRequest(request) << body; + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + assert (response.getContentType() == "text/plain"); + assert (response.getChunkedTransferEncoding()); + assert (rbody == body); + + body.assign(1000, 'y'); + cs.setKeepAlive(false); + cs.sendRequest(request) << body; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + assert (response.getContentType() == "text/plain"); + assert (response.getChunkedTransferEncoding()); + assert (!response.getKeepAlive()); + assert (rbody == body); +} + + +void HTTPSServerTest::testClosedRequestKeepAlive() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(true); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + std::string body(5000, 'x'); + HTTPRequest request("POST", "/echoBody"); + request.setContentType("text/plain"); + cs.sendRequest(request) << body; + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH); + assert (response.getContentType() == "text/plain"); + assert (!response.getChunkedTransferEncoding()); + assert (!response.getKeepAlive()); + assert (rbody == body); + int n = (int) rbody.size(); +} + + +void HTTPSServerTest::test100Continue() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(false); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + std::string body(5000, 'x'); + HTTPRequest request("POST", "/echoBody"); + request.setContentLength((int) body.length()); + request.setContentType("text/plain"); + request.set("Expect", "100-Continue"); + cs.sendRequest(request) << body; + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getContentLength() == body.size()); + assert (response.getContentType() == "text/plain"); + assert (rbody == body); +} + + +void HTTPSServerTest::testRedirect() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(false); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + HTTPRequest request("GET", "/redirect"); + cs.sendRequest(request); + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getStatus() == HTTPResponse::HTTP_FOUND); + assert (response.get("Location") == "http://www.appinf.com/"); + assert (rbody.empty()); +} + + +void HTTPSServerTest::testAuth() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(false); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + HTTPRequest request("GET", "/auth"); + cs.sendRequest(request); + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED); + assert (response.get("WWW-Authenticate") == "Basic realm=\"/auth\""); + assert (rbody.empty()); +} + + +void HTTPSServerTest::testNotImpl() +{ + SecureServerSocket svs(0); + HTTPServerParams* pParams = new HTTPServerParams; + pParams->setKeepAlive(false); + HTTPServer srv(new RequestHandlerFactory, svs, pParams); + srv.start(); + + HTTPSClientSession cs("localhost", svs.address().port()); + HTTPRequest request("GET", "/notImpl"); + cs.sendRequest(request); + HTTPResponse response; + std::string rbody; + cs.receiveResponse(response) >> rbody; + assert (response.getStatus() == HTTPResponse::HTTP_NOT_IMPLEMENTED); + assert (rbody.empty()); +} + + +void HTTPSServerTest::setUp() +{ +} + + +void HTTPSServerTest::tearDown() +{ +} + + +CppUnit::Test* HTTPSServerTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSServerTest"); + + CppUnit_addTest(pSuite, HTTPSServerTest, testIdentityRequest); + CppUnit_addTest(pSuite, HTTPSServerTest, testChunkedRequest); + CppUnit_addTest(pSuite, HTTPSServerTest, testClosedRequest); + CppUnit_addTest(pSuite, HTTPSServerTest, testIdentityRequestKeepAlive); + CppUnit_addTest(pSuite, HTTPSServerTest, testChunkedRequestKeepAlive); + CppUnit_addTest(pSuite, HTTPSServerTest, testClosedRequestKeepAlive); + CppUnit_addTest(pSuite, HTTPSServerTest, test100Continue); + CppUnit_addTest(pSuite, HTTPSServerTest, testRedirect); + CppUnit_addTest(pSuite, HTTPSServerTest, testAuth); + CppUnit_addTest(pSuite, HTTPSServerTest, testNotImpl); + + return pSuite; +} diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.h b/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.h new file mode 100644 index 000000000..f70caa2ae --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.h @@ -0,0 +1,69 @@ +// +// HTTPSServerTest.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSServerTest.h#1 $ +// +// Definition of the HTTPSServerTest class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef HTTPSServerTest_INCLUDED +#define HTTPSServerTest_INCLUDED + + +#include "Poco/Net/Net.h" +#include "CppUnit/TestCase.h" + + +class HTTPSServerTest: public CppUnit::TestCase +{ +public: + HTTPSServerTest(const std::string& name); + ~HTTPSServerTest(); + + void testIdentityRequest(); + void testChunkedRequest(); + void testClosedRequest(); + void testIdentityRequestKeepAlive(); + void testChunkedRequestKeepAlive(); + void testClosedRequestKeepAlive(); + void test100Continue(); + void testRedirect(); + void testAuth(); + void testNotImpl(); + + void setUp(); + void tearDown(); + + static CppUnit::Test* suite(); + +private: +}; + + +#endif // HTTPSServerTest_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSServerTestSuite.cpp b/NetSSL_OpenSSL/testsuite/src/HTTPSServerTestSuite.cpp new file mode 100644 index 000000000..516695637 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSServerTestSuite.cpp @@ -0,0 +1,44 @@ +// +// HTTPSServerTestSuite.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSServerTestSuite.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "HTTPSServerTestSuite.h" +#include "HTTPSServerTest.h" + + +CppUnit::Test* HTTPSServerTestSuite::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSServerTestSuite"); + + pSuite->addTest(HTTPSServerTest::suite()); + + return pSuite; +} diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSServerTestSuite.h b/NetSSL_OpenSSL/testsuite/src/HTTPSServerTestSuite.h new file mode 100644 index 000000000..699568c87 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSServerTestSuite.h @@ -0,0 +1,49 @@ +// +// HTTPSServerTestSuite.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSServerTestSuite.h#1 $ +// +// Definition of the HTTPSServerTestSuite class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef HTTPSServerTestSuite_INCLUDED +#define HTTPSServerTestSuite_INCLUDED + + +#include "CppUnit/TestSuite.h" + + +class HTTPSServerTestSuite +{ +public: + static CppUnit::Test* suite(); +}; + + +#endif // HTTPSServerTestSuite_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.cpp b/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.cpp new file mode 100644 index 000000000..8a2d5ac9e --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.cpp @@ -0,0 +1,153 @@ +// +// HTTPSStreamFactoryTest.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "HTTPSStreamFactoryTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Net/HTTPSStreamFactory.h" +#include "Poco/Net/NetException.h" +#include "Poco/URI.h" +#include "Poco/Exception.h" +#include "Poco/StreamCopier.h" +#include "HTTPSTestServer.h" +#include +#include + + +using Poco::Net::HTTPSStreamFactory; +using Poco::Net::NetException; +using Poco::Net::HTTPException; +using Poco::URI; +using Poco::StreamCopier; + + +HTTPSStreamFactoryTest::HTTPSStreamFactoryTest(const std::string& name): CppUnit::TestCase(name) +{ +} + + +HTTPSStreamFactoryTest::~HTTPSStreamFactoryTest() +{ +} + + +void HTTPSStreamFactoryTest::testNoRedirect() +{ + HTTPSTestServer server; + HTTPSStreamFactory factory; + URI uri("https://localhost/large"); + uri.setPort(server.port()); + std::auto_ptr pStr(factory.open(uri)); + std::ostringstream ostr; + StreamCopier::copyStream(*pStr.get(), ostr); + assert (ostr.str() == HTTPSTestServer::LARGE_BODY); +} + + +void HTTPSStreamFactoryTest::testEmptyPath() +{ + HTTPSTestServer server; + HTTPSStreamFactory factory; + URI uri("https://localhost"); + uri.setPort(server.port()); + std::auto_ptr pStr(factory.open(uri)); + std::ostringstream ostr; + StreamCopier::copyStream(*pStr.get(), ostr); + assert (ostr.str() == HTTPSTestServer::SMALL_BODY); +} + + +void HTTPSStreamFactoryTest::testRedirect() +{ + HTTPSTestServer server; + HTTPSStreamFactory factory; + URI uri("https://localhost/redirect"); + uri.setPort(server.port()); + std::auto_ptr pStr(factory.open(uri)); + std::ostringstream ostr; + StreamCopier::copyStream(*pStr.get(), ostr); + assert (ostr.str() == HTTPSTestServer::LARGE_BODY); +} + + +void HTTPSStreamFactoryTest::testProxy() +{ + HTTPSTestServer server; + HTTPSStreamFactory factory("proxy.aon.at", 8080); + URI uri("https://wwws.appinf.com/"); + std::auto_ptr pStr(factory.open(uri)); + std::ostringstream ostr; + StreamCopier::copyStream(*pStr.get(), ostr); + assert (ostr.str().length() > 0); +} + + +void HTTPSStreamFactoryTest::testError() +{ + HTTPSTestServer server; + HTTPSStreamFactory factory; + URI uri("https://localhost/notfound"); + uri.setPort(server.port()); + try + { + std::istream* pStr = factory.open(uri); + fail("not found - must throw"); + } + catch (HTTPException& exc) + { + std::string m = exc.displayText(); + } +} + + +void HTTPSStreamFactoryTest::setUp() +{ +} + + +void HTTPSStreamFactoryTest::tearDown() +{ +} + + +CppUnit::Test* HTTPSStreamFactoryTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSStreamFactoryTest"); + + CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testNoRedirect); + CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testEmptyPath); + CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testRedirect); + CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testProxy); + CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testError); + + return pSuite; +} diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.h b/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.h new file mode 100644 index 000000000..b1f6283e4 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.h @@ -0,0 +1,64 @@ +// +// HTTPSStreamFactoryTest.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSStreamFactoryTest.h#1 $ +// +// Definition of the HTTPSStreamFactoryTest class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef HTTPSStreamFactoryTest_INCLUDED +#define HTTPSStreamFactoryTest_INCLUDED + + +#include "Poco/Net/Net.h" +#include "CppUnit/TestCase.h" + + +class HTTPSStreamFactoryTest: public CppUnit::TestCase +{ +public: + HTTPSStreamFactoryTest(const std::string& name); + ~HTTPSStreamFactoryTest(); + + void testNoRedirect(); + void testEmptyPath(); + void testRedirect(); + void testProxy(); + void testError(); + + void setUp(); + void tearDown(); + + static CppUnit::Test* suite(); + +private: +}; + + +#endif // HTTPSStreamFactoryTest_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.cpp b/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.cpp new file mode 100644 index 000000000..11ad00467 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.cpp @@ -0,0 +1,238 @@ +// +// HTTPSTestServer.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "HTTPSTestServer.h" +#include "Poco/Net/SecureStreamSocket.h" +#include "Poco/Net/SocketAddress.h" +#include "Poco/Timespan.h" +#include "Poco/NumberFormatter.h" +#include + + +using Poco::Net::Socket; +using Poco::Net::StreamSocket; +using Poco::Net::SecureStreamSocket; +using Poco::Net::SecureServerSocket; +using Poco::Net::SocketAddress; +using Poco::NumberFormatter; + + +const std::string HTTPSTestServer::SMALL_BODY("This is some random text data returned by the server"); +const std::string HTTPSTestServer::LARGE_BODY(4000, 'x'); + + +HTTPSTestServer::HTTPSTestServer(): + _socket(SocketAddress()), + _thread("HTTPSTestServer"), + _stop(false) +{ + _thread.start(*this); + _ready.wait(); + _lastRequest.reserve(4000); +} + + +HTTPSTestServer::~HTTPSTestServer() +{ + _stop = true; + _thread.join(); +} + + +Poco::UInt16 HTTPSTestServer::port() const +{ + return _socket.address().port(); +} + + +const std::string& HTTPSTestServer::lastRequest() const +{ + return _lastRequest; +} + + +void HTTPSTestServer::run() +{ + _ready.set(); + Poco::Timespan span(250000); + while (!_stop) + { + if (_socket.poll(span, Socket::SELECT_READ)) + { + StreamSocket ss = _socket.acceptConnection(); + try + { + _lastRequest.clear(); + char buffer[256]; + int n = ss.receiveBytes(buffer, sizeof(buffer)); + while (n > 0 && !_stop) + { + _lastRequest.append(buffer, n); + if (!requestComplete()) + n = ss.receiveBytes(buffer, sizeof(buffer)); + else + n = 0; + } + std::string response = handleRequest(); + ss.sendBytes(response.data(), (int) response.size()); + Poco::Thread::sleep(1000); + } + catch (Poco::Exception& exc) + { + std::cerr << "HTTPSTestServer: " << exc.displayText() << std::endl; + } + } + } +} + + +bool HTTPSTestServer::requestComplete() const +{ + return ((_lastRequest.substr(0, 3) == "GET" || _lastRequest.substr(0, 4) == "HEAD") && + (_lastRequest.find("\r\n\r\n") != std::string::npos)) || + (_lastRequest.find("\r\n0\r\n") != std::string::npos); +} + + +std::string HTTPSTestServer::handleRequest() const +{ + std::string response; + response.reserve(16000); + if (_lastRequest.substr(0, 10) == "GET /small" || + _lastRequest.substr(0, 11) == "HEAD /small") + { + std::string body(SMALL_BODY); + response.append("HTTP/1.0 200 OK\r\n"); + response.append("Content-Type: text/plain\r\n"); + response.append("Content-Length: "); + response.append(NumberFormatter::format((int) body.size())); + response.append("\r\n"); + response.append("Connection: Close\r\n"); + response.append("\r\n"); + if (_lastRequest.substr(0, 3) == "GET") + response.append(body); + } + else if (_lastRequest.substr(0, 10) == "GET /large" || + _lastRequest.substr(0, 11) == "HEAD /large" || + _lastRequest.substr(0, 36) == "GET http://www.somehost.com:80/large") + { + std::string body(LARGE_BODY); + response.append("HTTP/1.0 200 OK\r\n"); + response.append("Content-Type: text/plain\r\n"); + response.append("Content-Length: "); + response.append(NumberFormatter::format((int) body.size())); + response.append("\r\n"); + response.append("Connection: Close\r\n"); + response.append("\r\n"); + if (_lastRequest.substr(0, 3) == "GET") + response.append(body); + } + else if (_lastRequest.substr(0, 4) == "POST") + { + std::string::size_type pos = _lastRequest.find("\r\n\r\n"); + pos += 4; + std::string body = _lastRequest.substr(pos); + response.append("HTTP/1.0 200 OK\r\n"); + response.append("Content-Type: text/plain\r\n"); + if (_lastRequest.find("Content-Length") != std::string::npos) + { + response.append("Content-Length: "); + response.append(NumberFormatter::format((int) body.size())); + response.append("\r\n"); + } + else if (_lastRequest.find("chunked") != std::string::npos) + { + response.append("Transfer-Encoding: chunked\r\n"); + } + response.append("Connection: Close\r\n"); + response.append("\r\n"); + response.append(body); + } + else if (_lastRequest.substr(0, 15) == "HEAD /keepAlive") + { + std::string body(SMALL_BODY); + response.append("HTTP/1.1 200 OK\r\n"); + response.append("Connection: keep-alive\r\n"); + response.append("Content-Type: text/plain\r\n"); + response.append("Content-Length: "); + response.append(NumberFormatter::format((int) body.size())); + response.append("\r\n\r\n"); + response.append("HTTP/1.1 200 OK\r\n"); + response.append("Connection: Keep-Alive\r\n"); + response.append("Content-Type: text/plain\r\n"); + response.append("Content-Length: "); + response.append(NumberFormatter::format((int) body.size())); + response.append("\r\n\r\n"); + response.append(body); + body = LARGE_BODY; + response.append("HTTP/1.1 200 OK\r\n"); + response.append("Connection: keep-alive\r\n"); + response.append("Content-Type: text/plain\r\n"); + response.append("Transfer-Encoding: chunked\r\n\r\n"); + response.append(NumberFormatter::formatHex((unsigned) body.length())); + response.append("\r\n"); + response.append(body); + response.append("\r\n0\r\n"); + response.append("HTTP/1.1 200 OK\r\n"); + response.append("Connection: close\r\n"); + response.append("Content-Type: text/plain\r\n"); + response.append("Content-Length: "); + response.append(NumberFormatter::format((int) body.size())); + response.append("\r\n\r\n"); + } + else if (_lastRequest.substr(0, 13) == "GET /redirect") + { + response.append("HTTP/1.0 302 Found\r\n"); + response.append("Location: /large\r\n"); + response.append("\r\n"); + } + else if (_lastRequest.substr(0, 13) == "GET /notfound") + { + response.append("HTTP/1.0 404 Not Found\r\n"); + response.append("\r\n"); + } + else if (_lastRequest.substr(0, 5) == "GET /" || + _lastRequest.substr(0, 6) == "HEAD /") + { + std::string body(SMALL_BODY); + response.append("HTTP/1.0 200 OK\r\n"); + response.append("Content-Type: text/plain\r\n"); + response.append("Content-Length: "); + response.append(NumberFormatter::format((int) body.size())); + response.append("\r\n"); + response.append("Connection: Close\r\n"); + response.append("\r\n"); + if (_lastRequest.substr(0, 3) == "GET") + response.append(body); + } + return response; +} diff --git a/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.h b/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.h new file mode 100644 index 000000000..cc985c634 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.h @@ -0,0 +1,81 @@ +// +// HTTPSTestServer.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/HTTPSTestServer.h#1 $ +// +// Definition of the HTTPSTestServer class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef HTTPSTestServer_INCLUDED +#define HTTPSTestServer_INCLUDED + + +#include "Poco/Net/Net.h" +#include "Poco/Net/SecureServerSocket.h" +#include "Poco/Thread.h" +#include "Poco/Event.h" + + +class HTTPSTestServer: public Poco::Runnable + /// A simple sequential echo server. +{ +public: + HTTPSTestServer(); + /// Creates the HTTPSTestServer. + + ~HTTPSTestServer(); + /// Destroys the HTTPSTestServer. + + Poco::UInt16 port() const; + /// Returns the port the echo server is + /// listening on. + + void run(); + /// Does the work. + + const std::string& lastRequest() const; + /// Returns the last request. + + static const std::string SMALL_BODY; + static const std::string LARGE_BODY; + +protected: + bool requestComplete() const; + std::string handleRequest() const; + +private: + Poco::Net::SecureServerSocket _socket; + Poco::Thread _thread; + Poco::Event _ready; + bool _stop; + std::string _lastRequest; +}; + + +#endif // HTTPSTestServer_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/NetSSLTestSuite.cpp b/NetSSL_OpenSSL/testsuite/src/NetSSLTestSuite.cpp new file mode 100644 index 000000000..9650a8fba --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/NetSSLTestSuite.cpp @@ -0,0 +1,50 @@ +// +// OpenSSLTestSuite.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/NetSSLTestSuite.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "NetSSLTestSuite.h" + +#include "HTTPSClientTestSuite.h" +#include "TCPServerTestSuite.h" +#include "HTTPSServerTestSuite.h" + + +CppUnit::Test* NetSSLTestSuite::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("OpenSSLTestSuite"); + + + pSuite->addTest(HTTPSClientTestSuite::suite()); + pSuite->addTest(TCPServerTestSuite::suite()); + pSuite->addTest(HTTPSServerTestSuite::suite()); + + return pSuite; +} diff --git a/NetSSL_OpenSSL/testsuite/src/NetSSLTestSuite.h b/NetSSL_OpenSSL/testsuite/src/NetSSLTestSuite.h new file mode 100644 index 000000000..952bf403b --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/NetSSLTestSuite.h @@ -0,0 +1,49 @@ +// +// NetSSLTestSuite.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/NetSSLTestSuite.h#1 $ +// +// Definition of the NetSSLTestSuite class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef NetSSLTestSuite_INCLUDED +#define NetSSLTestSuite_INCLUDED + + +#include "CppUnit/TestSuite.h" + + +class NetSSLTestSuite +{ +public: + static CppUnit::Test* suite(); +}; + + +#endif // NetSSLTestSuite_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/TCPServerTest.cpp b/NetSSL_OpenSSL/testsuite/src/TCPServerTest.cpp new file mode 100644 index 000000000..9a01632f2 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/TCPServerTest.cpp @@ -0,0 +1,277 @@ +// +// TCPServerTest.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/TCPServerTest.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "TCPServerTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Net/TCPServer.h" +#include "Poco/Net/TCPServerConnection.h" +#include "Poco/Net/TCPServerConnectionFactory.h" +#include "Poco/Net/TCPServerParams.h" +#include "Poco/Net/SecureStreamSocket.h" +#include "Poco/Net/SecureServerSocket.h" +#include "Poco/Thread.h" +#include + + +using Poco::Net::TCPServer; +using Poco::Net::TCPServerConnection; +using Poco::Net::TCPServerConnectionFactory; +using Poco::Net::TCPServerConnectionFactoryImpl; +using Poco::Net::TCPServerParams; +using Poco::Net::StreamSocket; +using Poco::Net::SecureStreamSocket; +using Poco::Net::SecureServerSocket; +using Poco::Net::SocketAddress; +using Poco::Thread; + + +namespace +{ + class EchoConnection: public TCPServerConnection + { + public: + EchoConnection(const StreamSocket& s): TCPServerConnection(s) + { + } + + void run() + { + StreamSocket& ss = socket(); + try + { + char buffer[256]; + int n = ss.receiveBytes(buffer, sizeof(buffer)); + while (n > 0) + { + ss.sendBytes(buffer, n); + n = ss.receiveBytes(buffer, sizeof(buffer)); + } + } + catch (Poco::Exception& exc) + { + std::cerr << "EchoConnection: " << exc.displayText() << std::endl; + } + } + }; +} + + +TCPServerTest::TCPServerTest(const std::string& name): CppUnit::TestCase(name) +{ +} + + +TCPServerTest::~TCPServerTest() +{ +} + + +void TCPServerTest::testOneConnection() +{ + SecureServerSocket svs(0); + TCPServer srv(new TCPServerConnectionFactoryImpl(), svs); + srv.start(); + assert (srv.currentConnections() == 0); + assert (srv.currentThreads() == 0); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 0); + + SocketAddress sa("localhost", svs.address().port()); + SecureStreamSocket ss1(sa); + std::string data("hello, world"); + ss1.sendBytes(data.data(), (int) data.size()); + char buffer[256]; + int n = ss1.receiveBytes(buffer, sizeof(buffer)); + assert (n > 0); + assert (std::string(buffer, n) == data); + assert (srv.currentConnections() == 1); + assert (srv.currentThreads() == 1); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 1); + ss1.close(); + Thread::sleep(300); + assert (srv.currentConnections() == 0); +} + + +void TCPServerTest::testTwoConnections() +{ + SecureServerSocket svs(0); + TCPServer srv(new TCPServerConnectionFactoryImpl(), svs); + srv.start(); + assert (srv.currentConnections() == 0); + assert (srv.currentThreads() == 0); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 0); + + SocketAddress sa("localhost", svs.address().port()); + SecureStreamSocket ss1(sa); + SecureStreamSocket ss2(sa); + std::string data("hello, world"); + ss1.sendBytes(data.data(), (int) data.size()); + ss2.sendBytes(data.data(), (int) data.size()); + + char buffer[256]; + int n = ss1.receiveBytes(buffer, sizeof(buffer)); + assert (n > 0); + assert (std::string(buffer, n) == data); + + n = ss2.receiveBytes(buffer, sizeof(buffer)); + assert (n > 0); + assert (std::string(buffer, n) == data); + + assert (srv.currentConnections() == 2); + assert (srv.currentThreads() == 2); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 2); + ss1.close(); + Thread::sleep(300); + assert (srv.currentConnections() == 1); + assert (srv.currentThreads() == 1); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 2); + ss2.close(); + + Thread::sleep(300); + assert (srv.currentConnections() == 0); +} + + +void TCPServerTest::testMultiConnections() +{ + SecureServerSocket svs(0); + TCPServerParams* pParams = new TCPServerParams; + pParams->setMaxThreads(4); + pParams->setMaxQueued(4); + pParams->setThreadIdleTime(100); + TCPServer srv(new TCPServerConnectionFactoryImpl(), svs, pParams); + srv.start(); + assert (srv.currentConnections() == 0); + assert (srv.currentThreads() == 0); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 0); + + SocketAddress sa("localhost", svs.address().port()); + SecureStreamSocket ss1(sa); + SecureStreamSocket ss2(sa); + SecureStreamSocket ss3(sa); + SecureStreamSocket ss4(sa); + std::string data("hello, world"); + ss1.sendBytes(data.data(), (int) data.size()); + ss2.sendBytes(data.data(), (int) data.size()); + ss3.sendBytes(data.data(), (int) data.size()); + ss4.sendBytes(data.data(), (int) data.size()); + + char buffer[256]; + int n = ss1.receiveBytes(buffer, sizeof(buffer)); + assert (n > 0); + assert (std::string(buffer, n) == data); + + n = ss2.receiveBytes(buffer, sizeof(buffer)); + assert (n > 0); + assert (std::string(buffer, n) == data); + + n = ss3.receiveBytes(buffer, sizeof(buffer)); + assert (n > 0); + assert (std::string(buffer, n) == data); + + n = ss4.receiveBytes(buffer, sizeof(buffer)); + assert (n > 0); + assert (std::string(buffer, n) == data); + + assert (srv.currentConnections() == 4); + assert (srv.currentThreads() == 4); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 4); + + SecureStreamSocket ss5(sa); + Thread::sleep(200); + assert (srv.queuedConnections() == 1); + SecureStreamSocket ss6(sa); + Thread::sleep(200); + assert (srv.queuedConnections() == 2); + + ss1.close(); + Thread::sleep(300); + assert (srv.currentConnections() == 4); + assert (srv.currentThreads() == 4); + assert (srv.queuedConnections() == 1); + assert (srv.totalConnections() == 5); + + ss2.close(); + Thread::sleep(300); + assert (srv.currentConnections() == 4); + assert (srv.currentThreads() == 4); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 6); + + ss3.close(); + Thread::sleep(300); + assert (srv.currentConnections() == 3); + assert (srv.currentThreads() == 3); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 6); + + ss4.close(); + Thread::sleep(300); + assert (srv.currentConnections() == 2); + assert (srv.currentThreads() == 2); + assert (srv.queuedConnections() == 0); + assert (srv.totalConnections() == 6); + + ss5.close(); + ss6.close(); + Thread::sleep(300); + assert (srv.currentConnections() == 0);} + + +void TCPServerTest::setUp() +{ +} + + +void TCPServerTest::tearDown() +{ +} + + +CppUnit::Test* TCPServerTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TCPServerTest"); + + CppUnit_addTest(pSuite, TCPServerTest, testOneConnection); + CppUnit_addTest(pSuite, TCPServerTest, testTwoConnections); + CppUnit_addTest(pSuite, TCPServerTest, testMultiConnections); + + return pSuite; +} diff --git a/NetSSL_OpenSSL/testsuite/src/TCPServerTest.h b/NetSSL_OpenSSL/testsuite/src/TCPServerTest.h new file mode 100644 index 000000000..6904d1226 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/TCPServerTest.h @@ -0,0 +1,62 @@ +// +// TCPServerTest.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/TCPServerTest.h#1 $ +// +// Definition of the TCPServerTest class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef TCPServerTest_INCLUDED +#define TCPServerTest_INCLUDED + + +#include "Poco/Net/Net.h" +#include "CppUnit/TestCase.h" + + +class TCPServerTest: public CppUnit::TestCase +{ +public: + TCPServerTest(const std::string& name); + ~TCPServerTest(); + + void testOneConnection(); + void testTwoConnections(); + void testMultiConnections(); + + void setUp(); + void tearDown(); + + static CppUnit::Test* suite(); + +private: +}; + + +#endif // TCPServerTest_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/TCPServerTestSuite.cpp b/NetSSL_OpenSSL/testsuite/src/TCPServerTestSuite.cpp new file mode 100644 index 000000000..4459e2616 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/TCPServerTestSuite.cpp @@ -0,0 +1,44 @@ +// +// TCPServerTestSuite.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/TCPServerTestSuite.cpp#1 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "TCPServerTestSuite.h" +#include "TCPServerTest.h" + + +CppUnit::Test* TCPServerTestSuite::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TCPServerTestSuite"); + + pSuite->addTest(TCPServerTest::suite()); + + return pSuite; +} diff --git a/NetSSL_OpenSSL/testsuite/src/TCPServerTestSuite.h b/NetSSL_OpenSSL/testsuite/src/TCPServerTestSuite.h new file mode 100644 index 000000000..6af24f91c --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/TCPServerTestSuite.h @@ -0,0 +1,49 @@ +// +// TCPServerTestSuite.h +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/TCPServerTestSuite.h#1 $ +// +// Definition of the TCPServerTestSuite class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef TCPServerTestSuite_INCLUDED +#define TCPServerTestSuite_INCLUDED + + +#include "CppUnit/TestSuite.h" + + +class TCPServerTestSuite +{ +public: + static CppUnit::Test* suite(); +}; + + +#endif // TCPServerTestSuite_INCLUDED diff --git a/NetSSL_OpenSSL/testsuite/src/WinDriver.cpp b/NetSSL_OpenSSL/testsuite/src/WinDriver.cpp new file mode 100644 index 000000000..445706f88 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/src/WinDriver.cpp @@ -0,0 +1,86 @@ +// +// WinDriver.cpp +// +// $Id: //poco/1.3/NetSSL_OpenSSL/testsuite/src/WinDriver.cpp#1 $ +// +// Windows test driver for Poco OpenSSL. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "WinTestRunner/WinTestRunner.h" +#include "NetSSLTestSuite.h" +#include "Poco/Util/Application.h" +#include "Poco/Net/HTTPStreamFactory.h" +#include "Poco/Net/HTTPSStreamFactory.h" + + +class NetSSLApp: public Poco::Util::Application +{ +public: + NetSSLApp() + { + } + + ~NetSSLApp() + { + } + +protected: + void initialize(Application& self) + { + loadConfiguration(); // load default configuration files, if present + Application::initialize(self); + } +}; + + +class TestDriver: public CppUnit::WinTestRunnerApp +{ + void TestMain() + { + CppUnit::WinTestRunner runner; + Poco::Net::HTTPStreamFactory::registerFactory(); + Poco::Net::HTTPSStreamFactory::registerFactory(); + NetSSLApp app; + std::string argv("OpenSSLTest"); + const char* pArgv = argv.c_str(); + try + { + app.init(1, (char**)&pArgv); + } + catch (Poco::Exception& exc) + { + app.logger().log(exc); + } + runner.addTest(NetSSLTestSuite::suite()); + runner.run(); + } +}; + + +static TestDriver theDriver; diff --git a/NetSSL_OpenSSL/testsuite/testrunner.xml b/NetSSL_OpenSSL/testsuite/testrunner.xml new file mode 100644 index 000000000..ed8b08790 --- /dev/null +++ b/NetSSL_OpenSSL/testsuite/testrunner.xml @@ -0,0 +1,42 @@ + + + + ${application.configDir}any.pem + ${application.configDir}rootcert.pem + none + 9 + true + ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH + + KeyFileHandler + + test + + + + AcceptCertificateHandler + + + + + + ${application.configDir}any.pem + ${application.configDir}rootcert.pem + relaxed + 9 + true + ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH + + KeyFileHandler + + test + + + + AcceptCertificateHandler + + + + + +