blob: 7a1ee67d464de8d7e6e4ba30b78d4f672aa1edc8 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
2 Copyright (C) 2006-2009 Werner Dittmann
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef _ZRTPQUEUE_H_
19#define _ZRTPQUEUE_H_
20
21#include <ccrtp/cqueue.h>
22#include <ccrtp/rtppkt.h>
23#include <libzrtpcpp/ZrtpCallback.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050024#include <libzrtpcpp/ZrtpConfigure.h>
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050025#include <CcrtpTimeoutProvider.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050026
27class __EXPORT ZrtpUserCallback;
28class __EXPORT ZRtp;
29
30NAMESPACE_COMMONCPP
31
32/**
33 * GNU ccRTP extension to support GNU ZRTP.
34 *
35 * ZRTP was developed by Phil Zimmermann and provides functions to
36 * negotiate keys and other necessary data (crypto data) to set-up
37 * the Secure RTP (SRTP) crypto context. Refer to Phil's ZRTP
38 * specification at his <a href="http://zfoneproject.com/">Zfone
39 * project</a> site to get more detailed imformation about the
40 * capabilities of ZRTP.
41 *
42 * <b>Short overview of the ZRTP implementation</b>
43 *
44 * ZRTP is a specific protocol to negotiate encryption algorithms
45 * and the required key material. ZRTP uses a RTP session to
46 * exchange its protocol messages.
47 *
48 * A complete GNU ZRTP implementation consists of two parts, the
49 * GNU ZRTP core and specific code that binds the GNU ZRTP core to
50 * the underlying RTP/SRTP stack and the operating system:
51 * <ul>
52 * <li>
53 * The GNU ZRTP core is independent of a specific RTP/SRTP
54 * stack and the operationg system and consists of the ZRTP
55 * protocol state engine, the ZRTP protocol messages, and the
56 * GNU ZRTP engine. The GNU ZRTP engine provides methods to
57 * setup ZRTP message and to analyze received ZRTP messages,
58 * to compute the crypto data required for SRTP, and to
59 * maintain the required hashes and HMAC.
60 * </li>
61 * <li>
62 * The second part of an implementation is specific
63 * <em>glue</em> code the binds the GNU ZRTP core to the
64 * actual RTP/SRTP implementation and other operating system
65 * specific services such as timers.
66 * </li>
67 * </ul>
68 *
69 * The GNU ZRTP core uses a callback interface class (refer to
70 * ZrtpCallback) to access RTP/SRTP or operating specific methods,
71 * for example to send data via the RTP/SRTP stack, to access
72 * timers, provide mutex handling, and to report events to the
73 * application.
74 *
75 * <b>The ZrtpQueue</b>
76 *
77 * ZrtpQueue implements code that is specific to the GNU ccRTP
78 * implementation. ZrtpQueue also implements the specific code to
79 * provide the mutex and timeout handling to the GNU ZRTP
80 * core. Both, the mutex and the timeout handling, use the GNU
81 * Common C++ library to stay independent of the operating
82 * seystem. For more information refer to the <a
83 * href="http://www.gnutelephony.org/index.php/GNU_Common_C%2B%2B">GNU
84 * Common C++</a> web site.
85 *
86 * To perform its tasks ZrtpQueue
87 * <ul>
88 * <li> extends GNU ccRTP classes to use the underlying
89 * ccRTP methods and the RTP/SRTP send and receive queues
90 * </li>
91 * <li> implements the ZrtpCallback interface to provide ccRTP
92 * access and other specific services (timer, mutex) to GNU
93 * ZRTP
94 * </li>
95 * <li> provides ZRTP specific methods that applications may use
96 * to control and setup GNU ZRTP
97 * </li>
98 * <li> can register and use an application specific callback
99 * class (refer to ZrtpUserCallback)
100 * </li>
101 * </ul>
102 *
103 * After instantiating a GNU ZRTP session (see below for a short
104 * example) applications may use the ZRTP specific methods of
105 * ZrtpQueue to control and setup GNU ZRTP, for example enable or
106 * disable ZRTP processing or getting ZRTP status information.
107 *
108 * GNU ZRTP provides a ZrtpUserCallback class that an application
109 * may extend and register with ZrtpQueue. GNU ZRTP and ZrtpQueue
110 * use the ZrtpUserCallback methods to report ZRTP events to the
111 * application. The application may display this information to
112 * the user or act otherwise.
113 *
114 * The following figure depicts the relationships between
115 * ZrtpQueue, ccRTP RTP/SRTP implementation, the GNU ZRTP core,
116 * and an application that provides an ZrtpUserCallback class.
117 *
118@verbatim
119
120 +----------+
121 | ccRTP |
122 | RTP/SRTP |
123 | |
124 +----------+
125 ^
126 | extends
127 |
128+----------------+ +-----+------+
129| Application | | | +-----------------+
130| instantiates | uses | ZrtpQueue | uses | |
131| a ZRTP Session +------+ implements +------+ GNU ZRTP |
132| and provides | |ZrtpCallback| | core |
133|ZrtpUserCallback| | | | implementation |
134+----------------+ +------------+ | (ZRtp et al) |
135 | |
136 +-----------------+
137@endverbatim
138 *
139 * Because ZrtpQueue extends the ccRTP RTP/SRTP implementation
140 * (AVPQueue) all public methods defined by ccRTP are also
141 * available for a ZRTP session. ZrtpQueue overwrites some of the
142 * public methods of ccRTP (AVPQueue) to implement ZRTP specific
143 * code.
144 *
145 * GNU ZRTP provides a <em>SymmetricZRTPSession</em> type to
146 * simplify its use. An application uses this type in the same way
147 * as it would use the normal ccRTP <em>SymmetricRTPSession</em>
148 * type. The following short code snippets show how an application
149 * could instantiate ccRTP and GNU ZRTP sessions. The first
150 * snippet shows how to instantiate a ccRTP session:
151 *
152 * @code
153 * ...
154 * #include <ccrtp/rtp.h>
155 * ...
156 * SymmetricRTPSession tx(pattern.getSsrc(),
157 * InetHostAddress("localhost"));
158 * ...
159 *
160 * @endcode
161 *
162 * The same code as above but using a GNU ZRTP session this time:
163 * @code
164 * ...
165 * #include <libzrtpcpp/zrtpccrtp.h>
166 * ...
167 * SymmetricZRTPSession tx(pattern.getSsrc(),
168 * InetHostAddress("localhost"));
169 * ...
170 *
171 * @endcode
172 *
173 * The only differences are the different include statements and
174 * the different session types.
175 *
176 * The <em>demo</em> folder contains a small example that shows
177 * how to use GNU ZRTP.
178 *
179 * Please refer to the GNU ccRTP documentation for a description
180 * of ccRTP methods and functions. This ZrtpQueue documentation
181 * shows the ZRTP specific extensions and describes overloaded
182 * methods and a possible different behaviour.
183 *
184 * @author Werner Dittmann <Werner.Dittmann@t-online.de>
185 */
186
187class __EXPORT ZrtpQueue : public AVPQueue, ZrtpCallback {
188
189public:
190
191 /**
192 * Initialize the ZrtpQueue.
193 *
194 * Before an application can use ZRTP it has to initialize the
195 * ZRTP implementation. This method initializes the timeout
196 * thread and opens a file that contains ZRTP specific
197 * information such as the applications ZID (ZRTP id) and its
198 * retained shared secrets.
199 *
200 * If one application requires several ZRTP sessions all
201 * sessions use the same timeout thread and use the same ZID
202 * file. Therefore an application does not need to do any
203 * synchronisation regading ZID files or timeouts. This is
204 * managed by the ZRTP implementation.
205 *
206 * The current implementation of ZrtpQueue does not support
207 * different ZID files for one application instance. This
208 * restriction may be removed in later versions.
209 *
210 * The application may specify its own ZID file name. If no
211 * ZID file name is specified it defaults to
212 * <code>$HOME/.GNUccRTP.zid</code> if the <code>HOME</code>
213 * environment variable is set. If it is not set the current
214 * directory is used.
215 *
216 * If the method could set up the timeout thread and open the ZID
217 * file then it enables ZRTP processing and returns.
218 *
219 * @param zidFilename
220 * The name of the ZID file, can be a relative or absolut
221 * filename.
222 *
223 * @param autoEnable
224 * if set to true the method automatically sets enableZrtp to
225 * true. This enables the ZRTP auto-sense mode. Default is true.
226 *
227 * @param config
228 * this parameter points to ZRTP configuration data. If it is
229 * NULL then ZrtpQueue uses a default setting. Default is NULL.
230 *
231 * @return
232 * 1 on success, ZRTP processing enabled, -1 on failure,
233 * ZRTP processing disabled.
234 *
235 */
236 int32_t initialize(const char *zidFilename, bool autoEnable = true,
237 ZrtpConfigure* config = NULL);
238
239 /*
240 * Applications use the following methods to control ZRTP, for example
241 * to enable ZRTP, set flags etc.
242 */
243
244 /**
245 * Enable or disable ZRTP processing.
246 *
247 * Call this method to enable or disable ZRTP processing after
248 * calling <code>initialize()</code>. This can be done before
249 * using a RTP session or at any time during a RTP session.
250 *
251 * Existing SRTP sessions or currently active ZRTP processing will
252 * not be stopped or disconnected.
253 *
254 * If the application enables ZRTP then:
255 * <ul>
256 * <li>ZrtpQueue starts to send ZRTP Hello packets after at least
257 * one RTP packet was sent and received on the associated RTP
258 * session. Thus if an application enables ZRTP and ZrtpQueue
259 * detects traffic on the RTP session then ZrtpQueue automatically
260 * starts the ZRTP protocol. This automatic start is convenient
261 * for applications that negotiate RTP parameters and set up RTP
262 * sessions but the actual RTP traffic starts some time later.
263 * </li>
264 * <li>ZrtpQueue analyses incoming packets to detect ZRTP
265 * messages. If ZRTP was started, either via automatic start (see
266 * above) or explicitly via startZrtp(), then ZrtpQueue
267 * forwards ZRTP packets to the GNU ZRTP core.
268 * </ul>
269 *
270 * @param onOff
271 * @c true to enable ZRTP, @c false to disable ZRTP
272 */
273 void setEnableZrtp(bool onOff);
274
275 /**
276 * Return the state of ZRTP enable state.
277 *
278 * @return @c true if ZRTP processing is enabled, @c false
279 * otherwise.
280 */
281 bool isEnableZrtp();
282
283 /**
284 * Set SAS as verified.
285 *
286 * The application may call this method if the user confirmed
287 * (verfied) the Short Authentication String (SAS) with the peer.
288 *
289 * ZRTP calls ZrtpUserCallback#showSAS after it computed the SAS
290 * and the application registered a user callback class. The
291 * application should display the SAS and provide a mechanism at
292 * the user interface that enables the user to confirm the SAS.
293 *
294 * ZRTP remembers the SAS confirmation status together with the
295 * retained secrets data. If both parties confirmed the SAS then
296 * ZRTP informs the application about this status on the next ZRTP
297 * session.
298 *
299 * For more detailed information regarding SAS please refer to the
300 * ZRTP specification, chapter 8.
301 */
302 void SASVerified();
303
304 /**
305 * Reset the SAS verfied flag for the current user's retained secrets.
306 *
307 */
308 void resetSASVerified();
309
310 /**
311 * To confirm a go clear request.
312 *
313 * Call this method if the user confirmed a go clear (secure mode off).
314 */
315 void goClearOk();
316
317 /**
318 * Request to switch off secure mode.
319 *
320 * Call this method is the user itself wants to switch off secure
321 * mode (go clear). After sending the "go clear" request to the peer
322 * ZRTP immediatly switch off SRTP processing. Every RTP data is sent
323 * in clear after the go clear request.
324 */
325 void requestGoClear();
326
327 /**
328 * Set the auxilliary secret.
329 *
330 * Use this method to set the srtps secret data. Refer to ZRTP
331 * specification, chapter 5.3 ff
332 *
333 * @param data
334 * Points to the auxilliary secret data.
335 * @param length
336 * Length of the auxilliary secrect in bytes
337 */
338 void setAuxSecret(uint8_t* data, int32_t length);
339
340 /**
341 * Set the application's callback class.
342 *
343 * The destructor of ZrtpQueue also destorys the user callback
344 * class if it was set. The application must not delete the
345 * callback object or use/reference the callback object after
346 * ZrtpQueue was destroyed.
347 *
348 * @param ucb
349 * Implementation of the application's ZrtpUserCallback class
350 */
351 void setUserCallback(ZrtpUserCallback* ucb);
352
353 /**
354 * Set the client ID for ZRTP Hello message.
355 *
356 * The GNU ccRTP client may set its id to identify itself in the
357 * ZRTP Hello message. The maximum length is 16 characters. A
358 * shorter id string is possible, it will be filled with blanks. A
359 * longer id string will be truncated to 16 characters. The
360 * standard client id is <code>'GNU ccRTP ZRTP '</code> (without
361 * the quotes).
362 *
363 * Setting the client's id must be done before calling
364 * ZrtpQueue#initialize() or ZrtpQueue#startZrtp() .
365 *
366 * @param id
367 * The client's id string
368 */
369 void setClientId(std::string id);
370
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500371 /**
Alexandre Lision51140e12013-12-02 10:54:09 -0500372 * Get the ZRTP Hello Hash data.
373 *
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500374 * Use this method to get the ZRTP Hello Hash data. The method
375 * returns the data as a string containing hex-digits. Refer
376 * to ZRTP specification, chapter 9.1.
Alexandre Lision51140e12013-12-02 10:54:09 -0500377 *
378 * @return
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500379 * a std:string containing the Hello hash value as hex-digits. The
380 * hello hash is available immediatly after calling
381 * ZrtpQueue#startZrtp. If ZRTP was not started the method returns
382 * an empty string.
Alexandre Lision51140e12013-12-02 10:54:09 -0500383 */
Alexandre Lisionddd731e2014-01-31 11:50:08 -0500384 std::string getHelloHash();
Alexandre Lision51140e12013-12-02 10:54:09 -0500385
386 /**
387 * Get the peer's ZRTP Hello Hash data.
388 *
389 * Use this method to get the peer's ZRTP Hello Hash data. The method
390 * returns the data as a string containing the ZRTP protocol version and
391 * hex-digits.
392 *
393 * The peer's hello hash is available only after ZRTP received a hello. If
394 * no data is available the function returns an empty string.
395 *
396 * Refer to ZRTP specification, chapter 8.
397 *
398 * @return
399 * a std:string containing the Hello version and the hello hash as hex digits.
400 */
401 std::string getPeerHelloHash();
402
403 /**
404 * Get Multi-stream parameters.
405 *
406 * Use this method to get the Multi-stream that were computed during
407 * the ZRTP handshake. An application may use these parameters to
408 * enable multi-stream processing for an associated SRTP session.
409 *
410 * Refer to chapter 5.4.2 in the ZRTP specification for further details
411 * and restriction how and when to use multi-stream mode.
412 *
413 * @return
414 * a string that contains the multi-stream parameters. The application
415 * must not modify the contents of this string, it is opaque data. The
416 * application may hand over this string to a new ZrtpQueue instance
417 * to enable multi-stream processing for this ZrtpQueue. If ZRTP was
418 * not started or ZRTP is not yet in secure state the method returns an
419 * empty string.
420 *
421 * @see setMultiStrParams()
422 */
423 std::string getMultiStrParams();
424
425 /**
426 * Set Multi-stream parameters.
427 *
428 * Use this method to set the parameters required to enable Multi-stream
429 * processing of ZRTP. The multi-stream parameters must be set before the
430 * application starts the ZRTP protocol engine.
431 *
432 * Refer to chapter 5.4.2 in the ZRTP specification for further details
433 * of multi-stream mode.
434 *
435 * @param parameters
436 * A string that contains the multi-stream parameters that this
437 * new ZrtpQueue instanace shall use.
438 *
439 * @see getMultiStrParams()
440 */
441 void setMultiStrParams(std::string parameters);
442
443 /**
444 * Check if this ZRTP use Multi-stream.
445 *
446 * Use this method to check if this ZRTP instance uses multi-stream. Even
447 * if the application provided multi-stram parameters it may happen that
448 * full DH mode was used. Refer to chapters 5.2 and 5.4.2 in the ZRTP #
449 * when this may happen.
450 *
451 * @return
452 * True if multi-stream is used, false otherwise.
453 */
454 bool isMultiStream();
455
456 /**
457 * Check if the other ZRTP client supports Multi-stream.
458 *
459 * Use this method to check if the other ZRTP client supports
460 * Multi-stream mode.
461 *
462 * @return
463 * True if multi-stream is available, false otherwise.
464 */
465 bool isMultiStreamAvailable();
466
467 /**
468 * Accept a PBX enrollment request.
469 *
470 * If a PBX service asks to enroll the MiTM key and the user accepts this
471 * requtes, for example by pressing an OK button, the client application
472 * shall call this method and set the parameter <code>accepted</code> to
473 * true. If the user does not accept the request set the parameter to
474 * false.
475 *
476 * @param accepted
477 * True if the enrollment request is accepted, false otherwise.
478 */
479 void acceptEnrollment(bool accepted);
480
481 /**
482 * Get the commited SAS rendering algorithm for this ZRTP session.
483 *
484 * @return the commited SAS rendering algorithm
485 */
486 std::string getSasType();
487
488 /**
489 * Get the computed SAS hash for this ZRTP session.
490 *
491 * A PBX ZRTP back-to-Back function uses this function to get the SAS
492 * hash of an enrolled client to construct the SAS relay packet for
493 * the other client.
494 *
495 * @return a refernce to the byte array that contains the full
496 * SAS hash.
497 */
498 uint8_t* getSasHash();
499
500 /**
501 * Send the SAS relay packet.
502 *
503 * The method creates and sends a SAS relay packet according to the ZRTP
504 * specifications. Usually only a MitM capable user agent (PBX) uses this
505 * function.
506 *
507 * @param sh the full SAS hash value
508 * @param render the SAS rendering algorithm
509 */
510 bool sendSASRelayPacket(uint8_t* sh, std::string render);
511
512 /**
513 * Check the state of the MitM mode flag.
514 *
515 * If true then this ZRTP session acts as MitM, usually enabled by a PBX
516 * client (user agent)
517 *
518 * @return state of mitmMode
519 */
520 bool isMitmMode();
521
522 /**
523 * Set the state of the MitM mode flag.
524 *
525 * If MitM mode is set to true this ZRTP session acts as MitM, usually
526 * enabled by a PBX client (user agent).
527 *
528 * @param mitmMode defines the new state of the mitmMode flag
529 */
530 void setMitmMode(bool mitmMode);
531
532 /**
533 * Enable or disable paranoid mode.
534 *
535 * The Paranoid mode controls the behaviour and handling of the SAS verify flag. If
536 * Panaoid mode is set to flase then ZRtp applies the normal handling. If Paranoid
537 * mode is set to true then the handling is:
538 *
539 * <ul>
540 * <li> always set the SAS verify flag to <code>false</code> at srtpSecretsOn() callback. The
541 * user interface (UI) must show <b>SAS not verified</b>. See implementation note below.</li>
542 * <li> don't set the SAS verify flag in the <code>Confirm</code> packets, thus forcing the other
543 * peer to report <b>SAS not verified</b>.</li>
544 * <li> ignore the <code>SASVerified()</code> function, thus do not set the SAS verified flag
545 * in the ZRTP cache. </li>
546 * <li> Disable the <em>Trusted PBX MitM</em> feature. Just send the <code>SASRelay</code> packet
547 * but do not process the relayed data. This protects the user from a malicious
548 * "trusted PBX".</li>
549 * </ul>
550 * ZRtp performs alls other steps during the ZRTP negotiations as usual, in particular it
551 * computes, compares, uses, and stores the retained secrets. This avoids unnecessary warning
552 * messages. The user may enable or disable the Paranoid mode on a call-by-call basis without
553 * breaking the key continuity data.
554 *
555 * <b>Implementation note:</b><br/>
556 * An application shall <b>always display the SAS if the SAS verify flag is <code>false</code></b>.
557 * The application shall remind the user to compare the SAS code, for example using larger fonts,
558 * different colours and other display features.
559 */
560 void setParanoidMode(bool yesNo);
561
562 /**
563 * Check status of paranoid mode.
564 *
565 * @return
566 * Returns true if paranoid mode is enabled.
567 */
568 bool isParanoidMode();
569
570 /**
571 * Check the state of the enrollment mode.
572 *
573 * If true then we will set the enrollment flag (E) in the confirm
574 * packets and performs the enrollment actions. A MitM (PBX) enrollment service sets this flagstarted this ZRTP
575 * session. Can be set to true only if mitmMode is also true.
576 * @return status of the enrollmentMode flag.
577 */
578 bool isEnrollmentMode();
579
580 /**
581 * Check the state of the enrollment mode.
582 *
583 * If true then we will set the enrollment flag (E) in the confirm
584 * packets and perform the enrollment actions. A MitM (PBX) enrollment
585 * service must sets this mode to true.
586 *
587 * Can be set to true only if mitmMode is also true.
588 *
589 * @param enrollmentMode defines the new state of the enrollmentMode flag
590 */
591 void setEnrollmentMode(bool enrollmentMode);
592
593 /**
Alexandre Lision51140e12013-12-02 10:54:09 -0500594 * Check if a peer's cache entry has a vaild MitM key.
595 *
596 * If true then the other peer ha a valid MtiM key, i.e. the peer has performed
597 * the enrollment procedure. A PBX ZRTP Back-2-Back application can use this function
598 * to check which of the peers is enrolled.
599 *
600 * @return True if the other peer has a valid Mitm key (is enrolled).
601 */
602 bool isPeerEnrolled();
603
604 /**
605 * Set the state of the SAS signature mode flag.
606 *
607 * If SAS signature mode is set to true this ZRTP session support SAS signature
608 * callbacks and signature transfer between clients.
609 *
610 * @param sasSignMode defines the new state of the sasSignMode flag
611 */
612 void setSignSas(bool sasSignMode);
613
614 /**
615 * Set signature data
616 *
617 * This functions stores signature data and transmitts it during ZRTP
618 * processing to the other party as part of the Confirm packets. Refer to
619 * chapters 6.7 and 8.2 in the ZRTP specification.
620 *
621 * @param data
622 * The signature data including the signature type block. The method
623 * copies this data into the Confirm packet at signature type block.
624 * @param length
625 * The length of the signature data in bytes. This length must be
626 * multiple of 4.
627 * @return
628 * True if the method stored the data, false otherwise.
629 */
630 bool setSignatureData(uint8* data, int32 length);
631
632 /**
633 * Get signature data
634 *
635 * This functions returns signature data that was receivied during ZRTP
636 * processing. Refer to chapters 6.7 and 8.2.
637 *
638 * @return
639 * Pointer to signature data. This is a pointer to volatile data that is
640 * only valid during the checkSASSignature() callback. The application
641 * shall copy the data if necessary.
642 */
643 const uint8* getSignatureData();
644
645 /**
646 * Get length of signature data
647 *
648 * This functions returns the length of signature data that was receivied
649 * during ZRTP processing. Refer to chapters 6.7 and 8.2.
650 *
651 * @return
652 * Length in bytes of the received signature data. The method returns
653 * zero if no signature data avilable.
654 */
655 int32 getSignatureLength();
656
657 /**
658 * Put data into the RTP output queue.
659 *
660 * This is used to create a data packet in the send queue.
661 * Sometimes a "NULL" or empty packet will be used instead, and
662 * these are known as "silent" packets. "Silent" packets are
663 * used simply to "push" the scheduler along more accurately
664 * by giving the appearence that a next packet is waiting to
665 * be sent and to provide a valid timestamp for that packet.
666 *
667 * This method overrides the same method in OutgoingDataQueue class.
668 * During ZRTP processing it may be necessary to control the
669 * flow of outgoing RTP payload packets (GoClear processing).
670 *
671 * @param stamp Timestamp for expected send time of packet.
672 * @param data Value or NULL if special "silent" packet.
673 * @param len May be 0 to indicate a default by payload type.
674 **/
675 void
676 putData(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
677
678 /**
679 * Immediatly send a data packet.
680 *
681 * This is used to create a data packet and send it immediately.
682 * Sometimes a "NULL" or empty packet will be used instead, and
683 * these are known as "silent" packets. "Silent" packets are
684 * used simply to "push" the scheduler along more accurately
685 * by giving the appearence that a next packet is waiting to
686 * be sent and to provide a valid timestamp for that packet.
687 *
688 * This method overrides the same method in OutgoingDataQueue
689 * class. During ZRTP processing it may be necessary to
690 * control the flow of outgoing RTP payload packets (GoClear
691 * processing).
692 *
693 * @param stamp Timestamp immediate send time of packet.
694 * @param data Value or NULL if special "silent" packet.
695 * @param len May be 0 to indicate a default by payload type.
696 **/
697 void
698 sendImmediate(uint32 stamp, const unsigned char* data = NULL, size_t len = 0);
699
700 /**
701 * Starts the ZRTP protocol engine.
702 *
703 * Applications may call this method to immediatly start the ZRTP protocol
704 * engine any time after initializing ZRTP and setting optinal parameters,
705 * for example client id or multi-stream parameters.
706 *
707 * If the application does not call this method but sucessfully initialized
708 * the ZRTP engine using <code>initialize()</code> then ZRTP also starts
709 * after the application sent and received RTP packets. An application can
710 * disable this automatic, delayed start of the ZRTP engine using
711 * <code>setEnableZrtp(false)</code> before sending or receiving RTP
712 * packets.
713 *
714 */
715 void startZrtp();
716
717 /**
718 * Stops the ZRTP protocol engine.
719 *
720 * Applications call this method to stop the ZRTP protocol
721 * engine.
722 *
723 */
724 void stopZrtp();
725
726 /**
727 * Get other party's ZID (ZRTP Identifier) data
728 *
729 * This functions returns the other party's ZID that was receivied
730 * during ZRTP processing.
731 *
732 * The ZID data can be retrieved after ZRTP receive the first Hello
733 * packet from the other party. The application may call this method
734 * for example during SAS processing in showSAS(...) user callback
735 * method.
736 *
737 * @param data
738 * Pointer to a data buffer. This buffer must have a size of
739 * at least 12 bytes (96 bit) (ZRTP Identifier, see chap. 4.9)
740 * @return
741 * Number of bytes copied into the data buffer - must be equivalent
742 * to 96 bit, usually 12 bytes.
743 */
744 int32 getPeerZid(uint8* data);
745
746protected:
747 friend class TimeoutProvider<std::string, ost::ZrtpQueue*>;
748
749 /**
750 * A hook that gets called if the decoding of an incoming SRTP
751 * was erroneous
752 *
753 * @param pkt
754 * The SRTP packet with error.
755 * @param errorCode
756 * The error code: -1 - SRTP authentication failure, -2 - replay
757 * check failed
758 * @return
759 * True: put the packet in incoming queue for further processing
760 * by the applications; false: dismiss packet. The default
761 * implementation returns false.
762 */
763 virtual bool
764 onSRTPPacketError(IncomingRTPPkt& pkt, int32 errorCode);
765
766 /**
767 * Handle timeout event forwarded by the TimeoutProvider.
768 *
769 * Just call the ZRTP engine for further processing.
770 */
771 void handleTimeout(const std::string &c);
772
773 /**
774 * This function is used by the service thread to process
775 * the next incoming packet and place it in the receive list.
776 *
777 * This class overloads the function of IncomingDataQueue
778 * implementation.
779 *
780 * @return number of payload bytes received, <0 if error.
781 */
782 virtual size_t takeInDataPacket();
783
784 /*
785 * The following methods implement the GNU ZRTP callback interface.
786 * For detailed documentation refer to file ZrtpCallback.h
787 */
788 int32_t sendDataZRTP(const unsigned char* data, int32_t length);
789
790 int32_t activateTimer(int32_t time);
791
792 int32_t cancelTimer();
793
794 void sendInfo(GnuZrtpCodes::MessageSeverity severity, int32_t subCode);
795
796 bool srtpSecretsReady(SrtpSecret_t* secrets, EnableSecurity part);
797
798 void srtpSecretsOff(EnableSecurity part);
799
800 void srtpSecretsOn(std::string c, std::string s, bool verified);
801
802 void handleGoClear();
803
804 void zrtpNegotiationFailed(GnuZrtpCodes::MessageSeverity severity, int32_t subCode);
805
806 void zrtpNotSuppOther();
807
808 void synchEnter();
809
810 void synchLeave();
811
812 void zrtpAskEnrollment(GnuZrtpCodes::InfoEnrollment info);
813
814 void zrtpInformEnrollment(GnuZrtpCodes::InfoEnrollment info);
815
816 void signSAS(uint8_t* sasHash);
817
818 bool checkSASSignature(uint8_t* sasHash);
819
820 /*
821 * End of ZrtpCallback functions.
822 */
823
824 ZrtpQueue(uint32 size = RTPDataQueue::defaultMembersHashSize,
825 RTPApplication& app = defaultApplication());
826
827 /**
828 * Local SSRC is given instead of computed by the queue.
829 */
830 ZrtpQueue(uint32 ssrc, uint32 size =
831 RTPDataQueue::defaultMembersHashSize,
832 RTPApplication& app = defaultApplication());
833
834 virtual ~ZrtpQueue();
835
836private:
837 void init();
838 size_t rtpDataPacket(unsigned char* packet, int32 rtn,
839 InetHostAddress network_address,
840 tpport_t transport_port);
841
842 ZRtp *zrtpEngine;
843 ZrtpUserCallback* zrtpUserCallback;
844
845 std::string clientIdString;
846
847 bool enableZrtp;
848
849 int32 secureParts;
850
851 int16 senderZrtpSeqNo;
852 ost::Mutex synchLock; // Mutex for ZRTP (used by ZrtpStateClass)
853 uint32 peerSSRC;
Alexandre Lision51140e12013-12-02 10:54:09 -0500854 bool started;
855 bool mitmMode;
856 bool signSas;
857 bool enableParanoidMode;
858};
859
860class IncomingZRTPPkt : public IncomingRTPPkt {
861
862public:
863 /**
864 * Build a ZRTP packet object from a data buffer.
865 *
866 * @param block pointer to the buffer the whole packet is stored in.
867 * @param len length of the whole packet, expressed in octets.
868 *
869 **/
870
871 IncomingZRTPPkt(const unsigned char* block, size_t len);
872
873 ~IncomingZRTPPkt()
874 { }
875
876 uint32
877 getZrtpMagic() const;
878
879 uint32
880 getSSRC() const;
881};
882
883class OutgoingZRTPPkt : public OutgoingRTPPkt {
884
885public:
886 /**
887 * Construct a new ZRTP packet to be sent.
888 *
889 * A new copy in memory (holding all this components
890 * along with the fixed header) is created.
891 *
892 * @param hdrext whole header extension.
893 * @param hdrextlen size of whole header extension, in octets.
894 **/
895 OutgoingZRTPPkt(const unsigned char* const hdrext, uint32 hdrextlen);
896 ~OutgoingZRTPPkt()
897 { }
898};
899
900END_NAMESPACE
901
902#endif
903
904/** EMACS **
905 * Local variables:
906 * mode: c++
907 * c-default-style: ellemtel
908 * c-basic-offset: 4
909 * End:
910 */
911