[Ffmpeg-devel] [RFC] MXF AES decryption

Reimar Döffinger Reimar.Doeffinger
Sun Feb 11 14:15:21 CET 2007


Hello,
On Sun, Feb 11, 2007 at 01:51:53PM +0100, Baptiste Coudurier wrote:
> Michael Niedermayer wrote:
> > On Thu, Feb 08, 2007 at 10:15:51PM +0100, Reimar D?ffinger wrote:
> >> On Sun, Jan 14, 2007 at 09:00:59PM +0100, Baptiste Coudurier wrote:
> >>> Reimar D?ffinger wrote:
> >>>> [...]
> >>>>>> @@ -768,9 +913,16 @@
> >>>>>>              av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
> >>>>>>              continue;
> >>>>>>          }
> >>>>>> +        essence_container_ul = descriptor->essence_container_ul;
> >>>>>> +        if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
> >>>>>> +           MXFCryptoContext *cc = mxf_find_track_cryptocontext(mxf, descriptor->linked_track_id);
> >>>>>> +           if (cc)
> >>>>>> +               essence_container_ul = cc->source_ul;
> >>>>>> +           mxf->sync_key = mxf_encrypted_triplet_key;
> >>>>>> +        }
> >>>>> That's wrong. descriptor essence container should be original source
> >>>>> coutainer. That file is broken. Working 100% solution must be
> >>>>> implemented, also linked track id is optional in S377M. Yes it is
> >>>>> complicated but a working 100% solution exists.
> >>>> I moved the sync_key setting someplace else, so the remaining code is
> >>>> only a hack. I hope you don't expect my to improve a hack without any
> >>>> samples that would need it (since it's a hack I'm actually tempted to
> >>>> just remove the linked_track_id check and DMSegment and CryptoFramework
> >>>> parsing and just grab the source_ul from the first best CryptoContext
> >>>> found).
> >>>>
> >>> Well if track count is only one, I agree that would be more convenient.
> >> Since the problems are probably caused by an old version of the spec and
> >> the code is needed for later per-track encryption key (if it will ever
> >> be implemented *g*) I decided to keep it.
> >> I fixed it to use source_track->track_id instead of
> >> descriptor->linked_track_id though.
> >> Does this version look again to be applied?
> > 
> > iam fine with the changes to avformat.h, and iam not mxf maintaier so no
> > comment about that
> 
> Applied.

I attached the remainder of the patch that contains functions that will
be useful if we ever encounter files with tracks that use different
encryption key, just for reference.

> Can we change av_aes_init to take const uint8_t * as key argument ?

IMO that's a good idea.

Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavformat/mxf.c
===================================================================
--- libavformat/mxf.c	(revision 7925)
+++ libavformat/mxf.c	(working copy)
@@ -62,6 +62,8 @@
     Track,
     EssenceContainerData,
     CryptoContext,
+    CryptoFramework,
+    DMSegment,
 };
 
 typedef struct MXFCryptoContext {
@@ -71,6 +73,20 @@
     UID source_container_ul;
 } MXFCryptoContext;
 
+typedef struct MXFDMSegment {
+    UID uid;
+    enum MXFMetadataSetType type;
+    int *trackids;
+    int trackids_count;
+    UID framework_ul;
+} MXFDMSegment;
+
+typedef struct MXFCryptoFramework {
+    UID uid;
+    enum MXFMetadataSetType type;
+    UID cryptocontext;
+} MXFCryptoFramework;
+
 typedef struct MXFStructuralComponent {
     UID uid;
     enum MXFMetadataSetType type;
@@ -379,6 +395,34 @@
     return 0;
 }
 
+static int mxf_read_metadata_dmsegment(MXFDMSegment *dmsegment, ByteIOContext *pb, int tag)
+{
+    switch(tag) {
+    case 0x6101:
+        get_buffer(pb, dmsegment->framework_ul, 16);
+        break;
+    case 0x6102:
+        dmsegment->trackids_count = get_be32(pb);
+        if (dmsegment->trackids_count >= UINT_MAX / sizeof(int))
+            return -1;
+        dmsegment->trackids = av_malloc(dmsegment->trackids_count * sizeof(int));
+        url_fskip(pb, 4); /* useless size of objects, always 4 according to specs */
+        get_buffer(pb, (uint8_t *)dmsegment->trackids, dmsegment->trackids_count * sizeof(int));
+        break;
+    }
+    return 0;
+}
+
+static int mxf_read_metadata_cryptographic_framework(MXFCryptoFramework *cryptoframework, ByteIOContext *pb, int tag)
+{
+    switch(tag) {
+    case 0xFFFF:
+        get_buffer(pb, cryptoframework->cryptocontext, 16);
+        break;
+    }
+    return 0;
+}
+
 static int mxf_read_metadata_content_storage(MXFContext *mxf, ByteIOContext *pb, int tag)
 {
     switch (tag) {
@@ -662,6 +706,27 @@
     return NULL;
 }
 
+static MXFCryptoContext *mxf_find_track_cryptocontext(MXFContext *mxf, int track_id)
+{
+    int i, j;
+    for (i = 0; i < mxf->metadata_sets_count; i++) {
+        if (mxf->metadata_sets[i]->type == DMSegment) {
+            MXFDMSegment *seg = (MXFDMSegment *)mxf->metadata_sets[i];
+            for (j = 0; j < seg->trackids_count; j++)
+                if (seg->trackids[j] == track_id) break;
+            if (j < seg->trackids_count || seg->trackids_count == 0) {
+                MXFCryptoContext *cc = NULL;
+                MXFCryptoFramework *cf = mxf_resolve_strong_ref(mxf, seg->framework_ul, CryptoFramework);
+                if (cf)
+                    cc = mxf_resolve_strong_ref(mxf, cf->cryptocontext, CryptoContext);
+                if (cc)
+                    return cc;
+            }
+        }
+    }
+    return NULL;
+}
+
 static int mxf_parse_structural_metadata(MXFContext *mxf)
 {
     MXFPackage *material_package = NULL;
@@ -859,6 +924,8 @@
     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_metadata_track, sizeof(MXFTrack), Track }, /* Static Track */
     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_metadata_track, sizeof(MXFTrack), Track }, /* Generic Track */
     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_metadata_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext },
+    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x41,0x00 }, mxf_read_metadata_dmsegment, sizeof(MXFDMSegment), DMSegment },
+    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x01,0x00,0x00 }, mxf_read_metadata_cryptographic_framework, sizeof(MXFCryptoFramework), CryptoFramework },
     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType },
 };
 
@@ -963,6 +1030,9 @@
         case MaterialPackage:
             av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs);
             break;
+        case DMSegment:
+            av_freep(&((MXFDMSegment *)mxf->metadata_sets[i])->trackids);
+            break;
         default:
             break;
         }



More information about the ffmpeg-devel mailing list