Add ability to deal with an unencrypted audio stream

This commit is contained in:
Mike Brady
2015-06-17 12:10:33 +01:00
parent 5c33488eaf
commit 2a1fe7afed
3 changed files with 56 additions and 59 deletions
+24 -23
View File
@@ -83,9 +83,9 @@ static aes_context dctx;
static pthread_t player_thread;
static int please_stop;
static int encrypted; // Normally the audio is encrypted, but it may not be
static int
connection_state_to_output; // if true, then play incoming stuff; if false drop everything
static int connection_state_to_output; // if true, then play incoming stuff; if false drop everything
static alac_file *decoder_info;
@@ -226,23 +226,23 @@ static void alac_decode(short *dest, uint8_t *buf, int len) {
unsigned char packet[MAX_PACKET];
unsigned char packetp[MAX_PACKET];
assert(len <= MAX_PACKET);
unsigned char iv[16];
int aeslen = len & ~0xf;
memcpy(iv, aesiv, sizeof(iv));
#ifdef HAVE_LIBPOLARSSL
aes_crypt_cbc(&dctx, AES_DECRYPT, aeslen, iv, buf, packet);
#endif
#ifdef HAVE_LIBSSL
AES_cbc_encrypt(buf, packet, aeslen, &aes, iv, AES_DECRYPT);
#endif
memcpy(packet + aeslen, buf + aeslen, len - aeslen);
int outsize;
alac_decode_frame(decoder_info, packet, dest, &outsize);
if (encrypted) {
unsigned char iv[16];
int aeslen = len & ~0xf;
memcpy(iv, aesiv, sizeof(iv));
#ifdef HAVE_LIBPOLARSSL
aes_crypt_cbc(&dctx, AES_DECRYPT, aeslen, iv, buf, packet);
#endif
#ifdef HAVE_LIBSSL
AES_cbc_encrypt(buf, packet, aeslen, &aes, iv, AES_DECRYPT);
#endif
memcpy(packet + aeslen, buf + aeslen, len - aeslen);
alac_decode_frame(decoder_info, packet, dest, &outsize);
} else {
alac_decode_frame(decoder_info, buf, dest, &outsize);
}
assert(outsize == FRAME_BYTES(frame_size));
}
@@ -1236,20 +1236,21 @@ void player_flush(uint32_t timestamp) {
int player_play(stream_cfg *stream) {
packet_count = 0;
encrypted = stream->encrypted;
if (config.buffer_start_fill > BUFFER_FRAMES)
die("specified buffer starting fill %d > buffer size %d", config.buffer_start_fill,
BUFFER_FRAMES);
if (encrypted) {
#ifdef HAVE_LIBPOLARSSL
memset(&dctx, 0, sizeof(aes_context));
aes_setkey_dec(&dctx, stream->aeskey, 128);
memset(&dctx, 0, sizeof(aes_context));
aes_setkey_dec(&dctx, stream->aeskey, 128);
#endif
#ifdef HAVE_LIBSSL
AES_set_decrypt_key(stream->aeskey, 128, &aes);
AES_set_decrypt_key(stream->aeskey, 128, &aes);
#endif
aesiv = stream->aesiv;
aesiv = stream->aesiv;
}
init_decoder(stream->fmtp);
// must be after decoder init
init_buffer();
+1
View File
@@ -4,6 +4,7 @@
#include "audio.h"
typedef struct {
int encrypted;
uint8_t aesiv[16], aeskey[16];
int32_t fmtp[12];
} stream_cfg;
+31 -36
View File
@@ -1135,20 +1135,16 @@ static void handle_announce(rtsp_conn_info *conn, rtsp_message *req, rtsp_messag
cp = next;
}
if (paesiv==NULL) {
warn("AESIV missing from ANNOUNCE");
}
if (prsaaeskey==NULL) {
warn("RSAAESKEY missing from ANNOUNCE");
}
if (pfmtp==NULL) {
warn("FMTP missing from ANNOUNCE");
if ((paesiv==NULL) && (prsaaeskey==NULL)) {
debug(1,"Unencrypted session requested?");
conn->stream.encrypted = 0;
} else {
conn->stream.encrypted = 1;
debug(1,"Encrypted session requested?");
}
if (!paesiv || !prsaaeskey || !pfmtp) {
warn("required params missing from the following ANNOUNCE message:");
if (!pfmtp) {
warn("FMTP params missing from the following ANNOUNCE message:");
// print each line of the request content
// the problem is that nextline has replace all returns, newlines, etc. by NULLs
char *cp = req->content;
@@ -1159,32 +1155,31 @@ static void handle_announce(rtsp_conn_info *conn, rtsp_message *req, rtsp_messag
cp+=strlen(cp)+1;
cp_left-=strlen(cp)+1;
}
char *next;
goto out;
}
int len, keylen;
uint8_t *aesiv = base64_dec(paesiv, &len);
if (len != 16) {
warn("client announced aeskey of %d bytes, wanted 16", len);
if (conn->stream.encrypted) {
int len, keylen;
uint8_t *aesiv = base64_dec(paesiv, &len);
if (len != 16) {
warn("client announced aeskey of %d bytes, wanted 16", len);
free(aesiv);
goto out;
}
memcpy(conn->stream.aesiv, aesiv, 16);
free(aesiv);
goto out;
}
memcpy(conn->stream.aesiv, aesiv, 16);
free(aesiv);
uint8_t *rsaaeskey = base64_dec(prsaaeskey, &len);
uint8_t *aeskey = rsa_apply(rsaaeskey, len, &keylen, RSA_MODE_KEY);
free(rsaaeskey);
if (keylen != 16) {
warn("client announced rsaaeskey of %d bytes, wanted 16", keylen);
uint8_t *rsaaeskey = base64_dec(prsaaeskey, &len);
uint8_t *aeskey = rsa_apply(rsaaeskey, len, &keylen, RSA_MODE_KEY);
free(rsaaeskey);
if (keylen != 16) {
warn("client announced rsaaeskey of %d bytes, wanted 16", keylen);
free(aeskey);
goto out;
}
memcpy(conn->stream.aeskey, aeskey, 16);
free(aeskey);
goto out;
}
memcpy(conn->stream.aeskey, aeskey, 16);
free(aeskey);
int i;
for (i = 0; i < sizeof(conn->stream.fmtp) / sizeof(conn->stream.fmtp[0]); i++)
conn->stream.fmtp[i] = atoi(strsep(&pfmtp, " \t"));
@@ -1446,11 +1441,11 @@ static void *rtsp_conversation_thread_func(void *pconn) {
struct method_handler *mh;
for (mh = method_handlers; mh->method; mh++) {
if (!strcmp(mh->method, req->method)) {
// debug(1,"RTSP Packet received of type \"%s\":",mh->method),
// msg_print_debug_headers(req);
//debug(1,"RTSP Packet received of type \"%s\":",mh->method),
//msg_print_debug_headers(req);
mh->handler(conn, req, resp);
// debug(1,"RTSP Response:");
// msg_print_debug_headers(resp);
//debug(1,"RTSP Response:");
//msg_print_debug_headers(resp);
break;
}
}