| 371 | } |
| 372 | |
| 373 | bool validate_encrypted_frame(outbound_frame_processor& processor, array_view<uint8_t> frame) |
| 374 | { |
| 375 | auto codec = processor.get_codec(); |
| 376 | if (codec != codec::cd_h264 && codec != codec::cd_h265) { |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | constexpr size_t padding = nalu_short_start_sequence_size - 1; |
| 381 | |
| 382 | const auto& unencrypted_ranges = processor.get_unencrypted_ranges(); |
| 383 | |
| 384 | // H264 and H265 ciphertexts cannot contain a 3 or 4 byte start code {0, 0, 1} |
| 385 | // otherwise the packetizer gets confused |
| 386 | // and the frame we get on the decryption side will be shifted and fail to decrypt |
| 387 | size_t encrypted_section_start = 0; |
| 388 | for (auto& range : unencrypted_ranges) { |
| 389 | if (encrypted_section_start == range.offset) { |
| 390 | encrypted_section_start += range.size; |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | auto start = encrypted_section_start - std::min(encrypted_section_start, size_t{padding}); |
| 395 | auto end = std::min(range.offset + padding, frame.size()); |
| 396 | if (next_h26x_nalu_index(frame.data() + start, end - start)) { |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | encrypted_section_start = range.offset + range.size; |
| 401 | } |
| 402 | |
| 403 | if (encrypted_section_start == frame.size()) { |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | auto start = encrypted_section_start - std::min(encrypted_section_start, size_t{padding}); |
| 408 | auto end = frame.size(); |
| 409 | if (next_h26x_nalu_index(frame.data() + start, end - start)) { |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | } |
| 417 |
no test coverage detected