blob: 560466f3a0de2797c1051720d180af896b91172e [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001#include <libzrtpcpp/crypto/aesCFB.h>
2#include <libzrtpcpp/crypto/twoCFB.h>
3#include <libzrtpcpp/ZrtpConfigure.h>
4#include <libzrtpcpp/ZrtpTextData.h>
5
6AlgorithmEnum::AlgorithmEnum(const AlgoTypes type, const char* name,
7 int32_t klen, const char* ra, encrypt_t en,
8 decrypt_t de, SrtpAlgorithms alId):
9 algoType(type) , algoName(name), keyLen(klen), readable(ra), encrypt(en),
10 decrypt(de), algoId(alId) {
11}
12
13AlgorithmEnum::~AlgorithmEnum()
14{
15}
16
17const char* AlgorithmEnum::getName() {
18 return algoName.c_str();
19}
20
21const char* AlgorithmEnum::getReadable() {
22 return readable.c_str();
23}
24
25int AlgorithmEnum::getKeylen() {
26 return keyLen;
27}
28
29SrtpAlgorithms AlgorithmEnum::getAlgoId() {
30 return algoId;
31}
32
33encrypt_t AlgorithmEnum::getEncrypt() {
34 return encrypt;
35}
36
37decrypt_t AlgorithmEnum::getDecrypt() {
38 return decrypt;
39}
40
41AlgoTypes AlgorithmEnum::getAlgoType() {
42 return algoType;
43}
44
45bool AlgorithmEnum::isValid() {
46 return (algoType != Invalid);
47}
48
49static AlgorithmEnum invalidAlgo(Invalid, "", 0, "", NULL, NULL, None);
50
51
52EnumBase::EnumBase(AlgoTypes a) : algoType(a) {
53}
54
55EnumBase::~EnumBase() {}
56
57void EnumBase::insert(const char* name) {
58 if (!name)
59 return;
60 AlgorithmEnum* e = new AlgorithmEnum(algoType, name, 0, "", NULL, NULL, None);
61 algos.push_back(e);
62}
63
64void EnumBase::insert(const char* name, int32_t klen, const char* ra,
65 encrypt_t enc, decrypt_t dec, SrtpAlgorithms alId) {
66 if (!name)
67 return;
68 AlgorithmEnum* e = new AlgorithmEnum(algoType, name, klen, ra, enc, dec, alId);
69 algos.push_back(e);
70}
71
72int EnumBase::getSize() {
73 return algos.size();
74}
75
76AlgoTypes EnumBase::getAlgoType() {
77 return algoType;
78}
79
80AlgorithmEnum& EnumBase::getByName(const char* name) {
81 std::vector<AlgorithmEnum* >::iterator b = algos.begin();
82 std::vector<AlgorithmEnum* >::iterator e = algos.end();
83
84 for (; b != e; b++) {
85 if (strncmp((*b)->getName(), name, 4) == 0) {
86 return *(*b);
87 }
88 }
89 return invalidAlgo;
90}
91
92AlgorithmEnum& EnumBase::getByOrdinal(int ord) {
93 std::vector<AlgorithmEnum* >::iterator b = algos.begin();
94 std::vector<AlgorithmEnum* >::iterator e = algos.end();
95
96 for (int i = 0; b != e; ++b) {
97 if (i == ord) {
98 return *(*b);
99 }
100 i++;
101 }
102 return invalidAlgo;
103}
104
105int EnumBase::getOrdinal(AlgorithmEnum& algo) {
106 std::vector<AlgorithmEnum* >::iterator b = algos.begin();
107 std::vector<AlgorithmEnum* >::iterator e = algos.end();
108
109 for (int i = 0; b != e; ++b) {
110 if (strncmp((*b)->getName(), algo.getName(), 4) == 0) {
111 return i;
112 }
113 i++;
114 }
115 return -1;
116}
117
118std::list<std::string>* EnumBase::getAllNames() {
119 std::vector<AlgorithmEnum* >::iterator b = algos.begin();
120 std::vector<AlgorithmEnum* >::iterator e = algos.end();
121
122 std::list<std::string>* strg = new std::list<std::string>();
123
124 for (; b != e; b++) {
125 std::string s((*b)->getName());
126 strg->push_back(s);
127 }
128 return strg;
129}
130
131
132/**
133 * Set up the enumeration list for available hash algorithms
134 */
135HashEnum::HashEnum() : EnumBase(HashAlgorithm) {
136 insert(s256);
137 insert(s384);
138}
139
140HashEnum::~HashEnum() {}
141
142/**
143 * Set up the enumeration list for available symmetric cipher algorithms
144 */
145SymCipherEnum::SymCipherEnum() : EnumBase(CipherAlgorithm) {
146 insert(aes3, 32, "AES-CM-256", aesCfbEncrypt, aesCfbDecrypt, Aes);
147 insert(aes1, 16, "AES-CM-128", aesCfbEncrypt, aesCfbDecrypt, Aes);
148 insert(two3, 32, "TWO-CM-256", twoCfbEncrypt, twoCfbDecrypt, TwoFish);
149 insert(two1, 16, "TWO-CM-128", twoCfbEncrypt, twoCfbDecrypt, TwoFish);
150}
151
152SymCipherEnum::~SymCipherEnum() {}
153
154/**
155 * Set up the enumeration list for available public key algorithms
156 */
157PubKeyEnum::PubKeyEnum() : EnumBase(PubKeyAlgorithm) {
158 insert(dh2k);
159 insert(dh3k);
160 insert(mult);
161 insert(ec25);
162 insert(ec38);
163}
164
165PubKeyEnum::~PubKeyEnum() {}
166
167/**
168 * Set up the enumeration list for available SAS algorithms
169 */
170SasTypeEnum::SasTypeEnum() : EnumBase(SasType) {
171 insert(b32);
172}
173
174SasTypeEnum::~SasTypeEnum() {}
175
176/**
177 * Set up the enumeration list for available SRTP authentications
178 */
179AuthLengthEnum::AuthLengthEnum() : EnumBase(AuthLength) {
180 insert(hs32, 32, "", NULL, NULL, Sha1);
181 insert(hs80, 80, "", NULL, NULL, Sha1);
182 insert(sk32, 32, "", NULL, NULL, Skein);
183 insert(sk64, 64, "", NULL, NULL, Skein);
184}
185
186AuthLengthEnum::~AuthLengthEnum() {}
187
188/*
189 * Here the global accessible enumerations for all implemented algorithms.
190 */
191HashEnum zrtpHashes;
192SymCipherEnum zrtpSymCiphers;
193PubKeyEnum zrtpPubKeys;
194SasTypeEnum zrtpSasTypes;
195AuthLengthEnum zrtpAuthLengths;
196
197/*
198 * The public methods are mainly a facade to the private methods.
199 */
200ZrtpConfigure::ZrtpConfigure() : enableTrustedMitM(false), enableSasSignature(false), enableParanoidMode(false) {}
201
202ZrtpConfigure::~ZrtpConfigure() {}
203
204void ZrtpConfigure::setStandardConfig() {
205 clear();
206
207 addAlgo(HashAlgorithm, zrtpHashes.getByName(s384));
208 addAlgo(HashAlgorithm, zrtpHashes.getByName(s256));
209
210 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(two3));
211 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(aes3));
212 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(two1));
213 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(aes1));
214
215 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(ec25));
216 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(dh3k));
217 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(ec38));
218 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(dh2k));
219 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(mult));
220
221 addAlgo(SasType, zrtpSasTypes.getByName(b32));
222
223 addAlgo(AuthLength, zrtpAuthLengths.getByName(sk32));
224 addAlgo(AuthLength, zrtpAuthLengths.getByName(sk64));
225 addAlgo(AuthLength, zrtpAuthLengths.getByName(hs32));
226 addAlgo(AuthLength, zrtpAuthLengths.getByName(hs80));
227}
228
229void ZrtpConfigure::setMandatoryOnly() {
230 clear();
231
232 addAlgo(HashAlgorithm, zrtpHashes.getByName(s256));
233
234 addAlgo(CipherAlgorithm, zrtpSymCiphers.getByName(aes1));
235
236 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(dh3k));
237 addAlgo(PubKeyAlgorithm, zrtpPubKeys.getByName(mult));
238
239 addAlgo(SasType, zrtpSasTypes.getByName(b32));
240
241 addAlgo(AuthLength, zrtpAuthLengths.getByName(hs32));
242 addAlgo(AuthLength, zrtpAuthLengths.getByName(hs80));
243
244}
245
246void ZrtpConfigure::clear() {
247 hashes.clear();
248 symCiphers.clear();
249 publicKeyAlgos.clear();
250 sasTypes.clear();
251 authLengths.clear();
252}
253
254int32_t ZrtpConfigure::addAlgo(AlgoTypes algoType, AlgorithmEnum& algo) {
255
256 return addAlgo(getEnum(algoType), algo);
257}
258
259int32_t ZrtpConfigure::addAlgoAt(AlgoTypes algoType, AlgorithmEnum& algo, int32_t index) {
260
261 return addAlgoAt(getEnum(algoType), algo, index);
262}
263
264AlgorithmEnum& ZrtpConfigure::getAlgoAt(AlgoTypes algoType, int32_t index) {
265
266 return getAlgoAt(getEnum(algoType), index);
267}
268
269int32_t ZrtpConfigure::removeAlgo(AlgoTypes algoType, AlgorithmEnum& algo) {
270
271 return removeAlgo(getEnum(algoType), algo);
272}
273
274int32_t ZrtpConfigure::getNumConfiguredAlgos(AlgoTypes algoType) {
275
276 return getNumConfiguredAlgos(getEnum(algoType));
277}
278
279bool ZrtpConfigure::containsAlgo(AlgoTypes algoType, AlgorithmEnum& algo) {
280
281 return containsAlgo(getEnum(algoType), algo);
282}
283
284void ZrtpConfigure::printConfiguredAlgos(AlgoTypes algoType) {
285
286 printConfiguredAlgos(getEnum(algoType));
287}
288
289/*
290 * The next methods are the private methods that implement the real
291 * details.
292 */
293AlgorithmEnum& ZrtpConfigure::getAlgoAt(std::vector<AlgorithmEnum* >& a, int32_t index) {
294
295 if (index >= (int)a.size())
296 return invalidAlgo;
297
298 std::vector<AlgorithmEnum* >::iterator b = a.begin();
299 std::vector<AlgorithmEnum* >::iterator e = a.end();
300
301 for (int i = 0; b != e; ++b) {
302 if (i == index) {
303 return *(*b);
304 }
305 i++;
306 }
307 return invalidAlgo;
308}
309
310int32_t ZrtpConfigure::addAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo) {
311 int size = (int)a.size();
312 if (size >= maxNoOfAlgos)
313 return -1;
314
315 if (!algo.isValid())
316 return -1;
317
318 if (containsAlgo(a, algo))
319 return (maxNoOfAlgos - size);
320
321 a.push_back(&algo);
322 return (maxNoOfAlgos - (int)a.size());
323}
324
325int32_t ZrtpConfigure::addAlgoAt(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo, int32_t index) {
326 if (index >= maxNoOfAlgos)
327 return -1;
328
329 int size = (int)a.size();
330
331 if (!algo.isValid())
332 return -1;
333
334// a[index] = &algo;
335
336 if (index >= size) {
337 a.push_back(&algo);
338 return maxNoOfAlgos - (int)a.size();
339 }
340 std::vector<AlgorithmEnum* >::iterator b = a.begin();
341 std::vector<AlgorithmEnum* >::iterator e = a.end();
342
343 for (int i = 0; b != e; ++b) {
344 if (i == index) {
345 a.insert(b, &algo);
346 break;
347 }
348 i++;
349 }
350 return (maxNoOfAlgos - (int)a.size());
351}
352
353int32_t ZrtpConfigure::removeAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo) {
354
355 if ((int)a.size() == 0 || !algo.isValid())
356 return maxNoOfAlgos;
357
358 std::vector<AlgorithmEnum* >::iterator b = a.begin();
359 std::vector<AlgorithmEnum* >::iterator e = a.end();
360
361 for (; b != e; ++b) {
362 if (strcmp((*b)->getName(), algo.getName()) == 0) {
363 a.erase(b);
364 break;
365 }
366 }
367 return (maxNoOfAlgos - (int)a.size());
368}
369
370int32_t ZrtpConfigure::getNumConfiguredAlgos(std::vector<AlgorithmEnum* >& a) {
371 return (int32_t)a.size();
372}
373
374bool ZrtpConfigure::containsAlgo(std::vector<AlgorithmEnum* >& a, AlgorithmEnum& algo) {
375
376 if ((int)a.size() == 0 || !algo.isValid())
377 return false;
378
379 std::vector<AlgorithmEnum* >::iterator b = a.begin();
380 std::vector<AlgorithmEnum* >::iterator e = a.end();
381
382 for (; b != e; ++b) {
383 if (strcmp((*b)->getName(), algo.getName()) == 0) {
384 return true;
385 }
386 }
387 return false;
388}
389
390void ZrtpConfigure::printConfiguredAlgos(std::vector<AlgorithmEnum* >& a) {
391
392 std::vector<AlgorithmEnum* >::iterator b = a.begin();
393 std::vector<AlgorithmEnum* >::iterator e = a.end();
394
395 for (; b != e; ++b) {
396 printf("print configured: name: %s\n", (*b)->getName());
397 }
398}
399
400std::vector<AlgorithmEnum* >& ZrtpConfigure::getEnum(AlgoTypes algoType) {
401
402 switch(algoType) {
403 case HashAlgorithm:
404 return hashes;
405 break;
406
407 case CipherAlgorithm:
408 return symCiphers;
409 break;
410
411 case PubKeyAlgorithm:
412 return publicKeyAlgos;
413 break;
414
415 case SasType:
416 return sasTypes;
417 break;
418
419 case AuthLength:
420 return authLengths;
421 break;
422
423 default:
424 break;
425 }
426 return hashes;
427}
428
429void ZrtpConfigure::setTrustedMitM(bool yesNo) {
430 enableTrustedMitM = yesNo;
431}
432
433bool ZrtpConfigure::isTrustedMitM() {
434 return enableTrustedMitM;
435}
436
437void ZrtpConfigure::setSasSignature(bool yesNo) {
438 enableSasSignature = yesNo;
439}
440
441bool ZrtpConfigure::isSasSignature() {
442 return enableSasSignature;
443}
444
445void ZrtpConfigure::setParanoidMode(bool yesNo) {
446 enableParanoidMode = yesNo;
447}
448
449bool ZrtpConfigure::isParanoidMode() {
450 return enableParanoidMode;
451}
452
453#if 0
454ZrtpConfigure config;
455
456main() {
457 printf("Start\n");
458 printf("size: %d\n", zrtpHashes.getSize());
459 AlgorithmEnum e = zrtpHashes.getByName("S256");
460 printf("algo name: %s\n", e.getName());
461 printf("algo type: %d\n", e.getAlgoType());
462
463 std::list<std::string>* names = zrtpHashes.getAllNames();
464 printf("size of name list: %d\n", names->size());
465 printf("first name: %s\n", names->front().c_str());
466 printf("last name: %s\n", names->back().c_str());
467
468 printf("free slots: %d (expected 6)\n", config.addAlgo(HashAlgorithm, e));
469
470 AlgorithmEnum e1(HashAlgorithm, "SHA384");
471 printf("free slots: %d (expected 5)\n", config.addAlgoAt(HashAlgorithm, e1, 0));
472 AlgorithmEnum e2 = config.getAlgoAt(HashAlgorithm, 0);
473 printf("algo name: %s (expected SHA384)\n", e2.getName());
474 printf("Num of configured algos: %d (expected 2)\n", config.getNumConfiguredAlgos(HashAlgorithm));
475 config.printConfiguredAlgos(HashAlgorithm);
476 printf("free slots: %d (expected 6)\n", config.removeAlgo(HashAlgorithm, e2));
477 e2 = config.getAlgoAt(HashAlgorithm, 0);
478 printf("algo name: %s (expected SHA256)\n", e2.getName());
479
480 printf("clearing config\n");
481 config.clear();
482 printf("size: %d\n", zrtpHashes.getSize());
483 e = zrtpHashes.getByName("S256");
484 printf("algo name: %s\n", e.getName());
485 printf("algo type: %d\n", e.getAlgoType());
486
487}
488
489#endif
490/** EMACS **
491 * Local variables:
492 * mode: c++
493 * c-default-style: ellemtel
494 * c-basic-offset: 4
495 * End:
496 */