/[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 3530 - (hide annotations) (download)
Mon Nov 22 06:24:14 2004 UTC (14 years, 10 months ago) by jason
Original Path: main/sip/resiprocate/dum/test/certServer.cxx
File size: 9055 byte(s)


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     #include "resiprocate/SipMessage.hxx"
12     #include "resiprocate/Symbols.hxx"
13     #include "resiprocate/Uri.hxx"
14     #include "resiprocate/NameAddr.hxx"
15     #include "resiprocate/Pkcs8Contents.hxx"
16     #include "resiprocate/X509Contents.hxx"
17     #include "resiprocate/dum/AppDialogSet.hxx"
18     #include "resiprocate/dum/ServerSubscription.hxx"
19     #include "resiprocate/dum/ServerPublication.hxx"
20     #include "resiprocate/dum/ClientAuthManager.hxx"
21     #include "resiprocate/dum/DialogUsageManager.hxx"
22     #include "resiprocate/dum/OutOfDialogHandler.hxx"
23     #include "resiprocate/dum/Profile.hxx"
24     #include "resiprocate/dum/RegistrationHandler.hxx"
25     #include "resiprocate/dum/SubscriptionHandler.hxx"
26     #include "resiprocate/dum/PublicationHandler.hxx"
27     #include "resiprocate/dum/DumShutdownHandler.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     class PublicationHandler : public ServerPublicationHandler
48     {
49     public:
50     PublicationHandler(Security& security) : mSecurity(security)
51     {
52     }
53    
54     virtual void onInitial(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
55     {
56     Contents* contents=0;
57     add(h, contents);
58     }
59    
60     virtual void onExpired(ServerPublicationHandle h, const Data& etag)
61     {
62     removeUserCertDER(h->getPublisher());
63     }
64    
65     virtual void onRefresh(ServerPublicationHandle, const Data& etag, const SipMessage& pub, int expires)
66     {
67     }
68    
69     virtual void onUpdate(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
70     {
71     Contents* contents=0;
72     add(h, contents);
73     }
74    
75     virtual void onRemoved(ServerPublicationHandle, const Data& etag, const SipMessage& pub, int expires)
76     {
77     removeUserCertDER(h->getPublisher());
78     }
79     private:
80     void add(ServerPublicationHandle h, Contents* contents)
81     {
82     X509Contents* x509 = dynamic_cast<X509Contents*>(contents);
83     assert(x509);
84     addUserCertDER(h->getPublisher(), x509->getBodyData());
85     }
86    
87     Security& mSecurity;
88     };
89    
90     class PrivateKeyPublicationHandler : public ServerPublicationHandler
91     {
92     public:
93     PrivateKeyPublicationHandler(Security& security) : mSecurity(security)
94     {
95     }
96    
97     virtual void onInitial(ServerPublicationHandle h, const Data& etag, const SipMessage& pub, int expires)
98     {
99     Contents* contents=0;
100     add(h, contents);
101     }
102    
103     virtual void onExpired(ServerPublicationHandle h, const Data& etag)
104     {
105     removeUserPrivateKeyDER(h->getPublisher());
106     }
107    
108     virtual void onRefresh(ServerPublicationHandle, const Data& etag, const SipMessage& pub, int expires)
109     {
110     }
111    
112     virtual void onUpdate(ServerPublicationHandle, const Data& etag, const SipMessage& pub, int expires)
113     {
114     Contents* contents=0;
115     add(h, contents);
116     }
117    
118     virtual void onRemoved(ServerPublicationHandle, const Data& etag, const SipMessage& pub, int expires)
119     {
120     removeUserPrivateKeyDER(h->getPublisher());
121     }
122    
123     private:
124     void add(ServerPublicationHandle h, Contents* contents)
125     {
126     Pkcs8Contents* pkcs8 = dynamic_cast<Pkcs8Contents*>(contents);
127     assert(pkcs8);
128     addUserPrivateKeyDER(h->getPublisher(), pkcs8->getBodyData())
129     }
130    
131     Security& mSecurity;
132     };
133    
134     class CertSubscriptionHandler : public ServerSubscriptionHandler
135     {
136     public:
137     CertSubscriptionHandler(Security& security) : mSecurity(security)
138     {
139     }
140    
141     virtual void onNewSubscription(ServerSubscriptionHandle h, const SipMessage& sub)
142     {
143     if (mSecurity.hasUserCert(h->getPublisher()))
144     {
145     X509Contents x509(mSecurity.getUserCertDER(h->getPublisher()));
146     h->send(h->update(&x509));
147     }
148     else
149     {
150     h->reject(404);
151     }
152     }
153    
154     virtual void onTerminated(ServerSubscriptionHandle)
155     {
156     }
157    
158     virtual void onError(ServerSubscriptionHandle, const SipMessage& msg)
159     {
160     }
161    
162     private:
163     Security& mSecurity;
164     };
165    
166     class PrivateKeySubscriptionHandler : public ServerSubscriptionHandler
167     {
168     public:
169     PrivateKeySubscriptionHandler(Security& security) : mSecurity(security)
170     {
171     }
172    
173     virtual void onNewSubscription(ServerSubscriptionHandle, const SipMessage& sub)
174     {
175     if (mSecurity.hasUserCert(h->getPublisher()))
176     {
177     Pkcs8Contents pkcs(mSecurity.getUserPrivateKeyDER(h->getPublisher()));
178     h->send(h->update(&pkcs));
179     }
180     else
181     {
182     h->reject(404);
183     }
184     }
185    
186     virtual void onTerminated(ServerSubscriptionHandle)
187     {
188     }
189    
190     virtual void onError(ServerSubscriptionHandle, const SipMessage& msg)
191     {
192     }
193     private:
194     Security& mSecurity;
195     };
196    
197    
198    
199     class CertServer : public OutOfDialogHandler, public DialogUsageManager
200     {
201     public:
202     CertServer(const resip::NameAddr& me) :
203     DialogUsageManager(),
204     mCertUpdater(getSecurity()),
205     mPrivateKeyUpdater(getSecurity()),
206     mCertServer(getSecurity()),
207     mPrivateKeyServer(getSecurity())
208     {
209     addTransport(UDP, 5100);
210     addTransport(TCP, 5100);
211     // addTlsTransport
212    
213     mProfile.clearSupportedMethods();
214     mProfile.addSupportedMethod(PUBLISH);
215     mProfile.addSupportedMethod(SUBSCRIBE);
216     mProfile.validateAcceptEnabled() = true;
217     mProfile.validateContentEnabled() = true;
218     mProfile.addSupportedMimeType(Pkcs8Contents::getStaticType());
219     mProfile.addSupportedMimeType(X509Contents::getStaticType());
220    
221     mProfile.setDefaultFrom(me);
222     setProfile(&mProfile);
223    
224     addServerSubscriptionHandler(Symbols::Credential, &mPrivateKeyServer);
225     addServerSubscriptionHandler(Symbols::Certificate, &mCertServer);
226     addServerPublicationHandler(Symbols::Credential, &mPrivateKeyUpdater);
227     addServerPublicationHandler(Symbols::Certificate, &mCertUpdater);
228     addOutOfDialogHandler(OPTIONS, this);
229    
230     //setServerAuthManager(std::auto_ptr<ServerAuthManager>(new ServerAuthManager(mProfile)));
231    
232     DialogUsageManager::run();
233     }
234    
235     ~CertServer()
236     {
237     }
238    
239     void run()
240     {
241     while ( !mDone )
242     {
243     while (process());
244     usleep(5);
245    
246     if (finished)
247     {
248     // graceful shutdown
249     exit(0);
250     }
251     }
252     }
253    
254     virtual void onSuccess(ClientOutOfDialogReqHandle, const SipMessage& successResponse)
255     {
256     }
257    
258     virtual void onFailure(ClientOutOfDialogReqHandle, const SipMessage& errorResponse)
259     {
260     }
261    
262     virtual void onReceivedRequest(ServerOutOfDialogReqHandle, const SipMessage& request)
263     {
264     }
265    
266     private:
267     Profile mProfile;
268     CertPublicationHandler mCertUpdater;
269     PrivateKeyPublicationHandler mPrivateKeyUpdater;
270     CertSubscriptionHandler mCertServer;
271     PrivateKeySubscriptionHandler mPrivateKeyServer;
272     bool mDone;
273     };
274    
275     int
276     main (int argc, char** argv)
277     {
278     char* logType = "COUT";
279     char* logLevel = "DEBUG";
280     char* myUrl = "sip:localhost:7001";
281     char* bindAddr = 0;
282     int v6 = 0;
283    
284     #if defined(HAVE_POPT_H)
285     struct poptOption table[] = {
286     {"log-type", 'l', POPT_ARG_STRING, &logType, 0, "where to send logging messages", "syslog|cerr|cout"},
287     {"log-level", 'v', POPT_ARG_STRING, &logLevel, 0, "specify the default log level", "DEBUG|INFO|WARNING|ALERT"},
288     {"name" , 'n', POPT_ARG_STRING, &myUrl, 0, "my url", 0},
289     {"bind", 'b', POPT_ARG_STRING, &bindAddr, 0, "interface address to bind to",0},
290     {"v6", '6', POPT_ARG_NONE, &v6 , 0, "ipv6", 0},
291     POPT_AUTOHELP
292     { NULL, 0, 0, NULL, 0 }
293     };
294     poptContext context = poptGetContext(NULL, argc, const_cast<const char**>(argv), table, 0);
295     poptGetNextOpt(context);
296     #endif
297     Log::initialize(logType, logLevel, argv[0]);
298    
299     #ifndef _WIN32
300     if ( signal( SIGPIPE, SIG_IGN) == SIG_ERR)
301     {
302     cerr << "Couldn't install signal handler for SIGPIPE" << endl;
303     exit(-1);
304     }
305    
306     if ( signal( SIGINT, signalHandler ) == SIG_ERR )
307     {
308     cerr << "Couldn't install signal handler for SIGINT" << endl;
309     exit( -1 );
310     }
311    
312     if ( signal( SIGTERM, signalHandler ) == SIG_ERR )
313     {
314     cerr << "Couldn't install signal handler for SIGTERM" << endl;
315     exit( -1 );
316     }
317     #endif
318    
319     NameAddr domain(myUrl);
320     CertServer server(domain);
321     server.run();
322     return 0;
323     }
324    

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.26