From patchwork Fri Jan 5 19:49:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Trimble X-Patchwork-Id: 7149 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.78.2 with SMTP id r2csp1165128jaa; Fri, 5 Jan 2018 11:50:26 -0800 (PST) X-Google-Smtp-Source: ACJfBoszc3A853VaSDPgM4Cu0qTK8EYJqWLGN6K1MR3lOpWujDZIYjXjwznSBEqt6B6C8xBr4Iv9 X-Received: by 10.28.32.206 with SMTP id g197mr3432333wmg.160.1515181826234; Fri, 05 Jan 2018 11:50:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515181826; cv=none; d=google.com; s=arc-20160816; b=fuMI/gibHnFA8NEEffC5NixxNa6+Fi0zIf1e3ijDHi/CzfD1vQg7bWBfH/UGTDtpNL iLh5SaOYm6EmTIs5HGSKl5et6NEWydLaElRDhaaaoFCDTnODjjBAmIasGFt/v3Ru8eXm fROkYc4q2hhzvn0kHJ9xQHeRMdfaGAVaC8DOGyssXySCF/fEOhiQDO5jDabeD7c9wMrZ F4Opk/bQY0xnPvoy+LpW6HuEKv/M6hqIDsG3ZAhlyZtDL68usG28wg6WQOqhUIp/piKc IdzakR/wotLA/+g/h4ngTlfCkVbUnLXA0YMtDTZoDfS3gb3znaDVGxTwP6pFW8ZQqOZF COWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:to:from:references:message-id :in-reply-to:date:mime-version:dkim-signature:delivered-to :arc-authentication-results; bh=EsOuXiSBIGgusddXswA3SmwJi6joIO+QsQPqZ5mfkoo=; b=gcSfpBP0T+N/gHGQ4kv3Ep7YO09NGgW2/vYe/Me7zdtBTydWRvAVroIYT9gVH3S/Hc 48mwLe/rHXip9Z/4J38t+F6dfdyNdJSLKKOIgWyVe1S1AXqiH5d7uRcbGAPdAP1kJssn KD+OfDNfV6r59ThmGm/25lV4nSuQY5o3maCKR+9s5+lZGPjXNhZTeGj+r3d20YvEZFVM fmOhisUNy3X9G3wNbQjszv2q0GFPUb0UZ010JMO0mr8Q6zmFcKgw5K5I93xHscE//lLh ezK6mHPFawxF81+P1rbxMtgUWw0StKU9pgr5Vhi3rT4025F5M+mRJlxdnlEDVNcXamxE 0emQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20161025 header.b=RSGAMRVw; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p72si3963124wmb.227.2018.01.05.11.50.25; Fri, 05 Jan 2018 11:50:26 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20161025 header.b=RSGAMRVw; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7982D689B93; Fri, 5 Jan 2018 21:50:24 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io0-f202.google.com (mail-io0-f202.google.com [209.85.223.202]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8AF94689B03 for ; Fri, 5 Jan 2018 21:50:18 +0200 (EET) Received: by mail-io0-f202.google.com with SMTP id f18so5252647iof.8 for ; Fri, 05 Jan 2018 11:50:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:in-reply-to:message-id:references:subject:from:to :cc; bh=6sskVsnNDOxLh5SRiuNqHvWStq0k6VxOOx9nkzyVrW8=; b=RSGAMRVwMuyKyy1vD7uE3AL/tToAZ4U5hUnSzMBKBBAJyIauGI9KyTe/gPc4AIXKUY +tvoo1B46GZHeZBTs3QPm+P9rOlwObnTn6KuxEdjAec3/wRjvm3flBu733TEFgMj6Vhj zlA+EL/BuKgd8uip2EJEKs09rf6m1m99T2CErMjd98xS8QNG/mh3HeFIgi6JmsQ3XxDg 1qiGmsu2U8J82bnpICQYj5vbgT+pm07/2FrGZKIuVpnwJFIXigaBa+Ut2S0HbcFZqGGX 6z/u6UcdKTwAyMD6rFskB19grzfCZrWN2tUA5g+356VhS5AV++apUL/NuCfSfA2ip0tb eThg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:in-reply-to:message-id :references:subject:from:to:cc; bh=6sskVsnNDOxLh5SRiuNqHvWStq0k6VxOOx9nkzyVrW8=; b=C3KfwqM/McuCD5XPRw42a++NdrPJMIlhRzy8OyPhDnX4HIW+b6pHvLCo66QQ3oRQdx 65utBDiR1c8xjptTAYqJUV+fqLB5rMf+O0r4pUd0Vn5W+iKM5rKibK41Ji0oF9JsHfXm E9bGOPtn0XUcQmZ0HUI19fx2vsEN/UjdjzFSx1mr26g+j63QErYekzqoGpnLq8o+Mk5r d8Y/TU/miD4MUuLr9uDt+rvq87A4jPdYAWKrVZNl665+J7sIAMtrUIHFBQ/aR+EsDQY8 t8hp2ssm2cPVmCv7RfYAvHEgkFatyWB8Zov+u8JMxgDagyFYdqkeqVYujomP0HxJWQuv 64CQ== X-Gm-Message-State: AKGB3mLE/pI9xSpSVb5Qo1X/Ed4ptyzt6D/fBGOtveo6nxymmwe5qSmz QjbS6wAR05QPm358I+nfqmbd49w+Nm8WFFDWDhxYi13ljd5dQwvuileWay+X+rnSpBKo7MymDRq 0C6c+1tEie56Yn3OeyxQCcY93h70kBcAo2zS3d+DFCEpwtp51kRDp9PsZ5PPdFPK3bJR9 MIME-Version: 1.0 X-Received: by 10.36.78.21 with SMTP id r21mr2477497ita.2.1515181816824; Fri, 05 Jan 2018 11:50:16 -0800 (PST) Date: Fri, 5 Jan 2018 11:49:27 -0800 In-Reply-To: <20180105194928.104085-1-modmaker@google.com> Message-Id: <20180105194928.104085-2-modmaker@google.com> References: <20180105194928.104085-1-modmaker@google.com> X-Mailer: git-send-email 2.16.0.rc0.223.g4a4ac83678-goog From: Jacob Trimble To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 2/3] avformat/mov: Fix parsing of saio/siaz atoms in encrypted content. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Jacob Trimble Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This doesn't support saio atoms with more than one offset. Signed-off-by: Jacob Trimble --- libavformat/isom.h | 6 ++ libavformat/mov.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) diff --git a/libavformat/isom.h b/libavformat/isom.h index 3794b1f0fd..3de8053da2 100644 --- a/libavformat/isom.h +++ b/libavformat/isom.h @@ -114,6 +114,12 @@ typedef struct MOVEncryptionIndex { // settings will be used. unsigned int nb_encrypted_samples; AVEncryptionInfo **encrypted_samples; + + uint8_t* auxiliary_info_sizes; + size_t auxiliary_info_sample_count; + uint8_t auxiliary_info_default_size; + size_t* auxiliary_offsets; ///< Absolute seek position + size_t auxiliary_offsets_count; } MOVEncryptionIndex; typedef struct MOVFragmentStreamInfo { diff --git a/libavformat/mov.c b/libavformat/mov.c index eb3fb71e2a..554b2be8bd 100644 --- a/libavformat/mov.c +++ b/libavformat/mov.c @@ -5835,6 +5835,165 @@ static int mov_read_senc(MOVContext *c, AVIOContext *pb, MOVAtom atom) return 0; } +static int mov_parse_auxiliary_info(MOVContext *c, MOVStreamContext *sc, AVIOContext *pb, MOVEncryptionIndex *encryption_index) +{ + AVEncryptionInfo **sample; + int64_t prev_pos; + size_t sample_count, sample_info_size, i; + int ret = 0; + + if (encryption_index->nb_encrypted_samples) + return 0; + sample_count = encryption_index->auxiliary_info_sample_count; + if (encryption_index->auxiliary_offsets_count != 1) { + av_log(c->fc, AV_LOG_ERROR, "Multiple auxiliary info chunks are not supported\n"); + return AVERROR_PATCHWELCOME; + } + + encryption_index->encrypted_samples = av_mallocz_array(sizeof(AVEncryptionInfo*), sample_count); + if (!encryption_index->encrypted_samples) + return AVERROR(ENOMEM); + encryption_index->nb_encrypted_samples = sample_count; + + prev_pos = avio_tell(pb); + if (avio_seek(pb, encryption_index->auxiliary_offsets[0], SEEK_SET) != encryption_index->auxiliary_offsets[i]) { + av_log(c->fc, AV_LOG_INFO, "Failed to seek for auxiliary info, will only parse senc atoms for encryption info\n"); + goto finish; + } + + for (i = 0; i < sample_count; i++) { + sample = &encryption_index->encrypted_samples[i]; + sample_info_size = encryption_index->auxiliary_info_default_size + ? encryption_index->auxiliary_info_default_size + : encryption_index->auxiliary_info_sizes[i]; + + ret = mov_read_sample_encryption_info(c, pb, sc, sample, sample_info_size > sc->cenc.per_sample_iv_size); + if (ret < 0) + goto finish; + } + +finish: + avio_seek(pb, prev_pos, SEEK_SET); + return ret; +} + +static int mov_read_saiz(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + MOVEncryptionIndex *encryption_index; + MOVStreamContext *sc; + int ret; + unsigned int sample_count; + + ret = get_current_encryption_info(c, &encryption_index, &sc); + if (ret != 1) + return ret; + + if (encryption_index->nb_encrypted_samples) { + // This can happen if we have both saio/saiz and senc atoms. + av_log(c->fc, AV_LOG_DEBUG, "ignoring duplicate encryption info in saiz\n"); + return 0; + } + + if (encryption_index->auxiliary_info_sample_count) { + av_log(c->fc, AV_LOG_ERROR, "duplicate saiz atom\n"); + return AVERROR_INVALIDDATA; + } + + avio_r8(pb); /* version */ + if (avio_rb24(pb) & 0x01) { /* flags */ + if (avio_rb32(pb) != sc->cenc.default_encrypted_sample->scheme) { + av_log(c->fc, AV_LOG_DEBUG, "ignoring saiz box with non-zero aux_info_type\n"); + return 0; + } + if (avio_rb32(pb) != 0) { + av_log(c->fc, AV_LOG_DEBUG, "ignoring saiz box with non-zero aux_info_type_parameter\n"); + return 0; + } + } + + encryption_index->auxiliary_info_default_size = avio_r8(pb); + sample_count = avio_rb32(pb); + encryption_index->auxiliary_info_sample_count = sample_count; + + if (encryption_index->auxiliary_info_default_size == 0) { + encryption_index->auxiliary_info_sizes = av_malloc(sample_count); + if (!encryption_index->auxiliary_info_sizes) + return AVERROR(ENOMEM); + if (avio_read(pb, encryption_index->auxiliary_info_sizes, sample_count) != sample_count) { + av_log(c->fc, AV_LOG_ERROR, "failed to read the auxiliary info\n"); + return AVERROR_INVALIDDATA; + } + } + + if (encryption_index->auxiliary_offsets_count) { + return mov_parse_auxiliary_info(c, sc, pb, encryption_index); + } + + return 0; +} + +static int mov_read_saio(MOVContext *c, AVIOContext *pb, MOVAtom atom) +{ + MOVEncryptionIndex *encryption_index; + MOVStreamContext *sc; + int i, ret; + unsigned int version, entry_count; + + ret = get_current_encryption_info(c, &encryption_index, &sc); + if (ret != 1) + return ret; + + if (encryption_index->nb_encrypted_samples) { + // This can happen if we have both saio/saiz and senc atoms. + av_log(c->fc, AV_LOG_DEBUG, "ignoring duplicate encryption info in saio\n"); + return 0; + } + + if (encryption_index->auxiliary_offsets_count) { + av_log(c->fc, AV_LOG_ERROR, "duplicate saio atom\n"); + return AVERROR_INVALIDDATA; + } + + version = avio_r8(pb); /* version */ + if (avio_rb24(pb) & 0x01) { /* flags */ + if (avio_rb32(pb) != sc->cenc.default_encrypted_sample->scheme) { + av_log(c->fc, AV_LOG_DEBUG, "ignoring saio box with non-zero aux_info_type\n"); + return 0; + } + if (avio_rb32(pb) != 0) { + av_log(c->fc, AV_LOG_DEBUG, "ignoring saio box with non-zero aux_info_type_parameter\n"); + return 0; + } + } + + entry_count = avio_rb32(pb); + encryption_index->auxiliary_offsets = av_malloc_array(sizeof(size_t), entry_count); + if (!encryption_index->auxiliary_offsets) + return AVERROR(ENOMEM); + encryption_index->auxiliary_offsets_count = entry_count; + + if (version == 0) { + for (i = 0; i < entry_count; i++) { + encryption_index->auxiliary_offsets[i] = avio_rb32(pb); + } + } else { + for (i = 0; i < entry_count; i++) { + encryption_index->auxiliary_offsets[i] = avio_rb64(pb); + } + } + if (c->frag_index.current >= 0) { + for (i = 0; i < entry_count; i++) { + encryption_index->auxiliary_offsets[i] += c->fragment.base_data_offset; + } + } + + if (encryption_index->auxiliary_info_sample_count) { + return mov_parse_auxiliary_info(c, sc, pb, encryption_index); + } + + return 0; +} + static int mov_read_schm(MOVContext *c, AVIOContext *pb, MOVAtom atom) { AVStream *st; @@ -6050,6 +6209,17 @@ static int cenc_filter(MOVContext *mov, MOVStreamContext *sc, AVPacket *pkt, int } if (encryption_index) { + if (encryption_index->auxiliary_info_sample_count && + !encryption_index->nb_encrypted_samples) { + av_log(mov->fc, AV_LOG_ERROR, "saiz atom found without saio\n"); + return AVERROR_INVALIDDATA; + } + if (encryption_index->auxiliary_offsets_count && + !encryption_index->nb_encrypted_samples) { + av_log(mov->fc, AV_LOG_ERROR, "saio atom found without saiz\n"); + return AVERROR_INVALIDDATA; + } + if (!encryption_index->nb_encrypted_samples) { // Full-sample encryption with default settings. encrypted_sample = sc->cenc.default_encrypted_sample; @@ -6194,6 +6364,8 @@ static const MOVParseTableEntry mov_default_parse_table[] = { { MKTAG('s','i','n','f'), mov_read_default }, { MKTAG('f','r','m','a'), mov_read_frma }, { MKTAG('s','e','n','c'), mov_read_senc }, +{ MKTAG('s','a','i','z'), mov_read_saiz }, +{ MKTAG('s','a','i','o'), mov_read_saio }, { MKTAG('s','c','h','m'), mov_read_schm }, { MKTAG('s','c','h','i'), mov_read_default }, { MKTAG('t','e','n','c'), mov_read_tenc }, @@ -6589,6 +6761,8 @@ static void mov_free_encryption_index(MOVEncryptionIndex **index) { av_encryption_info_free((*index)->encrypted_samples[i]); } av_freep(&(*index)->encrypted_samples); + av_freep(&(*index)->auxiliary_info_sizes); + av_freep(&(*index)->auxiliary_offsets); av_freep(index); }