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

Contents of /main/sip/resiprocate/dum/test/certServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3546 - (show annotations) (download)
Mon Nov 22 20:59:36 2004 UTC (15 years ago) by jason
File size: 11176 byte(s)
certServer compiles now

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

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27