summaryrefslogtreecommitdiff
path: root/cpp/test/IceSSL/certificateVerification/Client.cpp
blob: b8a3d168775fd07875c70781700ca3c731ea535c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <Ice/Ice.h>
#include <IceSSL/Plugin.h>
#include <IceSSL/Exception.h>
#include <TestCommon.h>
#include <Pinger.h>

using namespace std;

int
run(int argc, char* argv[], const Ice::CommunicatorPtr& communicator)
{
    const string ref1 = "pinger:ssl -p 12345 -t 2000";
    const string ref2 = "pinger:ssl -p 12346 -t 2000";
    const string ref3 = "pinger:ssl -p 12347 -t 2000";
    const string ref4 = "pinger:ssl -p 12348 -t 2000";
    const string ref5 = "pinger:ssl -p 12349 -t 2000";

    KeyManagerPrx km = KeyManagerPrx::checkedCast(communicator->stringToProxy("keyManager:tcp -p 12344 -t 2000"));

    Ice::ByteSeq serverTrustedCert;
    Ice::ByteSeq serverUntrustedCert;
    Ice::ByteSeq clientTrustedKey;
    Ice::ByteSeq clientTrustedCert;
    Ice::ByteSeq clientUntrustedKey;
    Ice::ByteSeq clientUntrustedCert;

    km->getServerCerts(serverTrustedCert,serverUntrustedCert);
    km->getTrustedClientKeys(clientTrustedKey,clientTrustedCert);
    km->getUntrustedClientKeys(clientUntrustedKey,clientUntrustedCert);

    Ice::PluginPtr plugin = communicator->getPluginManager()->getPlugin("IceSSL");
    IceSSL::PluginPtr sslPlugin = IceSSL::PluginPtr::dynamicCast(plugin);

    Ice::PropertiesPtr properties = communicator->getProperties();

    // Use test related paths - override values in TestUtil.py
    std::string clientCertPath = properties->getProperty("TestSSL.Client.CertPath");
    std::string serverCertPath = properties->getProperty("TestSSL.Server.CertPath");
    properties->setProperty("IceSSL.Client.CertPath", clientCertPath);
    properties->setProperty("IceSSL.Server.CertPath", serverCertPath);

    bool singleCertVerifier = false;
    if(properties->getProperty("TestSSL.Client.CertificateVerifier") == "singleCert")
    {
        singleCertVerifier = true;
    }

    if(!singleCertVerifier)
    {
        cout << "client and server trusted, client using stock certificate... ";

        properties->setProperty("IceSSL.Client.Config", "sslconfig_6.xml");
        sslPlugin->configure(IceSSL::Client);
        sslPlugin->addTrustedCertificate(IceSSL::Client, serverTrustedCert);
        try
        {
            PingerPrx pinger1 = PingerPrx::checkedCast(communicator->stringToProxy(ref1));
            pinger1->ping();
            cout << "ok" << endl;
        }
        catch(const Ice::LocalException& ex)
        {
            cout << ex << endl;
            km->shutdown();
            test(false);
        }
    }

    properties->setProperty("IceSSL.Client.Config", "sslconfig_7.xml");

    cout << "client and server do not trust each other... " << flush;

    // Neither Client nor Server will trust.
    sslPlugin->configure(IceSSL::Client);
    sslPlugin->addTrustedCertificate(IceSSL::Client, serverUntrustedCert);
    if(singleCertVerifier)
    {
        IceSSL::CertificateVerifierPtr certVerifier = sslPlugin->getSingleCertVerifier(serverUntrustedCert);
        sslPlugin->setCertificateVerifier(IceSSL::Client, certVerifier);
    }
    sslPlugin->setRSAKeys(IceSSL::Client, clientUntrustedKey, clientUntrustedCert);
    try
    {
        PingerPrx pinger2 = PingerPrx::checkedCast(communicator->stringToProxy(ref2));
        pinger2->ping();
        km->shutdown();
        test(false);
    }
    catch(const IceSSL::CertificateVerificationException&)
    {
	cout << "ok" << endl;
    }
    catch(const Ice::LocalException&)
    {
        km->shutdown();
        test(false);
    }

    cout << "client trusted, server not trusted... " << flush;

    // Client will not trust Server, but Server will trust Client.
    sslPlugin->setRSAKeys(IceSSL::Client, clientTrustedKey, clientTrustedCert);
    try
    {
        PingerPrx pinger3 = PingerPrx::checkedCast(communicator->stringToProxy(ref3));
        pinger3->ping();
        km->shutdown();
        test(false);
    }
    catch(const IceSSL::CertificateVerificationException&)
    {
	cout << "ok" << endl;
    }
    catch(const Ice::LocalException&)
    {
        km->shutdown();
        test(false);
    }

    cout << "client trusts server, server does not trust client... " << flush;

    // Client trusts, Server does not.
    sslPlugin->configure(IceSSL::Client);
    sslPlugin->addTrustedCertificate(IceSSL::Client, serverTrustedCert);
    if(singleCertVerifier)
    {
        IceSSL::CertificateVerifierPtr certVerifier = sslPlugin->getSingleCertVerifier(serverTrustedCert);
        sslPlugin->setCertificateVerifier(IceSSL::Client, certVerifier);
    }
    sslPlugin->setRSAKeys(IceSSL::Client, clientUntrustedKey, clientUntrustedCert);
    try
    {
        PingerPrx pinger4 = PingerPrx::checkedCast(communicator->stringToProxy(ref4));
        pinger4->ping();
        km->shutdown();
        test(false);
    }
    catch(const IceSSL::ProtocolException&)
    {
        // Note: We expect that the server will send an alert 48 back to the client,
        //       generating this exception.
	cout << "ok" << endl;
    }
    catch(const Ice::LocalException&)
    {
        km->shutdown();
        test(false);
    }

    cout << "both client and server trust each other... " << flush;

    // Both Client and Server trust.
    sslPlugin->setRSAKeys(IceSSL::Client, clientTrustedKey, clientTrustedCert);

    try
    {
        PingerPrx pinger5 = PingerPrx::checkedCast(communicator->stringToProxy(ref5));
	pinger5->ping();
	cout << "ok" << endl;
    }
    catch(const Ice::LocalException&)
    {
        km->shutdown();
        test(false);
    }

    cout << "shutting down... " << flush;
    km->shutdown();
    cout << "ok" << endl;

    return EXIT_SUCCESS;
}

int
main(int argc, char* argv[])
{
    int status;
    Ice::CommunicatorPtr communicator;

    try
    {
	communicator = Ice::initialize(argc, argv);
        Ice::PropertiesPtr properties = communicator->getProperties();
        Ice::StringSeq args = Ice::argsToStringSeq(argc, argv);
        args = properties->parseCommandLineOptions("TestSSL", args);
        Ice::stringSeqToArgs(args, argc, argv);
	status = run(argc, argv, communicator);
    }
    catch(const Ice::Exception& ex)
    {
	cerr << ex << endl;
	status = EXIT_FAILURE;
    }

    if(communicator)
    {
	try
	{
	    communicator->destroy();
	}
	catch(const Ice::Exception& ex)
	{
	    cerr << ex << endl;
	    status = EXIT_FAILURE;
	}
    }

    return status;
}