Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame^] | 2 | Copyright (C) 2009 - 2010 Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame^] | 5 | it under the terms of the GNU General Public License as published by |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 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 | /* |
| 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 | |
| 45 | enum AlgoTypes { |
| 46 | Invalid = 0, HashAlgorithm = 1, CipherAlgorithm, PubKeyAlgorithm, SasType, AuthLength |
| 47 | }; |
| 48 | |
| 49 | typedef void(*encrypt_t)(uint8_t*, int32_t, uint8_t*, uint8_t*, int32_t); |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 50 | typedef void(*decrypt_t)(uint8_t*, int32_t, uint8_t*, uint8_t*, int32_t); |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 51 | |
| 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 | */ |
| 62 | class AlgorithmEnum { |
| 63 | public: |
| 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 | |
| 161 | private: |
| 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 Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 185 | class __EXPORT EnumBase { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 186 | public: |
| 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 | |
| 245 | protected: |
| 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 | |
| 252 | private: |
| 253 | AlgoTypes algoType; |
| 254 | std::vector <AlgorithmEnum* > algos; |
| 255 | }; |
| 256 | |
| 257 | /** |
| 258 | * The enumaration subclasses that contain the supported algorithm enumerations. |
| 259 | */ |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 260 | class __EXPORT HashEnum : public EnumBase { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 261 | public: |
| 262 | HashEnum(); |
| 263 | ~HashEnum(); |
| 264 | }; |
| 265 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 266 | class __EXPORT SymCipherEnum : public EnumBase { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 267 | public: |
| 268 | SymCipherEnum(); |
| 269 | ~SymCipherEnum(); |
| 270 | }; |
| 271 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 272 | class __EXPORT PubKeyEnum : public EnumBase { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 273 | public: |
| 274 | PubKeyEnum(); |
| 275 | ~PubKeyEnum(); |
| 276 | }; |
| 277 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 278 | class __EXPORT SasTypeEnum : public EnumBase { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 279 | public: |
| 280 | SasTypeEnum(); |
| 281 | ~SasTypeEnum(); |
| 282 | }; |
| 283 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 284 | class __EXPORT AuthLengthEnum : public EnumBase { |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 285 | public: |
| 286 | AuthLengthEnum(); |
| 287 | ~AuthLengthEnum(); |
| 288 | }; |
| 289 | |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 290 | extern __EXPORT HashEnum zrtpHashes; |
| 291 | extern __EXPORT SymCipherEnum zrtpSymCiphers; |
| 292 | extern __EXPORT PubKeyEnum zrtpPubKeys; |
| 293 | extern __EXPORT SasTypeEnum zrtpSasTypes; |
| 294 | extern __EXPORT AuthLengthEnum zrtpAuthLengths; |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 295 | |
| 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 | */ |
| 312 | class __EXPORT ZrtpConfigure { |
| 313 | public: |
| 314 | ZrtpConfigure(); /* Creates Configuration data */ |
| 315 | ~ZrtpConfigure(); |
| 316 | |
| 317 | /** |
| 318 | * Set the maximum number of algorithms per algorithm type that an application can |
| 319 | * configure. |
| 320 | */ |
| 321 | static const int maxNoOfAlgos = 7; |
| 322 | /** |
| 323 | * Convenience function that sets a pre-defined standard configuration. |
| 324 | * |
| 325 | * The standard configuration consists of the following algorithms: |
| 326 | * <ul> |
| 327 | * <li> Hash: SHA256 </li> |
| 328 | * <li> Symmetric Cipher: AES 128, AES 256 </li> |
| 329 | * <li> Public Key Algorithm: DH2048, DH3027, MultiStream </li> |
| 330 | * <li> SAS type: libase 32 </li> |
| 331 | * <li> SRTP Authentication lengths: 32, 80 </li> |
| 332 | *</ul> |
| 333 | */ |
| 334 | void setStandardConfig(); |
| 335 | |
| 336 | /** |
| 337 | * Convenience function that sets the mandatory algorithms only. |
| 338 | * |
| 339 | * Mandatory algorithms are: |
| 340 | * <ul> |
| 341 | * <li> Hash: SHA256 </li> |
| 342 | * <li> Symmetric Cipher: AES 128 </li> |
| 343 | * <li> Public Key Algorithm: DH3027, MultiStream </li> |
| 344 | * <li> SAS type: libase 32 </li> |
| 345 | * <li> SRTP Authentication lengths: 32, 80 </li> |
| 346 | *</ul> |
| 347 | */ |
| 348 | void setMandatoryOnly(); |
| 349 | |
| 350 | /** |
| 351 | * Clear all configuration data. |
| 352 | * |
| 353 | * The functions clears all configuration data. |
| 354 | */ |
| 355 | void clear(); |
| 356 | |
| 357 | /** |
| 358 | * Add an algorithm to configuration data. |
| 359 | * |
| 360 | * Adds the specified algorithm to the configuration data. |
| 361 | * If no free configuration data slot is available the |
| 362 | * function does not add the algorithm and returns -1. The |
| 363 | * methods appends the algorithm to the existing algorithms. |
| 364 | * |
| 365 | * @param algoType |
| 366 | * Specifies which algorithm type to select |
| 367 | * @param algo |
| 368 | * The enumeration of the algorithm to add. |
| 369 | * @return |
| 370 | * Number of free configuration data slots or -1 on error |
| 371 | */ |
| 372 | int32_t addAlgo(AlgoTypes algoType, AlgorithmEnum& algo); |
| 373 | |
| 374 | /** |
| 375 | * Add an algorithm to configuration data at given index. |
| 376 | * |
| 377 | * Adds the specified algorithm to the configuration data vector |
| 378 | * at a given index. If the index is larger than the actual size |
| 379 | * of the configuration vector the method just appends the algorithm. |
| 380 | * |
| 381 | * @param algoType |
| 382 | * Specifies which algorithm type to select |
| 383 | * @param algo |
| 384 | * The enumeration of the algorithm to add. |
| 385 | * @param index |
| 386 | * The index where to add the algorihm |
| 387 | * @return |
| 388 | * Number of free configuration data slots or -1 on error |
| 389 | */ |
| 390 | int32_t addAlgoAt(AlgoTypes algoType, AlgorithmEnum& algo, int32_t index); |
| 391 | |
| 392 | /** |
| 393 | * Remove a algorithm from configuration data. |
| 394 | * |
| 395 | * Removes the specified algorithm from configuration data. If |
| 396 | * the algorithm was not configured previously the function does |
| 397 | * not modify the configuration data and returns the number of |
| 398 | * free configuration data slots. |
| 399 | * |
| 400 | * If an application removes all algorithms then ZRTP does not |
| 401 | * include any algorithm into the hello message and falls back |
| 402 | * to a predefined mandatory algorithm. |
| 403 | * |
| 404 | * @param algoType |
| 405 | * Specifies which algorithm type to select |
| 406 | * @param algo |
| 407 | * The enumeration of the algorithm to remove. |
| 408 | * @return |
| 409 | * Number of free configuration slots. |
| 410 | */ |
| 411 | int32_t removeAlgo(AlgoTypes algoType, AlgorithmEnum& algo); |
| 412 | |
| 413 | /** |
| 414 | * Returns the number of configured algorithms. |
| 415 | * |
| 416 | * @param algoType |
| 417 | * Specifies which algorithm type to select |
| 418 | * @return |
| 419 | * The number of configured algorithms (used configuration |
| 420 | * data slots) |
| 421 | */ |
| 422 | int32_t getNumConfiguredAlgos(AlgoTypes algoType); |
| 423 | |
| 424 | /** |
| 425 | * Returns the identifier of the algorithm at index. |
| 426 | * |
| 427 | * @param algoType |
| 428 | * Specifies which algorithm type to select |
| 429 | * @param index |
| 430 | * The index in the list of the algorihm type |
| 431 | * @return |
| 432 | * A pointer the the algorithm enumeration. If the index |
| 433 | * does not point to a configured slot then the function |
| 434 | * returns NULL. |
| 435 | * |
| 436 | */ |
| 437 | AlgorithmEnum& getAlgoAt(AlgoTypes algoType, int32_t index); |
| 438 | |
| 439 | /** |
| 440 | * Checks if the configuration data of the algorihm type already contains |
| 441 | * a specific algorithms. |
| 442 | * |
| 443 | * @param algoType |
| 444 | * Specifies which algorithm type to select |
| 445 | * @param algo |
| 446 | * The algorithm to check |
| 447 | * @return |
| 448 | * True if the algorithm was found, false otherwise. |
| 449 | * |
| 450 | */ |
| 451 | bool containsAlgo(AlgoTypes algoType, AlgorithmEnum& algo); |
| 452 | |
| 453 | /** |
| 454 | * Enables or disables trusted MitM processing. |
| 455 | * |
| 456 | * For further details of trusted MitM processing refer to ZRTP |
| 457 | * specification, chapter 7.3 |
| 458 | * |
| 459 | * @param yesNo |
| 460 | * If set to true then trusted MitM processing is enabled. |
| 461 | */ |
| 462 | void setTrustedMitM(bool yesNo); |
| 463 | |
| 464 | /** |
| 465 | * Check status of trusted MitM processing. |
| 466 | * |
| 467 | * @return |
| 468 | * Returns true if trusted MitM processing is enabled. |
| 469 | */ |
| 470 | bool isTrustedMitM(); |
| 471 | |
| 472 | /** |
| 473 | * Enables or disables SAS signature processing. |
| 474 | * |
| 475 | * For further details of trusted MitM processing refer to ZRTP |
| 476 | * specification, chapter 7.2 |
| 477 | * |
| 478 | * @param yesNo |
| 479 | * If set to true then certificate processing is enabled. |
| 480 | */ |
| 481 | void setSasSignature(bool yesNo); |
| 482 | |
| 483 | /** |
| 484 | * Check status of SAS signature processing. |
| 485 | * |
| 486 | * @return |
| 487 | * Returns true if certificate processing is enabled. |
| 488 | */ |
| 489 | bool isSasSignature(); |
| 490 | |
| 491 | /** |
| 492 | * Enables or disables paranoid mode. |
| 493 | * |
| 494 | * For further explanation of paranoid mode refer to the documentation |
| 495 | * of ZRtp class. |
| 496 | * |
| 497 | * @param yesNo |
| 498 | * If set to true then paranoid mode is enabled. |
| 499 | */ |
| 500 | void setParanoidMode(bool yesNo); |
| 501 | |
| 502 | /** |
| 503 | * Check status of paranoid mode. |
| 504 | * |
| 505 | * @return |
| 506 | * Returns true if paranoid mode is enabled. |
| 507 | */ |
| 508 | bool isParanoidMode(); |
| 509 | |
| 510 | /// Helper function to print some internal data |
| 511 | void printConfiguredAlgos(AlgoTypes algoTyp); |
| 512 | |
| 513 | private: |
| 514 | std::vector<AlgorithmEnum* > hashes; |
| 515 | std::vector<AlgorithmEnum* > symCiphers; |
| 516 | std::vector<AlgorithmEnum* > publicKeyAlgos; |
| 517 | std::vector<AlgorithmEnum* > sasTypes; |
| 518 | std::vector<AlgorithmEnum* > authLengths; |
| 519 | |
| 520 | bool enableTrustedMitM; |
| 521 | bool enableSasSignature; |
| 522 | bool enableParanoidMode; |
| 523 | |
| 524 | |
| 525 | AlgorithmEnum& getAlgoAt(std::vector<AlgorithmEnum* >& a, int32_t index); |
| 526 | int32_t addAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo); |
| 527 | int32_t addAlgoAt(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo, int32_t index); |
| 528 | int32_t removeAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo); |
| 529 | int32_t getNumConfiguredAlgos(std::vector<AlgorithmEnum* >& a); |
| 530 | bool containsAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo); |
| 531 | std::vector<AlgorithmEnum* >& getEnum(AlgoTypes algoType); |
| 532 | |
| 533 | void printConfiguredAlgos(std::vector<AlgorithmEnum* >& a); |
| 534 | |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 535 | protected: |
| 536 | |
| 537 | public: |
| 538 | }; |
| 539 | |
| 540 | /** |
| 541 | * @} |
| 542 | */ |
| 543 | #endif |
| 544 | |
| 545 | /** EMACS ** |
| 546 | * Local variables: |
| 547 | * mode: c++ |
| 548 | * c-default-style: ellemtel |
| 549 | * c-basic-offset: 4 |
| 550 | * End: |
| 551 | */ |