static int base64_helper(struct ast_channel *chan, const char *cmd, char *data, char *buf, struct ast_str **str, ssize_t len) { if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd); return -1; } if (cmd[7] == 'E') { if (buf) { ast_base64encode(buf, (unsigned char *) data, strlen(data), len); } else { if (len >= 0) { ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 4 / 3 + 2); } ast_base64encode(ast_str_buffer(*str) + ast_str_strlen(*str), (unsigned char *) data, strlen(data), ast_str_size(*str) - ast_str_strlen(*str)); ast_str_update(*str); } } else { int decoded_len; if (buf) { decoded_len = ast_base64decode((unsigned char *) buf, data, len); /* add a terminating null at the end of buf, or at the * end of our decoded string, which ever is less */ buf[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0'; } else { if (len >= 0) { ast_str_make_space(str, len ? len : ast_str_strlen(*str) + strlen(data) * 3 / 4 + 2); } decoded_len = ast_base64decode((unsigned char *) ast_str_buffer(*str) + ast_str_strlen(*str), data, ast_str_size(*str) - ast_str_strlen(*str)); if (len) /* add a terminating null at the end of our * buffer, or at the end of our decoded string, * which ever is less */ ast_str_buffer(*str)[decoded_len <= (len - 1) ? decoded_len : len - 1] = '\0'; else /* space for the null is allocated above */ ast_str_buffer(*str)[decoded_len] = '\0'; ast_str_update(*str); } } return 0; }
static int __ast_sign(struct ast_key *key, char *msg, char *sig) { unsigned char dsig[128]; int siglen = sizeof(dsig); int res; res = ast_sign_bin(key, msg, strlen(msg), dsig); if (!res) /* Success -- encode (256 bytes max as documented) */ ast_base64encode(sig, dsig, siglen, 256); return res; }
static int base64_encode(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) { if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Syntax: BASE64_ENCODE(<data>) - missing argument!\n"); return -1; } ast_base64encode(buf, (unsigned char *) data, strlen(data), len); return 0; }
struct ast_sdp_crypto *ast_sdp_crypto_alloc(void) { struct ast_sdp_crypto *p; int key_len; unsigned char remote_key[SRTP_MASTER_LEN]; if (!ast_rtp_engine_srtp_is_registered()) { return NULL; } if (!(p = ast_calloc(1, sizeof(*p)))) { return NULL; } p->tag = 1; if (res_srtp->get_random(p->local_key, sizeof(p->local_key)) < 0) { ast_sdp_crypto_destroy(p); return NULL; } ast_base64encode(p->local_key64, p->local_key, SRTP_MASTER_LEN, sizeof(p->local_key64)); key_len = ast_base64decode(remote_key, p->local_key64, sizeof(remote_key)); if (key_len != SRTP_MASTER_LEN) { ast_log(LOG_ERROR, "base64 encode/decode bad len %d != %d\n", key_len, SRTP_MASTER_LEN); ast_sdp_crypto_destroy(p); return NULL; } if (memcmp(remote_key, p->local_key, SRTP_MASTER_LEN)) { ast_log(LOG_ERROR, "base64 encode/decode bad key\n"); ast_sdp_crypto_destroy(p); return NULL; } ast_debug(1 , "local_key64 %s len %zu\n", p->local_key64, strlen(p->local_key64)); return p; }
static inline int at_response_cusd (pvt_t* pvt, char* str, size_t len) { ssize_t res; int type; int dcs; char* text; char text_utf8[1024]; char text_base64[8192]; if (at_parse_cusd (pvt, str, len, &type, &text, &dcs)) { ast_verb (1, "[%s] Error parsing CUSD: '%.*s'\n", pvt->id, (int) len, str); return 0; } if (pvt->ussd_use_ucs2_decoding) { res = conv_ucs2_8bit_hexstr_to_utf8 (text, strlen (text), text_utf8, sizeof (text_utf8)); if (res > 0) { text = text_utf8; } else { ast_log (LOG_ERROR, "[%s] Error parsing CUSD (convert UCS-2 to UTF-8): %s\n", pvt->id, text); return -1; } } else { if (dcs == 0) { res = conv_latin1_8bit_hexstr_to_utf8 (text, strlen (text), text_utf8, sizeof (text_utf8)); } // else if (dcs == 15) // !!!!! else { res = conv_latin1_7bit_hexstr_to_utf8 (text, strlen (text), text_utf8, sizeof (text_utf8)); } if (res > 0) { text = text_utf8; } else { ast_log (LOG_ERROR, "[%s] Error parsing CUSD (convert 8bit/7bit hexstring to UTF-8): %s\n", pvt->id, text); return -1; } } ast_verb (1, "[%s] Got USSD response: '%s'\n", pvt->id, text); ast_base64encode (text_base64, (unsigned char *) text, strlen (text), sizeof (text_base64)); #ifdef __MANAGER__ manager_event_new_ussd (pvt, text); manager_event_new_ussd_base64 (pvt, text_base64); #endif #ifdef __ALLOW_LOCAL_CHANNELS__ struct ast_channel* channel; snprintf (pvt->d_send_buf, sizeof (pvt->d_send_buf), "ussd@%s", pvt->context); if (channel = channel_local_request (pvt, pvt->d_send_buf, pvt->id, "ussd")) { pbx_builtin_setvar_helper (channel, "USSD", text); pbx_builtin_setvar_helper (channel, "USSD_BASE64", text_base64); if (ast_pbx_start (channel)) { ast_hangup (channel); ast_log (LOG_ERROR, "[%s] Unable to start pbx on incoming ussd\n", pvt->id); } } #endif /* __ALLOW_LOCAL_CHANNELS__ */ return 0; }
static inline int at_response_cmgr (pvt_t* pvt, char* str, size_t len) { at_queue_t* e; ssize_t res; char* from_number; char from_number_utf8[1024]; char* text; char text_utf8[1024]; char text_base64[8192]; if ((e = at_fifo_queue_head (pvt)) && e->res == RES_CMGR) { if (pvt->auto_delete_sms && e->ptype == 1) { if (at_send_cmgd (pvt, e->param.num, 0) || at_fifo_queue_add (pvt, CMD_AT_CMGD, RES_OK)) { ast_log (LOG_ERROR, "[%s] Error sending CMGD to delete SMS message\n", pvt->id); } } at_fifo_queue_rem (pvt); pvt->incoming_sms = 0; if (at_parse_cmgr (pvt, str, len, &from_number, &text)) { ast_log (LOG_ERROR, "[%s] Error parsing SMS message, disconnecting\n", pvt->id); return 0; } ast_debug (1, "[%s] Successfully read SMS message\n", pvt->id); if (pvt->use_ucs2_encoding) { res = conv_ucs2_8bit_hexstr_to_utf8 (text, strlen (text), text_utf8, sizeof (text_utf8)); if (res > 0) { text = text_utf8; } else { ast_log (LOG_ERROR, "[%s] Error parsing SMS (convert UCS-2 to UTF-8): %s\n", pvt->id, text); } res = conv_ucs2_8bit_hexstr_to_utf8 (from_number, strlen (from_number), from_number_utf8, sizeof (from_number_utf8)); if (res > 0) { from_number = from_number_utf8; } else { ast_log (LOG_ERROR, "[%s] Error parsing SMS from_number (convert UCS-2 to UTF-8): %s\n", pvt->id, from_number); } } ast_base64encode (text_base64, (unsigned char *) text, strlen (text), sizeof (text_base64)); ast_verb (1, "[%s] Got SMS from %s: '%s'\n", pvt->id, from_number, text); #ifdef __MANAGER__ manager_event_new_sms (pvt, from_number, text); manager_event_new_sms_base64 (pvt, from_number, text_base64); #endif #ifdef __ALLOW_LOCAL_CHANNELS__ struct ast_channel* channel; snprintf (pvt->d_send_buf, sizeof (pvt->d_send_buf), "sms@%s", pvt->context); if (channel = channel_local_request (pvt, pvt->d_send_buf, pvt->id, from_number)) { pbx_builtin_setvar_helper (channel, "SMS", text); pbx_builtin_setvar_helper (channel, "SMS_BASE64", text_base64); if (ast_pbx_start (channel)) { ast_hangup (channel); ast_log (LOG_ERROR, "[%s] Unable to start pbx on incoming sms\n", pvt->id); } } #endif /* __ALLOW_LOCAL_CHANNELS__ */ } else if (e) { ast_log (LOG_ERROR, "[%s] Received '+CMGR' when expecting '%s' response to '%s', ignoring\n", pvt->id, at_res2str (e->res), at_cmd2str (e->cmd)); } else { ast_log (LOG_ERROR, "[%s] Received unexpected '+CMGR'\n", pvt->id); } return 0; }