blob: 7f7c3dd7ce2ed0fdd7e5079722521d2c6952b226 [file] [log] [blame]
Alexandre Lision51140e12013-12-02 10:54:09 -05001/*
2 This class maps the ZRTP C calls to ZRTP C++ methods.
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05003 Copyright (C) 2010-2013 Werner Dittmann
Alexandre Lision51140e12013-12-02 10:54:09 -05004
5 This program is free software: you can redistribute it and/or modify
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05006 it under the terms of the GNU Lesser General Public License as published by
Alexandre Lision51140e12013-12-02 10:54:09 -05007 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>
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050022#include <libzrtpcpp/ZIDCache.h>
Alexandre Lision51140e12013-12-02 10:54:09 -050023#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);
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050058 const unsigned char* myZid = getZidCacheInstance()->getZid();
Alexandre Lision51140e12013-12-02 10:54:09 -050059
60 zrtpContext->zrtpEngine = new ZRtp((uint8_t*)myZid, zrtpContext->zrtpCallback,
61 clientIdString, zrtpContext->configure, mitmMode == 0 ? false : true);
62}
63
64void zrtp_DestroyWrapper(ZrtpContext* zrtpContext) {
65
66 if (zrtpContext == NULL)
67 return;
68
69 delete zrtpContext->zrtpEngine;
70 zrtpContext->zrtpEngine = NULL;
71
72 delete zrtpContext->zrtpCallback;
73 zrtpContext->zrtpCallback = NULL;
74
75 delete zrtpContext->configure;
76 zrtpContext->configure = NULL;
77
78 delete zrtpContext;
79}
80
81static int32_t zrtp_initZidFile(const char* zidFilename) {
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -050082 ZIDCache* zf = getZidCacheInstance();
Alexandre Lision51140e12013-12-02 10:54:09 -050083
84 if (!zf->isOpen()) {
85 std::string fname;
86 if (zidFilename == NULL) {
87 char *home = getenv("HOME");
88 std::string baseDir = (home != NULL) ? (std::string(home) + std::string("/."))
89 : std::string(".");
90 fname = baseDir + std::string("GNUccRTP.zid");
91 zidFilename = fname.c_str();
92 }
93 return zf->open((char *)zidFilename);
94 }
95 return 0;
96}
97
98int32_t zrtp_CheckCksum(uint8_t* buffer, uint16_t temp, uint32_t crc)
99{
100 return zrtpCheckCksum(buffer, temp, crc);
101}
102
103uint32_t zrtp_GenerateCksum(uint8_t* buffer, uint16_t temp)
104{
105 return zrtpGenerateCksum(buffer, temp);
106}
107
108uint32_t zrtp_EndCksum(uint32_t crc)
109{
110 return zrtpEndCksum(crc);
111}
112
113/*
114 * Applications use the following methods to control ZRTP, for example
115 * to enable ZRTP, set flags etc.
116 */
117void zrtp_startZrtpEngine(ZrtpContext* zrtpContext) {
118 if (zrtpContext && zrtpContext->zrtpEngine)
119 zrtpContext->zrtpEngine->startZrtpEngine();
120}
121
122void zrtp_stopZrtpEngine(ZrtpContext* zrtpContext) {
123 if (zrtpContext && zrtpContext->zrtpEngine)
124 zrtpContext->zrtpEngine->stopZrtp();
125}
126
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500127void zrtp_processZrtpMessage(ZrtpContext* zrtpContext, uint8_t *extHeader, uint32_t peerSSRC, size_t length) {
Alexandre Lision51140e12013-12-02 10:54:09 -0500128 if (zrtpContext && zrtpContext->zrtpEngine)
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500129 zrtpContext->zrtpEngine->processZrtpMessage(extHeader, peerSSRC, length);
Alexandre Lision51140e12013-12-02 10:54:09 -0500130}
131
132void zrtp_processTimeout(ZrtpContext* zrtpContext) {
133 if (zrtpContext && zrtpContext->zrtpEngine)
134 zrtpContext->zrtpEngine->processTimeout();
135}
136
137//int32_t zrtp_handleGoClear(ZrtpContext* zrtpContext, uint8_t *extHeader)
138//{
139// if (zrtpContext && zrtpContext->zrtpEngine)
140// return zrtpContext->zrtpEngine->handleGoClear(extHeader) ? 1 : 0;
141//
142// return 0;
143//}
144
145void zrtp_setAuxSecret(ZrtpContext* zrtpContext, uint8_t* data, int32_t length) {
146 if (zrtpContext && zrtpContext->zrtpEngine)
147 zrtpContext->zrtpEngine->setAuxSecret(data, length);
148}
149
150int32_t zrtp_inState(ZrtpContext* zrtpContext, int32_t state) {
151 if (zrtpContext && zrtpContext->zrtpEngine)
152 return zrtpContext->zrtpEngine->inState(state) ? 1 : 0;
153
154 return 0;
155}
156
157void zrtp_SASVerified(ZrtpContext* zrtpContext) {
158 if (zrtpContext && zrtpContext->zrtpEngine)
159 zrtpContext->zrtpEngine->SASVerified();
160}
161
162void zrtp_resetSASVerified(ZrtpContext* zrtpContext) {
163 if (zrtpContext && zrtpContext->zrtpEngine)
164 zrtpContext->zrtpEngine->resetSASVerified();
165}
166
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500167char* zrtp_getHelloHash(ZrtpContext* zrtpContext, int32_t index) {
Alexandre Lision51140e12013-12-02 10:54:09 -0500168 std::string ret;
169 if (zrtpContext && zrtpContext->zrtpEngine)
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500170 ret = zrtpContext->zrtpEngine->getHelloHash(index);
Alexandre Lision51140e12013-12-02 10:54:09 -0500171 else
172 return NULL;
173
174 if (ret.size() == 0)
175 return NULL;
176
177 char* retval = (char*)malloc(ret.size()+1);
178 strcpy(retval, ret.c_str());
179 return retval;
180}
181
182char* zrtp_getPeerHelloHash(ZrtpContext* zrtpContext) {
183 std::string ret;
184 if (zrtpContext && zrtpContext->zrtpEngine)
185 ret = zrtpContext->zrtpEngine->getPeerHelloHash();
186 else
187 return NULL;
188
189 if (ret.size() == 0)
190 return NULL;
191
192 char* retval = (char*)malloc(ret.size()+1);
193 strcpy(retval, ret.c_str());
194 return retval;
195}
196
197char* zrtp_getMultiStrParams(ZrtpContext* zrtpContext, int32_t *length) {
198 std::string ret;
199
200 *length = 0;
201 if (zrtpContext && zrtpContext->zrtpEngine)
202 ret = zrtpContext->zrtpEngine->getMultiStrParams();
203 else
204 return NULL;
205
206 if (ret.size() == 0)
207 return NULL;
208
209 *length = ret.size();
210 char* retval = (char*) malloc(ret.size());
211 ret.copy(retval, ret.size(), 0);
212 return retval;
213}
214
215void zrtp_setMultiStrParams(ZrtpContext* zrtpContext, char* parameters, int32_t length) {
216 if (!zrtpContext || !zrtpContext->zrtpEngine)
217 return;
218
219 if (parameters == NULL)
220 return;
221
222 std::string str("");
223 str.assign(parameters, length); // set chars (bytes) to the string
224
225 zrtpContext->zrtpEngine->setMultiStrParams(str);
226}
227
228int32_t zrtp_isMultiStream(ZrtpContext* zrtpContext) {
229 if (zrtpContext && zrtpContext->zrtpEngine)
230 return zrtpContext->zrtpEngine->isMultiStream() ? 1 : 0;
231
232 return 0;
233}
234
235int32_t zrtp_isMultiStreamAvailable(ZrtpContext* zrtpContext) {
236 if (zrtpContext && zrtpContext->zrtpEngine)
237 return zrtpContext->zrtpEngine->isMultiStreamAvailable() ? 1 : 0;
238
239 return 0;
240}
241
242void zrtp_acceptEnrollment(ZrtpContext* zrtpContext, int32_t accepted) {
243 if (zrtpContext && zrtpContext->zrtpEngine)
244 return zrtpContext->zrtpEngine->acceptEnrollment(accepted == 0 ? false : true);
245}
246
247int32_t zrtp_isEnrollmentMode(ZrtpContext* zrtpContext) {
248 if (zrtpContext && zrtpContext->zrtpEngine)
249 return zrtpContext->zrtpEngine->isEnrollmentMode() ? 1 : 0;
250
251 return 0;
252}
253
254void zrtp_setEnrollmentMode(ZrtpContext* zrtpContext, int32_t enrollmentMode) {
255 if (zrtpContext && zrtpContext->zrtpEngine)
256 return zrtpContext->zrtpEngine->setEnrollmentMode(enrollmentMode == 0 ? false : true);
257}
258
259int32_t isPeerEnrolled(ZrtpContext* zrtpContext) {
260 if (zrtpContext && zrtpContext->zrtpEngine)
261 return zrtpContext->zrtpEngine->isPeerEnrolled() ? 1 : 0;
262
263 return 0;
264}
265
266int32_t zrtp_sendSASRelayPacket(ZrtpContext* zrtpContext, uint8_t* sh, char* render) {
267 if (zrtpContext && zrtpContext->zrtpEngine) {
268 std::string rn(render);
269 return zrtpContext->zrtpEngine->sendSASRelayPacket(sh, rn) ? 1 : 0;
270 }
271 return 0;
272}
273
274
275const char* zrtp_getSasType(ZrtpContext* zrtpContext) {
276 if (zrtpContext && zrtpContext->zrtpEngine) {
277 std::string rn = zrtpContext->zrtpEngine->getSasType();
278 return rn.c_str();
279 }
280 return NULL;
281}
282
283
284uint8_t* zrtp_getSasHash(ZrtpContext* zrtpContext) {
285 if (zrtpContext && zrtpContext->zrtpEngine)
286 return zrtpContext->zrtpEngine->getSasHash();
287
288 return NULL;
289}
290
291int32_t zrtp_setSignatureData(ZrtpContext* zrtpContext, uint8_t* data, int32_t length) {
292 if (zrtpContext && zrtpContext->zrtpEngine)
293 return zrtpContext->zrtpEngine->setSignatureData(data, length) ? 1 : 0;
294
295 return 0;
296}
297
298const uint8_t* zrtp_getSignatureData(ZrtpContext* zrtpContext) {
299 if (zrtpContext && zrtpContext->zrtpEngine)
300 return zrtpContext->zrtpEngine->getSignatureData();
301
302 return 0;
303}
304
305int32_t zrtp_getSignatureLength(ZrtpContext* zrtpContext) {
306 if (zrtpContext && zrtpContext->zrtpEngine)
307 return zrtpContext->zrtpEngine->getSignatureLength();
308
309 return 0;
310}
311
312void zrtp_conf2AckSecure(ZrtpContext* zrtpContext) {
313 if (zrtpContext && zrtpContext->zrtpEngine)
314 zrtpContext->zrtpEngine->conf2AckSecure();
315}
316
317int32_t zrtp_getPeerZid(ZrtpContext* zrtpContext, uint8_t* data) {
318 if (data == NULL)
319 return 0;
320
321 if (zrtpContext && zrtpContext->zrtpEngine)
322 return zrtpContext->zrtpEngine->getPeerZid(data);
323
324 return 0;
325}
326
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -0500327int32_t zrtp_getNumberSupportedVersions(ZrtpContext* zrtpContext) {
328 return zrtpContext->zrtpEngine->getNumberSupportedVersions();
329}
330
331int32_t zrtp_getCurrentProtocolVersion(ZrtpContext* zrtpContext) {
332 return zrtpContext->zrtpEngine->getCurrentProtocolVersion();
333}
Alexandre Lision51140e12013-12-02 10:54:09 -0500334/*
335 * The following methods wrap the ZRTP Configure functions
336 */
337int32_t zrtp_InitializeConfig (ZrtpContext* zrtpContext)
338{
339 zrtpContext->configure = new ZrtpConfigure();
340 return 1;
341}
342
343static EnumBase* getEnumBase(zrtp_AlgoTypes type)
344{
345 switch(type) {
346 case zrtp_HashAlgorithm:
347 return &zrtpHashes;
348 break;
349
350 case zrtp_CipherAlgorithm:
351 return &zrtpSymCiphers;
352 break;
353
354 case zrtp_PubKeyAlgorithm:
355 return &zrtpPubKeys;
356 break;
357
358 case zrtp_SasType:
359 return &zrtpSasTypes;
360 break;
361
362 case zrtp_AuthLength:
363 return &zrtpAuthLengths;
364 break;
365
366 default:
367 return NULL;
368 }
369}
370
371char** zrtp_getAlgorithmNames(ZrtpContext* zrtpContext, Zrtp_AlgoTypes type)
372{
373 std::list<std::string>* names = NULL;
374 EnumBase* base = getEnumBase(type);
375
376 if (!base)
377 return NULL;
378
379 names = base->getAllNames();
380 int size = base->getSize();
381 char** cNames = new char* [size+1];
382 cNames[size] = NULL;
383
384 std::list<std::string >::iterator b = names->begin();
385 std::list<std::string >::iterator e = names->end();
386
387 for (int i = 0; b != e; b++, i++) {
388 cNames[i] = new char [(*b).size()+1];
389 strcpy(cNames[i], (*b).c_str());
390 }
391 return cNames;
392}
393
394void zrtp_freeAlgorithmNames(char** names)
395{
396 if (!names)
397 return;
398
399 for (char** cp = names; *cp; cp++)
400 delete *cp;
401
402 delete names;
403}
404
405void zrtp_setStandardConfig(ZrtpContext* zrtpContext)
406{
407 zrtpContext->configure->setStandardConfig();
408}
409
410void zrtp_setMandatoryOnly(ZrtpContext* zrtpContext)
411{
412 zrtpContext->configure->setMandatoryOnly();
413}
414
415int32_t zrtp_addAlgo(ZrtpContext* zrtpContext, zrtp_AlgoTypes algoType, const char* algo)
416{
417 EnumBase* base = getEnumBase(algoType);
418 AlgorithmEnum& a = base->getByName(algo);
419
420 return zrtpContext->configure->addAlgo((AlgoTypes)algoType, a);
421}
422
423int32_t zrtp_addAlgoAt(ZrtpContext* zrtpContext, zrtp_AlgoTypes algoType, const char* algo, int32_t index)
424{
425 EnumBase* base = getEnumBase(algoType);
426 AlgorithmEnum& a = base->getByName(algo);
427
428 return zrtpContext->configure->addAlgoAt((AlgoTypes)algoType, a, index);
429}
430
431int32_t zrtp_removeAlgo(ZrtpContext* zrtpContext, zrtp_AlgoTypes algoType, const char* algo)
432{
433 EnumBase* base = getEnumBase(algoType);
434 AlgorithmEnum& a = base->getByName(algo);
435
436 return zrtpContext->configure->removeAlgo((AlgoTypes)algoType, a);
437}
438
439int32_t zrtp_getNumConfiguredAlgos(ZrtpContext* zrtpContext, zrtp_AlgoTypes algoType)
440{
441 return zrtpContext->configure->getNumConfiguredAlgos((AlgoTypes)algoType);
442}
443
444const char* zrtp_getAlgoAt(ZrtpContext* zrtpContext, Zrtp_AlgoTypes algoType, int32_t index)
445{
446 AlgorithmEnum& a = zrtpContext->configure->getAlgoAt((AlgoTypes)algoType, index);
447 return a.getName();
448}
449
450int32_t zrtp_containsAlgo(ZrtpContext* zrtpContext, Zrtp_AlgoTypes algoType, const char* algo)
451{
452 EnumBase* base = getEnumBase(algoType);
453 AlgorithmEnum& a = base->getByName(algo);
454
455 return zrtpContext->configure->containsAlgo((AlgoTypes)algoType, a) ? 1 : 0;
456}
457
458void zrtp_setTrustedMitM(ZrtpContext* zrtpContext, int32_t yesNo)
459{
460 zrtpContext->configure->setTrustedMitM(yesNo ? true : false);
461}
462
463int32_t zrtp_isTrustedMitM(ZrtpContext* zrtpContext)
464{
465 return zrtpContext->configure->isTrustedMitM() ? 1 : 0;
466}
467
468void zrtp_setSasSignature(ZrtpContext* zrtpContext, int32_t yesNo)
469{
470 zrtpContext->configure->setSasSignature(yesNo ? true : false);
471}
472
473int32_t zrtp_isSasSignature(ZrtpContext* zrtpContext)
474{
475 return zrtpContext->configure->isSasSignature() ? 1 : 0;
476}