Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport][AdaptiveStream] Restored decrypter IV variable as class member #1141

Merged
merged 2 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/aes_decrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ std::string AESDecrypter::convertIV(const std::string &input)

void AESDecrypter::ivFromSequence(uint8_t *buffer, uint64_t sid)
{
memset(buffer, 0, 16);
AP4_BytesFromUInt64BE(buffer + 8, sid);
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/AdaptiveStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ bool AdaptiveStream::write_data(const void* buffer,

size_t insertPos(segment_buffer.size());
segment_buffer.resize(insertPos + buffer_size);
tree_.OnDataArrived(downloadInfo.m_segmentNumber, downloadInfo.m_psshSet,
tree_.OnDataArrived(downloadInfo.m_segmentNumber, downloadInfo.m_psshSet, m_decrypterIv,
reinterpret_cast<const uint8_t*>(buffer), segment_buffer, insertPos,
buffer_size, lastChunk);
}
Expand Down
4 changes: 4 additions & 0 deletions src/common/AdaptiveStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ namespace adaptive
AdaptiveTree::AdaptationSet* current_adp_;
AdaptiveTree::Representation *current_rep_;

// Decrypter IV used to decrypt HLS segment
// We need to store here because linked to representation
uint8_t m_decrypterIv[16];

static const size_t MAXSEGMENTBUFFER;
// number of segmentbuffers whith valid segment, always >= valid_segment_buffers_
size_t available_segment_buffers_;
Expand Down
1 change: 1 addition & 0 deletions src/common/AdaptiveTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ namespace adaptive

void AdaptiveTree::OnDataArrived(uint64_t segNum,
uint16_t psshSet,
uint8_t iv[16],
const uint8_t* src,
std::string& dst,
size_t dstOffset,
Expand Down
1 change: 1 addition & 0 deletions src/common/AdaptiveTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ class ATTR_DLL_LOCAL AdaptiveTree
virtual std::chrono::time_point<std::chrono::system_clock> GetRepLastUpdated(const Representation* rep) { return std::chrono::system_clock::now(); }
virtual void OnDataArrived(uint64_t segNum,
uint16_t psshSet,
uint8_t iv[16],
const uint8_t* src,
std::string& dst,
size_t dstOffset,
Expand Down
14 changes: 8 additions & 6 deletions src/parser/HLSTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ HLSTree::PREPARE_RESULT HLSTree::prepareRepresentation(Period* period,

void HLSTree::OnDataArrived(uint64_t segNum,
uint16_t psshSet,
uint8_t iv[16],
const uint8_t* src,
std::string& dst,
size_t dstOffset,
Expand Down Expand Up @@ -890,19 +891,20 @@ void HLSTree::OnDataArrived(uint64_t segNum,
else if (!dstOffset)
{
if (pssh.iv.empty())
m_decrypter->ivFromSequence(m_decrypterIv, segNum);
m_decrypter->ivFromSequence(iv, segNum);
else
{
memcpy(m_decrypterIv, pssh.iv.data(), pssh.iv.size() < 16 ? pssh.iv.size() : 16);
memset(iv, 0, 16);
memcpy(iv, pssh.iv.data(), pssh.iv.size() < 16 ? pssh.iv.size() : 16);
}
}
m_decrypter->decrypt(reinterpret_cast<const uint8_t*>(pssh.defaultKID_.data()), m_decrypterIv,
src, dst, dstOffset, dataSize, lastChunk);
m_decrypter->decrypt(reinterpret_cast<const uint8_t*>(pssh.defaultKID_.data()), iv, src, dst,
dstOffset, dataSize, lastChunk);
if (dataSize >= 16)
memcpy(m_decrypterIv, src + (dataSize - 16), 16);
memcpy(iv, src + (dataSize - 16), 16);
}
else
AdaptiveTree::OnDataArrived(segNum, psshSet, src, dst, dstOffset, dataSize, lastChunk);
AdaptiveTree::OnDataArrived(segNum, psshSet, iv, src, dst, dstOffset, dataSize, lastChunk);
}

//Called each time before we switch to a new segment
Expand Down
2 changes: 1 addition & 1 deletion src/parser/HLSTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ATTR_DLL_LOCAL HLSTree : public AdaptiveTree

virtual void OnDataArrived(uint64_t segNum,
uint16_t psshSet,
uint8_t iv[16],
const uint8_t* src,
std::string& dst,
size_t dstOffset,
Expand All @@ -66,7 +67,6 @@ class ATTR_DLL_LOCAL HLSTree : public AdaptiveTree
virtual bool ParseManifest(std::stringstream& stream);
virtual void RefreshLiveSegments() override;
std::unique_ptr<IAESDecrypter> m_decrypter;
uint8_t m_decrypterIv[16]{};

private:
int processEncryption(std::string baseUrl, std::map<std::string, std::string>& map);
Expand Down