1 /*
2  * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
3  * Copyright (c) 2006-2009 Philip R. Zimmermann.  All rights reserved.
4  * Contact: http://philzimmermann.com
5  * For licensing and other legal details, see the file zrtp_legal.c.
6  *
7  * Viktor Krykun <v.krikun at zfoneproject.com>
8  */
9 
10 #include "zrtp.h"
11 
12 #define _ZTU_ "zrtp main"
13 
14 /*----------------------------------------------------------------------------*/
15 extern zrtp_status_t zrtp_init_rng(zrtp_global_t* zrtp);
16 extern void zrtp_down_rng(zrtp_global_t* zrtp);
17 
18 extern zrtp_status_t zrtp_defaults_sas(zrtp_global_t* global_ctx);
19 extern zrtp_status_t zrtp_defaults_pkt(zrtp_global_t* global_ctx);
20 extern zrtp_status_t zrtp_defaults_atl(zrtp_global_t* global_ctx);
21 extern zrtp_status_t zrtp_defaults_aes_cipher(zrtp_global_t* global_ctx);
22 extern zrtp_status_t zrtp_defaults_hash(zrtp_global_t* global_ctx);
23 extern zrtp_status_t zrtp_prepare_pkt();
24 extern zrtp_status_t zrtp_done_pkt();
25 
26 
zrtp_config_defaults(zrtp_config_t * config)27 void zrtp_config_defaults(zrtp_config_t* config)
28 {
29 	zrtp_memset(config, 0, sizeof(zrtp_config_t));
30 
31 	zrtp_memcpy(config->client_id, "ZRTP def. peer", 15);
32 	config->lic_mode = ZRTP_LICENSE_MODE_PASSIVE;
33 
34 	ZSTR_SET_EMPTY(config->def_cache_path);
35 	zrtp_zstrncpyc(ZSTR_GV(config->def_cache_path), "./zrtp_def_cache_path.dat", 25);
36 
37 	config->cache_auto_store = 1; /* cache auto flushing should be enabled by default */
38 
39 #if (defined(ZRTP_USE_BUILTIN_CACHE) && (ZRTP_USE_BUILTIN_CACHE == 1))
40 	config->cb.cache_cb.on_init					= zrtp_def_cache_init;
41 	config->cb.cache_cb.on_down					= zrtp_def_cache_down;
42 	config->cb.cache_cb.on_put					= zrtp_def_cache_put;
43 	config->cb.cache_cb.on_put_mitm				= zrtp_def_cache_put_mitm;
44 	config->cb.cache_cb.on_get					= zrtp_def_cache_get;
45 	config->cb.cache_cb.on_get_mitm				= zrtp_def_cache_get_mitm;
46 	config->cb.cache_cb.on_set_verified			= zrtp_def_cache_set_verified;
47 	config->cb.cache_cb.on_get_verified			= zrtp_def_cache_get_verified;
48 	config->cb.cache_cb.on_reset_since			= zrtp_def_cache_reset_since;
49 	config->cb.cache_cb.on_presh_counter_set	= zrtp_def_cache_set_presh_counter;
50 	config->cb.cache_cb.on_presh_counter_get	= zrtp_def_cache_get_presh_counter;
51 #endif
52 
53 #if (defined(ZRTP_USE_BUILTIN_SCEHDULER) && (ZRTP_USE_BUILTIN_SCEHDULER == 1))
54 	config->cb.sched_cb.on_init					= zrtp_def_scheduler_init;
55 	config->cb.sched_cb.on_down					= zrtp_def_scheduler_down;
56 	config->cb.sched_cb.on_call_later			= zrtp_def_scheduler_call_later;
57 	config->cb.sched_cb.on_cancel_call_later	= zrtp_def_scheduler_cancel_call_later;
58 	config->cb.sched_cb.on_wait_call_later		= zrtp_def_scheduler_wait_call_later;
59 #endif
60 }
61 
zrtp_init(zrtp_config_t * config,zrtp_global_t ** zrtp)62 zrtp_status_t zrtp_init(zrtp_config_t* config, zrtp_global_t** zrtp)
63 {
64     zrtp_global_t* new_zrtp;
65     zrtp_status_t s = zrtp_status_ok;
66 
67 	ZRTP_LOG(3, (_ZTU_,"INITIALIZING LIBZRTP...\n"));
68 
69 	/* Print out configuration setting */
70 	zrtp_print_env_settings(config);
71 
72 	new_zrtp = zrtp_sys_alloc(sizeof(zrtp_global_t));
73 	if (!new_zrtp) {
74 		return zrtp_status_alloc_fail;
75     }
76 	zrtp_memset(new_zrtp, 0, sizeof(zrtp_global_t));
77 
78 	/*
79 	 * Apply configuration according to the config
80 	 */
81 	new_zrtp->lic_mode = config->lic_mode;
82 	new_zrtp->is_mitm = config->is_mitm;
83 	ZSTR_SET_EMPTY(new_zrtp->def_cache_path);
84 	zrtp_zstrcpy(ZSTR_GV(new_zrtp->def_cache_path), ZSTR_GV(config->def_cache_path));
85 	zrtp_memcpy(&new_zrtp->cb, &config->cb, sizeof(zrtp_callback_t));
86 	new_zrtp->cache_auto_store = config->cache_auto_store;
87 
88 	ZSTR_SET_EMPTY(new_zrtp->client_id);
89 	zrtp_memset(new_zrtp->client_id.buffer, ' ', sizeof(zrtp_client_id_t));
90 	zrtp_zstrncpyc( ZSTR_GV(new_zrtp->client_id),
91 					(const char*)config->client_id,
92 					sizeof(zrtp_client_id_t));
93 
94     /*
95 	 * General Initialization
96 	 */
97 	init_mlist(&new_zrtp->sessions_head);
98 
99     zrtp_mutex_init(&new_zrtp->sessions_protector);
100 
101     init_mlist(&new_zrtp->hash_head);
102     init_mlist(&new_zrtp->cipher_head);
103     init_mlist(&new_zrtp->atl_head);
104     init_mlist(&new_zrtp->pktype_head);
105     init_mlist(&new_zrtp->sas_head);
106 
107     /* Init RNG context */
108 	s = zrtp_init_rng(new_zrtp);
109     if (zrtp_status_ok != s) {
110 		ZRTP_LOG(1, (_ZTU_,"ERROR! zrtp_init_rng() failed:%s.\n", zrtp_log_status2str(s)));
111 		return zrtp_status_rng_fail;
112 	}
113 
114 	/* Initialize SRTP engine */
115 	s =  zrtp_srtp_init(new_zrtp);
116 	if (zrtp_status_ok != s) {
117 		ZRTP_LOG(1, (_ZTU_,"ERROR! zrtp_srtp_init() failed:<%s>\n", zrtp_log_status2str(s)));
118 		return zrtp_status_fail;
119     }
120 
121 	if (new_zrtp->cb.cache_cb.on_init)  {
122 		s = new_zrtp->cb.cache_cb.on_init(new_zrtp);
123 		if (zrtp_status_ok != s) {
124 			ZRTP_LOG(1, (_ZTU_,"ERROR! cache on_init() callback failed <%s>\n", zrtp_log_status2str(s)));
125 			zrtp_srtp_down(new_zrtp);
126 			return zrtp_status_fail;
127 		}
128 	}
129 
130 	if (new_zrtp->cb.sched_cb.on_init)  {
131 		s = new_zrtp->cb.sched_cb.on_init(new_zrtp);
132 		if (zrtp_status_ok != s) {
133 			ZRTP_LOG(1, (_ZTU_,"ERROR! scheduler on_init() callback failed <%s>\n", zrtp_log_status2str(s)));
134 			zrtp_srtp_down(new_zrtp);
135 			return zrtp_status_fail;
136 		}
137 	}
138 
139 	/* Load default crypto-components */
140     zrtp_prepare_pkt(new_zrtp);
141     zrtp_defaults_sas(new_zrtp);
142     zrtp_defaults_pkt(new_zrtp);
143     zrtp_defaults_atl(new_zrtp);
144     zrtp_defaults_aes_cipher(new_zrtp);
145     zrtp_defaults_hash(new_zrtp);
146 
147 	*zrtp = new_zrtp;
148 
149 	ZRTP_LOG(3, (_ZTU_,"INITIALIZING LIBZRTP - DONE\n"));
150     return  s;
151 }
152 
153 
154 /*----------------------------------------------------------------------------*/
zrtp_down(zrtp_global_t * zrtp)155 zrtp_status_t zrtp_down(zrtp_global_t* zrtp)
156 {
157 	ZRTP_LOG(3, (_ZTU_,"DESTROYING LIBZRTP...\n"));
158 
159     if (!zrtp) {
160 		return zrtp_status_bad_param;
161     }
162 
163     zrtp_comp_done(ZRTP_CC_HASH, zrtp);
164     zrtp_comp_done(ZRTP_CC_SAS, zrtp);
165     zrtp_comp_done(ZRTP_CC_CIPHER, zrtp);
166     zrtp_comp_done(ZRTP_CC_PKT, zrtp);
167     zrtp_comp_done(ZRTP_CC_ATL, zrtp);
168     zrtp_done_pkt(zrtp);
169 
170     zrtp_mutex_destroy(zrtp->sessions_protector);
171 
172 	zrtp_srtp_down(zrtp);
173 
174 	if (zrtp->cb.cache_cb.on_down) {
175 		zrtp->cb.cache_cb.on_down();
176 	}
177 	if (zrtp->cb.sched_cb.on_down) {
178 		zrtp->cb.sched_cb.on_down();
179 	}
180 
181 	zrtp_down_rng(zrtp);
182 
183 	zrtp_sys_free(zrtp);
184 
185 	ZRTP_LOG(3, (_ZTU_,"DESTROYING LIBZRTP - DONE\n"));
186 
187     return zrtp_status_ok;
188 }
189 
190 /*----------------------------------------------------------------------------*/
zrtp_session_init(zrtp_global_t * zrtp,zrtp_profile_t * profile,zrtp_zid_t zid,zrtp_signaling_role_t role,zrtp_session_t ** session)191 zrtp_status_t zrtp_session_init( zrtp_global_t* zrtp,
192 								zrtp_profile_t* profile,
193 								zrtp_zid_t zid,
194 								zrtp_signaling_role_t role,
195 								zrtp_session_t **session)
196 {
197     uint32_t i = 0;
198 	zrtp_status_t s = zrtp_status_fail;
199 	zrtp_session_t* new_session = NULL;
200 
201     if (!zrtp) {
202     	return zrtp_status_bad_param;
203     }
204 
205 	new_session = zrtp_sys_alloc(sizeof(zrtp_session_t));
206 	if (!new_session) {
207 		return zrtp_status_alloc_fail;
208 	}
209 
210     zrtp_memset(new_session, 0, sizeof(zrtp_session_t));
211 	new_session->id = zrtp->sessions_count++;
212 
213 	{
214 		zrtp_uchar32_t buff;
215 		ZRTP_LOG(3, (_ZTU_,"START SESSION INITIALIZATION. sID=%u.\n", new_session->id));
216 		ZRTP_LOG(3, (_ZTU_,"ZID=%s.\n", hex2str((const char*)zid, sizeof(zrtp_uchar12_t), (char*)buff, sizeof(buff)) ));
217 	}
218 
219 	do {
220 	/*
221 	 * Apply profile for the stream context: set flags and prepare Hello packet.
222 	 * If profile structure isn't provided, generate default.
223 	 */
224     if (!profile) {
225 		ZRTP_LOG(1, (_ZTU_,"Profile in NULL - loading default one.\n"));
226 		zrtp_profile_defaults(&new_session->profile, zrtp);
227     } else {
228 		ZRTP_LOG(1, (_ZTU_,"Loading User's profile:\n"));
229 		if (zrtp_status_ok != zrtp_profile_check(profile, zrtp)) {
230 			ZRTP_LOG(1, (_ZTU_,"ERROR! Can't apply wrong profile to the session sID=%u.\n", new_session->id));
231 			break;
232 		}
233 
234 		/* Adjust user's settings: force SHA-384 hash for ECDH-384P */
235 		if (zrtp_profile_find(profile, ZRTP_CC_PKT, ZRTP_PKTYPE_EC384P) > 0) {
236 			ZRTP_LOG(3, (_ZTU_,"User wants ECDH384 - auto-adjust profile to use SHA-384.\n"));
237 			profile->hash_schemes[0] = ZRTP_HASH_SHA384;
238 			profile->hash_schemes[1] = ZRTP_HASH_SHA256;
239 			profile->hash_schemes[2] = 0;
240 		}
241 
242 		zrtp_memcpy(&new_session->profile, profile, sizeof(zrtp_profile_t));
243 
244 		{
245 		int i;
246 		ZRTP_LOG(3, (_ZTU_,"   allowclear: %s\n", profile->allowclear?"ON":"OFF"));
247 		ZRTP_LOG(3, (_ZTU_,"   autosecure: %s\n", profile->autosecure?"ON":"OFF"));
248 		ZRTP_LOG(3, (_ZTU_," disclose_bit: %s\n", profile->disclose_bit?"ON":"OFF"));
249 		ZRTP_LOG(3, (_ZTU_," signal. role: %s\n", zrtp_log_sign_role2str(role)));
250 		ZRTP_LOG(3, (_ZTU_,"          TTL: %u\n", profile->cache_ttl));
251 
252 		ZRTP_LOG(3, (_ZTU_,"  SAS schemes: "));
253 		i=0;
254 		while (profile->sas_schemes[i]) {
255 			ZRTP_LOGC(3, ("%.4s ", zrtp_comp_id2type(ZRTP_CC_SAS, profile->sas_schemes[i++])));
256 		}
257 		ZRTP_LOGC(3, ("\n")); ZRTP_LOG(1, (_ZTU_,"     Ciphers: "));
258 		i=0;
259 		while (profile->cipher_types[i]) {
260 			ZRTP_LOGC(3, ("%.4s ", zrtp_comp_id2type(ZRTP_CC_CIPHER, profile->cipher_types[i++])));
261 		}
262 		ZRTP_LOGC(3, ("\n")); ZRTP_LOG(1, (_ZTU_,"   PK schemes: "));
263 		i=0;
264 		while (profile->pk_schemes[i]) {
265 			ZRTP_LOGC(3, ("%.4s ", zrtp_comp_id2type(ZRTP_CC_PKT, profile->pk_schemes[i++])));
266 		}
267 		ZRTP_LOGC(3, ("\n")); ZRTP_LOG(1, (_ZTU_,"          ATL: "));
268 		i=0;
269 		while (profile->auth_tag_lens[i]) {
270 			ZRTP_LOGC(3, ("%.4s ", zrtp_comp_id2type(ZRTP_CC_ATL, profile->auth_tag_lens[i++])));
271 		}
272 		ZRTP_LOGC(3, ("\n")); ZRTP_LOG(1, (_ZTU_,"      Hashes: "));
273 		i=0;
274 		while (profile->hash_schemes[i]) {
275 			ZRTP_LOGC(3, ("%.4s ", zrtp_comp_id2type(ZRTP_CC_HASH, profile->hash_schemes[i++])));
276 		}
277 		ZRTP_LOGC(3, ("\n"));
278 		}
279 	}
280 
281 	/* Set ZIDs */
282 	ZSTR_SET_EMPTY(new_session->zid);
283     ZSTR_SET_EMPTY(new_session->peer_zid);
284 	zrtp_zstrncpyc(ZSTR_GV(new_session->zid), (const char*)zid, sizeof(zrtp_zid_t));
285 
286 	new_session->zrtp = zrtp;
287 	new_session->signaling_role = role;
288 	new_session->mitm_alert_detected = 0;
289 
290 	/*
291 	 * Allocate memory for holding secrets and initialize with random values.
292 	 * Actual values will be written from the cache at the beginning of the protocol.
293 	 */
294 	new_session->secrets.rs1 = _zrtp_alloc_shared_secret(new_session);
295 	new_session->secrets.rs2 = _zrtp_alloc_shared_secret(new_session);
296 	new_session->secrets.auxs = _zrtp_alloc_shared_secret(new_session);
297 	new_session->secrets.pbxs = _zrtp_alloc_shared_secret(new_session);
298 
299 	if ( !new_session->secrets.rs1 || !new_session->secrets.rs2 ||
300 		 !new_session->secrets.auxs || !new_session->secrets.pbxs) {
301 		ZRTP_LOG(1, (_ZTU_,"ERROR! Can't allocate shared secrets sID=%u\n.", new_session->id));
302 		s = zrtp_status_alloc_fail;
303 		break;
304 	}
305 
306 	/* Initialize SAS values */
307 	ZSTR_SET_EMPTY(new_session->sas1);
308 	ZSTR_SET_EMPTY(new_session->sas2);
309 	ZSTR_SET_EMPTY(new_session->sasbin);
310 	ZSTR_SET_EMPTY(new_session->zrtpsess);
311 
312     /* Clear all stream structures */
313     for (i=0; i<ZRTP_MAX_STREAMS_PER_SESSION ; i++) {
314 		new_session->streams[i].state		= ZRTP_STATE_NONE;
315 		new_session->streams[i].prev_state	= ZRTP_STATE_NONE;
316 		new_session->streams[i].mode		= ZRTP_STREAM_MODE_UNKN;
317     }
318 
319     /* Initialize synchronization objects */
320 	s = zrtp_mutex_init(&new_session->streams_protector);
321     if (zrtp_status_ok != s) {
322 		ZRTP_LOG(1, (_ZTU_,"ERROR! can't initialize Stream protector. sID=%u.\n", new_session->id));
323 		break;
324 	}
325 	s = zrtp_mutex_init(&new_session->init_protector);
326     if (zrtp_status_ok != s) {
327 		ZRTP_LOG(1, (_ZTU_,"ERROR! can't initialize Init protector. sID=%u.\n", new_session->id));
328 		break;
329 	}
330 
331 	s = zrtp_status_ok;
332 	} while (0);
333 
334 	if (zrtp_status_ok != s) {
335 		zrtp_sys_free(new_session);
336 		return s;
337 	}
338 
339     /* Add new session to the global list */
340     zrtp_mutex_lock(zrtp->sessions_protector);
341     mlist_add(&zrtp->sessions_head, &new_session->_mlist);
342     zrtp_mutex_unlock(zrtp->sessions_protector);
343 
344 	*session = new_session;
345 
346     ZRTP_LOG(3, (_ZTU_,"Session initialization - DONE. sID=%u.\n\n", new_session->id));
347 
348     return zrtp_status_ok;
349 }
350 
351 /*----------------------------------------------------------------------------*/
zrtp_session_down(zrtp_session_t * session)352 void zrtp_session_down(zrtp_session_t *session)
353 {
354 	int i =0;
355 
356     if (!session) {
357 		return;
358 	}
359 
360 	/* Stop ZRTP engine and clear all crypto sources for every stream in the session. */
361 	zrtp_mutex_lock(session->streams_protector);
362 	for(i=0; i<ZRTP_MAX_STREAMS_PER_SESSION; i++) {
363 		zrtp_stream_t *the_stream = &session->streams[i];
364 		zrtp_stream_stop(the_stream);
365 	}
366 	zrtp_mutex_unlock(session->streams_protector);
367 
368 	/* Release memory allocated on initialization */
369 	if (session->secrets.rs1) {
370 		zrtp_sys_free(session->secrets.rs1);
371 	}
372 	if (session->secrets.rs2) {
373 		zrtp_sys_free(session->secrets.rs2);
374 	}
375 	if (session->secrets.auxs) {
376 		zrtp_sys_free(session->secrets.auxs);
377 	}
378 	if (session->secrets.pbxs) {
379 		zrtp_sys_free(session->secrets.pbxs);
380 	}
381 
382 	/* We don't need the session key anymore - clear it */
383 	zrtp_wipe_zstring(ZSTR_GV(session->zrtpsess));
384 
385 	/* Removing session from the global list */
386 	zrtp_mutex_lock(session->zrtp->sessions_protector);
387 	mlist_del(&session->_mlist);
388 	zrtp_mutex_unlock(session->zrtp->sessions_protector);
389 
390 	zrtp_mutex_destroy(session->streams_protector);
391 	zrtp_mutex_destroy(session->init_protector);
392 
393 	zrtp_sys_free(session);
394 }
395 
396 /*----------------------------------------------------------------------------*/
zrtp_stream_attach(zrtp_session_t * session,zrtp_stream_t ** stream)397 zrtp_status_t zrtp_stream_attach(zrtp_session_t *session, zrtp_stream_t** stream)
398 {
399     uint32_t i = 0;
400 	zrtp_status_t s = zrtp_status_fail;
401     zrtp_stream_t* new_stream = NULL;
402 
403 	ZRTP_LOG(3, (_ZTU_,"ATTACH NEW STREAM to sID=%d:\n", session->id));
404 
405 	/*
406 	 * Initialize first unused stream. If there are no available streams return error.
407 	 */
408     zrtp_mutex_lock(session->streams_protector);
409     for (i=0; i<ZRTP_MAX_STREAMS_PER_SESSION; i++) {
410 		if (ZRTP_STATE_NONE == session->streams[i].state) {
411 			new_stream = &session->streams[i];
412 			zrtp_memset(new_stream, 0, sizeof(zrtp_stream_t));
413 			break;
414  		}
415     }
416 	zrtp_mutex_unlock(session->streams_protector);
417 
418 	if (!new_stream) {
419 		ZRTP_LOG(1, (_ZTU_,"\tWARNING! Can't attach one more stream. Limit is reached."
420 					 " Use #ZRTP_MAX_STREAMS_PER_SESSION. sID=%u\n", session->id));
421 		return zrtp_status_alloc_fail;
422 	}
423 
424 	/*
425 	 * Initialize the private data stream with default initial values
426 	 */
427 	zrtp_mutex_init(&new_stream->stream_protector);
428 	_zrtp_change_state(new_stream, ZRTP_STATE_ACTIVE);
429 	new_stream->mode	= ZRTP_STREAM_MODE_CLEAR;
430 	new_stream->id		= session->zrtp->streams_count++;
431 	new_stream->session = session;
432 	new_stream->zrtp	= session->zrtp;
433 	new_stream->mitm_mode = ZRTP_MITM_MODE_UNKN;
434 	new_stream->is_hello_received = 0;
435 
436 	ZSTR_SET_EMPTY(new_stream->cc.hmackey);
437 	ZSTR_SET_EMPTY(new_stream->cc.peer_hmackey);
438 	ZSTR_SET_EMPTY(new_stream->cc.zrtp_key);
439 	ZSTR_SET_EMPTY(new_stream->cc.peer_zrtp_key);
440 
441 	new_stream->dh_cc.initialized_with	= ZRTP_COMP_UNKN;
442 	bnBegin(&new_stream->dh_cc.peer_pv);
443 	ZSTR_SET_EMPTY(new_stream->dh_cc.dhss);
444 
445 	ZRTP_LOG(3, (_ZTU_,"\tEmpty slot was found - initializing new stream with ID=%u.\n", new_stream->id));
446 
447 	do {
448 	zrtp_string32_t hash_buff = ZSTR_INIT_EMPTY(hash_buff);
449 	zrtp_hash_t *hash = zrtp_comp_find(ZRTP_CC_HASH, ZRTP_HASH_SHA256, new_stream->zrtp);
450 	s = zrtp_status_algo_fail;
451 
452 	if (sizeof(uint16_t) !=  zrtp_randstr( new_stream->zrtp,
453 										  (uint8_t*)&new_stream->media_ctx.high_out_zrtp_seq,
454 										  sizeof(uint16_t))) {
455 		break;
456 	}
457 
458 	/*
459 	 * Compute and store message hashes to prevent DoS attacks.
460 	 * Generate H0 as a random nonce and compute H1, H2 and H3
461 	 * using the leftmost 128 bits from every hash.
462 	 * Then insert these directly into the message structures.
463      */
464 
465 	zrtp_memset(&new_stream->messages, 0, sizeof(new_stream->messages));
466 	ZSTR_SET_EMPTY(new_stream->messages.h0);
467 	ZSTR_SET_EMPTY(new_stream->messages.signaling_hash);
468 
469 	/* Generate Random nonce, compute H1 and store in the DH packet */
470 	new_stream->messages.h0.length = (uint16_t)zrtp_randstr( new_stream->zrtp,
471 															 (unsigned char*)new_stream->messages.h0.buffer,
472 															 ZRTP_MESSAGE_HASH_SIZE);
473 	if (ZRTP_MESSAGE_HASH_SIZE != new_stream->messages.h0.length) {
474 		break;
475 	}
476 
477 	s = hash->hash(hash, ZSTR_GV(new_stream->messages.h0), ZSTR_GV(hash_buff));
478 	if (zrtp_status_ok != s) {
479 		break;
480 	}
481 	zrtp_memcpy(new_stream->messages.dhpart.hash, hash_buff.buffer, ZRTP_MESSAGE_HASH_SIZE);
482 
483 	/* Compute H2 for the Commit */
484 	s = hash->hash_c(hash, (char*)new_stream->messages.dhpart.hash, ZRTP_MESSAGE_HASH_SIZE, ZSTR_GV(hash_buff));
485 	if (zrtp_status_ok != s) {
486 		break;
487 	}
488 	zrtp_memcpy(new_stream->messages.commit.hash, hash_buff.buffer, ZRTP_MESSAGE_HASH_SIZE);
489 
490 	/* Compute H3 for the Hello message */
491 	s = hash->hash_c(hash, (char*)new_stream->messages.commit.hash, ZRTP_MESSAGE_HASH_SIZE, ZSTR_GV(hash_buff));
492 	if (zrtp_status_ok != s) {
493 		break;
494 	}
495 	zrtp_memcpy(new_stream->messages.hello.hash, hash_buff.buffer, ZRTP_MESSAGE_HASH_SIZE);
496 
497 	s = zrtp_status_ok;
498 	} while (0);
499 
500 	if (zrtp_status_ok != s) {
501 		ZRTP_LOG(1, (_ZTU_,"\tERROR! Fail to compute messages hashes <%s>.\n", zrtp_log_status2str(s)));
502 		return s;
503 	}
504 
505     /*
506 	 * Preparing HELLO based on user's profile
507 	 */
508 	ZRTP_LOG(3, (_ZTU_,"\tPreparing ZRTP Hello according to the Session profile.\n"));
509 	{
510 	zrtp_packet_Hello_t* hello = &new_stream->messages.hello;
511 	uint8_t i = 0;
512 	int8_t* comp_ptr = NULL;
513 
514 	/* Set Protocol Version and ClientID */
515 	zrtp_memcpy(hello->version, ZRTP_PROTOCOL_VERSION, ZRTP_VERSION_SIZE);
516 	zrtp_memcpy(hello->cliend_id, session->zrtp->client_id.buffer, session->zrtp->client_id.length);
517 
518 	/* Set flags. */
519 	hello->pasive	=  (ZRTP_LICENSE_MODE_PASSIVE == session->zrtp->lic_mode) ? 1 : 0;
520 	hello->uflag	= (ZRTP_LICENSE_MODE_UNLIMITED == session->zrtp->lic_mode) ? 1 : 0;
521 	hello->mitmflag = session->zrtp->is_mitm;
522 	hello->sigflag	= 0;
523 
524 	zrtp_memcpy(hello->zid, session->zid.buffer, session->zid.length);
525 
526 	comp_ptr = (int8_t*)hello->comp;
527 	i = 0;
528 	while ( session->profile.hash_schemes[i]) {
529 		zrtp_memcpy( comp_ptr,
530 					 zrtp_comp_id2type(ZRTP_CC_HASH, session->profile.hash_schemes[i++]),
531 					 ZRTP_COMP_TYPE_SIZE );
532 		comp_ptr += ZRTP_COMP_TYPE_SIZE;
533 	}
534 	hello->hc = i;
535 
536 	i = 0;
537 	while (session->profile.cipher_types[i]) {
538 		zrtp_memcpy( comp_ptr,
539 					 zrtp_comp_id2type(ZRTP_CC_CIPHER, session->profile.cipher_types[i++]),
540 					 ZRTP_COMP_TYPE_SIZE );
541 		comp_ptr += ZRTP_COMP_TYPE_SIZE;
542 	}
543 	hello->cc = i;
544 
545 	i = 0;
546 	while (session->profile.auth_tag_lens[i] ) {
547 		zrtp_memcpy( comp_ptr,
548 					 zrtp_comp_id2type(ZRTP_CC_ATL, session->profile.auth_tag_lens[i++]),
549 					 ZRTP_COMP_TYPE_SIZE );
550 		comp_ptr += ZRTP_COMP_TYPE_SIZE;
551 	}
552 	hello->ac = i;
553 
554 	i = 0;
555 	while (session->profile.pk_schemes[i] ) {
556 		zrtp_memcpy( comp_ptr,
557 					 zrtp_comp_id2type(ZRTP_CC_PKT, session->profile.pk_schemes[i++]),
558 					 ZRTP_COMP_TYPE_SIZE );
559 		comp_ptr += ZRTP_COMP_TYPE_SIZE;
560 	}
561 	hello->kc = i;
562 
563 	i = 0;
564 	while (session->profile.sas_schemes[i]) {
565 		zrtp_memcpy( comp_ptr,
566 					zrtp_comp_id2type(ZRTP_CC_SAS, session->profile.sas_schemes[i++]),
567 					ZRTP_COMP_TYPE_SIZE );
568 		comp_ptr += ZRTP_COMP_TYPE_SIZE;
569 	}
570 	hello->sc = i;
571 
572 	/*
573 	 * Hmac will appear at the end of the message, after the dynamic portion.
574 	 * i is the length of the dynamic part.
575 	 */
576 	i = (hello->hc + hello->cc + hello->ac + hello->kc + hello->sc) * ZRTP_COMP_TYPE_SIZE;
577 	_zrtp_packet_fill_msg_hdr( new_stream,
578 							   ZRTP_HELLO,
579 							   ZRTP_HELLO_STATIC_SIZE + i + ZRTP_HMAC_SIZE,
580 							   &hello->hdr);
581 	}
582 
583 	*stream = new_stream;
584 
585 	ZRTP_LOG(3, (_ZTU_,"ATTACH NEW STREAM - DONE.\n"));
586     return zrtp_status_ok;
587 }
588 
589 /*---------------------------------------------------------------------------*/
zrtp_signaling_hash_get(zrtp_stream_t * stream,char * hash_buff,uint32_t hash_buff_length)590 zrtp_status_t zrtp_signaling_hash_get( zrtp_stream_t* stream,
591 									   char *hash_buff,
592 									   uint32_t hash_buff_length)
593 {
594 	zrtp_string32_t hash_str = ZSTR_INIT_EMPTY(hash_str);
595 	zrtp_hash_t *hash = NULL;
596 
597 	if (!stream || !hash_buff) {
598 		return zrtp_status_bad_param;
599 	}
600 
601 	if (ZRTP_SIGN_ZRTP_HASH_LENGTH > hash_buff_length) {
602 		return zrtp_status_buffer_size;
603 	}
604 
605 	if (stream->state < ZRTP_STATE_ACTIVE) {
606 		return zrtp_status_wrong_state;
607 	}
608 
609 	hash = zrtp_comp_find(ZRTP_CC_HASH, ZRTP_HASH_SHA256, stream->zrtp);
610 	hash->hash_c( hash,
611 		 	     (const char*)&stream->messages.hello.hdr,
612 				  zrtp_ntoh16(stream->messages.hello.hdr.length) * 4,
613 				  ZSTR_GV(hash_str) );
614 
615 	hex2str(hash_str.buffer, ZRTP_MESSAGE_HASH_SIZE, hash_buff, hash_buff_length);
616 
617 	return zrtp_status_ok;
618 }
619 
zrtp_signaling_hash_set(zrtp_stream_t * ctx,const char * hash_buff,uint32_t hash_buff_length)620 zrtp_status_t zrtp_signaling_hash_set( zrtp_stream_t* ctx,
621 									   const char *hash_buff,
622 									   uint32_t hash_buff_length)
623 {
624 	if (!ctx || !hash_buff) {
625 		return zrtp_status_bad_param;
626 	}
627 
628 	if (ZRTP_SIGN_ZRTP_HASH_LENGTH > hash_buff_length) {
629 		return zrtp_status_buffer_size;
630 	}
631 
632 	if (ctx->state != ZRTP_STATE_ACTIVE) {
633 		return zrtp_status_wrong_state;
634 	}
635 
636 	str2hex(hash_buff,
637 			ZRTP_SIGN_ZRTP_HASH_LENGTH,
638 			ctx->messages.signaling_hash.buffer,
639 			ctx->messages.signaling_hash.max_length);
640 	ctx->messages.signaling_hash.length = ZRTP_MESSAGE_HASH_SIZE;
641 
642 	ZRTP_LOG(3, (_ZTU_,"SIGNALLING HAS was ADDED for the comparison. ID=%u\n", ctx->id));
643 	ZRTP_LOG(3, (_ZTU_,"Hash=%.*s.\n", ZRTP_SIGN_ZRTP_HASH_LENGTH, hash_buff));
644 
645 	return zrtp_status_ok;
646 }
647 
648 
649 /*----------------------------------------------------------------------------*/
650 static const char* zrtp_pkt2str[] = {
651 	"Preshared",
652 	"Multistream",
653 	"DH-2048",
654 	"ECDH-256",
655 	"DH-3072",
656 	"ECDH-384",
657 	"ECDH-521",
658 	"DH-4096"
659 };
660 
661 static const char* zrtp_hash2str[] = {
662 	"SHA-256",
663 	"SHA1",
664 	"SHA-384"
665 };
666 
667 static const char* zrtp_cipher2str[] = {
668 	"AES-128",
669 	"AES-256"
670 };
671 
672 static const char* zrtp_atl2str[] = {
673 	"HMAC-SHA1 32 bit",
674 	"HMAC-SHA1 80 bit"
675 };
676 
677 static const char* zrtp_sas2str[] = {
678 	"Base-32",
679 	"Base-256"
680 };
681 
zrtp_session_get(zrtp_session_t * session,zrtp_session_info_t * info)682 zrtp_status_t zrtp_session_get(zrtp_session_t *session, zrtp_session_info_t *info)
683 {
684 	int i=0;
685 	if (!session || !info) {
686 		return zrtp_status_bad_param;
687 	}
688 
689 	zrtp_memset(info, 0, sizeof(zrtp_session_info_t));
690 
691 	ZSTR_SET_EMPTY(info->peer_clientid);
692 	ZSTR_SET_EMPTY(info->peer_version);
693 	ZSTR_SET_EMPTY(info->zid);
694 	ZSTR_SET_EMPTY(info->peer_zid);
695 	ZSTR_SET_EMPTY(info->sas1);
696 	ZSTR_SET_EMPTY(info->sasbin);
697 	ZSTR_SET_EMPTY(info->sas2);
698 	ZSTR_SET_EMPTY(info->auth_name);
699 	ZSTR_SET_EMPTY(info->cipher_name);
700 	ZSTR_SET_EMPTY(info->hash_name);
701 	ZSTR_SET_EMPTY(info->sas_name);
702 	ZSTR_SET_EMPTY(info->pk_name);
703 
704 	info->id = session->id;
705 	zrtp_zstrcpy(ZSTR_GV(info->zid), ZSTR_GV(session->zid));
706 	zrtp_zstrcpy(ZSTR_GV(info->peer_zid), ZSTR_GV(session->peer_zid));
707 
708 	for (i=0; i<ZRTP_MAX_STREAMS_PER_SESSION; i++) {
709 		zrtp_stream_t* full_stream = &session->streams[i];
710 		if ((full_stream->state > ZRTP_STATE_ACTIVE) && !ZRTP_IS_STREAM_FAST(full_stream))
711 		{
712 			zrtp_zstrcpyc(ZSTR_GV(info->pk_name), zrtp_pkt2str[full_stream->pubkeyscheme->base.id-1]);
713 
714 			zrtp_zstrncpyc( ZSTR_GV(info->peer_clientid),
715 						   (const char*)full_stream->messages.peer_hello.cliend_id, 16);
716 			zrtp_zstrncpyc( ZSTR_GV(info->peer_version),
717 						   (const char*)full_stream->messages.peer_hello.version, 4);
718 
719 			info->secrets_ttl = full_stream->cache_ttl;
720 		}
721 	}
722 
723 	info->sas_is_ready = (session->zrtpsess.length > 0) ? 1 : 0;
724 	if (info->sas_is_ready) {
725 		zrtp_zstrcpy(ZSTR_GV(info->sas1), ZSTR_GV(session->sas1));
726 		zrtp_zstrcpy(ZSTR_GV(info->sas2), ZSTR_GV(session->sas2));
727 		zrtp_zstrcpy(ZSTR_GV(info->sasbin), ZSTR_GV(session->sasbin));
728 		info->sas_is_base256 = (ZRTP_SAS_BASE256 == session->sasscheme->base.id);
729 
730 		info->sas_is_verified = 0;
731 		if (session->zrtp->cb.cache_cb.on_get_verified) {
732 			session->zrtp->cb.cache_cb.on_get_verified( ZSTR_GV(session->zid),
733 													    ZSTR_GV(session->peer_zid),
734 													    &info->sas_is_verified);
735 		}
736 
737 		zrtp_zstrcpyc(ZSTR_GV(info->hash_name), zrtp_hash2str[session->hash->base.id-1]);
738 		zrtp_zstrcpyc(ZSTR_GV(info->cipher_name), zrtp_cipher2str[session->blockcipher->base.id-1]);
739 		zrtp_zstrcpyc(ZSTR_GV(info->auth_name), zrtp_atl2str[session->authtaglength->base.id-1]);
740 		zrtp_zstrcpyc(ZSTR_GV(info->sas_name), zrtp_sas2str[session->sasscheme->base.id-1]);
741 
742 		info->cached_flags	= session->secrets.cached_curr;
743 		info->matches_flags= session->secrets.matches_curr;
744 		info->wrongs_flags	= session->secrets.wrongs_curr;
745 	}
746 
747 	return zrtp_status_ok;
748 }
749 
zrtp_stream_get(zrtp_stream_t * stream,zrtp_stream_info_t * info)750 zrtp_status_t zrtp_stream_get(zrtp_stream_t *stream, zrtp_stream_info_t *info)
751 {
752 	if (!stream || !info) {
753 		return zrtp_status_bad_param;
754 	}
755 
756 	zrtp_memset(info, 0, sizeof(zrtp_stream_info_t));
757 
758 	info->id			= stream->id;
759 	info->state			= stream->state;
760 	info->mode			= stream->mode;
761 	info->mitm_mode		= stream->mitm_mode;
762 
763 	if (stream->state > ZRTP_STATE_ACTIVE) {
764 		info->last_error	= stream->last_error;
765 		info->peer_passive	= stream->peer_passive;
766 		info->res_allowclear= stream->allowclear;
767 		info->peer_disclose	= stream->peer_disclose_bit;
768 		info->peer_mitm		= stream->peer_mitm_flag;
769 	}
770 
771 	return zrtp_status_ok;
772 }
773 
zrtp_session_set_userdata(zrtp_session_t * session,void * udata)774 void zrtp_session_set_userdata(zrtp_session_t *session, void* udata) {
775 	session->usr_data = udata;
776 }
zrtp_session_get_userdata(zrtp_session_t * session)777 void* zrtp_session_get_userdata(zrtp_session_t *session) {
778 	return session->usr_data;
779 }
780 
zrtp_stream_set_userdata(zrtp_stream_t * stream,void * udata)781 void zrtp_stream_set_userdata(zrtp_stream_t *stream, void* udata) {
782 	stream->usr_data = udata;
783 }
zrtp_stream_get_userdata(const zrtp_stream_t * stream)784 void* zrtp_stream_get_userdata(const zrtp_stream_t *stream) {
785 	return stream->usr_data;
786 }
787 
788 /*----------------------------------------------------------------------------*/
zrtp_profile_defaults(zrtp_profile_t * profile,zrtp_global_t * zrtp)789 void zrtp_profile_defaults(zrtp_profile_t* profile, zrtp_global_t* zrtp)
790 {
791 	zrtp_memset(profile, 0, sizeof(zrtp_profile_t));
792 
793 	profile->autosecure			= 1;
794 	profile->allowclear			= 0;
795 	profile->discovery_optimization = 1;
796 	profile->cache_ttl			= ZRTP_CACHE_DEFAULT_TTL;
797 
798 	profile->sas_schemes[0]		= ZRTP_SAS_BASE256;
799 	profile->sas_schemes[1]		= ZRTP_SAS_BASE32;
800 	profile->cipher_types[0]	= ZRTP_CIPHER_AES256;
801 	profile->cipher_types[1]	= ZRTP_CIPHER_AES128;
802 	profile->auth_tag_lens[0]	= ZRTP_ATL_HS32;
803 	profile->auth_tag_lens[1]   = ZRTP_ATL_HS80;
804 	profile->hash_schemes[0]	= ZRTP_HASH_SHA256;
805 
806 	if (zrtp && (ZRTP_LICENSE_MODE_PASSIVE == zrtp->lic_mode)) {
807 		profile->pk_schemes[0]		= ZRTP_PKTYPE_DH2048;
808 		profile->pk_schemes[1]		= ZRTP_PKTYPE_EC256P;
809 		profile->pk_schemes[2]		= ZRTP_PKTYPE_DH3072;
810 	} else {
811 		profile->pk_schemes[0]		= ZRTP_PKTYPE_EC256P;
812 		profile->pk_schemes[1]		= ZRTP_PKTYPE_DH3072;
813 		profile->pk_schemes[2]		= ZRTP_PKTYPE_DH2048;
814 	}
815 	profile->pk_schemes[3]		= ZRTP_PKTYPE_MULT;
816 }
817 
818 /*----------------------------------------------------------------------------*/
zrtp_profile_check(const zrtp_profile_t * profile,zrtp_global_t * zrtp)819 zrtp_status_t zrtp_profile_check(const zrtp_profile_t* profile, zrtp_global_t* zrtp)
820 {
821     uint8_t i = 0;
822 
823 	if (!profile || !zrtp) {
824 		return zrtp_status_bad_param;
825 	}
826 
827     /*
828      * Fail if the required base components are not present in the profile.
829      */
830     if (0 > zrtp_profile_find(profile, ZRTP_CC_HASH, ZRTP_HASH_SHA256)) {
831 		ZRTP_LOG(1, (_ZTU_,"WARNING! can't find 'SHA256  ' in profile.\n"));
832         return zrtp_status_fail;
833     }
834 
835     if (0 > zrtp_profile_find(profile, ZRTP_CC_SAS, ZRTP_SAS_BASE32)) {
836         ZRTP_LOG(1, (_ZTU_,"WARNING! can't find 'base32' in profile.\n"));
837         return zrtp_status_fail;
838     }
839 
840     if (0 > zrtp_profile_find(profile, ZRTP_CC_CIPHER, ZRTP_CIPHER_AES128)) {
841         ZRTP_LOG(1, (_ZTU_,"WARNING! can't find 'AES1287  ' in profile.\n"));
842         return zrtp_status_fail;
843     }
844 
845     if (0 > zrtp_profile_find(profile, ZRTP_CC_PKT, ZRTP_PKTYPE_DH3072)) {
846         ZRTP_LOG(1, (_ZTU_,"WARNING! can't find 'DH3K' in profile.\n"));
847         return zrtp_status_fail;
848     }
849 
850 	if (0 > zrtp_profile_find(profile, ZRTP_CC_PKT, ZRTP_PKTYPE_MULT)) {
851         ZRTP_LOG(1, (_ZTU_,"WARNING! can't find 'Mult' in profile.\n"));
852         return zrtp_status_fail;
853     }
854 
855     if (0 > zrtp_profile_find(profile, ZRTP_CC_ATL, ZRTP_ATL_HS32)) {
856         ZRTP_LOG(1, (_ZTU_,"WARNING! can't find '32      ' in profile.\n"));
857         return zrtp_status_fail;
858     }
859 
860 	if (0 > zrtp_profile_find(profile, ZRTP_CC_ATL, ZRTP_ATL_HS80)) {
861         ZRTP_LOG(1, (_ZTU_,"WARNING! can't find '80      ' in profile.\n"));
862         return zrtp_status_fail;
863     }
864 
865     /*
866      * Check that each component in the profile is in the global set of components.
867      */
868 	i = 0;
869 	while (profile->sas_schemes[i]) {
870 		if (!zrtp_comp_find(ZRTP_CC_SAS, profile->sas_schemes[i++], zrtp)) {
871     		return zrtp_status_fail;
872 		}
873 	}
874 
875 	i = 0;
876     while (profile->cipher_types[i]) {
877 		if (!zrtp_comp_find( ZRTP_CC_CIPHER, profile->cipher_types[i++], zrtp)) {
878     	    return zrtp_status_fail;
879 		}
880     }
881 
882 	i = 0;
883 	while (profile->pk_schemes[i]) {
884 		if (!zrtp_comp_find(ZRTP_CC_PKT, profile->pk_schemes[i++], zrtp)) {
885 				return zrtp_status_fail;
886 		}
887     }
888 
889 	i = 0;
890     while (profile->auth_tag_lens[i]) {
891 		if (!zrtp_comp_find(ZRTP_CC_ATL, profile->auth_tag_lens[i++], zrtp)) {
892     	    return zrtp_status_fail;
893 		}
894     }
895 
896 	i = 0;
897     while (profile->hash_schemes[i]) {
898 		if (!zrtp_comp_find(ZRTP_CC_HASH, profile->hash_schemes[i++], zrtp)) {
899     	    return zrtp_status_fail;
900 		}
901 	}
902 
903 	/* Can't use Preshared with No cahce */
904 	if (NULL == zrtp->cb.cache_cb.on_get) {
905 		i = 0;
906 		while (profile->pk_schemes[i]) {
907 			if (ZRTP_PKTYPE_PRESH == profile->pk_schemes[i++]) {
908 				ZRTP_LOG(1, (_ZTU_,"WARNING! can't use Preshared PK with no cache.\n"));
909 				return zrtp_status_fail;
910 			}
911 		}
912 	}
913 
914     return zrtp_status_ok;
915 }
916 
917 
918 /*----------------------------------------------------------------------------*/
zrtp_profile_find(const zrtp_profile_t * profile,zrtp_crypto_comp_t type,uint8_t id)919 int zrtp_profile_find(const zrtp_profile_t* profile, zrtp_crypto_comp_t type, uint8_t id)
920 
921 {
922 	uint8_t* prof_elem = NULL;
923     unsigned int i = 0;
924 
925     if (!profile || !id) {
926 		return -1;
927     }
928 
929     switch (type)
930     {
931 	case ZRTP_CC_HASH:
932 		prof_elem = (uint8_t*)profile->hash_schemes;
933 		break;
934 	case ZRTP_CC_SAS:
935 		prof_elem = (uint8_t*)profile->sas_schemes;
936 		break;
937 	case ZRTP_CC_CIPHER:
938 		prof_elem = (uint8_t*)profile->cipher_types;
939 		break;
940 	case ZRTP_CC_PKT:
941 		prof_elem = (uint8_t*)profile->pk_schemes;
942 		break;
943 	case ZRTP_CC_ATL:
944 		prof_elem = (uint8_t*)profile->auth_tag_lens;
945 		break;
946 	default:
947 		return -1;
948     }
949 
950 
951 	i = 0;
952 	while ( prof_elem[i] ) {
953 		if (id == prof_elem[i++]) return i;
954     }
955 
956     return -1;
957 }
958 
959 
960 /*============================================================================*/
961 /*  ZRTP components management part											  */
962 /*============================================================================*/
963 
964 
965 /*----------------------------------------------------------------------------*/
966 #define DESTROY_COMP(mac_node, mac_tmp, mac_type, mac_head, mac_comp)\
967 { \
968     mac_node = mac_tmp = NULL;\
969     mac_comp = NULL;\
970     mlist_for_each_safe(mac_node, mac_tmp, mac_head) \
971     {\
972 	mac_comp = (zrtp_comp_t*) mlist_get_struct(mac_type, mlist, mac_node); \
973 	if (mac_comp->free)\
974 		mac_comp->free((mac_type*)mac_comp);\
975 	mlist_del(mac_node);\
976 	zrtp_sys_free(mac_comp);\
977     } \
978 }break
979 
zrtp_comp_done(zrtp_crypto_comp_t type,zrtp_global_t * zrtp)980 zrtp_status_t zrtp_comp_done(zrtp_crypto_comp_t type, zrtp_global_t* zrtp)
981 {
982     mlist_t* node = NULL;
983     mlist_t* tmp = NULL;
984     zrtp_comp_t* comp = NULL;
985 
986     switch (type)
987     {
988 	case ZRTP_CC_HASH:
989 	    DESTROY_COMP(node, tmp, zrtp_hash_t, &zrtp->hash_head, comp);
990 	case ZRTP_CC_SAS:
991 	    DESTROY_COMP(node, tmp, zrtp_sas_scheme_t, &zrtp->sas_head, comp);
992 	case ZRTP_CC_CIPHER:
993 	    DESTROY_COMP(node, tmp, zrtp_cipher_t, &zrtp->cipher_head, comp);
994 	case ZRTP_CC_PKT:
995 	    DESTROY_COMP(node, tmp, zrtp_pk_scheme_t, &zrtp->pktype_head, comp);
996 	case ZRTP_CC_ATL:
997 	    DESTROY_COMP(node, tmp, zrtp_auth_tag_length_t, &zrtp->atl_head, comp);
998 	default:
999 		break;
1000     }
1001 
1002     return zrtp_status_ok;
1003 }
1004 
1005 /*----------------------------------------------------------------------------*/
1006 #define ZRTP_COMP_INIT(mac_type, mac_head, mac_elem)\
1007 {\
1008     mac_type* mac_e = (mac_type*)mac_elem; \
1009     mlist_add_tail(mac_head, &mac_e->mlist);\
1010     if (mac_e->base.init)\
1011     	    mac_e->base.init((mac_type*)mac_e);\
1012 } break;\
1013 
zrtp_comp_register(zrtp_crypto_comp_t type,void * comp,zrtp_global_t * zrtp)1014 zrtp_status_t zrtp_comp_register( zrtp_crypto_comp_t type,
1015 								  void *comp,
1016 								  zrtp_global_t* zrtp )
1017 {
1018     switch (type)
1019     {
1020 	case ZRTP_CC_HASH:
1021 		ZRTP_COMP_INIT(zrtp_hash_t, &zrtp->hash_head, comp);
1022 	case ZRTP_CC_SAS:
1023 		ZRTP_COMP_INIT(zrtp_sas_scheme_t, &zrtp->sas_head, comp);
1024 	case ZRTP_CC_CIPHER:
1025 		ZRTP_COMP_INIT(zrtp_cipher_t, &zrtp->cipher_head, comp);
1026 	case ZRTP_CC_ATL:
1027 		ZRTP_COMP_INIT(zrtp_auth_tag_length_t, &zrtp->atl_head, comp);
1028 	case ZRTP_CC_PKT:
1029 		ZRTP_COMP_INIT(zrtp_pk_scheme_t, &zrtp->pktype_head, comp);
1030 	default:
1031 		break;
1032     }
1033 
1034     return zrtp_status_ok;
1035 }
1036 
1037 /*----------------------------------------------------------------------------*/
1038 #define ZRTP_COMP_FIND(mac_head, mac_id, mac_type, res)\
1039 {\
1040     mlist_t* mac_node = NULL;\
1041     mlist_for_each(mac_node, mac_head)\
1042     {\
1043 	zrtp_comp_t* mac_e = (zrtp_comp_t*) mlist_get_struct(mac_type, mlist, mac_node);\
1044 	if ( mac_id == mac_e->id )\
1045 	{\
1046 	    res = (mac_type*)mac_e;\
1047 	    break;\
1048 	}\
1049     }\
1050 } break;
1051 
zrtp_comp_find(zrtp_crypto_comp_t type,uint8_t id,zrtp_global_t * zrtp)1052 void* zrtp_comp_find(zrtp_crypto_comp_t type, uint8_t id, zrtp_global_t* zrtp)
1053 {
1054     void* res = NULL;
1055 
1056     switch (type)
1057     {
1058 	case ZRTP_CC_HASH:
1059 	    ZRTP_COMP_FIND(&zrtp->hash_head, id, zrtp_hash_t, res);
1060 	case ZRTP_CC_SAS:
1061 	    ZRTP_COMP_FIND(&zrtp->sas_head, id, zrtp_sas_scheme_t, res);
1062 	case ZRTP_CC_CIPHER:
1063 	    ZRTP_COMP_FIND(&zrtp->cipher_head, id, zrtp_cipher_t, res);
1064 	case ZRTP_CC_PKT:
1065 	    ZRTP_COMP_FIND(&zrtp->pktype_head, id, zrtp_pk_scheme_t, res);
1066 	case ZRTP_CC_ATL:
1067 	    ZRTP_COMP_FIND(&zrtp->atl_head, id, zrtp_auth_tag_length_t, res);
1068 	default:
1069 		break;
1070     }
1071 
1072     return res ;
1073 }
1074 
1075 /*----------------------------------------------------------------------------*/
zrtp_comp_id2type(zrtp_crypto_comp_t type,uint8_t id)1076 char* zrtp_comp_id2type(zrtp_crypto_comp_t type, uint8_t id)
1077 {
1078 	if (ZRTP_COMP_UNKN == id)
1079 		return "Unkn";
1080 
1081     switch (type)
1082     {
1083 	case ZRTP_CC_HASH:
1084 		switch (id)
1085 		{
1086 		case ZRTP_HASH_SHA256: return ZRTP_S256;
1087 		case ZRTP_HASH_SHA384: return ZRTP_S384;
1088 		default: return "Unkn";
1089 		}
1090 		break;
1091 
1092 	case ZRTP_CC_SAS:
1093 		switch (id)
1094 		{
1095 		case ZRTP_SAS_BASE32:	return ZRTP_B32;
1096 		case ZRTP_SAS_BASE256:  return ZRTP_B256;
1097 		default: return "Unkn";
1098 		}
1099 		break;
1100 
1101 	case ZRTP_CC_CIPHER:
1102 		switch (id)
1103 		{
1104 		case ZRTP_CIPHER_AES128: return ZRTP_AES1;
1105 		case ZRTP_CIPHER_AES256: return ZRTP_AES3;
1106 		default: return "Unkn";
1107 		}
1108 		break;
1109 
1110 	case ZRTP_CC_PKT:
1111 		switch (id)
1112 		{
1113 		case ZRTP_PKTYPE_PRESH:  return ZRTP_PRESHARED;
1114 		case ZRTP_PKTYPE_MULT:	 return ZRTP_MULT;
1115 		case ZRTP_PKTYPE_DH2048: return ZRTP_DH2K;
1116 		case ZRTP_PKTYPE_DH3072: return ZRTP_DH3K;
1117 		case ZRTP_PKTYPE_EC256P: return ZRTP_EC256P;
1118 		case ZRTP_PKTYPE_EC384P: return ZRTP_EC384P;
1119 		case ZRTP_PKTYPE_EC521P: return ZRTP_EC521P;
1120 		default: return "Unkn";
1121 		}
1122 		break;
1123 
1124 	case ZRTP_CC_ATL:
1125 		switch (id)
1126 		{
1127 		case ZRTP_ATL_HS32: return ZRTP_HS32;
1128 		case ZRTP_ATL_HS80: return ZRTP_HS80;
1129 		default: return "Unkn";
1130 		}
1131 		break;
1132 
1133 	default:
1134 		return "Unkn";
1135     }
1136 }
1137 
1138 /*----------------------------------------------------------------------------*/
zrtp_comp_type2id(zrtp_crypto_comp_t type,char * name)1139 uint8_t zrtp_comp_type2id(zrtp_crypto_comp_t type, char* name)
1140 {
1141     switch (type)
1142     {
1143 	case ZRTP_CC_HASH:
1144 		if (!zrtp_memcmp(ZRTP_S256, name, ZRTP_COMP_TYPE_SIZE)) {
1145 			return ZRTP_HASH_SHA256;
1146 		}
1147 		if (!zrtp_memcmp(ZRTP_S384, name, ZRTP_COMP_TYPE_SIZE)) {
1148 			return ZRTP_HASH_SHA384;
1149 		}
1150 		break;
1151 
1152 	case ZRTP_CC_SAS:
1153 		if (!zrtp_memcmp(ZRTP_B32, name, ZRTP_COMP_TYPE_SIZE)) {
1154 			return ZRTP_SAS_BASE32;
1155 		}
1156 		if (!zrtp_memcmp(ZRTP_B256, name, ZRTP_COMP_TYPE_SIZE)) {
1157 			return ZRTP_SAS_BASE256;
1158 		}
1159 		break;
1160 
1161 	case ZRTP_CC_CIPHER:
1162 		if (!zrtp_memcmp(ZRTP_AES1, name, ZRTP_COMP_TYPE_SIZE)) {
1163 			return ZRTP_CIPHER_AES128;
1164 		}
1165 		if (!zrtp_memcmp(ZRTP_AES3, name, ZRTP_COMP_TYPE_SIZE)) {
1166 			return ZRTP_CIPHER_AES256;
1167 		}
1168 		break;
1169 
1170 	case ZRTP_CC_PKT:
1171 		if (!zrtp_memcmp(ZRTP_PRESHARED, name, ZRTP_COMP_TYPE_SIZE)) {
1172 			return ZRTP_PKTYPE_PRESH;
1173 		}
1174 		if (!zrtp_memcmp(ZRTP_MULT, name, ZRTP_COMP_TYPE_SIZE)) {
1175 			return ZRTP_PKTYPE_MULT;
1176 		}
1177 		if (!zrtp_memcmp(ZRTP_DH3K, name, ZRTP_COMP_TYPE_SIZE)) {
1178 			return ZRTP_PKTYPE_DH3072;
1179 		}
1180 		if (!zrtp_memcmp(ZRTP_DH2K, name, ZRTP_COMP_TYPE_SIZE)) {
1181 			return ZRTP_PKTYPE_DH2048;
1182 		}
1183 		if (!zrtp_memcmp(ZRTP_EC256P, name, ZRTP_COMP_TYPE_SIZE)) {
1184 			return ZRTP_PKTYPE_EC256P;
1185 		}
1186 		if (!zrtp_memcmp(ZRTP_EC384P, name, ZRTP_COMP_TYPE_SIZE)) {
1187 			return ZRTP_PKTYPE_EC384P;
1188 		}
1189 		if (!zrtp_memcmp(ZRTP_EC521P, name, ZRTP_COMP_TYPE_SIZE)) {
1190 			return ZRTP_PKTYPE_EC521P;
1191 		}
1192 		break;
1193 
1194 	case ZRTP_CC_ATL:
1195 		if ( !zrtp_memcmp(ZRTP_HS32, name, ZRTP_COMP_TYPE_SIZE)) {
1196 			return ZRTP_ATL_HS32;
1197 		}
1198 		if (!zrtp_memcmp(ZRTP_HS80, name, ZRTP_COMP_TYPE_SIZE)) {
1199 			return ZRTP_ATL_HS80;
1200 		}
1201 		break;
1202 
1203 	default:
1204 		return 0;
1205     }
1206 
1207 	return 0;
1208 }
1209