From f696f02c673082b83a8c5503dc098c5b913742f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbjo=CC=88rn=20Einarsson?= Date: Fri, 8 Nov 2024 15:37:58 +0100 Subject: [PATCH] fix: offset in mdat when decrypting (issue #378) --- CHANGELOG.md | 1 + mp4/crypto.go | 3 +++ mp4/crypto_test.go | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f47e4202..c4010c5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `cmd/mp4ff-encrypt` did not parse command line - `SeigSampleGroupEntry` calculated skipBytes incorrectly - `cmd/mp4ff-pslister` did not parse annex B HEVC correctly +- error when decrypting and re-encrypting a segement (issue #378) ### Removed diff --git a/mp4/crypto.go b/mp4/crypto.go index 7a069c8f..2e74d4e9 100644 --- a/mp4/crypto.go +++ b/mp4/crypto.go @@ -596,6 +596,9 @@ func DecryptFragment(frag *Fragment, di DecryptInfo, key []byte) error { trun.DataOffset -= int32(nrBytesRemoved) } } + if frag.Mdat.StartPos > frag.Moof.StartPos { + frag.Mdat.StartPos -= nrBytesRemoved + } return nil } diff --git a/mp4/crypto_test.go b/mp4/crypto_test.go index 4dfdf927..b74433f6 100644 --- a/mp4/crypto_test.go +++ b/mp4/crypto_test.go @@ -213,6 +213,22 @@ func TestEncryptDecrypt(t *testing.T) { if !bytes.Equal(rawSeg, decSegBuf.Bytes()) { t.Errorf("segment not equal after encryption+decryption") } + + // Make a new encryption to check that the decrypted segment is OK + // for re-encryption (Issue #378). + + pd2, err := InitProtect(encInit.Init, key, iv, c.scheme, kidUUID, nil) + if err != nil { + t.Error(err) + } + for _, s := range decode.Segments { + for _, f := range s.Fragments { + err := EncryptFragment(f, key, iv, pd2) + if err != nil { + t.Errorf("Error re-encrypting fragment: %v\n", err) + } + } + } }) } } @@ -238,3 +254,7 @@ func TestDecryptInit(t *testing.T) { } } } + +func TestDecryptEncrypt(t *testing.T) { + +}