Skip to content

Commit 7ab34e7

Browse files
committed
Improve encrypted RTSP message validation
1 parent 955f13a commit 7ab34e7

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/RtspConnection.c

+26-1
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,44 @@ static bool unsealRtspMessage(char* rawMessage, int rawMessageLen, PRTSP_MESSAGE
157157
int decryptedMessageLen;
158158
bool success;
159159

160+
// If the server just closed the connection without responding with anything,
161+
// there's no point in proceeding any further trying to parse it.
162+
if (rawMessageLen == 0) {
163+
return false;
164+
}
165+
160166
if (encryptedRtspEnabled) {
161167
PENC_RTSP_HEADER encryptedMessage;
162168
uint32_t seq;
169+
uint32_t typeAndLen;
170+
uint32_t len;
163171
uint8_t iv[12] = { 0 };
164172

165173
if (rawMessageLen <= (int)sizeof(ENC_RTSP_HEADER)) {
174+
Limelog("RTSP encrypted header too small\n");
166175
return false;
167176
}
168177

169178
encryptedMessage = (PENC_RTSP_HEADER)rawMessage;
170-
seq = BE32(encryptedMessage->sequenceNumber);
179+
typeAndLen = BE32(encryptedMessage->typeAndLength);
180+
181+
if (!(typeAndLen & ENCRYPTED_RTSP_BIT)) {
182+
Limelog("Rejecting unencrypted RTSP message\n");
183+
return false;
184+
}
185+
186+
len = typeAndLen & ~ENCRYPTED_RTSP_BIT;
187+
if (len + sizeof(ENC_RTSP_HEADER) > rawMessageLen) {
188+
Limelog("Rejecting partial encrypted RTSP message\n");
189+
return false;
190+
}
191+
else if (len + sizeof(ENC_RTSP_HEADER) < rawMessageLen) {
192+
Limelog("Rejecting encrypted RTSP message with excess data\n");
193+
return false;
194+
}
171195

172196
// Populate the IV in little endian byte order
197+
seq = BE32(encryptedMessage->sequenceNumber);
173198
iv[3] = (uint8_t)(seq >> 24);
174199
iv[2] = (uint8_t)(seq >> 16);
175200
iv[1] = (uint8_t)(seq >> 8);

0 commit comments

Comments
 (0)