/[resiprocate]/main/resip/dum/test/certServer.cxx
ViewVC logotype

Contents of /main/resip/dum/test/certServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5929 - (show annotations) (download)
Thu Feb 16 22:36:43 2006 UTC (13 years, 9 months ago) by dworley
File size: 14606 byte(s)
Start cleaning up svn:eol-style and EOLs in files.
Note that text and program files that can be used on both Un*x and Windows
should have svn:eol-style=native so they check out correctly on both
platforms.

1 #ifdef WIN32
2 # define usleep(t) Sleep(t)
3 #endif
4
5 #if defined (HAVE_POPT_H)
6 #include <popt.h>
7 #endif
8
9 #include <signal.h>
10
11 #include "resip/stack/NameAddr.hxx"
12 #include "resip/stack/Pkcs8Contents.hxx"
13 #include "resip/stack/SipMessage.hxx"
14 #include "resip/stack/Symbols.hxx"
15 #include "resip/stack/Uri.hxx"
16 #include "resip/stack/X509Contents.hxx"
17 #include "resip/dum/AppDialogSet.hxx"
18 #include "resip/dum/ClientAuthManager.hxx"
19 #include "resip/dum/DialogUsageManager.hxx"
20 #include "resip/dum/DumShutdownHandler.hxx"
21 #include "resip/dum/OutOfDialogHandler.hxx"
22 #include "resip/dum/MasterProfile.hxx"
23 #include "resip/dum/PublicationHandler.hxx"
24 #include "resip/dum/RegistrationHandler.hxx"
25 #include "resip/dum/ServerPublication.hxx"
26 #include "resip/dum/ServerSubscription.hxx"
27 #include "resip/dum/SubscriptionHandler.hxx"
28 #include "rutil/Log.hxx"
29 #include "rutil/Logger.hxx"
30 #include "rutil/Random.hxx"
31 #include "rutil/Subsystem.hxx"
32
33 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
34
35 using namespace std;
36 using namespace resip;
37
38 static bool finished=false;
39
40 void
41 signalHandler(int signo)
42 {
43 std::cerr << "Shutting down" << endl;
44 finished = true;
45 }
46
47 // When a publish comes in, we should let any outstanding subscriptions know
48 // about it.
49
50 class CertSubscriptionHandler;
51 class PrivateKeySubscriptionHandler;
52
53 class CertPublicationHandler : public ServerPublicationHandler
54 {
55 public:
56 CertPublicationHandler(Security& security) : mSecurity(security)
57 {
58 }
59
60 virtual void onInitial(ServerPublicationHandle h,
61 const Data& etag,
62 const SipMessage& pub,
63 const Contents* contents,
64 const SecurityAttributes* attrs,
65 int expires)
66 {
67 add(h, contents);
68 }
69
70 virtual void onExpired(ServerPublicationHandle h, const Data& etag)
71 {
72 mSecurity.removeUserCert(h->getPublisher());
73 }
74
75 virtual void onRefresh(ServerPublicationHandle,
76 const Data& etag,
77 const SipMessage& pub,
78 const Contents* contents,
79 const SecurityAttributes* attrs,
80 int expires)
81 {
82 }
83
84 virtual void onUpdate(ServerPublicationHandle h,
85 const Data& etag,
86 const SipMessage& pub,
87 const Contents* contents,
88 const SecurityAttributes* attrs,
89 int expires)
90 {
91 add(h, contents);
92 }
93
94 virtual void onRemoved(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
95 {
96 mSecurity.removeUserCert(h->getPublisher());
97 }
98 private:
99 void add(ServerPublicationHandle h, const Contents* contents)
100 {
101 if (h->getDocumentKey() == h->getPublisher())
102 {
103 const X509Contents* x509 = dynamic_cast<const X509Contents*>(contents);
104 assert(x509);
105 mSecurity.addUserCertDER(h->getPublisher(), x509->getBodyData());
106 h->send(h->accept(200));
107 }
108 else
109 {
110 h->send(h->accept(403)); // !jf! is this the correct code?
111 }
112 }
113
114 Security& mSecurity;
115 };
116
117 class PrivateKeyPublicationHandler : public ServerPublicationHandler
118 {
119 public:
120 PrivateKeyPublicationHandler(Security& security) : mSecurity(security)
121 {
122 }
123
124 virtual void onInitial(ServerPublicationHandle h,
125 const Data& etag,
126 const SipMessage& pub,
127 const Contents* contents,
128 const SecurityAttributes* attrs,
129 int expires)
130 {
131 add(h, contents);
132 }
133
134 virtual void onExpired(ServerPublicationHandle h, const Data& etag)
135 {
136 mSecurity.removeUserPrivateKey(h->getPublisher());
137 }
138
139 virtual void onRefresh(ServerPublicationHandle,
140 const Data& etag,
141 const SipMessage& pub,
142 const Contents* contents,
143 const SecurityAttributes* attrs,
144 int expires)
145 {
146 }
147
148 virtual void onUpdate(ServerPublicationHandle h,
149 const Data& etag,
150 const SipMessage& pub,
151 const Contents* contents,
152 const SecurityAttributes* attrs,
153 int expires)
154 {
155 add(h, contents);
156 }
157
158 virtual void onRemoved(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
159 {
160 mSecurity.removeUserPrivateKey(h->getPublisher());
161 }
162
163 private:
164 void add(ServerPublicationHandle h, const Contents* contents)
165 {
166 if (h->getDocumentKey() == h->getPublisher())
167 {
168 const Pkcs8Contents* pkcs8 = dynamic_cast<const Pkcs8Contents*>(contents);
169 assert(pkcs8);
170 mSecurity.addUserPrivateKeyDER(h->getPublisher(), pkcs8->getBodyData());
171 }
172 else
173 {
174 h->send(h->accept(403)); // !jf! is this the correct code?
175 }
176 }
177
178 Security& mSecurity;
179 };
180
181 class CertSubscriptionHandler : public ServerSubscriptionHandler
182 {
183 public:
184 CertSubscriptionHandler(Security& security) : mSecurity(security)
185 {
186 }
187
188 virtual void onNewSubscription(ServerSubscriptionHandle h, const SipMessage& sub)
189 {
190 if (!mSecurity.hasUserCert(h->getDocumentKey()))
191 {
192 // !jf! really need to do this async. send neutral state in the meantime,
193 // blah blah blah
194 mSecurity.generateUserCert(h->getDocumentKey());
195 }
196
197 if (mSecurity.hasUserCert(h->getDocumentKey()))
198 {
199 X509Contents x509(mSecurity.getUserCertDER(h->getDocumentKey()));
200 h->send(h->update(&x509));
201 }
202 else
203 {
204 h->reject(404);
205 }
206 }
207
208 virtual void onPublished(ServerSubscriptionHandle associated,
209 ServerPublicationHandle publication,
210 const Contents* contents,
211 const SecurityAttributes* attrs)
212 {
213 associated->send(associated->update(contents));
214 }
215
216
217 virtual void onTerminated(ServerSubscriptionHandle)
218 {
219 }
220
221 virtual void onError(ServerSubscriptionHandle, const SipMessage& msg)
222 {
223 }
224
225 private:
226 Security& mSecurity;
227 };
228
229 class PrivateKeySubscriptionHandler : public ServerSubscriptionHandler
230 {
231 public:
232 PrivateKeySubscriptionHandler(Security& security) : mSecurity(security)
233 {
234 }
235
236 virtual void onNewSubscription(ServerSubscriptionHandle h, const SipMessage& sub)
237 {
238 if (h->getDocumentKey() != h->getSubscriber())
239 {
240 h->send(h->accept(403)); // !jf! is this the correct code?
241 }
242 else if (mSecurity.hasUserCert(h->getDocumentKey()))
243 {
244 Pkcs8Contents pkcs(mSecurity.getUserPrivateKeyDER(h->getDocumentKey()));
245 h->send(h->update(&pkcs));
246 }
247 else
248 {
249 h->reject(404);
250 }
251 }
252
253 virtual void onPublished(ServerSubscriptionHandle associated,
254 ServerPublicationHandle publication,
255 const Contents* contents,
256 const SecurityAttributes* attrs)
257 {
258 associated->send(associated->update(contents));
259 }
260
261 virtual void onTerminated(ServerSubscriptionHandle)
262 {
263 }
264
265 virtual void onError(ServerSubscriptionHandle, const SipMessage& msg)
266 {
267 }
268
269 private:
270 Security& mSecurity;
271 };
272
273
274
275 class CertServer : public OutOfDialogHandler, public DialogUsageManager
276 {
277 public:
278 CertServer(const resip::NameAddr& me, SipStack& stack) :
279 DialogUsageManager(stack),
280 mCertServer(*getSecurity()),
281 mPrivateKeyServer(*getSecurity()),
282 mCertUpdater(*getSecurity()),
283 mPrivateKeyUpdater(*getSecurity()),
284 mDone(false)
285 {
286 addTransport(UDP, 5100);
287 addTransport(TCP, 5100);
288 addTransport(TLS, 5101, V4, Data::Empty, me.uri().host(), Data::Empty);
289
290 mProfile = new MasterProfile;
291 mProfile->clearSupportedMethods();
292 mProfile->addSupportedMethod(PUBLISH);
293 mProfile->addSupportedMethod(SUBSCRIBE);
294 mProfile->validateAcceptEnabled() = true;
295 mProfile->validateContentEnabled() = true;
296 mProfile->addSupportedMimeType(PUBLISH, Pkcs8Contents::getStaticType());
297 mProfile->addSupportedMimeType(SUBSCRIBE, Pkcs8Contents::getStaticType());
298 mProfile->addSupportedMimeType(PUBLISH, X509Contents::getStaticType());
299 mProfile->addSupportedMimeType(SUBSCRIBE, X509Contents::getStaticType());
300
301 mProfile.setDefaultFrom(me);
302 setMasterProfile(mProfile);
303
304 addServerSubscriptionHandler(Symbols::Credential, &mPrivateKeyServer);
305 addServerSubscriptionHandler(Symbols::Certificate, &mCertServer);
306 addServerPublicationHandler(Symbols::Credential, &mPrivateKeyUpdater);
307 addServerPublicationHandler(Symbols::Certificate, &mCertUpdater);
308 addOutOfDialogHandler(OPTIONS, this);
309
310 //setServerAuthManager(std::auto_ptr<ServerAuthManager>(new ServerAuthManager(mProfile)));
311
312 DialogUsageManager::run();
313 }
314
315 ~CertServer()
316 {
317 }
318
319 void run()
320 {
321 while ( !mDone )
322 {
323 while (process());
324 usleep(5);
325
326 if (finished)
327 {
328 // graceful shutdown
329 exit(0);
330 }
331 }
332 }
333
334 virtual void onSuccess(ClientOutOfDialogReqHandle, const SipMessage& successResponse)
335 {
336 }
337
338 virtual void onFailure(ClientOutOfDialogReqHandle, const SipMessage& errorResponse)
339 {
340 }
341
342 virtual void onReceivedRequest(ServerOutOfDialogReqHandle, const SipMessage& request)
343 {
344 }
345
346 private:
347 SharedPtr<MasterProfile> mProfile;
348 CertSubscriptionHandler mCertServer;
349 PrivateKeySubscriptionHandler mPrivateKeyServer;
350 CertPublicationHandler mCertUpdater;
351 PrivateKeyPublicationHandler mPrivateKeyUpdater;
352 bool mDone;
353 };
354
355 int
356 main (int argc, char** argv)
357 {
358 char* logType = "COUT";
359 char* logLevel = "DEBUG";
360 char* myUrl = "sip:localhost:7001";
361 char* bindAddr = 0;
362 int v6 = 0;
363
364 #if defined(HAVE_POPT_H)
365 struct poptOption table[] = {
366 {"log-type", 'l', POPT_ARG_STRING, &logType, 0, "where to send logging messages", "syslog|cerr|cout"},
367 {"log-level", 'v', POPT_ARG_STRING, &logLevel, 0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
368 {"name" , 'n', POPT_ARG_STRING, &myUrl, 0, "my url", 0},
369 {"bind", 'b', POPT_ARG_STRING, &bindAddr, 0, "interface address to bind to",0},
370 {"v6", '6', POPT_ARG_NONE, &v6 , 0, "ipv6", 0},
371 POPT_AUTOHELP
372 { NULL, 0, 0, NULL, 0 }
373 };
374 poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
375 poptGetNextOpt(context);
376 #endif
377 Log::initialize(logType, logLevel, argv[0]);
378
379 #ifndef _WIN32
380 if ( signal( SIGPIPE, SIG_IGN) == SIG_ERR)
381 {
382 cerr << "Couldn't install signal handler for SIGPIPE" << endl;
383 exit(-1);
384 }
385
386 if ( signal( SIGINT, signalHandler ) == SIG_ERR )
387 {
388 cerr << "Couldn't install signal handler for SIGINT" << endl;
389 exit( -1 );
390 }
391
392 if ( signal( SIGTERM, signalHandler ) == SIG_ERR )
393 {
394 cerr << "Couldn't install signal handler for SIGTERM" << endl;
395 exit( -1 );
396 }
397 #endif
398
399 NameAddr domain(myUrl);
400 SipStack stack;
401 CertServer server(domain, stack);
402 server.run();
403 return 0;
404 }
405
406
407 /* ====================================================================
408 * The Vovida Software License, Version 1.0
409 *
410 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
411 *
412 * Redistribution and use in source and binary forms, with or without
413 * modification, are permitted provided that the following conditions
414 * are met:
415 *
416 * 1. Redistributions of source code must retain the above copyright
417 * notice, this list of conditions and the following disclaimer.
418 *
419 * 2. Redistributions in binary form must reproduce the above copyright
420 * notice, this list of conditions and the following disclaimer in
421 * the documentation and/or other materials provided with the
422 * distribution.
423 *
424 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
425 * and "Vovida Open Communication Application Library (VOCAL)" must
426 * not be used to endorse or promote products derived from this
427 * software without prior written permission. For written
428 * permission, please contact vocal@vovida.org.
429 *
430 * 4. Products derived from this software may not be called "VOCAL", nor
431 * may "VOCAL" appear in their name, without prior written
432 * permission of Vovida Networks, Inc.
433 *
434 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
435 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
436 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
437 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
438 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
439 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
440 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
441 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
442 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
443 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
444 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
445 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
446 * DAMAGE.
447 *
448 * ====================================================================
449 *
450 * This software consists of voluntary contributions made by Vovida
451 * Networks, Inc. and many individuals on behalf of Vovida Networks,
452 * Inc. For more information on Vovida Networks, Inc., please see
453 * <http://www.vovida.org/>.
454 *
455 */

Properties

Name Value
svn:eol-style native

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27