blob: bb78cef79e94da352749914dedee7e4ec435ed58 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05002 Copyright (C) 2009 - 2013 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05003
4 This program is free software: you can redistribute it and/or modify
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05005 it under the terms of the GNU Lesser General Public License as published by
Alexandre Lision51140e12013-12-02 10:54:09 -05006 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/*
19 * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
20 */
21
22#ifndef _ZRTPCONFIGURE_H_
23#define _ZRTPCONFIGURE_H_
24
25/**
26 * @file ZrtpConfigure.h
27 * @brief The ZRTP configure functions
28 * @ingroup GNU_ZRTP
29 * @{
30 */
31
32#include <stdio.h>
33#include <stdint.h>
34#include <list>
35#include <string>
36#include <vector>
37#include <string.h>
38
39#include <libzrtpcpp/ZrtpCallback.h>
40
41/**
42 * This enumerations list all configurable algorithm types.
43 */
44
45enum AlgoTypes {
46 Invalid = 0, HashAlgorithm = 1, CipherAlgorithm, PubKeyAlgorithm, SasType, AuthLength
47};
48
49typedef void(*encrypt_t)(uint8_t*, int32_t, uint8_t*, uint8_t*, int32_t);
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050050typedef void(*decrypt_t)(uint8_t*, int32_t, uint8_t*, uint8_t*, int32_t);
Alexandre Lision51140e12013-12-02 10:54:09 -050051
52/**
53 * The algorithm enumration class.
54 *
55 * This simple class is just a container of an algorithm's name and
56 * its associated algorithm type. We use this class together with the
57 * EnumBase class to implement a Java-like enum class functionality
58 * (not fully, but OK for our use case).
59 *
60 * An application shall use the get / check methods to retrieve information.
61 */
62class AlgorithmEnum {
63public:
64 /**
65 * Create an AlgorithmEnum object.
66 *
67 * @param type
68 * Defines the algorithm type
69 * @param name
70 * Set the names of the algorithm. The name is copied
71 * and the call may reuse the space.
72 * @param klen
73 * The key length for this algorihm in byte, for example 16 or 32
74 * @param ra
75 * A human readable short string that describes the algorihm.
76 * @param en
77 * Pointer to the encryption function of this algorithn
78 * @param de
79 * Pointer to the decryption funtions of this algorithm.
80 * @param alId
81 * The algorithm id used by SRTP to identify an algorithm type, for
82 * example Skein, Sha1, Aes, ...
83 *
84 * @see AlgoTypes
85 */
86 AlgorithmEnum(const AlgoTypes type, const char* name, int32_t klen,
87 const char* ra, encrypt_t en, decrypt_t de, SrtpAlgorithms alId);
88
89 /**
90 * AlgorithmEnum destructor
91 */
92 ~AlgorithmEnum();
93
94 /**
95 * Get the algorihm's name
96 *
97 * @returns
98 * Algorithm's name as null terminated C-string. The
99 * application must not free this memory.
100 */
101 const char* getName();
102
103 /**
104 * Get the algorihm's readable name
105 *
106 * @returns
107 * Algorithm's readable name as null terminated C-string. The
108 * application must not free this memory.
109 */
110 const char* getReadable();
111
112 /**
113 * Get the algorihm's key length.
114 *
115 * @returns
116 * An integer definig the key length in bytes.
117 */
118 int getKeylen();
119
120 /**
121 * Get the algorihm's integer id.
122 *
123 * @returns
124 * An integer that defines the algorithm.
125 */
126 SrtpAlgorithms getAlgoId();
127 /**
128 * Get the algorihm's key length.
129 *
130 * @returns
131 * An integer definig the key length in bytes.
132 */
133 encrypt_t getEncrypt();
134
135 /**
136 * Get the algorihm's key length.
137 *
138 * @returns
139 * An integer definig the key length in bytes.
140 */
141 decrypt_t getDecrypt();
142
143 /**
144 * Get the algorithm type of this AlgorithmEnum object.
145 *
146 * @returns
147 * The algorithm type.
148 *
149 * @see AlgoTypes
150 */
151 AlgoTypes getAlgoType();
152
153 /**
154 * Check if this AlgorithmEnum object is valid
155 *
156 * @returns
157 * @c true if the object is valid, @c false otherwise
158 */
159 bool isValid();
160
161private:
162 AlgoTypes algoType;
163 std::string algoName;
164 int32_t keyLen;
165 std::string readable;
166 encrypt_t encrypt;
167 decrypt_t decrypt;
168 SrtpAlgorithms algoId;
169};
170
171/**
172 * EnumBase provides methods to store and access algorithm enumerations of
173 * a specific algorithm type.
174 *
175 * An application shall use the get / check methods to retrieve information
176 * from the preset Algorithm Enumerations.
177 *
178 * @see AlgoTypes
179 * @see zrtpHashes
180 * @see zrtpSymCiphers
181 * @see zrtpPubKeys
182 * @see zrtpSasTypes
183 * @see zrtpAuthLengths
184 */
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500185class __EXPORT EnumBase {
Alexandre Lision51140e12013-12-02 10:54:09 -0500186public:
187 /**
188 * Get an AlgorithmEnum by its name
189 *
190 * @param name
191 * The name of the AlgorithmEnum to search.
192 * @returns
193 * The AlgorithmEnum if found or an invalid AlgorithmEnum if the name
194 * was not found
195 */
196 AlgorithmEnum& getByName(const char* name);
197
198 /**
199 * Return all names of all currently stored AlgorithmEnums
200 *
201 * @return
202 * A C++ std::list of C++ std::strings that contain the names.
203 */
204 std::list<std::string>* getAllNames();
205
206 /**
207 * Get the number of currently stored AlgorithmEnums
208 *
209 * @return
210 * The number of currently stored AlgorithmEnums
211 */
212 int getSize();
213
214 /**
215 * Get the AlgoTypes to which this EnumBase belongs.
216 *
217 * @return
218 * The AlgoTypes of this EnumBase.
219 * @see AlgoTypes.
220 */
221 AlgoTypes getAlgoType();
222
223 /**
224 * Return the AlgorithmEnum by its ordinal number
225 *
226 * @param ord
227 * The ordinal number of the AlgorithmEnum.
228 * @return
229 * The AlgorithmEnum if found, an invalid Algorithm otherwise.
230 */
231 AlgorithmEnum& getByOrdinal(int ord);
232
233 /**
234 * Get the ordinal number of an AlgorithmEnum
235 *
236 * @param algo
237 * Return toe ordinal numer of this AlgorithmEnum.
238 *
239 * @return
240 * Return the ordinal number of this AlgorithmEnum if found,
241 * -1 otherwise.
242 */
243 int getOrdinal(AlgorithmEnum& algo);
244
245protected:
246 EnumBase(AlgoTypes algo);
247 ~EnumBase();
248 void insert(const char* name);
249 void insert(const char* name, int32_t klen,
250 const char* ra, encrypt_t en, decrypt_t de, SrtpAlgorithms alId);
251
252private:
253 AlgoTypes algoType;
254 std::vector <AlgorithmEnum* > algos;
255};
256
257/**
258 * The enumaration subclasses that contain the supported algorithm enumerations.
259 */
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500260class __EXPORT HashEnum : public EnumBase {
Alexandre Lision51140e12013-12-02 10:54:09 -0500261public:
262 HashEnum();
263 ~HashEnum();
264};
265
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500266class __EXPORT SymCipherEnum : public EnumBase {
Alexandre Lision51140e12013-12-02 10:54:09 -0500267public:
268 SymCipherEnum();
269 ~SymCipherEnum();
270};
271
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500272class __EXPORT PubKeyEnum : public EnumBase {
Alexandre Lision51140e12013-12-02 10:54:09 -0500273public:
274 PubKeyEnum();
275 ~PubKeyEnum();
276};
277
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500278class __EXPORT SasTypeEnum : public EnumBase {
Alexandre Lision51140e12013-12-02 10:54:09 -0500279public:
280 SasTypeEnum();
281 ~SasTypeEnum();
282};
283
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500284class __EXPORT AuthLengthEnum : public EnumBase {
Alexandre Lision51140e12013-12-02 10:54:09 -0500285public:
286 AuthLengthEnum();
287 ~AuthLengthEnum();
288};
289
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500290extern __EXPORT HashEnum zrtpHashes;
291extern __EXPORT SymCipherEnum zrtpSymCiphers;
292extern __EXPORT PubKeyEnum zrtpPubKeys;
293extern __EXPORT SasTypeEnum zrtpSasTypes;
294extern __EXPORT AuthLengthEnum zrtpAuthLengths;
Alexandre Lision51140e12013-12-02 10:54:09 -0500295
296/**
297 * ZRTP configuration data.
298 *
299 * This class contains data and functions to set ZRTP configuration data.
300 * An application may use this class to set configuration information for
301 * ZRTP. ZRTP uses this configuration information to announce various
302 * algorithms via its Hello message. An application may use this class to
303 * restrict or allow use of algorithms.
304 *
305 * The constructor does not set any algorithms, thus it is an empty
306 * configuration. An application may use this empty configuration and
307 * hand it over to ZRTP. In this case ZRTP does not announce any algorithms
308 * in its Hello message and uses mandatory algorithms only.
309 *
310 * An application can configure implemented algorithms only.
311 */
312class __EXPORT ZrtpConfigure {
313public:
314 ZrtpConfigure(); /* Creates Configuration data */
315 ~ZrtpConfigure();
316
317 /**
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500318 * Define the algorithm selection policies.
319 */
320 typedef enum _policies {
321 Standard = 1,
322 PreferNonNist = 2
323 } Policy;
324
325 /**
Alexandre Lision51140e12013-12-02 10:54:09 -0500326 * Set the maximum number of algorithms per algorithm type that an application can
327 * configure.
328 */
329 static const int maxNoOfAlgos = 7;
330 /**
331 * Convenience function that sets a pre-defined standard configuration.
332 *
333 * The standard configuration consists of the following algorithms:
334 * <ul>
335 * <li> Hash: SHA256 </li>
336 * <li> Symmetric Cipher: AES 128, AES 256 </li>
337 * <li> Public Key Algorithm: DH2048, DH3027, MultiStream </li>
338 * <li> SAS type: libase 32 </li>
339 * <li> SRTP Authentication lengths: 32, 80 </li>
340 *</ul>
341 */
342 void setStandardConfig();
343
344 /**
345 * Convenience function that sets the mandatory algorithms only.
346 *
347 * Mandatory algorithms are:
348 * <ul>
349 * <li> Hash: SHA256 </li>
350 * <li> Symmetric Cipher: AES 128 </li>
351 * <li> Public Key Algorithm: DH3027, MultiStream </li>
352 * <li> SAS type: libase 32 </li>
353 * <li> SRTP Authentication lengths: 32, 80 </li>
354 *</ul>
355 */
356 void setMandatoryOnly();
357
358 /**
359 * Clear all configuration data.
360 *
361 * The functions clears all configuration data.
362 */
363 void clear();
364
365 /**
366 * Add an algorithm to configuration data.
367 *
368 * Adds the specified algorithm to the configuration data.
369 * If no free configuration data slot is available the
370 * function does not add the algorithm and returns -1. The
371 * methods appends the algorithm to the existing algorithms.
372 *
373 * @param algoType
374 * Specifies which algorithm type to select
375 * @param algo
376 * The enumeration of the algorithm to add.
377 * @return
378 * Number of free configuration data slots or -1 on error
379 */
380 int32_t addAlgo(AlgoTypes algoType, AlgorithmEnum& algo);
381
382 /**
383 * Add an algorithm to configuration data at given index.
384 *
385 * Adds the specified algorithm to the configuration data vector
386 * at a given index. If the index is larger than the actual size
387 * of the configuration vector the method just appends the algorithm.
388 *
389 * @param algoType
390 * Specifies which algorithm type to select
391 * @param algo
392 * The enumeration of the algorithm to add.
393 * @param index
394 * The index where to add the algorihm
395 * @return
396 * Number of free configuration data slots or -1 on error
397 */
398 int32_t addAlgoAt(AlgoTypes algoType, AlgorithmEnum& algo, int32_t index);
399
400 /**
401 * Remove a algorithm from configuration data.
402 *
403 * Removes the specified algorithm from configuration data. If
404 * the algorithm was not configured previously the function does
405 * not modify the configuration data and returns the number of
406 * free configuration data slots.
407 *
408 * If an application removes all algorithms then ZRTP does not
409 * include any algorithm into the hello message and falls back
410 * to a predefined mandatory algorithm.
411 *
412 * @param algoType
413 * Specifies which algorithm type to select
414 * @param algo
415 * The enumeration of the algorithm to remove.
416 * @return
417 * Number of free configuration slots.
418 */
419 int32_t removeAlgo(AlgoTypes algoType, AlgorithmEnum& algo);
420
421 /**
422 * Returns the number of configured algorithms.
423 *
424 * @param algoType
425 * Specifies which algorithm type to select
426 * @return
427 * The number of configured algorithms (used configuration
428 * data slots)
429 */
430 int32_t getNumConfiguredAlgos(AlgoTypes algoType);
431
432 /**
433 * Returns the identifier of the algorithm at index.
434 *
435 * @param algoType
436 * Specifies which algorithm type to select
437 * @param index
438 * The index in the list of the algorihm type
439 * @return
440 * A pointer the the algorithm enumeration. If the index
441 * does not point to a configured slot then the function
442 * returns NULL.
443 *
444 */
445 AlgorithmEnum& getAlgoAt(AlgoTypes algoType, int32_t index);
446
447 /**
448 * Checks if the configuration data of the algorihm type already contains
449 * a specific algorithms.
450 *
451 * @param algoType
452 * Specifies which algorithm type to select
453 * @param algo
454 * The algorithm to check
455 * @return
456 * True if the algorithm was found, false otherwise.
457 *
458 */
459 bool containsAlgo(AlgoTypes algoType, AlgorithmEnum& algo);
460
461 /**
462 * Enables or disables trusted MitM processing.
463 *
464 * For further details of trusted MitM processing refer to ZRTP
465 * specification, chapter 7.3
466 *
467 * @param yesNo
468 * If set to true then trusted MitM processing is enabled.
469 */
470 void setTrustedMitM(bool yesNo);
471
472 /**
473 * Check status of trusted MitM processing.
474 *
475 * @return
476 * Returns true if trusted MitM processing is enabled.
477 */
478 bool isTrustedMitM();
479
480 /**
481 * Enables or disables SAS signature processing.
482 *
483 * For further details of trusted MitM processing refer to ZRTP
484 * specification, chapter 7.2
485 *
486 * @param yesNo
487 * If set to true then certificate processing is enabled.
488 */
489 void setSasSignature(bool yesNo);
490
491 /**
492 * Check status of SAS signature processing.
493 *
494 * @return
495 * Returns true if certificate processing is enabled.
496 */
497 bool isSasSignature();
498
499 /**
500 * Enables or disables paranoid mode.
501 *
502 * For further explanation of paranoid mode refer to the documentation
503 * of ZRtp class.
504 *
505 * @param yesNo
506 * If set to true then paranoid mode is enabled.
507 */
508 void setParanoidMode(bool yesNo);
509
510 /**
511 * Check status of paranoid mode.
512 *
513 * @return
514 * Returns true if paranoid mode is enabled.
515 */
516 bool isParanoidMode();
517
518 /// Helper function to print some internal data
519 void printConfiguredAlgos(AlgoTypes algoTyp);
520
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500521 Policy getSelectionPolicy() {return selectionPolicy;}
522 void setSelectionPolicy(Policy pol) {selectionPolicy = pol;}
523
Alexandre Lision51140e12013-12-02 10:54:09 -0500524 private:
525 std::vector<AlgorithmEnum* > hashes;
526 std::vector<AlgorithmEnum* > symCiphers;
527 std::vector<AlgorithmEnum* > publicKeyAlgos;
528 std::vector<AlgorithmEnum* > sasTypes;
529 std::vector<AlgorithmEnum* > authLengths;
530
531 bool enableTrustedMitM;
532 bool enableSasSignature;
533 bool enableParanoidMode;
534
535
536 AlgorithmEnum& getAlgoAt(std::vector<AlgorithmEnum* >& a, int32_t index);
537 int32_t addAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo);
538 int32_t addAlgoAt(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo, int32_t index);
539 int32_t removeAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo);
540 int32_t getNumConfiguredAlgos(std::vector<AlgorithmEnum* >& a);
541 bool containsAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo);
542 std::vector<AlgorithmEnum* >& getEnum(AlgoTypes algoType);
543
544 void printConfiguredAlgos(std::vector<AlgorithmEnum* >& a);
545
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500546 Policy selectionPolicy;
547
Alexandre Lision51140e12013-12-02 10:54:09 -0500548 protected:
549
550 public:
551};
552
553/**
554 * @}
555 */
556#endif
557
558/** EMACS **
559 * Local variables:
560 * mode: c++
561 * c-default-style: ellemtel
562 * c-basic-offset: 4
563 * End:
564 */