int AmZRTP::init() { AmConfigReader cfg; string cfgname=add2path(AmConfig::ModConfigPath, 1, "zrtp.conf"); if(cfg.loadFile(cfgname)) { ERROR("No %s config file present.\n", cfgname.c_str()); return -1; } cache_path = cfg.getParameter("cache_path"); string zid = cfg.getParameter("zid"); if (zid.length() != sizeof(zrtp_zid_t)) { ERROR("ZID of this instance MUST be set for ZRTP.\n"); ERROR("ZID needs to be %u characters long.\n", sizeof(zrtp_zid_t)); return -1; } for (int i=0;i<12;i++) zrtp_instance_zid[i]=zid[i]; DBG("initializing ZRTP library with ZID '%s', cache path '%s'.\n", zid.c_str(), cache_path.c_str()); if ( zrtp_status_ok != zrtp_init(&zrtp_global, "zrtp_sems") ) { ERROR("Some error during zrtp initialization\n"); return -1; } zrtp_add_entropy(&zrtp_global, NULL, 0); DBG("ZRTP initialized ok.\n"); return 0; }
zrtp_status_t zrtp_test_endpoint_create(zrtp_test_endpoint_cfg_t* cfg, const char *name, zrtp_test_id_t* id) { zrtp_status_t s; unsigned i; char cache_file_path[ZRTP_TEST_STR_LEN]; zrtp_endpoint_t *new_endpoint; if (g_test_endpoints_count >= K_ZRTP_TEST_MAX_ENDPOINTS) return zrtp_status_alloc_fail; new_endpoint = &g_test_endpoints[g_test_endpoints_count++]; zrtp_memset(new_endpoint, 0, sizeof(zrtp_endpoint_t)); /* Copy configuration, we will use it later to clean up after ourselves */ zrtp_memcpy(&new_endpoint->cfg, cfg, sizeof(zrtp_test_endpoint_cfg_t)); /* Remember endpoint name */ strcpy(new_endpoint->name, name); new_endpoint->id = g_endpoints_counter++; /* Adjust cache file path so each endpoint will use it's own file. */ sprintf(cache_file_path, "./%s_cache.dat", name); zrtp_zstrcpyc(ZSTR_GV(new_endpoint->cfg.zrtp.def_cache_path), cache_file_path); /* Initialize libzrtp engine for this endpoint */ s = zrtp_init(&new_endpoint->cfg.zrtp, &new_endpoint->zrtp); if (zrtp_status_ok == s) { *id = new_endpoint->id; /* Generate random ZID */ zrtp_randstr(new_endpoint->zrtp, new_endpoint->zid, sizeof(new_endpoint->zid)); } /* Create Input queue*/ s = zrtp_test_queue_create(&new_endpoint->input_queue); if (zrtp_status_ok != s) { return s; } /* Start processing loop */ new_endpoint->is_running = 1; for (i = 0; i<K_ZRTP_TEST_PROCESSORS_COUNT; i++) { if (0 != zrtp_thread_create(process_incoming, new_endpoint)) { return zrtp_status_fail; } if (cfg->generate_traffic) { if (0 != zrtp_thread_create(process_outgoing, new_endpoint)) { return zrtp_status_fail; } } } return s; }
void setup() { zrtp_status_t s; zrtp_config_t zrtp_config; zrtp_config_defaults(&zrtp_config); s = zrtp_init(&zrtp_config, &zrtp); assert_int_equal(s, zrtp_status_ok); }
static int module_init(void) { zrtp_status_t s; char config_path[256] = ""; char zrtp_zid_path[256] = ""; FILE *f; int ret, err; (void)conf_get_bool(conf_cur(), "zrtp_hash", &use_sig_hash); zrtp_log_set_log_engine(zrtp_log); zrtp_config_defaults(&zrtp_config); str_ncpy(zrtp_config.client_id, "baresip/zrtp", sizeof(zrtp_config.client_id)); zrtp_config.lic_mode = ZRTP_LICENSE_MODE_UNLIMITED; zrtp_config.cb.misc_cb.on_send_packet = on_send_packet; zrtp_config.cb.event_cb.on_zrtp_secure = on_zrtp_secure; zrtp_config.cb.event_cb.on_zrtp_security_event = on_zrtp_security_event; err = conf_path_get(config_path, sizeof(config_path)); if (err) { warning("zrtp: could not get config path: %m\n", err); return err; } ret = re_snprintf(zrtp_config.def_cache_path.buffer, zrtp_config.def_cache_path.max_length, "%s/zrtp_cache.dat", config_path); if (ret < 0) { warning("zrtp: could not write cache path\n"); return ENOMEM; } zrtp_config.def_cache_path.length = ret; if (re_snprintf(zrtp_zid_path, sizeof(zrtp_zid_path), "%s/zrtp_zid", config_path) < 0) return ENOMEM; if ((f = fopen(zrtp_zid_path, "rb")) != NULL) { if (fread(zid, sizeof(zid), 1, f) != 1) { if (feof(f) || ferror(f)) { warning("zrtp: invalid zrtp_zid file\n"); } } } else if ((f = fopen(zrtp_zid_path, "wb")) != NULL) { rand_bytes(zid, sizeof(zid)); if (fwrite(zid, sizeof(zid), 1, f) != 1) { warning("zrtp: zrtp_zid file write failed\n"); } info("zrtp: generated new persistent ZID (%s)\n", zrtp_zid_path); } else { err = errno; warning("zrtp: fopen() %s (%m)\n", zrtp_zid_path, err); } if (f) (void) fclose(f); s = zrtp_init(&zrtp_config, &zrtp_global); if (zrtp_status_ok != s) { warning("zrtp: zrtp_init() failed (status = %d)\n", s); return ENOSYS; } menc_register(baresip_mencl(), &menc_zrtp); debug("zrtp: cache_file: %s\n", zrtp_config.def_cache_path.buffer); debug(" zid_file: %s\n", zrtp_zid_path); debug(" zid: %w\n", zid, sizeof(zid)); return cmd_register(baresip_commands(), cmdv, ARRAY_SIZE(cmdv)); }
static int module_init(void) { zrtp_status_t s; char config_path[256] = ""; char zrtp_zid_path[256] = ""; FILE *f; int err; zrtp_config_defaults(&zrtp_config); zrtp_config.cache_type = ZRTP_CACHE_FILE; err = conf_path_get(config_path, sizeof(config_path)); if (err) { warning("zrtp: could not get config path: %m\n", err); return err; } if (re_snprintf(zrtp_config.cache_file_cfg.cache_path, sizeof(zrtp_config.cache_file_cfg.cache_path), "%s/zrtp_cache.dat", config_path) < 0) return ENOMEM; if (re_snprintf(zrtp_zid_path, sizeof(zrtp_zid_path), "%s/zrtp_zid", config_path) < 0) return ENOMEM; if ((f = fopen(zrtp_zid_path, "rb")) != NULL) { if (fread(zrtp_config.zid, sizeof(zrtp_config.zid), 1, f) != 1) { if (feof(f) || ferror(f)) { warning("zrtp: invalid zrtp_zid file\n"); } } } else if ((f = fopen(zrtp_zid_path, "wb")) != NULL) { rand_bytes(zrtp_config.zid, sizeof(zrtp_config.zid)); if (fwrite(zrtp_config.zid, sizeof(zrtp_config.zid), 1, f) != 1) { warning("zrtp: zrtp_zid file write failed\n"); } info("zrtp: generated new persistent ZID (%s)\n", zrtp_zid_path); } else { err = errno; warning("zrtp: fopen() %s (%m)\n", zrtp_zid_path, err); } if (f) (void) fclose(f); str_ncpy(zrtp_config.client_id, "baresip/zrtp", sizeof(zrtp_config.client_id)); zrtp_config.lic_mode = ZRTP_LICENSE_MODE_UNLIMITED; zrtp_config.cb.misc_cb.on_send_packet = on_send_packet; zrtp_config.cb.event_cb.on_zrtp_secure = on_zrtp_secure; s = zrtp_init(&zrtp_config, &zrtp_global); if (zrtp_status_ok != s) { warning("zrtp: zrtp_init() failed (status = %d)\n", s); return ENOSYS; } menc_register(&menc_zrtp); return cmd_register(cmdv, ARRAY_SIZE(cmdv)); }
int AmZRTP::init() { zrtp_log_set_log_engine(zrtp_log); AmConfigReader cfg; string cfgname=add2path(AmConfig::ModConfigPath, 1, "zrtp.conf"); if(cfg.loadFile(cfgname)) { ERROR("No %s config file present.\n", cfgname.c_str()); return -1; } cache_path = cfg.getParameter("cache_path"); if (cfg.hasParameter("zid_hex")) { string zid_hex = cfg.getParameter("zid_hex"); if (zid_hex.size() != 2*sizeof(zrtp_instance_zid)) { ERROR("zid_hex config parameter in zrtp.conf must be %lu characters long.\n", sizeof(zrtp_zid_t)*2); return -1; } for (size_t i=0;i<sizeof(zrtp_instance_zid);i++) { unsigned int h; if (reverse_hex2int(zid_hex.substr(i*2, 2), h)) { ERROR("in zid_hex in zrtp.conf: '%s' is no hex number\n", zid_hex.substr(i*2, 2).c_str()); return -1; } zrtp_instance_zid[i]=h % 0xff; } } else if (cfg.hasParameter("zid")) { string zid = cfg.getParameter("zid"); WARN("zid parameter in zrtp.conf is only supported for backwards compatibility. Please use zid_hex\n"); if (zid.length() != sizeof(zrtp_zid_t)) { ERROR("zid config parameter in zrtp.conf must be %lu characters long.\n", sizeof(zrtp_zid_t)); return -1; } for (size_t i=0;i<zid.length();i++) zrtp_instance_zid[i]=zid[i]; } else { // generate one string zid_hex; for (size_t i=0;i<sizeof(zrtp_instance_zid);i++) { zrtp_instance_zid[i]=get_random() % 0xff; zid_hex+=char2hex(zrtp_instance_zid[i], true); } WARN("Generated random ZID. To support key continuity through key cache " "on the peers, add this to zrtp.conf: 'zid_hex=\"%s\"'", zid_hex.c_str()); } DBG("initializing ZRTP library with cache path '%s'.\n", cache_path.c_str()); zrtp_config_defaults(&zrtp_config); strcpy(zrtp_config.client_id, SEMS_CLIENT_ID); memcpy((char*)zrtp_config.zid, (char*)zrtp_instance_zid, sizeof(zrtp_zid_t)); zrtp_config.lic_mode = ZRTP_LICENSE_MODE_UNLIMITED; strncpy(zrtp_config.cache_file_cfg.cache_path, cache_path.c_str(), 256); zrtp_config.cb.misc_cb.on_send_packet = AmZRTP::on_send_packet; zrtp_config.cb.event_cb.on_zrtp_secure = AmZRTP::on_zrtp_secure; zrtp_config.cb.event_cb.on_zrtp_security_event = AmZRTP::on_zrtp_security_event; zrtp_config.cb.event_cb.on_zrtp_protocol_event = AmZRTP::on_zrtp_protocol_event; if ( zrtp_status_ok != zrtp_init(&zrtp_config, &zrtp_global) ) { ERROR("Error during ZRTP initialization\n"); return -1; } size_t rand_bytes = cfg.getParameterInt("random_entropy_bytes", 172); if (rand_bytes) { INFO("adding %zd bytes entropy from /dev/random to ZRTP entropy pool\n", rand_bytes); FILE* fd = fopen("/dev/random", "r"); if (!fd) { ERROR("opening /dev/random for adding entropy to the pool\n"); return -1; } void* p = malloc(rand_bytes); if (p==NULL) return -1; size_t read_bytes = fread(p, 1, rand_bytes, fd); if (read_bytes != rand_bytes) { ERROR("reading %zd bytes from /dev/random\n", rand_bytes); return -1; } zrtp_entropy_add(zrtp_global, (const unsigned char*)p, read_bytes); free(p); } // zrtp_add_entropy(zrtp_global, NULL, 0); // fixme DBG("ZRTP initialized ok.\n"); return 0; }