Skip to content

Commit

Permalink
Improve encrypted RTSP message validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed Feb 3, 2024
1 parent 955f13a commit 7ab34e7
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/RtspConnection.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,44 @@ static bool unsealRtspMessage(char* rawMessage, int rawMessageLen, PRTSP_MESSAGE
int decryptedMessageLen;
bool success;

// If the server just closed the connection without responding with anything,
// there's no point in proceeding any further trying to parse it.
if (rawMessageLen == 0) {
return false;
}

if (encryptedRtspEnabled) {
PENC_RTSP_HEADER encryptedMessage;
uint32_t seq;
uint32_t typeAndLen;
uint32_t len;
uint8_t iv[12] = { 0 };

if (rawMessageLen <= (int)sizeof(ENC_RTSP_HEADER)) {
Limelog("RTSP encrypted header too small\n");
return false;
}

encryptedMessage = (PENC_RTSP_HEADER)rawMessage;
seq = BE32(encryptedMessage->sequenceNumber);
typeAndLen = BE32(encryptedMessage->typeAndLength);

if (!(typeAndLen & ENCRYPTED_RTSP_BIT)) {
Limelog("Rejecting unencrypted RTSP message\n");
return false;
}

len = typeAndLen & ~ENCRYPTED_RTSP_BIT;
if (len + sizeof(ENC_RTSP_HEADER) > rawMessageLen) {
Limelog("Rejecting partial encrypted RTSP message\n");
return false;
}
else if (len + sizeof(ENC_RTSP_HEADER) < rawMessageLen) {
Limelog("Rejecting encrypted RTSP message with excess data\n");
return false;
}

// Populate the IV in little endian byte order
seq = BE32(encryptedMessage->sequenceNumber);
iv[3] = (uint8_t)(seq >> 24);
iv[2] = (uint8_t)(seq >> 16);
iv[1] = (uint8_t)(seq >> 8);
Expand Down

0 comments on commit 7ab34e7

Please sign in to comment.