sipxportlib  Version 3.3
OsSocketCrypto.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2008 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2008 SIPez LLC.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // Copyright (C) 2008 Mutualink, Inc.
9 // Licensed to SIPfoundry under a Contributor Agreement.
10 //
11 // $$
13 
14 // Author: Sergey Kostanbaev <Sergey DOT Kostanbaev AT sipez DOT com>
15 #ifdef HAVE_SSL
16 
17 #ifndef _OsSocketCrypto_h_
18 #define _OsSocketCrypto_h_
19 
20 // SYSTEM INCLUDES
21 #include <assert.h>
22 // APPLICATION INCLUDES
23 #include "os/OsSocket.h"
24 #include "utl/UtlCryptoData.h"
25 #include "utl/UtlCryptoKey.h"
26 
27 #include "os/OsMulticastSocket.h"
28 #include "os/OsDatagramSocket.h"
29 #include "os/OsNatDatagramSocket.h"
30 
31 // DEFINES
32 #define MAX_CRYPTOBUFFER 3000
33 
34 // MACROS
35 // EXTERNAL FUNCTIONS
36 // EXTERNAL VARIABLES
37 // STRUCTS
38 // TYPEDEFS
39 // FORWARD DECLARATIONS
40 
51 class OsSocketCryptoProxy
52 {
53 /* //////////////////////////////// PUBLIC //////////////////////////////// */
54 public:
55 
56 /* =============================== CREATORS =============================== */
58 
59 
61  OsSocketCryptoProxy(OsSocket* pureSocket,
62  int (OsSocket::*read1)(char*, int),
63  int (OsSocket::*read2)(char*, int, UtlString*, int*),
64  int (OsSocket::*read3)(char*, int, struct in_addr*, int*),
65  int (OsSocket::*read4)(char*, int, long),
66  int (OsSocket::*write1)(const char*, int),
67  int (OsSocket::*write2)(const char*, int, const char*, int),
68  int (OsSocket::*write3)(const char*, int, long),
69  const char* pEncBinData,
70  int encBinLength,
71  const char* pDecBinData,
72  int decBinLength);
73 
75  virtual ~OsSocketCryptoProxy();
76 
78 
79 /* ============================= MANIPULATORS ============================= */
81 
82 
84  int write(const char* buffer, int bufferLength);
85 
87  int write(const char* buffer, int bufferLength,
88  const char* ipAddress, int port);
89 
91  int write(const char* buffer, int bufferLength, long waitMilliseconds);
92 
93 
95  int read(char* buffer, int bufferLength);
96 
98  int read(char* buffer, int bufferLength,
99  UtlString* fromAddress, int* fromPort);
100 
102  int read(char* buffer, int bufferLength,
103  struct in_addr* ipAddress, int* port);
104 
106  int read(char* buffer, int bufferLength, long waitMilliseconds);
107 
108 
110 
111 /* ============================== ACCESSORS =============================== */
113 
114 
115 
117 
118 /* =============================== INQUIRY ================================ */
120 
121 
122 
124 
125 /* ////////////////////////////// PROTECTED /////////////////////////////// */
126 protected:
127 
128 
129 /* /////////////////////////////// PRIVATE //////////////////////////////// */
130 private:
131 
133  UtlBoolean setSymKeys(const char* pEncBinData,
134  int encBinLength,
135  const char* pDecBinData,
136  int decBinLength);
137 
138  UtlBoolean mDoPadding;
139  uint8_t mCryptoBuffer[MAX_CRYPTOBUFFER];
140  uint8_t mDecodeBuffer[MAX_CRYPTOBUFFER];
141 
142  UtlCryptoKey* mpRtpEncryptKey;
143  UtlCryptoKey* mpRtpDecryptKey;
144 
145  OsSocket* mPureSocket;
146 
148  UtlBoolean decode(char* buffer, int bufferLength, int originalLength, int& decodedLen);
149 
151  UtlBoolean encode(const char* buffer, int bufferLength, int& encodedLen);
152 
154  int (OsSocket::*mRead1)(char* buffer, int bufferLength);
155 
157  int (OsSocket::*mRead2)(char* buffer, int bufferLength,
158  UtlString* fromAddress, int* fromPort);
159 
161  int (OsSocket::*mRead3)(char* buffer, int bufferLength,
162  struct in_addr* ipAddress, int* port);
163 
165  int (OsSocket::*mRead4)(char* buffer, int bufferLength, long waitMilliseconds);
166 
168  int (OsSocket::*mWrite1)(const char* buffer, int bufferLength);
169 
171  int (OsSocket::*mWrite2)(const char* buffer, int bufferLength,
172  const char* ipAddress, int port);
173 
175  int (OsSocket::*mWrite3)(const char* buffer, int bufferLength,
176  long waitMilliseconds);
177 };
178 
179 /* ============================ INLINE METHODS ============================ */
180 
182 
183 class OsMulticastSocketCrypto : public OsMulticastSocket
184 {
185 public:
186  OsMulticastSocketCrypto(int multicastPortNum,
187  const char* multicastHost,
188  int localHostPortNum,
189  const char* localHost,
190  const char* pEncBinData,
191  int encBinLength,
192  const char* pDecBinData,
193  int decBinLength)
194  : OsMulticastSocket(multicastPortNum, multicastHost, localHostPortNum, localHost)
195  , mCryptoProxy(this,
196  (int (OsSocket::*)(char*, int))&OsMulticastSocketCrypto::readProxy1,
197  (int (OsSocket::*)(char*, int, UtlString*, int*))&OsMulticastSocketCrypto::readProxy2,
198  (int (OsSocket::*)(char*, int, struct in_addr*, int*))&OsMulticastSocketCrypto::readProxy3,
199  (int (OsSocket::*)(char*, int, long))&OsMulticastSocketCrypto::readProxy4,
200  (int (OsSocket::*)(const char*, int))&OsMulticastSocketCrypto::writeProxy1,
201  (int (OsSocket::*)(const char*, int, const char*, int))&OsMulticastSocketCrypto::writeProxy2,
202  NULL,
203  pEncBinData, encBinLength, pDecBinData, decBinLength)
204  {
205  };
206 
207  int write(const char* buffer, int bufferLength)
208  {
209  return mCryptoProxy.write(buffer, bufferLength);
210  }
211 
212  int write(const char* buffer, int bufferLength,
213  const char* ipAddress, int port)
214  {
215  return mCryptoProxy.write(buffer, bufferLength, ipAddress, port);
216  }
217 
218  int read(char* buffer, int bufferLength)
219  {
220  return mCryptoProxy.read(buffer, bufferLength);
221  }
222 
223  int read(char* buffer, int bufferLength,
224  UtlString* fromAddress, int* fromPort)
225  {
226  return mCryptoProxy.read(buffer, bufferLength, fromAddress, fromPort);
227  }
228 
229  int read(char* buffer, int bufferLength,
230  struct in_addr* ipAddress, int* port)
231  {
232  return mCryptoProxy.read(buffer, bufferLength, ipAddress, port);
233  }
234 
235  int read(char* buffer, int bufferLength,
236  long waitMilliseconds)
237  {
238  return mCryptoProxy.read(buffer, bufferLength, waitMilliseconds);
239  }
240 
241 protected:
242  int writeProxy1(const char* buffer, int bufferLength)
243  {
244  return OsMulticastSocket::write(buffer, bufferLength);
245  }
246 
247  int writeProxy2(const char* buffer, int bufferLength,
248  const char* ipAddress, int port)
249  {
250  return OsMulticastSocket::write(buffer, bufferLength, ipAddress, port);
251  }
252 
253 
254  int readProxy1(char* buffer, int bufferLength)
255  {
256  return OsMulticastSocket::read(buffer, bufferLength);
257  }
258 
259  int readProxy2(char* buffer, int bufferLength,
260  UtlString* fromAddress, int* fromPort)
261  {
262  return OsSocket::read(buffer, bufferLength, fromAddress, fromPort);
263  }
264 
265  int readProxy3(char* buffer, int bufferLength,
266  struct in_addr* ipAddress, int* port)
267  {
268  return OsSocket::read(buffer, bufferLength, ipAddress, port);
269  }
270 
271  int readProxy4(char* buffer, int bufferLength,
272  long waitMilliseconds)
273  {
274  return OsSocket::read(buffer, bufferLength, waitMilliseconds);
275  }
276 
277 private:
278  OsSocketCryptoProxy mCryptoProxy;
279 };
280 
282 
283 class OsNatDatagramSocketCrypto : public OsNatDatagramSocket
284 {
285 public:
286  OsNatDatagramSocketCrypto(int remoteHostPortNum,
287  const char* remoteHost,
288  int localHostPortNum,
289  const char* localHost,
290  OsNotification *pNotification,
291  const char* pEncBinData,
292  int encBinLength,
293  const char* pDecBinData,
294  int decBinLength)
295  : OsNatDatagramSocket(remoteHostPortNum, remoteHost, localHostPortNum, localHost, pNotification)
296  , mCryptoProxy(this,
297  (int (OsSocket::*)(char*, int))&OsNatDatagramSocketCrypto::readProxy1,
298  (int (OsSocket::*)(char*, int, UtlString*, int*))&OsNatDatagramSocketCrypto::readProxy2,
299  (int (OsSocket::*)(char*, int, struct in_addr*, int*))&OsNatDatagramSocketCrypto::readProxy3,
300  (int (OsSocket::*)(char*, int, long))&OsNatDatagramSocketCrypto::readProxy4,
301  (int (OsSocket::*)(const char*, int))&OsNatDatagramSocketCrypto::writeProxy1,
302  (int (OsSocket::*)(const char*, int, const char*, int))&OsNatDatagramSocketCrypto::writeProxy2,
303  (int (OsSocket::*)(const char*, int, long))&OsNatDatagramSocketCrypto::writeProxy3,
304  pEncBinData, encBinLength, pDecBinData, decBinLength)
305  {
306  };
307 
308  int write(const char* buffer, int bufferLength)
309  {
310  return mCryptoProxy.write(buffer, bufferLength);
311  }
312 
313  int write(const char* buffer, int bufferLength,
314  const char* ipAddress, int port)
315  {
316  return mCryptoProxy.write(buffer, bufferLength, ipAddress, port);
317  }
318 
319  int write(const char* buffer, int bufferLength, long waitMilliseconds)
320  {
321  return mCryptoProxy.write(buffer, bufferLength, waitMilliseconds);
322  }
323 
324  int read(char* buffer, int bufferLength)
325  {
326  return mCryptoProxy.read(buffer, bufferLength);
327  }
328 
329  int read(char* buffer, int bufferLength,
330  UtlString* fromAddress, int* fromPort)
331  {
332  return mCryptoProxy.read(buffer, bufferLength, fromAddress, fromPort);
333  }
334 
335  int read(char* buffer, int bufferLength,
336  struct in_addr* ipAddress, int* port)
337  {
338  return mCryptoProxy.read(buffer, bufferLength, ipAddress, port);
339  }
340 
341  int read(char* buffer, int bufferLength,
342  long waitMilliseconds)
343  {
344  return mCryptoProxy.read(buffer, bufferLength, waitMilliseconds);
345  }
346 
347 protected:
348  int writeProxy1(const char* buffer, int bufferLength)
349  {
350  return OsNatDatagramSocket::write(buffer, bufferLength);
351  }
352 
353  int writeProxy2(const char* buffer, int bufferLength,
354  const char* ipAddress, int port)
355  {
356  return OsNatDatagramSocket::write(buffer, bufferLength, ipAddress, port);
357  }
358 
359  int writeProxy3(const char* buffer, int bufferLength,
360  long waitMilliseconds)
361  {
362  return OsNatDatagramSocket::write(buffer, bufferLength, waitMilliseconds);
363  }
364 
365 
366  int readProxy1(char* buffer, int bufferLength)
367  {
368  return OsNatDatagramSocket::read(buffer, bufferLength);
369  }
370 
371  int readProxy2(char* buffer, int bufferLength,
372  UtlString* fromAddress, int* fromPort)
373  {
374  return OsNatDatagramSocket::read(buffer, bufferLength, fromAddress, fromPort);
375  }
376 
377  int readProxy3(char* buffer, int bufferLength,
378  struct in_addr* ipAddress, int* port)
379  {
380  return OsNatDatagramSocket::read(buffer, bufferLength, ipAddress, port);
381  }
382 
383  int readProxy4(char* buffer, int bufferLength,
384  long waitMilliseconds)
385  {
386  return OsNatDatagramSocket::read(buffer, bufferLength, waitMilliseconds);
387  }
388 private:
389  OsSocketCryptoProxy mCryptoProxy;
390 };
391 
392 #endif // _OsSocketCrypto_h_
393 
394 #endif
Definition: OsSocket.h:79
A generic cryptography key base class.
Definition: UtlCryptoKey.h:36
int read(int fd, void *buffer, unsigned int count)
unsigned char uint8_t
Definition: stdint.h:78
Definition: OsNotification.h:35
virtual int read(char *buffer, int bufferLength)
Definition: OsNatDatagramSocket.cpp:107
#define NULL
Definition: UtlDefs.h:29
virtual int read(char *buffer, int bufferLength)
param: waitMilliseconds - The maximum number of milliseconds to block. This may be set to zero...
Definition: OsSocket.cpp:235
virtual int write(const char *buffer, int bufferLength)
param: remoteHostName - remote host to send datagram(s) in subsequent calls to write (overloaded vers...
Definition: OsDatagramSocket.cpp:328
Definition: OsNatDatagramSocket.h:100
Definition: UtlString.h:48
int write(int fd, const void *buffer, unsigned int count)
virtual int read(char *buffer, int bufferLength)
param: buffer - the bytes to be written to the socket param: bufferLength - the number of bytes conta...
Definition: OsDatagramSocket.cpp:499
Definition: OsMulticastSocket.h:33
int UtlBoolean
Definition: UtlDefs.h:41
virtual int write(const char *buffer, int bufferLength)
Definition: OsNatDatagramSocket.cpp:239