SHARE
TWEET

Secure socket with Poco

a guest Feb 27th, 2020 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Poco/Exception.h"
  2. #include "Poco/StreamCopier.h"
  3.  
  4. #include "Poco/URI.h"
  5. #include "Poco/URIStreamOpener.h"
  6. #include "Poco/Net/HTTPStreamFactory.h"
  7. #include "Poco/Net/SecureStreamSocket.h"
  8.  
  9. #include "Poco/Net/AcceptCertificateHandler.h"
  10. #include "Poco/Net/InvalidCertificateHandler.h"
  11. #include "Poco/Net/Context.h"
  12. #include "Poco/Net/SSLManager.h"
  13. #include "Poco/Net/PrivateKeyPassphraseHandler.h"
  14. #include "Poco/Net/NetSSL.h"
  15. #include "Poco/Net/VerificationErrorArgs.h"
  16. #include <Poco/Util/Application.h>
  17.  
  18. #include <vector>
  19. #include <string>
  20. #include <memory>
  21. #include <iostream>
  22.  
  23. #pragma comment(lib, "Crypt32.lib")
  24. #pragma comment(lib, "ws2_32.lib")
  25.  
  26. class MyPrivateKeyPassphraseHandler : public Poco::Net::PrivateKeyPassphraseHandler
  27. {
  28. public:
  29.     MyPrivateKeyPassphraseHandler(bool onServerSize) : Poco::Net::PrivateKeyPassphraseHandler(onServerSize)
  30.     {
  31.     }
  32.  
  33.     void onPrivateKeyRequested(const void* pSender, std::string& privateKey) override
  34.     {
  35.     }
  36. };
  37.  
  38. class MyInvalidCertificateHandler : public Poco::Net::InvalidCertificateHandler
  39. {
  40. public:
  41.     MyInvalidCertificateHandler(bool handleErrorsOnServerSize)
  42.         : Poco::Net::InvalidCertificateHandler(handleErrorsOnServerSize)
  43.     {
  44.  
  45.     }
  46.  
  47.     void onInvalidCertificate(const void* pSender, Poco::Net::VerificationErrorArgs& errorCert) override
  48.     {
  49.         errorCert.setIgnoreError(true);
  50.     }
  51. };
  52.  
  53. int main(int argc, char** argv)
  54. {
  55.     Poco::SharedPtr<MyInvalidCertificateHandler> invalidCertificateHandlerPtr(new MyInvalidCertificateHandler(false));
  56.     Poco::SharedPtr<MyPrivateKeyPassphraseHandler> privateKeyPassphraseHandlerPtr(new MyPrivateKeyPassphraseHandler(false));
  57.  
  58.     Poco::Net::Context::Ptr context = new Poco::Net::Context(Poco::Net::Context::Usage::CLIENT_USE, "");
  59.     Poco::Net::SSLManager::instance().initializeClient(privateKeyPassphraseHandlerPtr, invalidCertificateHandlerPtr, context);
  60.     Poco::Net::SocketAddress address("www.example.org", 443);
  61.     Poco::Net::SecureStreamSocket stream(address);
  62.  
  63.     const char* request = "GET / HTTP/1.0\nHost: www.example.org\n\n";
  64.     stream.sendBytes((void*)request, strlen(request));
  65.  
  66.     char recvBuff[2048];
  67.     stream.receiveBytes((void*)recvBuff, sizeof(recvBuff));
  68.     return 0;
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top