/** returned key need to be fred.*/ static uint8_t *key_with_salt(C_SrtpSecret_t* s, int32_t role) { uint8_t *saltedKey; const int pad=128; if (role == Initiator) { saltedKey=ortp_malloc0((s->initKeyLen + s->initSaltLen + pad)/8); memcpy(saltedKey, s->keyInitiator, s->initKeyLen/8); memcpy(saltedKey + s->initKeyLen/8, s->saltInitiator, s->initSaltLen/8); } else { saltedKey=ortp_malloc0((s->respKeyLen + s->respSaltLen + pad)/8); memcpy(saltedKey, s->keyResponder, s->respKeyLen/8); memcpy(saltedKey + s->respKeyLen/8, s->saltResponder, s->respSaltLen/8); } return saltedKey; }
static void __ortp_logv_out(OrtpLogLevel lev, const char *fmt, va_list args){ const char *lname="undef"; char *msg; struct timeval tp; struct tm *lt; #ifndef _WIN32 struct tm tmbuf; #endif time_t tt; ortp_gettimeofday(&tp,NULL); tt = (time_t)tp.tv_sec; #ifdef _WIN32 lt = localtime(&tt); #else lt = localtime_r(&tt,&tmbuf); #endif if (__log_file==NULL) __log_file=stderr; switch(lev){ case ORTP_DEBUG: lname="debug"; break; case ORTP_MESSAGE: lname="message"; break; case ORTP_WARNING: lname="warning"; break; case ORTP_ERROR: lname="error"; break; case ORTP_FATAL: lname="fatal"; break; default: ortp_fatal("Bad level !"); } msg=ortp_strdup_vprintf(fmt,args); #if defined(_MSC_VER) && !defined(_WIN32_WCE) #ifndef _UNICODE OutputDebugStringA(msg); OutputDebugStringA("\r\n"); #else { int len=strlen(msg); wchar_t *tmp=(wchar_t*)ortp_malloc0((len+1)*sizeof(wchar_t)); mbstowcs(tmp,msg,len); OutputDebugStringW(tmp); OutputDebugStringW(L"\r\n"); ortp_free(tmp); } #endif #endif fprintf(__log_file,"%i-%.2i-%.2i %.2i:%.2i:%.2i:%.3i ortp-%s-%s" ENDLINE ,1900+lt->tm_year,1+lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec ,(int)(tp.tv_usec/1000), lname,msg); fflush(__log_file); ortp_free(msg); }
static OrtpNetworkSimulatorCtx* simulator_ctx_new(void){ OrtpNetworkSimulatorCtx *ctx=(OrtpNetworkSimulatorCtx*)ortp_malloc0(sizeof(OrtpNetworkSimulatorCtx)); qinit(&ctx->latency_q); qinit(&ctx->q); qinit(&ctx->send_q); ortp_mutex_init(&ctx->mutex,NULL); return ctx; }
static bool_t ortp_init_srtp_policy(srtp_t srtp, srtp_policy_t* policy, enum ortp_srtp_crypto_suite_t suite, ssrc_t ssrc, const char* b64_key) { uint8_t* key; int key_size; err_status_t err; unsigned b64_key_length = strlen(b64_key); switch (suite) { case AES_128_SHA1_32: crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp); // srtp doc says: not adapted to rtcp... crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtcp); break; case AES_128_NO_AUTH: crypto_policy_set_aes_cm_128_null_auth(&policy->rtp); // srtp doc says: not adapted to rtcp... crypto_policy_set_aes_cm_128_null_auth(&policy->rtcp); break; case NO_CIPHER_SHA1_80: crypto_policy_set_null_cipher_hmac_sha1_80(&policy->rtp); crypto_policy_set_null_cipher_hmac_sha1_80(&policy->rtcp); break; case AES_128_SHA1_80: /*default mode*/ default: crypto_policy_set_rtp_default(&policy->rtp); crypto_policy_set_rtcp_default(&policy->rtcp); } //b64_decode(char const *src, size_t srcLen, void *dest, size_t destSize) key_size = b64_decode(b64_key, b64_key_length, 0, 0); if (key_size != policy->rtp.cipher_key_len) { ortp_error("Key size (%d) doesn't match the selected srtp profile (required %d)", key_size, policy->rtp.cipher_key_len); return FALSE; } key = (uint8_t*) ortp_malloc0(key_size+2); /*srtp uses padding*/ if (b64_decode(b64_key, b64_key_length, key, key_size) != key_size) { ortp_error("Error decoding key"); ortp_free(key); return FALSE; } policy->ssrc = ssrc; policy->key = key; policy->next = NULL; err = ortp_srtp_add_stream(srtp, policy); if (err != err_status_ok) { ortp_error("Failed to add incoming stream to srtp session (%d)", err); ortp_free(key); return FALSE; } ortp_free(key); return TRUE; }
static char *argv_to_line(int argc, char *argv[]) { int i; int line_length; char *line; assert( argc>=0 ); if(argc == 0) return NULL; line_length = strlen(argv[0]); for(i=1; i<argc; i++) { line_length += strlen(argv[i]) + 1; } line = ortp_malloc0((line_length +1) * sizeof(char)); strcat(line, argv[0]); for(i=1; i<argc; i++) { strcat(line, " "); strcat(line, argv[i]); } return line; }
static OrtpNetworkSimulatorCtx* simulator_ctx_new(void){ OrtpNetworkSimulatorCtx *ctx=(OrtpNetworkSimulatorCtx*)ortp_malloc0(sizeof(OrtpNetworkSimulatorCtx)); qinit(&ctx->q); return ctx; }
static int add_srtp_stream(srtp_t srtp, MSCryptoSuite suite, uint32_t ssrc, const char* b64_key, bool_t inbound) { srtp_policy_t policy; uint8_t* key; int key_size; err_status_t err; unsigned b64_key_length = strlen(b64_key); ssrc_t ssrc_conf; memset(&policy,0,sizeof(policy)); switch(suite){ case MS_AES_128_SHA1_32: crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy.rtp); // srtp doc says: not adapted to rtcp... crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy.rtcp); break; case MS_AES_128_NO_AUTH: crypto_policy_set_aes_cm_128_null_auth(&policy.rtp); // srtp doc says: not adapted to rtcp... crypto_policy_set_aes_cm_128_null_auth(&policy.rtcp); break; case MS_NO_CIPHER_SHA1_80: crypto_policy_set_null_cipher_hmac_sha1_80(&policy.rtp); crypto_policy_set_null_cipher_hmac_sha1_80(&policy.rtcp); break; case MS_AES_128_SHA1_80: /*default mode*/ crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtp); crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy.rtcp); break; case MS_AES_256_SHA1_80: crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtp); crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtcp); break; case MS_AES_256_SHA1_32: crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy.rtp); crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy.rtcp); break; case MS_CRYPTO_SUITE_INVALID: return -1; break; } key_size = b64_decode(b64_key, b64_key_length, 0, 0); if (key_size != policy.rtp.cipher_key_len) { ortp_error("Key size (%d) doesn't match the selected srtp profile (required %d)", key_size, policy.rtp.cipher_key_len); return -1; } key = (uint8_t*) ortp_malloc0(key_size+2); /*srtp uses padding*/ if (b64_decode(b64_key, b64_key_length, key, key_size) != key_size) { ortp_error("Error decoding key"); ortp_free(key); return -1; } if (!inbound) policy.allow_repeat_tx=1; /*necessary for telephone-events*/ /*ssrc_conf.type=inbound ? ssrc_any_inbound : ssrc_specific;*/ ssrc_conf.type=ssrc_specific; ssrc_conf.value=ssrc; policy.ssrc = ssrc_conf; policy.key = key; policy.next = NULL; err = srtp_add_stream(srtp, &policy); if (err != err_status_ok) { ortp_error("Failed to add stream to srtp session (%d)", err); ortp_free(key); return -1; } ortp_free(key); return 0; }