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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3551 - (hide annotations) (download)
Mon Nov 22 23:34:31 2004 UTC (15 years ago) by jason
Original Path: main/sip/resiprocate/dum/test/certServer.cxx
File size: 12013 byte(s)
addTlsTransport
1 jason 3530 #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 jason 3546 #include "resiprocate/NameAddr.hxx"
12     #include "resiprocate/Pkcs8Contents.hxx"
13 jason 3530 #include "resiprocate/SipMessage.hxx"
14     #include "resiprocate/Symbols.hxx"
15     #include "resiprocate/Uri.hxx"
16     #include "resiprocate/X509Contents.hxx"
17     #include "resiprocate/dum/AppDialogSet.hxx"
18     #include "resiprocate/dum/ClientAuthManager.hxx"
19     #include "resiprocate/dum/DialogUsageManager.hxx"
20 jason 3546 #include "resiprocate/dum/DumShutdownHandler.hxx"
21 jason 3530 #include "resiprocate/dum/OutOfDialogHandler.hxx"
22     #include "resiprocate/dum/Profile.hxx"
23 jason 3546 #include "resiprocate/dum/PublicationHandler.hxx"
24 jason 3530 #include "resiprocate/dum/RegistrationHandler.hxx"
25 jason 3546 #include "resiprocate/dum/ServerPublication.hxx"
26     #include "resiprocate/dum/ServerSubscription.hxx"
27 jason 3530 #include "resiprocate/dum/SubscriptionHandler.hxx"
28     #include "resiprocate/os/Log.hxx"
29     #include "resiprocate/os/Logger.hxx"
30     #include "resiprocate/os/Random.hxx"
31     #include "resiprocate/os/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 jason 3546 // 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 jason 3530 {
55     public:
56 jason 3546 CertPublicationHandler(Security& security) : mSecurity(security)
57 jason 3530 {
58     }
59    
60 jason 3546 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 jason 3530 {
67     add(h, contents);
68     }
69    
70     virtual void onExpired(ServerPublicationHandle h, const Data& etag)
71     {
72 jason 3546 mSecurity.removeUserCert(h->getPublisher());
73 jason 3530 }
74    
75 jason 3546 virtual void onRefresh(ServerPublicationHandle,
76     const Data& etag,
77     const SipMessage& pub,
78     const Contents* contents,
79     const SecurityAttributes* attrs,
80     int expires)
81 jason 3530 {
82     }
83    
84 jason 3546 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 jason 3530 {
91     add(h, contents);
92     }
93    
94 jason 3546 virtual void onRemoved(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
95 jason 3530 {
96 jason 3546 mSecurity.removeUserCert(h->getPublisher());
97 jason 3530 }
98     private:
99 jason 3546 void add(ServerPublicationHandle h, const Contents* contents)
100 jason 3530 {
101 jason 3550 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 jason 3530 }
113    
114     Security& mSecurity;
115     };
116    
117     class PrivateKeyPublicationHandler : public ServerPublicationHandler
118     {
119     public:
120     PrivateKeyPublicationHandler(Security& security) : mSecurity(security)
121     {
122     }
123    
124 jason 3546 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 jason 3530 {
131     add(h, contents);
132     }
133    
134     virtual void onExpired(ServerPublicationHandle h, const Data& etag)
135     {
136 jason 3546 mSecurity.removeUserPrivateKey(h->getPublisher());
137 jason 3530 }
138    
139 jason 3546 virtual void onRefresh(ServerPublicationHandle,
140     const Data& etag,
141     const SipMessage& pub,
142     const Contents* contents,
143     const SecurityAttributes* attrs,
144     int expires)
145 jason 3530 {
146     }
147    
148 jason 3546 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 jason 3530 {
155     add(h, contents);
156     }
157    
158 jason 3546 virtual void onRemoved(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
159 jason 3530 {
160 jason 3546 mSecurity.removeUserPrivateKey(h->getPublisher());
161 jason 3530 }
162    
163     private:
164 jason 3546 void add(ServerPublicationHandle h, const Contents* contents)
165 jason 3530 {
166 jason 3550 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 jason 3530 }
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 jason 3550 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(), Data::Empty);
195     }
196    
197 jason 3546 if (mSecurity.hasUserCert(h->getDocumentKey()))
198 jason 3530 {
199 jason 3546 X509Contents x509(mSecurity.getUserCertDER(h->getDocumentKey()));
200 jason 3530 h->send(h->update(&x509));
201     }
202     else
203     {
204     h->reject(404);
205     }
206     }
207    
208 jason 3546 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 jason 3530 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 jason 3546 virtual void onNewSubscription(ServerSubscriptionHandle h, const SipMessage& sub)
237 jason 3530 {
238 jason 3551 if (h->getDocumentKey() != h->getSubscriber())
239 jason 3530 {
240 jason 3550 h->send(h->accept(403)); // !jf! is this the correct code?
241     }
242     else if (mSecurity.hasUserCert(h->getDocumentKey()))
243     {
244 jason 3546 Pkcs8Contents pkcs(mSecurity.getUserPrivateKeyDER(h->getDocumentKey()));
245 jason 3530 h->send(h->update(&pkcs));
246     }
247     else
248     {
249     h->reject(404);
250     }
251     }
252    
253 jason 3546 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 jason 3530 virtual void onTerminated(ServerSubscriptionHandle)
262     {
263     }
264    
265     virtual void onError(ServerSubscriptionHandle, const SipMessage& msg)
266     {
267     }
268 jason 3550
269 jason 3530 private:
270     Security& mSecurity;
271     };
272    
273    
274    
275     class CertServer : public OutOfDialogHandler, public DialogUsageManager
276     {
277     public:
278     CertServer(const resip::NameAddr& me) :
279     DialogUsageManager(),
280 jason 3546 mCertServer(getSecurity()),
281     mPrivateKeyServer(getSecurity()),
282 jason 3530 mCertUpdater(getSecurity()),
283 jason 3546 mPrivateKeyUpdater(getSecurity())
284 jason 3530 {
285     addTransport(UDP, 5100);
286     addTransport(TCP, 5100);
287 jason 3551 addTransport(TLS, 5101, V4, Data::Empty, me.uri().host(), Data::Empty);
288 jason 3530
289     mProfile.clearSupportedMethods();
290     mProfile.addSupportedMethod(PUBLISH);
291     mProfile.addSupportedMethod(SUBSCRIBE);
292     mProfile.validateAcceptEnabled() = true;
293     mProfile.validateContentEnabled() = true;
294     mProfile.addSupportedMimeType(Pkcs8Contents::getStaticType());
295     mProfile.addSupportedMimeType(X509Contents::getStaticType());
296    
297     mProfile.setDefaultFrom(me);
298     setProfile(&mProfile);
299    
300     addServerSubscriptionHandler(Symbols::Credential, &mPrivateKeyServer);
301     addServerSubscriptionHandler(Symbols::Certificate, &mCertServer);
302     addServerPublicationHandler(Symbols::Credential, &mPrivateKeyUpdater);
303     addServerPublicationHandler(Symbols::Certificate, &mCertUpdater);
304     addOutOfDialogHandler(OPTIONS, this);
305    
306     //setServerAuthManager(std::auto_ptr<ServerAuthManager>(new ServerAuthManager(mProfile)));
307    
308     DialogUsageManager::run();
309     }
310    
311     ~CertServer()
312     {
313     }
314    
315     void run()
316     {
317     while ( !mDone )
318     {
319     while (process());
320     usleep(5);
321    
322     if (finished)
323     {
324     // graceful shutdown
325     exit(0);
326     }
327     }
328     }
329    
330     virtual void onSuccess(ClientOutOfDialogReqHandle, const SipMessage& successResponse)
331     {
332     }
333    
334     virtual void onFailure(ClientOutOfDialogReqHandle, const SipMessage& errorResponse)
335     {
336     }
337    
338     virtual void onReceivedRequest(ServerOutOfDialogReqHandle, const SipMessage& request)
339     {
340     }
341    
342     private:
343     Profile mProfile;
344 jason 3546 CertSubscriptionHandler mCertServer;
345     PrivateKeySubscriptionHandler mPrivateKeyServer;
346 jason 3530 CertPublicationHandler mCertUpdater;
347     PrivateKeyPublicationHandler mPrivateKeyUpdater;
348     bool mDone;
349     };
350    
351     int
352     main (int argc, char** argv)
353     {
354     char* logType = "COUT";
355     char* logLevel = "DEBUG";
356     char* myUrl = "sip:localhost:7001";
357     char* bindAddr = 0;
358     int v6 = 0;
359    
360     #if defined(HAVE_POPT_H)
361     struct poptOption table[] = {
362     {"log-type", 'l', POPT_ARG_STRING, &logType, 0, "where to send logging messages", "syslog|cerr|cout"},
363     {"log-level", 'v', POPT_ARG_STRING, &logLevel, 0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
364     {"name" , 'n', POPT_ARG_STRING, &myUrl, 0, "my url", 0},
365     {"bind", 'b', POPT_ARG_STRING, &bindAddr, 0, "interface address to bind to",0},
366     {"v6", '6', POPT_ARG_NONE, &v6 , 0, "ipv6", 0},
367     POPT_AUTOHELP
368     { NULL, 0, 0, NULL, 0 }
369     };
370     poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
371     poptGetNextOpt(context);
372     #endif
373     Log::initialize(logType, logLevel, argv[0]);
374    
375     #ifndef _WIN32
376     if ( signal( SIGPIPE, SIG_IGN) == SIG_ERR)
377     {
378     cerr << "Couldn't install signal handler for SIGPIPE" << endl;
379     exit(-1);
380     }
381    
382     if ( signal( SIGINT, signalHandler ) == SIG_ERR )
383     {
384     cerr << "Couldn't install signal handler for SIGINT" << endl;
385     exit( -1 );
386     }
387    
388     if ( signal( SIGTERM, signalHandler ) == SIG_ERR )
389     {
390     cerr << "Couldn't install signal handler for SIGTERM" << endl;
391     exit( -1 );
392     }
393     #endif
394    
395     NameAddr domain(myUrl);
396     CertServer server(domain);
397     server.run();
398     return 0;
399     }
400    

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27