Skip to content
 

RCF 1.2 + OpenSSL

Being asked about code snippet showing real working example of using RCF together with OpenSSL, I decided to create such a refined self–contained example.
I tried to make it as close to available docs and samples as possible, still keeping it compilable and working.

This example will show probably the most simple case: server holding a certificate and client verifying that certificate. So before running you’ll need to generate both: Ca and Server certs.

Here goes server part:

#include <cstdlib>
#include <iostream>
#include <vector>

#include <RCF/Idl.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/TcpEndpoint.hpp>
#include <RCF/FilterService.hpp>
#include <RCF/OpenSslEncryptionFilter.hpp>
#include <RCF/SessionObjectFactoryService.hpp>

RCF_BEGIN(I_X, "I_X")
RCF_METHOD_R1(int, test, int)
RCF_END(I_X)

class X
{
public:
    int test(int x)
    {
        std::vector<RCF::FilterPtr> transportFilters;
        RCF::getCurrentRcfSession().getTransportFilters(transportFilters);

        if (transportFilters.size() == 1)
        {
            boost::shared_ptr<RCF::OpenSslEncryptionFilter> filterPtr =
                boost::dynamic_pointer_cast<RCF::OpenSslEncryptionFilter>(
                    transportFilters.front());

            if (filterPtr)
            {
                RCF::getCurrentRcfSession().lockTransportFilters();
                RCF::getCurrentRcfSession().unlockTransportFilters();

                return x*x;
            }
        }

        return -1;
    }
};

int main()
{
    RCF::RcfServer server(RCF::TcpEndpoint(50001));

    RCF::SessionObjectFactoryServicePtr sofsPtr(new RCF::SessionObjectFactoryService());
    server.addService(sofsPtr);

    sofsPtr->bind<I_X, X>();

    RCF::FilterServicePtr fsPtr(new RCF::FilterService());
    fsPtr->addFilterFactory(RCF::FilterFactoryPtr(
        new RCF::OpenSslEncryptionFilterFactory("/path.../server.pem", "")));
    server.addService(fsPtr);

    server.start();

    std::cout << "Press Enter to exit..." << std::endl;
    std::cin.get();

    return (EXIT_SUCCESS);
}

and here goes client part:

#include <cstdlib>
#include <iostream>
#include <vector>

#include <RCF/Idl.hpp>
#include <RCF/RcfServer.hpp>
#include <RCF/TcpEndpoint.hpp>
#include <RCF/FilterService.hpp>
#include <RCF/OpenSslEncryptionFilter.hpp>
#include <RCF/SessionObjectFactoryService.hpp>

RCF_BEGIN(I_X, "I_X")
RCF_METHOD_R1(int, test, int)
RCF_END(I_X)

int main()
{
    RcfClient<I_X> client(RCF::TcpEndpoint(50001));

    client.getClientStub().createRemoteSessionObject();

    client.getClientStub().requestTransportFilters(
        RCF::FilterPtr(
            new RCF::OpenSslEncryptionFilter("", "", "/path.../ca.pem")));

    std::cout << client.test(21) << std::endl;

    return (EXIT_SUCCESS);
}

Very simple and very similar to example in RCF’s docs.

Leave a Reply