blob: 1e1d2c88286521b0807d9cd013c19338dceb7121 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
2 This class maps the ZRTP C calls to ZRTP C++ methods.
3 Copyright (C) 2010 Werner Dittmann
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20#include <libzrtpcpp/ZrtpCallback.h>
21#include <libzrtpcpp/ZrtpConfigure.h>
22#include <libzrtpcpp/ZIDFile.h>
23#include <libzrtpcpp/ZRtp.h>
24#include <libzrtpcpp/ZrtpCallbackWrapper.h>
25#include <libzrtpcpp/ZrtpCWrapper.h>
26#include <libzrtpcpp/ZrtpCrc32.h>
27
28static int32_t zrtp_initZidFile(const char* zidFilename);
29
30ZrtpContext* zrtp_CreateWrapper()
31{
32 ZrtpContext* zc = new ZrtpContext;
33 zc->configure = 0;
34 zc->zrtpEngine = 0;
35 zc->zrtpCallback = 0;
36
37 return zc;
38}
39
40void zrtp_initializeZrtpEngine(ZrtpContext* zrtpContext,
41 zrtp_Callbacks *cb, const char* id,
42 const char* zidFilename,
43 void* userData,
44 int32_t mitmMode)
45{
46 std::string clientIdString(id);
47
48 zrtpContext->zrtpCallback = new ZrtpCallbackWrapper(cb, zrtpContext);
49 zrtpContext->userData = userData;
50
51 if (zrtpContext->configure == 0) {
52 zrtpContext->configure = new ZrtpConfigure();
53 zrtpContext->configure->setStandardConfig();
54 }
55
56 // Initialize ZID file (cache) and get my own ZID
57 zrtp_initZidFile(zidFilename);
58 ZIDFile* zf = ZIDFile::getInstance();
59 const unsigned char* myZid = zf->getZid();
60
61 zrtpContext->zrtpEngine = new ZRtp((uint8_t*)myZid, zrtpContext->zrtpCallback,
62 clientIdString, zrtpContext->configure, mitmMode == 0 ? false : true);
63}
64
65void zrtp_DestroyWrapper(ZrtpContext* zrtpContext) {
66
67 if (zrtpContext == NULL)
68 return;
69
70 delete zrtpContext->zrtpEngine;
71 zrtpContext->zrtpEngine = NULL;
72
73 delete zrtpContext->zrtpCallback;
74 zrtpContext->zrtpCallback = NULL;
75
76 delete zrtpContext->configure;
77 zrtpContext->configure = NULL;
78
79 delete zrtpContext;
80}
81
82static int32_t zrtp_initZidFile(const char* zidFilename) {
83 ZIDFile* zf = ZIDFile::getInstance();
84
85 if (!zf->isOpen()) {
86 std::string fname;
87 if (zidFilename == NULL) {
88 char *home = getenv("HOME");
89 std::string baseDir = (home != NULL) ? (std::string(home) + std::string("/."))
90 : std::string(".");
91 fname = baseDir + std::string("GNUccRTP.zid");
92 zidFilename = fname.c_str();
93 }
94 return zf->open((char *)zidFilename);
95 }
96 return 0;
97}
98
99int32_t zrtp_CheckCksum(uint8_t* buffer, uint16_t temp, uint32_t crc)
100{
101 return zrtpCheckCksum(buffer, temp, crc);
102}
103
104uint32_t zrtp_GenerateCksum(uint8_t* buffer, uint16_t temp)
105{
106 return zrtpGenerateCksum(buffer, temp);
107}
108
109uint32_t zrtp_EndCksum(uint32_t crc)
110{
111 return zrtpEndCksum(crc);
112}
113
114/*
115 * Applications use the following methods to control ZRTP, for example
116 * to enable ZRTP, set flags etc.
117 */
118void zrtp_startZrtpEngine(ZrtpContext* zrtpContext) {
119 if (zrtpContext && zrtpContext->zrtpEngine)
120 zrtpContext->zrtpEngine->startZrtpEngine();
121}
122
123void zrtp_stopZrtpEngine(ZrtpContext* zrtpContext) {
124 if (zrtpContext && zrtpContext->zrtpEngine)
125 zrtpContext->zrtpEngine->stopZrtp();
126}
127
128void zrtp_processZrtpMessage(ZrtpContext* zrtpContext, uint8_t *extHeader, uint32_t peerSSRC) {
129 if (zrtpContext && zrtpContext->zrtpEngine)
130 zrtpContext->zrtpEngine->processZrtpMessage(extHeader, peerSSRC);
131}
132
133void zrtp_processTimeout(ZrtpContext* zrtpContext) {
134 if (zrtpContext && zrtpContext->zrtpEngine)
135 zrtpContext->zrtpEngine->processTimeout();
136}
137
138//int32_t zrtp_handleGoClear(ZrtpContext* zrtpContext, uint8_t *extHeader)
139//{
140// if (zrtpContext && zrtpContext->zrtpEngine)
141// return zrtpContext->zrtpEngine->handleGoClear(extHeader) ? 1 : 0;
142//
143// return 0;
144//}
145
146void zrtp_setAuxSecret(ZrtpContext* zrtpContext, uint8_t* data, int32_t length) {
147 if (zrtpContext && zrtpContext->zrtpEngine)
148 zrtpContext->zrtpEngine->setAuxSecret(data, length);
149}
150
151int32_t zrtp_inState(ZrtpContext* zrtpContext, int32_t state) {
152 if (zrtpContext && zrtpContext->zrtpEngine)
153 return zrtpContext->zrtpEngine->inState(state) ? 1 : 0;
154
155 return 0;
156}
157
158void zrtp_SASVerified(ZrtpContext* zrtpContext) {
159 if (zrtpContext && zrtpContext->zrtpEngine)
160 zrtpContext->zrtpEngine->SASVerified();
161}
162
163void zrtp_resetSASVerified(ZrtpContext* zrtpContext) {
164 if (zrtpContext && zrtpContext->zrtpEngine)
165 zrtpContext->zrtpEngine->resetSASVerified();
166}
167
168char* zrtp_getHelloHash(ZrtpContext* zrtpContext) {
169 std::string ret;
170 if (zrtpContext && zrtpContext->zrtpEngine)
171 ret = zrtpContext->zrtpEngine->getHelloHash();
172 else
173 return NULL;
174
175 if (ret.size() == 0)
176 return NULL;
177
178 char* retval = (char*)malloc(ret.size()+1);
179 strcpy(retval, ret.c_str());
180 return retval;
181}
182
183char* zrtp_getPeerHelloHash(ZrtpContext* zrtpContext) {
184 std::string ret;
185 if (zrtpContext && zrtpContext->zrtpEngine)
186 ret = zrtpContext->zrtpEngine->getPeerHelloHash();
187 else
188 return NULL;
189
190 if (ret.size() == 0)
191 return NULL;
192
193 char* retval = (char*)malloc(ret.size()+1);
194 strcpy(retval, ret.c_str());
195 return retval;
196}
197
198char* zrtp_getMultiStrParams(ZrtpContext* zrtpContext, int32_t *length) {
199 std::string ret;
200
201 *length = 0;
202 if (zrtpContext && zrtpContext->zrtpEngine)
203 ret = zrtpContext->zrtpEngine->getMultiStrParams();
204 else
205 return NULL;
206
207 if (ret.size() == 0)
208 return NULL;
209
210 *length = ret.size();
211 char* retval = (char*) malloc(ret.size());
212 ret.copy(retval, ret.size(), 0);
213 return retval;
214}
215
216void zrtp_setMultiStrParams(ZrtpContext* zrtpContext, char* parameters, int32_t length) {
217 if (!zrtpContext || !zrtpContext->zrtpEngine)
218 return;
219
220 if (parameters == NULL)
221 return;
222
223 std::string str("");
224 str.assign(parameters, length); // set chars (bytes) to the string
225
226 zrtpContext->zrtpEngine->setMultiStrParams(str);
227}
228
229int32_t zrtp_isMultiStream(ZrtpContext* zrtpContext) {
230 if (zrtpContext && zrtpContext->zrtpEngine)
231 return zrtpContext->zrtpEngine->isMultiStream() ? 1 : 0;
232
233 return 0;
234}
235
236int32_t zrtp_isMultiStreamAvailable(ZrtpContext* zrtpContext) {
237 if (zrtpContext && zrtpContext->zrtpEngine)
238 return zrtpContext->zrtpEngine->isMultiStreamAvailable() ? 1 : 0;
239
240 return 0;
241}
242
243void zrtp_acceptEnrollment(ZrtpContext* zrtpContext, int32_t accepted) {
244 if (zrtpContext && zrtpContext->zrtpEngine)
245 return zrtpContext->zrtpEngine->acceptEnrollment(accepted == 0 ? false : true);
246}
247
248int32_t zrtp_isEnrollmentMode(ZrtpContext* zrtpContext) {
249 if (zrtpContext && zrtpContext->zrtpEngine)
250 return zrtpContext->zrtpEngine->isEnrollmentMode() ? 1 : 0;
251
252 return 0;
253}
254
255void zrtp_setEnrollmentMode(ZrtpContext* zrtpContext, int32_t enrollmentMode) {
256 if (zrtpContext && zrtpContext->zrtpEngine)
257 return zrtpContext->zrtpEngine->setEnrollmentMode(enrollmentMode == 0 ? false : true);
258}
259
260int32_t isPeerEnrolled(ZrtpContext* zrtpContext) {
261 if (zrtpContext && zrtpContext->zrtpEngine)
262 return zrtpContext->zrtpEngine->isPeerEnrolled() ? 1 : 0;
263
264 return 0;
265}
266
267int32_t zrtp_sendSASRelayPacket(ZrtpContext* zrtpContext, uint8_t* sh, char* render) {
268 if (zrtpContext && zrtpContext->zrtpEngine) {
269 std::string rn(render);
270 return zrtpContext->zrtpEngine->sendSASRelayPacket(sh, rn) ? 1 : 0;
271 }
272 return 0;
273}
274
275
276const char* zrtp_getSasType(ZrtpContext* zrtpContext) {
277 if (zrtpContext && zrtpContext->zrtpEngine) {
278 std::string rn = zrtpContext->zrtpEngine->getSasType();
279 return rn.c_str();
280 }
281 return NULL;
282}
283
284
285uint8_t* zrtp_getSasHash(ZrtpContext* zrtpContext) {
286 if (zrtpContext && zrtpContext->zrtpEngine)
287 return zrtpContext->zrtpEngine->getSasHash();
288
289 return NULL;
290}
291
292int32_t zrtp_setSignatureData(ZrtpContext* zrtpContext, uint8_t* data, int32_t length) {
293 if (zrtpContext && zrtpContext->zrtpEngine)
294 return zrtpContext->zrtpEngine->setSignatureData(data, length) ? 1 : 0;
295
296 return 0;
297}
298
299const uint8_t* zrtp_getSignatureData(ZrtpContext* zrtpContext) {
300 if (zrtpContext && zrtpContext->zrtpEngine)
301 return zrtpContext->zrtpEngine->getSignatureData();
302
303 return 0;
304}
305
306int32_t zrtp_getSignatureLength(ZrtpContext* zrtpContext) {
307 if (zrtpContext && zrtpContext->zrtpEngine)
308 return zrtpContext->zrtpEngine->getSignatureLength();
309
310 return 0;
311}
312
313void zrtp_conf2AckSecure(ZrtpContext* zrtpContext) {
314 if (zrtpContext && zrtpContext->zrtpEngine)
315 zrtpContext->zrtpEngine->conf2AckSecure();
316}
317
318int32_t zrtp_getPeerZid(ZrtpContext* zrtpContext, uint8_t* data) {
319 if (data == NULL)
320 return 0;
321
322 if (zrtpContext && zrtpContext->zrtpEngine)
323 return zrtpContext->zrtpEngine->getPeerZid(data);
324
325 return 0;
326}
327
328/*
329 * The following methods wrap the ZRTP Configure functions
330 */
331int32_t zrtp_InitializeConfig (ZrtpContext* zrtpContext)
332{
333 zrtpContext->configure = new ZrtpConfigure();
334 return 1;
335}
336
337static EnumBase* getEnumBase(zrtp_AlgoTypes type)
338{
339 switch(type) {
340 case zrtp_HashAlgorithm:
341 return &zrtpHashes;
342 break;
343
344 case zrtp_CipherAlgorithm:
345 return &zrtpSymCiphers;
346 break;
347
348 case zrtp_PubKeyAlgorithm:
349 return &zrtpPubKeys;
350 break;
351
352 case zrtp_SasType:
353 return &zrtpSasTypes;
354 break;
355
356 case zrtp_AuthLength:
357 return &zrtpAuthLengths;
358 break;
359
360 default:
361 return NULL;
362 }
363}
364
365char** zrtp_getAlgorithmNames(ZrtpContext* zrtpContext, Zrtp_AlgoTypes type)
366{
367 std::list<std::string>* names = NULL;
368 EnumBase* base = getEnumBase(type);
369
370 if (!base)
371 return NULL;
372
373 names = base->getAllNames();
374 int size = base->getSize();
375 char** cNames = new char* [size+1];
376 cNames[size] = NULL;
377
378 std::list<std::string >::iterator b = names->begin();
379 std::list<std::string >::iterator e = names->end();
380
381 for (int i = 0; b != e; b++, i++) {
382 cNames[i] = new char [(*b).size()+1];
383 strcpy(cNames[i], (*b).c_str());
384 }
385 return cNames;
386}
387
388void zrtp_freeAlgorithmNames(char** names)
389{
390 if (!names)
391 return;
392
393 for (char** cp = names; *cp; cp++)
394 delete *cp;
395
396 delete names;
397}
398
399void zrtp_setStandardConfig(ZrtpContext* zrtpContext)
400{
401 zrtpContext->configure->setStandardConfig();
402}
403
404void zrtp_setMandatoryOnly(ZrtpContext* zrtpContext)
405{
406 zrtpContext->configure->setMandatoryOnly();
407}
408
409int32_t zrtp_addAlgo(ZrtpContext* zrtpContext, zrtp_AlgoTypes algoType, const char* algo)
410{
411 EnumBase* base = getEnumBase(algoType);
412 AlgorithmEnum& a = base->getByName(algo);
413
414 return zrtpContext->configure->addAlgo((AlgoTypes)algoType, a);
415}
416
417int32_t zrtp_addAlgoAt(ZrtpContext* zrtpContext, zrtp_AlgoTypes algoType, const char* algo, int32_t index)
418{
419 EnumBase* base = getEnumBase(algoType);
420 AlgorithmEnum& a = base->getByName(algo);
421
422 return zrtpContext->configure->addAlgoAt((AlgoTypes)algoType, a, index);
423}
424
425int32_t zrtp_removeAlgo(ZrtpContext* zrtpContext, zrtp_AlgoTypes algoType, const char* algo)
426{
427 EnumBase* base = getEnumBase(algoType);
428 AlgorithmEnum& a = base->getByName(algo);
429
430 return zrtpContext->configure->removeAlgo((AlgoTypes)algoType, a);
431}
432
433int32_t zrtp_getNumConfiguredAlgos(ZrtpContext* zrtpContext, zrtp_AlgoTypes algoType)
434{
435 return zrtpContext->configure->getNumConfiguredAlgos((AlgoTypes)algoType);
436}
437
438const char* zrtp_getAlgoAt(ZrtpContext* zrtpContext, Zrtp_AlgoTypes algoType, int32_t index)
439{
440 AlgorithmEnum& a = zrtpContext->configure->getAlgoAt((AlgoTypes)algoType, index);
441 return a.getName();
442}
443
444int32_t zrtp_containsAlgo(ZrtpContext* zrtpContext, Zrtp_AlgoTypes algoType, const char* algo)
445{
446 EnumBase* base = getEnumBase(algoType);
447 AlgorithmEnum& a = base->getByName(algo);
448
449 return zrtpContext->configure->containsAlgo((AlgoTypes)algoType, a) ? 1 : 0;
450}
451
452void zrtp_setTrustedMitM(ZrtpContext* zrtpContext, int32_t yesNo)
453{
454 zrtpContext->configure->setTrustedMitM(yesNo ? true : false);
455}
456
457int32_t zrtp_isTrustedMitM(ZrtpContext* zrtpContext)
458{
459 return zrtpContext->configure->isTrustedMitM() ? 1 : 0;
460}
461
462void zrtp_setSasSignature(ZrtpContext* zrtpContext, int32_t yesNo)
463{
464 zrtpContext->configure->setSasSignature(yesNo ? true : false);
465}
466
467int32_t zrtp_isSasSignature(ZrtpContext* zrtpContext)
468{
469 return zrtpContext->configure->isSasSignature() ? 1 : 0;
470}